Created
December 14, 2014 11:49
-
-
Save emersion/e87000bbf49a417f91eb to your computer and use it in GitHub Desktop.
PCM streaming with Node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var fs = require('fs'); | |
| var net = require('net'); | |
| var PulseAudio = require('pulseaudio'); | |
| var context = PulseAudio(); | |
| context.on('connection', function(){ | |
| context.source(function (list) { | |
| var devName = ''; | |
| for (var i = 0; i < list.length; i++) { | |
| var dev = list[i]; | |
| if (dev.name.indexOf('alsa_output.pci-') == 0) { | |
| devName = dev.name; | |
| break; | |
| } | |
| } | |
| console.log(list, devName); | |
| if (!devName) { | |
| console.error('No device found'); | |
| return; | |
| } | |
| var stream = context.record({ | |
| device: devName | |
| }); | |
| stream.on('state', function(state){ | |
| console.log(state); | |
| }); | |
| stream.on('connection', function(){ | |
| console.log('GO'); | |
| }); | |
| stream.on('error', function(err){ | |
| console.error('err', err); | |
| }); | |
| //stream.pipe(fs.createWriteStream('output.raw')); | |
| var client = net.connect({ | |
| host: 'raspberrypi', | |
| port: 8081 | |
| }, function () { | |
| stream.pipe(client); | |
| }); | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var net = require('net'); | |
| var fs = require('fs'); | |
| var Speaker = require('speaker'); | |
| net.createServer(function (conn) { | |
| // Create the Speaker instance | |
| var speaker = new Speaker({ | |
| channels: 2, // 2 channels | |
| bitDepth: 16, // 16-bit samples | |
| sampleRate: 44100 // 44,100 Hz sample rate | |
| }); | |
| // PCM data from stdin gets piped into the speaker | |
| conn.pipe(speaker); | |
| //conn.pipe(fs.createWriteStream('output.raw')); | |
| }).listen(8081); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment