How to insert silence pcm data when there is no data piping in

这一生的挚爱 提交于 2021-01-07 01:59:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!