-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup-notes.js
116 lines (94 loc) · 1.84 KB
/
group-notes.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
var openNotes = {};
var v = [];
var queue = [];
var windowLength = 50;
var windowOpen = false;
var windowStart;
var openChord;
inlets = 2;
setinletassist(0,"pitch");
setinletassist(1,"velocity");
setoutletassist(0,"chord");
function clear() {
openNotes = {};
v = [];
queue = [];
windowLength = 50;
windowOpen = false;
}
function status() {
post(JSON.stringify({
on: keys(openNotes),
wo: windowOpen,
q: queue.length,
q1: queue.length ? queue[0] : null
}));
post();
}
function msg_int(f) {
v[inlet] = f;
if (inlet == 0) {
acceptNote();
}
}
function acceptNote() {
closeWindow();
if (v[1] === 0) {
// Got a note-off
var chord = openNotes[v[0]];
if (chord === undefined) {
post('Got a off note for ' + v[0] + ' when none was expected');
post();
return;
}
chord.d[v[0]] = Date.now() - chord.s;
if (--chord.o < 1)
rectifyQueue();
openNotes[v[0]] = undefined;
return;
}
if (!windowOpen) {
var n = Date.now();
windowOpen = true;
windowStart = n;
openChord = {
s: n,
q: null,
p: [],
v: [],
d: {},
o: 0
};
if (queue.length) {
queue[queue.length - 1].q = n - queue[queue.length - 1].s;
rectifyQueue();
}
queue.push(openChord);
//;(new Task(closeWindow, this)).schedule(windowLength+1);
}
openChord.p.push(v[0]);
openChord.v.push(v[1]);
openChord.o++;
openNotes[v[0]] = openChord;
}
function rectifyQueue() {
while (queue.length && queue[0].o < 1 && !!queue[0].q) {
var c = queue.shift();
var notes = [];
for (var i=0; i<c.p.length; i++) {
notes.push([c.p[i], c.v[i], c.d[c.p[i]]].join('-'));
}
var cstr = notes.join('/') + '?' + c.q;
outlet(0, cstr);
}
}
function closeWindow() {
if (Date.now() >= windowStart + windowLength) {
windowOpen = false;
}
}
function keys(o) {
var r = [];
for (var k in o) r.push(k);
return r;
}