-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.js
More file actions
74 lines (67 loc) · 2.18 KB
/
controller.js
File metadata and controls
74 lines (67 loc) · 2.18 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
var controller = (function () {
var nextId = 0;
return function (spec, my) {
var that, rhythms, beat;
my = my || {};
that = {};
rhythms = [];
var lenience = spec.lenience || 100;
var syncLevel = spec.syncLevel || 0.9;
var name = spec.name;
if(!name) {
name = "Controller "+nextId;
nextId++;
}
that.beat = function () {
var now = (new Date()).getTime();
for (i in rhythms) {
var r = rhythms[i];
if (now - r.start < spec.lenience && !r.hit[beat]) {
//console.log(name+" hit");
r.hit[beat] = true;
r.synch++;
} else {
//console.log(name+" miss");
r.synch--;
}
}
}
var newBar = function () {
for (i in rhythms) {
var r = rhythms[i];
var level = r.synch / r.rhythm.beats();
//console.log(level);
if (level > syncLevel) {
spec.event.notify('synchronised',{
'rhythm': r,
'level': level,
'controller': name,
});
}
r.hit = [];
r.synch = 0;
}
}
if (spec.event) {
spec.event.subscribe("beat", function (data) {
if(!rhythms[data.rhythm.id()]) {
rhythms[data.rhythm.id()] = {
'start': (new Date()).getTime(),
'hit': [],
'synch': 0,
'rhythm': data.rhythm,
}
} else {
rhythms[data.rhythm.id()].start = (new Date()).getTime();
}
});
spec.event.subscribe("tick", function (data) {
beat = data.beat;
if (beat === 0) {
newBar();
}
});
}
return that;
};
}());