Commit cf65b373 authored by Max-Ferdinand Suffel's avatar Max-Ferdinand Suffel
Browse files

add sound receiver

parent 3cab1af9
......@@ -44,7 +44,7 @@ rl = readline.createInterface(process.stdin, process.stdout);
rl.on('close', function() {
if (nmwSerialPortOpen) {
// Disconnect from the Neurosky MindWave.
nmwSerialPort.write(new Buffer([DISCONNECT], 'hex'), function(error, results) {
nmwSerialPort.write(new Buffer([DISCONNECT], 'hex'), function(error, results) { // TODO: remove results!
if (error || results != 1) {
console.log("Could not disconnect from the Neurosky MindWave." + err);
process.exit(1);
......
<html>
<head>
<script src="http://www.html5rocks.com/en/tutorials/webaudio/intro/js/buffer-loader.js"></script>
<script>
window.addEventListener('load', init, false);
function init() {
var audioCxt;
try {
window.AudioContext = window.AudioContext || window.webkitAudioContext;
audioCxt = new AudioContext();
console.log('Audio Context established');
} catch (e) {
alert('Web Audio API is not supported\n');
}
var socket = new WebSocket('ws://localhost:9000');
socket.binaryType = "arraybuffer";
socket.onopen = function(e) {
function float32To16bitPCM(input) {
var output = new Int16Array(input.length);
for (var i = 0; i < input.length; ++i) {
var sample = input[i]; //Math.max(-1, Math.min(1, buffer[l]));
output[i] = (sample < 0) ? (sample * 0x8000) : (sample * 0x7FFF);
}
return output.buffer;
}
bufferLoader = new BufferLoader(audioCxt, ['techno.mp3'], finishedLoading);
bufferLoader.load();
function finishedLoading(bufferList) {
var source1 = audioCxt.createBufferSource();
source1.buffer = bufferList[0];
/*var split = audioCxt.createChannelSplitter(2);
var merge = audioCxt.createChannelMerger(2);
source1.connect(split);
split.connect(merge, 0, 0);
split.connect(merge, 0, 1);*/
var audioProcessor = audioCxt.createScriptProcessor(0, 1, 1);
audioProcessor.onaudioprocess = function(e) {
var leftChannelData = e.inputBuffer.getChannelData(0);
socket.send(float32To16bitPCM(leftChannelData));
};
//merge.connect(audioProcessor);
source1.connect(audioProcessor);
audioProcessor.connect(audioCxt.destination);
source1.start(0);
}
};
}
</script>
</head>
<body>
</body>
</html>
{
"name": "SoundBrainServer",
"version": "0.0.1",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "mfs",
"license": "ISC"
}
//var Speaker = require('speaker');
//var speaker = new Speaker({channels: 1, bitDepth: 16, sampleRate: 44100});
// Create serial port object.
var SerialPort = require("serialport").SerialPort;
var teensySerialPort = new SerialPort("/dev/ttyACM0", {
baudRate : 115200 // USB full-speed.
});
teensySerialPort.on("open", function (err) {
if (err) throw err;
teensySerialPort.on("data", receiveSerialData);
console.log("Serial port /dev/ttyACM0 successfully openend.");
});
function receiveSerialData (data) {
console.log(data.toString('ascii'));
//for (var i = 0; i < data.length; i++) {
// var next_byte = data.readUInt8(i);
// console.log(next_byte.toString(16));
//}
}
function writeAndDrain (data, callback) {
teensySerialPort.write(data, function () {
teensySerialPort.drain(callback);
});
}
// Create WebSocket server object.
var ws = require('nodejs-websocket');
var wsServer;
var port = 9000;
wsServer = ws.createServer(function (conn) {
console.log('Websocket new connection...');
var len = 0;
conn.on('binary', function(stream) {
stream.on('data', function(data) {
//speaker.write(data);
writeAndDrain(data, function(err) {
if (err) throw err;
len += data.length;
console.log(len);
});
});
});
conn.on("close", function (code, reason) {
console.log("Websocket connection closed [code=" + code + ",reason=" + reason + "]");
});
}).listen(port);
console.log('Websocket server listening on port: ', port);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment