0

I'm generally new to Node and JS but not to the idea of streams and backpressure. I've been making a Discord bot using discordjs and I'm facing a backpressure issue.

I'm recording the User's voice in PCM format and I'm saving it to a file:userStream.pipe(transcoder).pipe(file);

Now the user's audio does NOT contain "silence", ie if the user stops talking the stream stops emitting.

I want to artificially add the silence during runtime. I've found an npm package, audio-mixer, that can mix PCM streams. I've also found another package, pcm-silence that generates the silence, but it doesn't really limit how much silence it generates.

Currently the code looks something like this:

    const userInput = mixer.input({
      channels: 1,
      volume: 100,
      bitDepth: 16,
      sampleRate: 16_000,
    });

    const silenceInput = mixer.input({
      channels: 1,
      volume: 100,
      bitDepth: 16,
      sampleRate: 16_000,
    });
    const silence = new SilentStream();
    userStream.pipe(transcoder).pipe(userInput);
    silence.pipe(silenceInput);
    
    mixer.pipe(file);

From what I understand, none of the two really handle backpressure.

The generator looks like this:

class SilentStream extends Readable {
  ...
  _read(size) {
    this.push(this.generateSilence(size));
  }
  ...
}

and the mixer, although much more complicated, looks like this:

class InputStream extends Writable {
    _write(chunk, encoding, next) {
        if (!this.hasData) {
            this.hasData = true;
        }
        this.buffer = Buffer.concat([this.buffer, chunk]);
        next();
    }
}

_read just generates some silence and pushes it, but without checking if it should

Likewise, _write just adds the data to its own buffer and moves on to the next, without having any limit, causing the app to become unresponsive.

I'm comfortable chaning either of the two implementation, but I don't really understand where I would put the backpressure check and in what form.

Appreciate the help!

Edit: I realise it might be important to explain that Mixer is a Readable takes 0..n InputStream and mixes them. Is it maybe that Writable is not the right type for InputStream?

Alex V.
  • 31
  • 4

0 Answers0