Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding 'silent frames' to a Node.js stream when no data is received

I am creating a Discord.js bot using Node.js that records the audio of users in a voice channel. It joins a channel and starts listening to each user separately. It records to a .pcm file (so only the raw data).

Now, this works, but the nature of Discord's audio stream is causing a problem. The audio stream obtained from Discord's API only sends data when the specific user is speaking, not when they are silent. This results in the moments a user speaks being pasted after each other, without the silence inbetween.

As an example, I speak for 5 seconds, then stop talking for 5 seconds, then start talking again, and so on. If I do this for 1 minute, I will get a file that is only 30 seconds long, since the 5 seconds of silence are not recorded in the stream.

The code looks something like this (receiver is what the Discord API provides for a voice connection, the stream ends arbitrarily when I give a command):

const audioStream = receiver.createStream(user, {mode:'pcm', end:'manual'};
const outputStream = fs.createWriteStream('SOME_PATH');

audioStream.pipe(outputStream);

audioStream.on('end', () => {
    console.log('Ended stream')
});

The audioStream output is a 16-bit little-endian 44100 Hz stream (so only when the user is speaking).

Is there a way I can fill in the data gaps with silent frames of some sort? Or perhaps keep a stream of silence running and only put in data when it comes in?

like image 833
JasperJ Avatar asked Mar 25 '20 01:03

JasperJ


People also ask

What is a stream in Node JS?

In Node.js, `Stream` is name of a module that implements an API for working with streaming data. The Node.js stream API has evolved significantly since it was first introduced in 2009. The constantly evolving API creates confusion around the various ways to implement and availability to mix different interfaces.

What is the evolution of the NodeJS stream API?

The Node.js stream API has evolved significantly since it was first introduced in 2009. The constantly evolving API creates confusion around the various ways to implement and availability to mix different interfaces.

What is express Node JS?

Full code implementation & Use-Case. The Express Node.js module empowers Node.js web developers to set up Web Apps and REST APIs with minimal hassle.

How to create a NodeJS server in Linux?

Step 1. Initialize a workspace folder (root folder). Within the folder, open a terminal and proceed to run the following commands: The above creates a package.json file and installs the Express NodeJS module into the application respectively. Step 2. Create a file named server.js and add in the following lines:


1 Answers

Have you tried using this with a WritableStream?

It's from discord.js voice library and silence frame is declared as:

const SILENCE_FRAME = Buffer.from([0xf8, 0xff, 0xfe]);
like image 180
Andromeda Avatar answered Oct 20 '22 06:10

Andromeda