How can I incorporate streams?
e-radio-fk opened this issue · 2 comments
Hello,
I am trying to use the mpg123-decoder
on a Node app I am working on.
Currently, my app creates a readStream from a wav file (bitDepth 16bit, sampleRate 44100) and subsequently plays it.
Problem is, wav files are really big and I want to be able to play mp3 files as well.
What I want to do is something like this:
// Make a transform stream
class Mp3ToPcmTransformStream extends Transform {
constructor(options) {
super(options);
this.decoder = new MPEGDecoder();
}
_transform(chunk, encoding, callback) {
this.decoder.decode(chunk, (err, pcmChunk, format) => {
if (err) {
console.error('Error decoding MP3:', err);
return callback();
}
console.log('Audio format:', format);
this.push(pcmChunk);
callback();
});
}
}
//..
file = fs.createReadStream('file.mp3');
var mp3ToPCM = new Mp3ToPcmTransformStream();
file.pipe(mp3ToPCM);
mp3ToPCM.pipe(outputDevice);
The so called outputDevice is confirmed to work with 16bit, 44100 pcm feed.
How can I achieve this?
What you have in your example is generally correct, except you will need to call the decode
method with the correct parameters described in the docs.
You will need to await
for the decoder to compile when you instantiate it before calling decode
.
Finally, when you're done decoding, you can call the free
method to free up the wasm memory.
Closing due to inactivity.