-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIOI.js
More file actions
107 lines (86 loc) · 1.92 KB
/
IOI.js
File metadata and controls
107 lines (86 loc) · 1.92 KB
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
/*
Large oscillator beat tracking based on (Large 1995)
Inlet 0 = Timeticks (bang)
Inlet 1 = Note onset (bang)
Outlet 0 = Period (float (seconds))
Outlet 1 = Phase (float [0,1])
*/
inlets = 2;
outlets = 2;
var IOI_nb = 5;
var mid = Math.floor(IOI_nb / 2);
var IOI_counter = 0;
var IOI_array = new Array(IOI_nb);
var zeroTime = 0.;
var lastTime = 0.;
var nowTime = 0.;
var note_counter = 0;
var period = 0.;
var phi = 0.;
var t_exp = 0.;
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
function loadbang() {
reset();
}
function bang() {
if (inlet === 1) {
note_counter += 1;
}
if (period === 0.) {
if (note_counter > 0) {
if (zeroTime === 0.) {
zeroTime = new Date().getTime();
}
nowTime = (new Date().getTime() - zeroTime)/1000.;
IOI_array[IOI_counter % IOI_nb] = nowTime - lastTime;
note_counter = 0;
lastTime = nowTime;
IOI_counter += 1;
}
if (IOI_counter > IOI_nb) {
period = median();
note_counter = 0;
}
} else if (inlet === 0) {
update();
}
}
function reset() {
zeroTime = 0.;
phi = 0;
t_exp = 0.;
period = 0.;
IOI_array = new Array(IOI_nb);
IOI_counter = 0;
outlet(1, phi);
outlet(0, period);
}
function IOI_nb(x) {
IOI_nb = x;
}
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
function update() {
nowTime = new Date().getTime();
deltaTime = (nowTime - zeroTime) / 1000.0;
while (deltaTime > (t_exp + (period/2.))) {
t_exp += period
}
phi = (((deltaTime - t_exp) / period) + 1) % 1.;
if (note_counter > 0 && period != 0.) {
update_IOI();
period = median();
note_counter = 0;
}
outlet(1, phi);
outlet(0, period);
}
function update_IOI() {
nowTime = (new Date().getTime() - zeroTime)/1000.;
IOI_array[IOI_counter % IOI_nb] = nowTime - lastTime;
lastTime = nowTime;
IOI_counter += 1;
}
function median() {
nums = IOI_array.slice().sort();
return IOI_nb % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2;
}