Discord.js voice resampling issues.

Not sure if this goes in djs-questions or other-js-ts

Hello! Currently I'm trying to get voice recognition on my discord bot through voice chat using Vosk, and I'm getting there however, I do not know how to resample the voice stream I am provided via connection.receiver.subscribe.
This isn't exactly to do with discord.js but any help would still be appreciated šŸ™‚

Here's my code for context:
opusStream = connection.receiver.subscribe(userId, {
  end: {
    behavior: EndBehaviorType.AfterSilence,
    duration: 2000,
  },
});

// PROBLEMS ARE HERE I BELIEVE?
const prism = await import('prism-media'); // Required for audio decoding
const pcmStream = new prism.opus.Decoder({
  rate: 16000,
  channels: 1,
  frameSize: 320,
});

opusStream.pipe(pcmStream);
// on the discord.js voice-example gh page, the author uses pipeline(), but I think this also works?

pcmStream.on('data', (chunk) => {
  // Voice detection here. This works if I'm able to fully pipe it.
});


Whenever I do anything with opusStream it continually throws RangeError [ERR_OUT_OF_RANGE]: The value of "sourceEnd" is out of range. It must be >= 0 && <= 3. Received 8 at the end of the stream.
After I record my audio again it throws: Error [ERR_STREAM_PUSH_AFTER_EOF]: stream.push() after EOF

I think discord gives 48kHz audio back, but I need it in 16kHz.

DISCORD.JS Version: 14.17.2
DISCORD.JS@Voice Version: 0.18.0
Was this page helpful?