-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspawntest.js
152 lines (136 loc) · 3.99 KB
/
spawntest.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env node
pty = require('node-pty');
cp = require('child_process');
try {
gpio = require('onoff').Gpio;
} catch(e) {
gpio = null;
}
var re_tick = /\(tick (.*?)\)/g;
if (gpio) {
var syncpin = new gpio(17, "out");
function syncping() {
syncpin.writeSync(1);
setTimeout(function() {
syncpin.writeSync(0);
}, 4);
}
} else {
function syncping() {
console.log("sync");
}
}
function ms() {
//var t = process.hrtime();
//return (t[0] * 1000 + t[1] / 1000000);
return (new Date()).getTime();
}
/*var scheduled = [];
//var lastpop = 0;
var hrTime = process.hrtime()
var start = hrTime[0] * 1000 + hrTime[1] / 1000000;
setInterval(function() {
var hrTime = process.hrtime()
var now = (hrTime[0] * 1000 + hrTime[1] / 1000000 - start);
if (now > scheduled[0]) {
syncping();
console.log("pop", scheduled[0], now);
scheduled.shift(0);
//lastpop = now;
}
}, 1);*/
var last = Number.MAX_VALUE;
var ticker = null;
var last_info = null;
var lastrow = -1;
var lasttime = 0;
var sampleclock = 0;
function handledata(data) {
var now = ms();
var lines = data.toString().split("\r\n");
for (var l=0; l<lines.length; l++) {
if (lines[l].length > 2) {
try {
var s = JSON.parse(lines[l]);
} catch(e) {
var s = {};
}
if (s) {
/*
last_info = s;
if (s.time < last && !ticker) {
clearInterval(ticker);
syncping();
var len = Math.round(60000.0 / s.bpm);
setTimeout(function() {
ticker = setInterval(function() {
//console.log("last", last_info);
syncping();
}, len);
}, len - 50);
console.log("sync rate:", len);
}
//console.log(s);
console.log();
last = s.time;
*/
//console.log(lines[l]);
//console.log(s.abstime);
//sampleclock += s.time - lasttime;
//lasttime = s.time;
if (s.row % 4 == 0 && s.row != lastrow) {
var nextTick = s.time_alsa_delay - (ms() - s.time_hw);
setTimeout(syncping, Math.max(0, nextTick));
console.log("tick", s.row, now, s.time_hw, s.time_alsa, s.time_alsa_delay, nextTick);
console.log(nextTick);
//console.log("push", s.abstime, s.row);
//scheduled.push(s.abstime);
lastrow = s.row;
}
}
}
}
}
/*var start = (new Date()).getTime();
var last_tick = -1;
var last_time = 0;
function handledata(data) {
var now = (new Date()).getTime();
console.log(" --> ", now - start);
var lines = data.toString().split("\r\n");
//console.log(lines);
for (var l=0; l<lines.length; l++) {
var str = lines[l];
if (str.length > 2) {
console.log(str);
var s = JSON.parse(str);
if (s.time < last_time) {
last_tick = -1;
}
last_time = s.time;
var calcframe = s.time / (60000.0 / s.bpm / 8.0);
console.log(s.time, s.row, calcframe, Math.round(calcframe) / 8.0);
while (Math.round(calcframe) > last_tick * 4.0) {
console.log("syncping");
// TODO: should queue these up with 3ms delay
syncping();
last_tick += 1;
}
}
}
console.log();
}*/
// xmp = pty.spawn("./xmp-wrap", ["-l", "./test.it"]);
//xmp = cp.spawn("stdbuf", ["-i0", "-o0", "-e0", "./xmp-wrap", "-l", "./modules/xerxes-mfc.xm"]);
//xmp = cp.spawn("stdbuf", ["-i0", "-o0", "-e0", "./xmp-wrap", "-d", "alsa", "-D", "buffer=25", "-D", "period=25", "-l", "./test.it"]);
// setting buffer size can reduce the per-tick-print interval to 15ms which means less sync jitter
//xmp = cp.spawn("./xmp-wrap", ["-d", "alsa", "-D", "buffer=25", "-D", "period=25", "-l", "./test.it"]);
//xmp.stdout.on("data", handledata);
xmp = pty.spawn("./xmp-wrap", ["-l", process.argv.pop()], function() {});
xmp.on("data", handledata);
//setTimeout(function() {
//process.exit();
//}, 1);
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.on('data', process.exit.bind(process, 0));