Below are some code snippets from three of my functions to start, pause, and resume a readable stream in Node.js. However, I would like a better way to control the Speaker()
object besides initiating another one.
I am using the spotify-web
module to get an audio stream from spotify. Could I just call new Speaker()
each time instead of using a dedicated object? How can I address new Speaker()
after the decoded stream has been piped to it?
The code below works for what I would like to do but I feel like there is a better way. I am new to Node.js and the idea of Passthrough Streams so any ideas or alternatives for stream control would be appreciated. Thanks in advance for any and all help!
// Lame decoder & speaker objects
var lame = new Lame.Decoder();
var spkr = new Speaker();
/* pipe a readable passthrough stream to the decoder
* and then to coreaudio via speaker obj.
*
* snippet from start stream function()
*/
stream
.pipe(lame)
.pipe(spkr)
/* unpipe the stream
* pause the stream at current position
*/
stream
.unpipe(lame)
.unpipe(spkr.end());
stream.pause();
/* stream from its last position
* how can I reuse spkr()?
*/
stream
.pipe(lame)
.pipe(new Speaker());