-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlarge_osc.js
More file actions
114 lines (88 loc) · 1.9 KB
/
large_osc.js
File metadata and controls
114 lines (88 loc) · 1.9 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
108
109
110
111
112
113
114
/*
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 gamma = 1.; // Width receptive field (2 -> about pi/4, 8 -> pi/8)
var n1 = 0.2; // Coupling strength phase tracking
var n2 = 0.2; // Coupling strength period tracking
var zeroTime = 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();
note_counter = 0;
} else {
period = new Date().getTime();
period = (period - zeroTime) / 1000.0;
note_counter = 0;
}
}
} else if (inlet === 0) {
update();
}
}
function reset() {
gamma = 1.;
n1 = 0.2;
n2 = 0.2;
zeroTime = 0.;
phi = 0;
period = 0.;
t_exp = 0.;
outlet(1, phi);
outlet(0, period);
}
function n1(x) {
n1 = x;
}
function n2(x) {
n2 = x;
}
function gamma(x) {
gamma = 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) {
update_large(phi);
note_counter = 0;
}
outlet(1, phi);
outlet(0, period);
}
function update_large(phi)
{
coupling_func = coupling(phi, gamma, period);
period += n1 * coupling_func;
t_exp += n2 * coupling_func;
}
function coupling(phi, gamma, period) {
return period/(2*Math.PI) * Math.pow((1./cosh(gamma * (Math.cos(2*Math.PI*phi)-1))), 2) * Math.sin(2*Math.PI*phi);
}
function cosh(x) {
return (Math.exp(x) + Math.exp(-x))/2.;
}