TooTallNate / node-speaker

Output PCM audio data to the speakers
648 stars 145 forks source link

Error: write after end #98

Closed josete89 closed 4 years ago

josete89 commented 7 years ago

ERROR Error: write after end at writeAfterEnd (/home/pi/RetroPie/homebot/node_modules/speaker/node_modules/readable-stream/lib/_stream_writable.js:222:12) at Speaker.Writable.write (/home/pi/RetroPie/homebot/node_modules/speaker/node_modules/readable-stream/lib/_stream_writable.js:262:20) at Decoder.ondata (/home/pi/RetroPie/homebot/node_modules/readable-stream/lib/_stream_readable.js:572:20)

trafalmejo commented 5 years ago

Getting the same error. I am receiving audio from WATSON Cloud service

  textToSpeech.synthesize(synthesizeParams)
  .then(audio => {
    audio.pipe(speaker);
  })
  .catch(err => {
    console.log('err:', err);
  });

But it breaks after I receive the second audio.

trafalmejo commented 5 years ago

My solution for this was creating a speaker instance per call

  textToSpeech.synthesize(synthesizeParams)
  .then(audio => {
    const speaker = new Speaker({
      channels: 1,          // 2 channels
      sampleRate: 22050     // 44,100 Hz sample rate
    })
    audio.pipe(speaker);
  })
  .catch(err => {
    console.log('err:', err);
  });
Nick-lab commented 4 years ago

very nice @trafalmejo that helped me as well

LinusU commented 4 years ago

This happens when you are writing to the stream after you have ended.

The correct solution is to either create a new Speaker for each stream you want to pipe, or to pipe with { end: false }