var WebSocket = require('ws');
var serverAddr = 'ws://192.168.10.100:8080/entry'; // Server URL
var ws = new WebSocket(serverAddr);
var upmBuzzer = require('jsupm_buzzer');
var numBuzzers = 3; // Number of buzzers [1,5]
var volume = 27; // Volume [0,100]
var inputs = [3, 5, 6, 9];
var buzzers = [];
var r = 80;
var f3js = require('f3js');
var c = f3js.createContainer();
c.x = 50;
c.y = 50;
var c1 = 43
, c2 = 36
, x = 70
, y = 140
, width = 70;
var a = c.createPath();
var ps = a.extrude(60);
ps[0].y = 50;
ps[0].x = 270;
f3js.add(ps[0]);
var dx = [ 65, 145, 65, -15]
, dy = [-15, 65, 145, 65];
function addPetal(layout, module) {
var cc = f3js.createContainer();
var pp = cc.createPath()
.moveTo(-25, 10)
.curveTo(-15, 30, 15, 30, 25, 10)
.moveTo(-25, 10)
.lineTo(-25 ,-5)
.moveTo(25, 10)
.lineTo(25 ,-5);
// draw arc
var thetaOffset = 25 / r;
var theta = ((Math.PI * 2) - (thetaOffset * 2 * numBuzzers)) / numBuzzers;
var n = 50;
for (var j = 0; j <= n; j ++) {
var t = thetaOffset + (theta / n) * j;
pp.lineTo(
Math.sin(t) * r,
Math.cos(t) * r - r
);
}
if(module) {
cc.add(module)
cc.drawRectangle(15, -5, 5, 10);
}
c.add(cc, layout);
}
var circle = c.drawCircle(65, 65, r);
circle.layout = 'distribute';
for (var i = 0; i < numBuzzers; i ++) {
var buzzer = new upmBuzzer.Buzzer(inputs[i]);
buzzer.setVolume(volume*0.01);
buzzer.stopSound();
buzzer.stopSound();
buzzers.push(buzzer);
addPetal(circle, buzzer)
}
circle = c.drawCircle(285, 65, r);
circle.layout = 'distribute';
for (var i = 0; i < numBuzzers; i ++) {
addPetal(circle)
}
var opposite = [], dh = 2;
var pathl = c.jln(0, 40, 0, 90, 3, dh);
pathl.internal = true;
ps = pathl.extrude(60);
ps[0].x = -40; ps[0].y = 180;
c.add(ps[0]);
opposite.push(ps[1]);
var pathr = c.jln(130, 90, 130, 40, 3, dh);
pathr.internal = true;
ps = pathr.extrude(60);
ps[0].x = 20; ps[0].y = 180;
ps[0].drawRectangle(35, 60 - dh*2 - 5, 10, 3);
ps[0].drawRectangle(5, 60 - dh*2 - 12, 15, 10);
c.add(ps[0]);
opposite.push(ps[1]);
var patht = c.jln(100, 25, 30, 25, 3, dh);
patht.internal = true;
ps = patht.extrude(60);
ps[0].x = 80; ps[0].y = 180;
c.add(ps[0]);
opposite.push(ps[1]);
var pathb = c.jln(30, 105, 100, 105, 3, dh);
pathb.internal = true;
ps = pathb.extrude(60);
ps[0].x = 160; ps[0].y = 180;
c.add(ps[0]);
opposite.push(ps[1]);
for (var i = 0; i < 4; i ++) {
opposite[i].x += 220;
c.add(opposite[i]);
}
opposite[0].x += 130 + dh;
opposite[1].x -= 130 + dh;
/*
var pathlc = c.createPath();
pathlc.moveTo(220, 40).dovetailTo(220, 85, 3, 1, false, false, true);
var pathrc = c.createPath();
pathrc.moveTo(350, 40).dovetailTo(350, 90, 3, 1, false, false, true);
var pathtc = c.createPath();
pathtc.moveTo(250, 25).dovetailTo(320, 25, 3, 1, false, false, true);
var pathbc = c.createPath();
pathbc.moveTo(250, 100).dovetailTo(320, 100, 3, 1, false, false, true);
*/
ws.on('message', function(message) {
if (mute) return;
var data = JSON.parse(message);
switch (data.message) {
case 'note_on':
if(buzzers[data.channel]) {
buzzers[data.channel].setVolume(volume);
buzzers[data.channel].playSound(1000000 / data.frequency, 0);
}
break;
case 'note_off':
if(buzzers[data.channel]) {
buzzers[data.channel].stopSound();
buzzers[data.channel].stopSound();
}
break;
}
});