Commit c765e480 authored by Karan Goel's avatar Karan Goel
Browse files

make lights work, change instrument, make music work

parent f5d86274
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');
app.listen(80);
function handler (req, res) {
res.writeHead(200);
res.write('hi');
res.end();
}
var this_socket = null;
var data = require('./data/record.json');
var i = 0;
function sendData() {
this_socket.emit('brain', data[i] );
i++;
}
io.on('connection', function (socket) {
i = 0;
this_socket = socket;
setInterval(sendData, 500);
});
\ No newline at end of file
......@@ -223,7 +223,7 @@ function processPacket() {
++payloadPointer;
// TODO: move to multi byte code
connect_timer = null;
clearInterval(connect_timer);
switch (code) {
case CODE_ATTENTION:
......
......@@ -118,17 +118,6 @@ function sendAudioData(socket, data) {
var EEG_SOCKET_SERVER = 'http://localhost:3000';
var AUDIO_SOCKET_SERVER = 'ws://localhost:9000';
var nbrPoints = 48;
var cycleLength = 60 * 3; // 60*n minutes
var startTime = (new Date()).getTime();
var PI = 3.1415927;
var PI2 = PI * 2;
var tines = [];
var lastSound = [];
var data = [];
var eegData = [];
// INITIALIZE WAVEFORM MAKERS
var waveform = new Waveform({
......@@ -207,19 +196,30 @@ var chart = new CanvasJS.Chart("eegWave", {
chart.render();
var nbrPoints = 48;
var cycleLength = 60 * 3; // 60*n minutes
var startTime = (new Date()).getTime();
var PI = 3.1415927;
var PI2 = PI * 2;
var tines = [];
var lastSound = [];
var data = [];
var eegData = [];
// Instruments we support
var instruments = [
"acoustic_grand_piano",
"synth_drum",
"pad_2_warm",
"sitar",
"bright_acoustic_piano"
"bright_acoustic_piano",
"acoustic_bass"
];
// Instrument that we play
var instrument = 0;
var instrument = 3;
var relaySocket; // TODO: refactor
var relaySocket;
var attention = 0;
var meditation = 0;
RefreshFrame = function () {
......@@ -232,12 +232,18 @@ RefreshFrame = function () {
if (Math.floor(a / PI2) !== Math.floor(tines[i] / PI2)) {
delay = i / 4;
velocity = 127;
note = MIDI.pianoKeyOffset + meditation;
sendLightData(relaySocket, "red", Math.min(note, 255));
note = MIDI.pianoKeyOffset;
if (attention > 50 && meditation > 50) {
note += (attention + meditation) / 2;
} else if (attention > 50 && meditation < 50) {
note += meditation;
} else {
note += attention;
}
MIDI.noteOn(instrument, note, velocity, delay);
lastSound[i] = ms;
data.push(Math.cos(note));
......@@ -264,8 +270,8 @@ var updateChart = function (attn, medit) {
});
// updating legend text with updated with y Value
chart.options.data[0].legendText = " Attention " + attn;
chart.options.data[1].legendText = " Meditation" + medit;
chart.options.data[0].legendText = " Attention " + attn;
chart.options.data[1].legendText = " Meditation " + medit;
chart.render();
......@@ -284,23 +290,19 @@ socket.on('get-EEG', function (data) {
var min_data = 0;
var max_data = 100;
attention = eeg_data.attention;
meditation = eeg_data.meditation;
nbrPoints = Math.round(min_nbrPoints + ((max_nbrPoints - min_nbrPoints) * (eeg_data.attention - min_data)) / (max_data - min_data));
nbrPoints *= 0.5;
updateChart(eeg_data.attention, eeg_data.meditation);
RefreshFrame();
// eegData.push(Math.cos(eeg_data.attention));
// eegWaveform.update({
// data: eegData
// });
sendLightData(relaySocket, "blue", Math.min(eeg_data.attention, 255));
sendLightData(relaySocket, "red", Math.min(eeg_data.meditation, 255));
RefreshFrame();
} else {
// eegData.push(Math.cos(90));
// eegWaveform.update({
// data: eegData
// });
console.log('Connecting...')
}
});
......@@ -319,10 +321,9 @@ window.onload = function () {
instruments: instruments,
callback: function() {
MIDI.programChange(0, 0);
MIDI.programChange(1, 118);
MIDI.programChange(2, 89);
MIDI.programChange(3, 104);
MIDI.programChange(4, 1);
MIDI.programChange(1, 104);
MIDI.programChange(2, 1);
MIDI.programChange(3, 32);
MIDI_INITIALIZED = true;
},
......
Supports Markdown
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