-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
114 lines (98 loc) · 3.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// app.js
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var telnet = require('telnet-client');
var tconnect = new telnet();
var config = require('./config');
app.use(express.static(__dirname + '/node_modules'));
app.use(express.static(__dirname + '/assets'));
app.get('/', function(req, res,next) {
res.sendFile(__dirname + '/index.html');
});
var tparams = {
host: config.FLUIDSYNTH_HOST,
port: config.FLUIDSYNTH_PORT,
shellPrompt: '/ # ',
timeout: config.FLUIDSYNTH_TIMEOUT,
// removeEcho: 4
};
function changeinst(channel,inst,fontId, bank) {
var flcmd = 'select '+channel+' '+ fontId + ' ' + bank + ' ' + inst;
console.log("fluidsynth: ", flcmd);
tconnect.send(flcmd, function(err, response) {
// console.log(response);
});
}
function currentinstruments() {
var current;
tconnect.send('channels', function(err, chans) {
current=chans;
});
console.log(current);
io.emit('message', { cord: current })
}
var connectRetry = null;
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
function telnetConnect(){
clearTimeout(connectRetry);
console.log("Fluidsynth Telnet: connecting to "+tparams.host+":"+tparams.port+"... ");
tconnect.connect(tparams).then(function () {
console.log("Fluidsynth Telnet: connection success");
io.emit('status','connected');
});
}
var voices;
function getvoices(client) {
clearTimeout(voices);
tconnect.send('voice_count', function (data) {
var voices = data.split(': ')[1];
client.emit('voices', voices);
voices = setTimeout(getvoices, config.STATUS_UPDATE_INTERVAL);
});
}
function dumpInstruments(font,client){
tconnect.send('inst '+font, function(err, ins) {
client.emit('instrumentdump', { package: ins });
});
}
io.on('connection', function(client) {
console.log('server connected');
telnetConnect();
tconnect.on('error', function () {
console.error("Fluidsynth Telnet: connection failed, retrying in "+ (config.FLUIDSYNTH_RETRY / 1000) +" seconds...");
connectRetry = setTimeout(telnetConnect, config.FLUIDSYNTH_RETRY);
});
client.on('queryFont',function(font){
if (isNumeric(font)){
dumpInstruments(font,client);
}
});
client.on('changeinst', function(data) {
var channel = data.channel;
var inst = data.instrumentId;
var fontId = data.fontId;
var bankId = data.bankId;
if ( isNumeric(channel) && isNumeric(inst) ) {
changeinst(channel,inst,fontId,bankId);
}
});
client.on('getinstruments', function(){
tconnect.send('channels', function(err, ins) {
var raw_list = ins.split("\n");
var channel_list = [];
for (i=0; i < raw_list.length - 1; i++){
channel_list[i] = raw_list[i].split(", ")[1].trim();
}
io.emit('current', { channels: channel_list });
});
});
// getvoices(client);
});
io.on('error', function(error) {
console.log(error);
});
server.listen(7000);