Skip to content

Instantly share code, notes, and snippets.

@philhartung
Last active June 11, 2020 20:41
Show Gist options
  • Save philhartung/a335120324a74fd5976ddacbb3192714 to your computer and use it in GitHub Desktop.
Save philhartung/a335120324a74fd5976ddacbb3192714 to your computer and use it in GitHub Desktop.
Play raw audio from RTP stream
const Speaker = require('speaker');
const dgram = require('dgram');
const client = dgram.createSocket('udp4');
var speaker = new Speaker({
channels: 2,
bitDepth: 16,
sampleRate: 48000
})
//udp stuff for rtp
client.on('listening', function() {
client.addMembership('239.69.0.186', '192.168.1.175');
});
client.on('message', function(message, remote) {
var bufferL24 = message.slice(12); // cut off header (assuming no CSRCs), audio is L24
var samples = bufferL24.length / 3; // calc samples
var bufferL16 = Buffer.alloc(samples * 2); //create L16 buffer
// "convert" L24 to L16
for(var i = 0; i < samples; i++){
bufferL16.writeUInt16LE(bufferL24.readUInt8(i*3 + 1) + (bufferL24.readUInt8(i*3) << 8), (i * 2));
}
speaker.write(bufferL16);
});
client.bind(5004);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment