Created
July 12, 2016 14:57
-
-
Save ajalab/181a643b5eb6681663c792747c08ad8c to your computer and use it in GitHub Desktop.
intel edison synthesizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var express = require("express"); | |
var app = express(); | |
var http = require("http").Server(app); | |
var io = require("socket.io")(http); | |
var os = require("os"); | |
var flock = require("flocking"); | |
/* Server */ | |
app.use(express.static("static")); | |
app.get("/", function (req,res) { | |
res.sendFile(__dirname + "/static/keyboard.html"); | |
}); | |
var server = http.listen(3000, function () { | |
/* get ip address */ | |
var interfaces = os.networkInterfaces(); | |
var wlan = interfaces["wlan0"]; | |
var addr, host, port; | |
addr = server.address(); | |
port = addr.port; | |
if (wlan && wlan.length > 0) { | |
host = wlan[0].address; | |
} else { | |
host = addr.address; | |
} | |
console.log("keyboard is at http://" + host + ":" + port + "/"); | |
}); | |
io.on("connection", function (socket) { | |
console.log("user connected"); | |
socket.on("pressStart", pressStart); | |
socket.on("pressEnd", pressEnd); | |
}); | |
/* Synth */ | |
var enviro = flock.init(); | |
var notes = { | |
"c1": 440 * Math.pow(2, -9 / 12), | |
"c#1": 440 * Math.pow(2, -8 / 12), | |
"d1": 440 * Math.pow(2, -7 / 12), | |
"d#1": 440 * Math.pow(2, -6 / 12), | |
"e1": 440 * Math.pow(2, -5 / 12), | |
"f1": 440 * Math.pow(2, -4 / 12), | |
"f#1": 440 * Math.pow(2, -3 / 12), | |
"g1": 440 * Math.pow(2, -2 / 12), | |
"g#1": 440 * Math.pow(2, -1 / 12), | |
"a1": 440 * Math.pow(2, 0 / 12), | |
"a#1": 440 * Math.pow(2, 1 / 12), | |
"b1": 440 * Math.pow(2, 2 / 12), | |
"c2": 440 * Math.pow(2, 3 / 12), | |
"c#2": 440 * Math.pow(2, 4 / 12), | |
"d2": 440 * Math.pow(2, 5 / 12), | |
"d#2": 440 * Math.pow(2, 6 / 12), | |
"e2": 440 * Math.pow(2, 7 / 12), | |
}; | |
/* 以下変更 */ | |
var sources_num = 5; /* max */ | |
var sources = []; | |
var sources_used = []; | |
var s = flock.synth({ | |
synthDef: { | |
ugen: "flock.ugen.sum", | |
sources: (function () { | |
for (var i = 0; i < sources_num; i++) { | |
sources_used[i] = null; | |
sources[i] = { | |
id: "osc" + i, | |
ugen: "flock.ugen.sin", | |
freq: 880, | |
mul: { | |
ugen: "flock.ugen.envGen", | |
envelope: { | |
type: "flock.envelope.adsr", | |
attack: 0, | |
decay: 1.0, | |
peak: 0.15, | |
sustain: 1.0, | |
release: 1.0, | |
}, | |
gate: 0.0 | |
} | |
}; | |
} | |
return sources; | |
})() | |
} | |
}); | |
enviro.play(); | |
function pressStart(key) { | |
if (! (key in notes)) { | |
console.log("invalid key", key); | |
return; | |
} | |
for (var i = 0; i < sources_num; i++) { | |
if (sources_used[i] == null) { | |
sources_used[i] = key; | |
s.set("osc" + i + ".freq", notes[key]); | |
s.set("osc" + i + ".mul.gate", 1.0); | |
console.log("start", key, "osc: ", i); | |
break; | |
} | |
} | |
} | |
function pressEnd(key) { | |
if (! (key in notes)) { | |
console.log("invalid key", key); | |
return; | |
} | |
for (var i = 0; i < sources_num; i++) { | |
if (sources_used[i] == key) { | |
s.set("osc" + i + ".mul.gate", 0.0); | |
sources_used[i] = null; | |
console.log("end", key, "osc: ", i); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment