I'm trying to use the speaker module to stream audio from a web request, returning an unending stream of audio data in Buffer form to my speakers.
So I'm trying to use the res.on('data', . . .) event to do something with the chunks, but I just can't seem to figure out what to do.
I've thought of something along those lines:
const https = require('https');
const Speaker = require('speaker');
var speaker = new Speaker({
channels: 2,
bitDepth: 16,
sampleRate: 44100
});
https.get('<url>', (res) => {
res.on('data', chunk => {
/* Somehow convert the Buffer to PCM audio data and
* give it to the speaker
*/
});
});
I've been googling and trying for 2 days, and figured I'd ask here now.
The res object you get in callback is a stream, and the speaker instance you created is a writable stream (from docs) , so you can just simply pipe them together. Some thing like this :
// Not tested code.
const https = require('https');
const Speaker = require('speaker');
let speaker = new Speaker({
channels: 2,
bitDepth: 16,
sampleRate: 44100
});
https.get('<url>', (res) => {
res.pipe(speaker);
});