Permalink
Browse files

Step 4+5. Binary JS server. Audio generation

  • Loading branch information...
alexeybondarenko committed Aug 6, 2017
1 parent 255c1d7 commit 41e37989008903860043d58cbc5c1665e9a72c40
@@ -3,6 +3,18 @@
console.log('App is running');
// AUDIO CONTEXT
var context;
window.AudioContext = window.AudioContext || window.webkitAudioContext;
context = new AudioContext();
function playSound(audioBuffer) {
var source = context.createBufferSource();
source.buffer = audioBuffer;
source.connect(context.destination);
source.start(0);
}
// WEBSOCKETS
var client = new BinaryClient('ws://' + location.hostname + ':3001');
@@ -16,6 +28,7 @@
function handleReceiveAudioData(data) {
console.log('receive audio data', data);
context.decodeAudioData(data, playSound);
}
function handleEndAudioStream(data) {
@@ -43,8 +56,8 @@
}
function handleMidiMessage(e) {
console.log(e);
if (!MIDIStream || e.data[0] !== 0x90) return;
console.log(e);
MIDIStream.write(e.data);
}
@@ -1,6 +1,9 @@
const Express = require('express');
const ejs = require('ejs');
const path = require('path');
const fs = require('fs');
const binaryServer = require('binaryjs').BinaryServer;
const app = new Express();
@@ -21,4 +24,37 @@ app.listen(app.get('PORT'), (error) => {
process.exit(1);
}
console.log(`Server started and is listening at http://localhost:${app.get('PORT')}`);
})
});
const socket = new binaryServer({
port: 3001,
});
function playTone(tone, stream) {
if (tone > 61 || tone < 1) {
console.log('undefined tone', tone);
return;
}
const file = fs.createReadStream(path.resolve(__dirname, 'wav', `${tone}.wav`));
file.pipe(stream);
file.on('end', () => {
file.unpipe(stream);
});
return file;
}
socket.on('connection', (client) => {
client.on('stream', (stream, meta) => {
stream.on('data', (data) => {
console.log(data);
const tone = data.readInt8(1);
playTone(tone, stream);
});
stream.on('end', () => {
console.log('end of stream');
});
});
});
BIN +60.4 KB server/wav/1.wav
Binary file not shown.
BIN +60.4 KB server/wav/10.wav
Binary file not shown.
BIN +60.6 KB server/wav/11.wav
Binary file not shown.
BIN +60.5 KB server/wav/12.wav
Binary file not shown.
BIN +60.6 KB server/wav/13.wav
Binary file not shown.
BIN +60.4 KB server/wav/14.wav
Binary file not shown.
BIN +60.5 KB server/wav/15.wav
Binary file not shown.
BIN +60.6 KB server/wav/16.wav
Binary file not shown.
BIN +60.6 KB server/wav/17.wav
Binary file not shown.
BIN +60.6 KB server/wav/18.wav
Binary file not shown.
BIN +60.5 KB server/wav/19.wav
Binary file not shown.
BIN +60.6 KB server/wav/2.wav
Binary file not shown.
BIN +60.5 KB server/wav/20.wav
Binary file not shown.
BIN +60.5 KB server/wav/21.wav
Binary file not shown.
BIN +60.6 KB server/wav/22.wav
Binary file not shown.
BIN +60.5 KB server/wav/23.wav
Binary file not shown.
BIN +60.5 KB server/wav/24.wav
Binary file not shown.
BIN +60.5 KB server/wav/25.wav
Binary file not shown.
BIN +60.5 KB server/wav/26.wav
Binary file not shown.
BIN +60.5 KB server/wav/27.wav
Binary file not shown.
BIN +60.5 KB server/wav/28.wav
Binary file not shown.
BIN +60.5 KB server/wav/29.wav
Binary file not shown.
BIN +60.5 KB server/wav/3.wav
Binary file not shown.
BIN +60.5 KB server/wav/30.wav
Binary file not shown.
BIN +60.6 KB server/wav/31.wav
Binary file not shown.
BIN +60.4 KB server/wav/32.wav
Binary file not shown.
BIN +60.6 KB server/wav/33.wav
Binary file not shown.
BIN +60.5 KB server/wav/34.wav
Binary file not shown.
BIN +60.5 KB server/wav/35.wav
Binary file not shown.
BIN +60.4 KB server/wav/36.wav
Binary file not shown.
BIN +60.5 KB server/wav/37.wav
Binary file not shown.
BIN +60.4 KB server/wav/38.wav
Binary file not shown.
BIN +60.6 KB server/wav/39.wav
Binary file not shown.
BIN +60.5 KB server/wav/4.wav
Binary file not shown.
BIN +60.5 KB server/wav/40.wav
Binary file not shown.
BIN +60.4 KB server/wav/41.wav
Binary file not shown.
BIN +60.5 KB server/wav/42.wav
Binary file not shown.
BIN +60.5 KB server/wav/43.wav
Binary file not shown.
BIN +60.5 KB server/wav/44.wav
Binary file not shown.
BIN +60.5 KB server/wav/45.wav
Binary file not shown.
BIN +60.5 KB server/wav/46.wav
Binary file not shown.
BIN +60.5 KB server/wav/47.wav
Binary file not shown.
BIN +60.4 KB server/wav/48.wav
Binary file not shown.
BIN +60.5 KB server/wav/49.wav
Binary file not shown.
BIN +60.5 KB server/wav/5.wav
Binary file not shown.
BIN +60.6 KB server/wav/50.wav
Binary file not shown.
BIN +60.6 KB server/wav/51.wav
Binary file not shown.
BIN +60.5 KB server/wav/52.wav
Binary file not shown.
BIN +60.6 KB server/wav/53.wav
Binary file not shown.
BIN +60.6 KB server/wav/54.wav
Binary file not shown.
BIN +60.5 KB server/wav/55.wav
Binary file not shown.
BIN +60.6 KB server/wav/56.wav
Binary file not shown.
BIN +60.6 KB server/wav/57.wav
Binary file not shown.
BIN +60.6 KB server/wav/58.wav
Binary file not shown.
BIN +60.6 KB server/wav/59.wav
Binary file not shown.
BIN +60.6 KB server/wav/6.wav
Binary file not shown.
BIN +60.6 KB server/wav/60.wav
Binary file not shown.
BIN +60.6 KB server/wav/61.wav
Binary file not shown.
BIN +60.6 KB server/wav/7.wav
Binary file not shown.
BIN +60.5 KB server/wav/8.wav
Binary file not shown.
BIN +60.5 KB server/wav/9.wav
Binary file not shown.

0 comments on commit 41e3798

Please sign in to comment.