-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsynchroniser.js
More file actions
77 lines (64 loc) · 1.91 KB
/
synchroniser.js
File metadata and controls
77 lines (64 loc) · 1.91 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
synchroniser = function (spec, my) {
var that;
my = my || {};
that = {};
var beats = [];
my.rhythms = [];
var barTime = spec.barTime || 4;
var beats = spec.beats || 8;
var syncLevel = spec.syncLevel || 0.9;
var leniency = spec.leniency || 0.1;
var beatTime = barTime / beats;
var synchronisation = [];
var hit = [];
var enabled = [];
for (i in spec.rhythms) {
my.rhythms.push(rhythm({'pattern': spec.rhythms[i]}));
synchronisation.push(0);
hit.push([])
}
that.enable = function (beatNo) {
enabled[beatNo] = true;
}
that.disable = function (beatNo) {
enabled[beatNo] = false;
}
that.beat = function (time) {
time = time % barTime;
if (time < beats[beats.length-1]) {
console.log("Overran bar");
time += barTime;
//return;
//that.endBar();
}
beats.push(time);
var beatNo = Math.floor(time / beatTime);
var beatDelay = time % beatTime;
for (i in my.rhythms) {
if (my.rhythms[i].hasBeat(beatNo)) {
if (hit[i][beatNo] || beatDelay > leniency) {
synchronisation[i]--;
} else {
hit[i][beatNo] = true;
synchronisation[i]++;
}
} else {
synchronisation[i]--;
}
}
}
that.endBar = function () {
var synched = [];
beats = [];
for (i in synchronisation) {
if (enabled[i] && synchronisation[i]/my.rhythms[i].beats() >= syncLevel) {
synched.push(i);
}
console.log(synchronisation[i]/my.rhythms[i].beats());
synchronisation[i] = 0;
hit[i] = [];
}
return synched;
}
return that;
}