TooTallNate / node-speaker

Output PCM audio data to the speakers
655 stars 147 forks source link

Noise at the beginning of playback #161

Open OnkelTem opened 3 years ago

OnkelTem commented 3 years ago

My system: Linux Kubuntu 20.04

When I try to play something, it crumples the beginning second or two of the recording and I hear a noise.

My code is doing something like this:

const stream = fs.createReadStream('test/fixtures/01.wav');
const transcoderStream = new prism.FFmpeg({
  args: ['-i', '-', '-f', 's16le', '-ar', '16000', '-channel_layout', 'mono'],
});
const speaker = new Speaker({
  channels: 1,
  bitDepth: 16,
  sampleRate: 16000,
});
stream
    .pipe(transcoderStream)
    .pipe(speaker);

When I run this script repeatedly, the artifact is somehow different each time. I.e." khhhh", "phftttt", "kchttttttt" LOL

It looks like there's some data left in some playback buffer, but I have no idea what it could be.

This is how I shutdown my script:

process.on('SIGINT', async function () {
  stream.destroy();
  await pEvent(stream, 'close');
  process.exit();
});

I also tried calling speaker.destroy(); but it didn't make any difference for the issue.

If I however output the same stream to aplay for example:

stream
    .pipe(transcoderStream)
    .pipe(process.stdout);

with:

$ node play-wav.js | aplay -f S16_LE -r 16000

there are no any artifacts and occasional restarts don't bring them.

So there seems to be a problem in the Speaker playback tract.

What is going wrong?