Hey everyone! I’m working on a music bot for my Discord server using Node.js and discord.js. I want to stream audio from Voicemeeter through my bot. The problem is, I’m getting PCM audio data, but Discord bots can’t play that directly.
I’ve tried a bunch of things:
- Piping the PCM data straight to the bot (got an error)
- Using a PassThrough stream (bot joined but no sound)
- Trying to encode with opus (got a weird buffer size error)
- Messing with prism-media and some other npm packages (more errors)
I’m pretty stuck now. Does anyone know how to turn this PCM stream into something the Discord bot can actually play, like opus or wav? I’d really appreciate any help or ideas!
Here’s a bit of the code I tried with opus:
const encoder = new OpusEncoder(48000, 2);
const audioStream = new PassThrough();
// When we get new audio data
if (audioFrame.isValid) {
const encoded = encoder.encode(audioFrame.data);
audioStream.write(encoded);
}
// Try to play it
bot.playAudio(audioStream);
Any thoughts on what I’m doing wrong or how to fix this? Thanks!
hey there! have u tried using node-opus? it’s pretty solid for encoding PCM to opus. u could do smth like:
const opus = require(‘node-opus’);
const encoder = new opus.OpusEncoder(48000, 2);
// encode ur PCM data
let encoded = encoder.encode(pcmData);
// then play it with ur bot
hope that helps! lmk if u need more info 
I’ve been down this road before, and it can be tricky. One approach that worked well for me was using the ‘fluent-ffmpeg’ library. It’s a Node.js wrapper for FFmpeg that makes the conversion process much smoother.
Here’s a rough idea of how you could set it up:
const ffmpeg = require('fluent-ffmpeg');
const stream = require('stream');
const pcmInputStream = new stream.Readable();
pcmInputStream._read = () => {}; // Implement this based on your PCM source
ffmpeg(pcmInputStream)
.inputFormat('s16le')
.audioFrequency(48000)
.audioChannels(2)
.format('opus')
.pipe(audioOutputStream);
// Use audioOutputStream with your Discord bot
This setup converts your PCM stream to Opus on-the-fly. It’s been reliable in my projects and might be worth a shot for your bot. Just make sure to npm install fluent-ffmpeg first.
As someone who’s worked on Discord bots before, I can relate to your struggle with audio streaming. Have you considered using FFmpeg? It’s a powerful tool for audio conversion that integrates well with Node.js.
You could set up a child process to run FFmpeg, pipe your PCM data into it, and configure it to output opus or wav. Something like this:
const ffmpeg = spawn(‘ffmpeg’, [
‘-f’, ‘s16le’,
‘-ar’, ‘48000’,
‘-ac’, ‘2’,
‘-i’, ‘pipe:0’,
‘-c:a’, ‘libopus’,
‘-f’, ‘opus’,
‘pipe:1’
]);
Then pipe your PCM data into ffmpeg.stdin and use ffmpeg.stdout as your audio source for the Discord bot. This approach has worked well for me in similar situations.
Just make sure you have FFmpeg installed on your system. Hope this helps!