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

merge

parents ec1cb895 c765e480
......@@ -8,5 +8,8 @@
"start": "node server.js"
},
"author": "mfs",
"license": "ISC"
"license": "ISC",
"dependencies": {
"nodejs-websocket": "^1.2.0"
}
}
......@@ -15,7 +15,11 @@ function connectToSerialPort () {
process.exit(0);
}
console.log("Connect to Teensy via serial port attempt: #" + serialPortReconnectAttempt);
teensySerialPort = new SerialPort("/dev/ttyACM0", {
// teensySerialPort = new SerialPort("/dev/ttyACM0", {
// baudRate : 115200 // USB full-speed.
// });
// WINDOWS
teensySerialPort = new SerialPort("COM7", {
baudRate : 115200 // USB full-speed.
});
teensySerialPort.on("open", serialPortOnOpen);
......
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:
......
......@@ -13,15 +13,21 @@
<script src="./inc/base64binary.js" type="text/javascript"></script>
<script src="./js/waveform.js"></script>
<script src="./js/canvasjs.min.js"></script>
<script src="https://cdn.socket.io/socket.io-1.3.2.js"></script>
</head>
<style type="text/css">
body {
font-family: tahoma;
background: #000;
}
#wave {
width: 100%;
height: 200px;
height: 100px;
margin-bottom: 30px;
position: absolute;
top: 10%;
......@@ -29,14 +35,14 @@
#eegWave {
width: 100%;
height: 200px;
height: 300px;
margin-bottom: 30px;
position: absolute;
top: 60%;
}
h1 {
color: #000;
color: #fff;
}
</style>
......@@ -47,7 +53,6 @@
</div>
<div id="eegWave">
<h1>EEG Data: (Attention)</h1>
</div>
......@@ -113,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({
......@@ -136,29 +130,97 @@ gradient.addColorStop(0.0, "#f60");
gradient.addColorStop(1.0, "#ff1b00");
waveform.innerColor = gradient;
var eegWaveform = new Waveform({
container: document.getElementById("eegWave"),
interpolate: false
});
var ctx2 = eegWaveform.context;
var gradient2 = ctx2.createLinearGradient(0, 0, 0, eegWaveform.height);
gradient2.addColorStop(0.0, "#ff6");
gradient2.addColorStop(1.0, "#fff");
eegWaveform.innerColor = gradient2;
var attention_values = [];
var meditation_values = [];
var chart = new CanvasJS.Chart("eegWave", {
backgroundColor: "#000",
title:{
text: "Attention and Meditation Values",
fontSize: 30,
fontColor: "white",
fontFamily: "tahoma"
},
animationEnabled: true,
axisX:{
gridColor: "Silver",
tickColor: "silver",
labelFontColor: "white",
gridThickness: 1
},
toolTip:{
shared:true
},
theme: "theme2",
axisY: {
gridColor: "Silver",
tickColor: "silver",
labelFontColor: "white",
gridThickness: 1
},
legend:{
verticalAlign: "center",
horizontalAlign: "right"
},
data: [{
// attention values
type: "line",
xValueType: "dateTime",
showInLegend: true,
name: "Attention",
dataPoints: attention_values
},
{
// meditation values
type: "line",
xValueType: "dateTime",
showInLegend: true,
name: "Meditation" ,
dataPoints: meditation_values
}],
legend:{
cursor:"pointer",
fontColor: "white",
itemclick:function(e) {
if (typeof(e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
e.dataSeries.visible = false;
}
else{
e.dataSeries.visible = true;
}
chart.render();
}
}
});
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 = 4;
var instrument = 3;
var relaySocket; // TODO: refactor
var relaySocket;
var attention = 0;
var meditation = 0;
RefreshFrame = function () {
var speed = (PI2 * nbrPoints) / cycleLength;
......@@ -170,15 +232,21 @@ RefreshFrame = function () {
if (Math.floor(a / PI2) !== Math.floor(tines[i] / PI2)) {
delay = i / 4;
velocity = 127;
note = MIDI.pianoKeyOffset + i;
note = MIDI.pianoKeyOffset;
sendLightData(relaySocket, "red", note*2.5);
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(i/25) - 0.2 + r*0.3);
data.push(Math.cos(note));
waveform.update({
data: data
});
......@@ -188,31 +256,55 @@ RefreshFrame = function () {
};
var socket = io(EEG_SOCKET_SERVER);
socket.on('connection', function() {
socket.on('get-EEG', function (data) {
data = JSON.parse(data);
console.log(data.toString());
if (data.attention > 0 && data.meditation > 0) {
RefreshFrame();
var min_nbrPoints = 48;
var max_nbrPoints = 96;
var min_data = 0;
var max_data = 100;
nbrPoints = min_nbrPoints + ((max_nbrPoints - min_nbrPoints) * (data.attention - min_data)) / (max_data - min_data);
eegData.push(Math.cos(data.attention - 0.2 + 0.3));
eegWaveform.update({
data: eegData
});
} else {
eegData.push(Math.cos(90));
eegWaveform.update({
data: eegData
});
console.log('Connecting...')
}
var updateChart = function (attn, medit) {
var time = new Date;
// pushing the new values
attention_values.push({
x: time.getTime(),
y: attn
});
meditation_values.push({
x: time.getTime(),
y: medit
});
// updating legend text with updated with y Value
chart.options.data[0].legendText = " Attention " + attn;
chart.options.data[1].legendText = " Meditation " + medit;
chart.render();
};
var MIDI_INITIALIZED = false;
var socket = io(EEG_SOCKET_SERVER);
socket.on('get-EEG', function (data) {
eeg_data = JSON.parse(data);
if (MIDI_INITIALIZED &&
eeg_data.attention > 0 && eeg_data.meditation > 0) {
var min_nbrPoints = 48;
var max_nbrPoints = 96;
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);
sendLightData(relaySocket, "blue", Math.min(eeg_data.attention, 255));
sendLightData(relaySocket, "red", Math.min(eeg_data.meditation, 255));
RefreshFrame();
} else {
console.log('Connecting...')
}
});
......@@ -229,12 +321,11 @@ 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);
RefreshFrame();
// setInterval(RefreshFrame, 200);
MIDI.programChange(1, 104);
MIDI.programChange(2, 1);
MIDI.programChange(3, 32);
MIDI_INITIALIZED = true;
},
callbackPCM: function (pcmBuffer) {
sendAudioData(relaySocket, pcmBuffer);
......
This source diff could not be displayed because it is too large. You can view the blob instead.
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