问题
I'm currently working on Discord.js voice receiver and I need it to continuously send PCM audio data by writing silent packet when there is no actual audio data coming in.
I found this discussion before but I don't understand/know how to insert silent packets when there's no actual packet is coming in
Receiver code:
const r = connection.receiver.createStream(user, {
mode: 'pcm',
end: 'manual'
});
// Nothing is piped when user is not talking
// u.stream is PassThrough stream
r.pipe(u.stream);
Prefered approach is to make a class that extends PassThrough or any stream type so I can pipe the receiver data to it and when theres is no data coming through (from the receiver), it will send silent packet (generated using 'pcm-silence' package) and will look something like this:
const fs = require('fs');
const passStream = new SilentStreamClass();
const r = connection.receiver.createStream(user, {
mode: 'pcm',
end: 'manual'
});
r.pipe(passStream);
passStream.pipe(fs.createWriteStream('./user.pcm'));
// When user is not talking, it will filled with silent packet.
Thanks in advance!
回答1:
After some time searching the internet, I found a npm package audio-mixer
to mix multiple pcm audio stream and it will pipe silent packet when theres no data to process, worked like a charm!
来源:https://stackoverflow.com/questions/65479694/how-to-insert-silence-pcm-data-when-there-is-no-data-piping-in