As of now, it is very difficult to play audio in Node.js. Most solutions suggested on the Stackoverflow question require using native modules and have all broken in one way or another. Community implementations of the Web Audio API have made good progress but are incomplete and buggy. My experience using web-audio-api has not been good.
Node.js should port over the Web Audio API present in browsers so that the same functionality can be achieved on the client-end, or at the very least provide middleware for piping audio streams to hardware, like speaker.
I'm not going to close this outright but I'd say this is unlikely to happen for the same reason node doesn't support e.g. webgl.
It's been a couple of days and no one else chimed in so I'm taking the liberty of closing this out. Thanks anyway for taking the time to file your feature request.
Although it's unlikely for Node to support things like this directly, it could be worth looking at compiling the web audio source in chromium into an npm module (however that might be difficult).
An alternative is to automate a browser to play the audio, e.g. with playwright. Although this has the caveat of loading an entire browser, and the browser needs to be visible for audio to work. But here's an example:
import playwright from "playwright";
const browser = await playwright.chromium.launch({ headless: false });
const page = await browser.newPage();
page.evaluate(() => {
const ctx = new AudioContext();
const osc = ctx.createOscillator(230);
osc.connect(ctx.destination);
osc.start();
});
Most helpful comment
I'm not going to close this outright but I'd say this is unlikely to happen for the same reason node doesn't support e.g. webgl.