forked from possan/druuum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.js
69 lines (61 loc) · 1.54 KB
/
player.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
(function(exports) {
var Player = function() {
this.bpm = 0;
this.position = 0;
this.pattern = null;
this.sampler = null;
this.delay = 0;
this.interval = 1000;
this.setBPM(120);
this.timer = null;
this.beattimer = 0;
this.lasttime = 0;
}
Player.prototype.tick = function() {
this.pattern.position = this.position;
// console.log('Player tick', this.pattern.position);
for(var i=0; i<this.pattern.tracks.length; i++) {
var trk = this.pattern.tracks[i];
if (trk.steps[this.position] == 1) {
this.sampler.trigSlice(trk.slice);
}
}
this.position ++;
this.position %= 16;
}
Player.prototype.subtimer = function() {
var t = (new Date()).getTime();
if (this.lasttime == 0)
this.lasttime = t;
var dt = t - this.lasttime;
this.lasttime = t;
this.beattimer += dt;
// console.log('subtimer', this.beattimer, dt);
if (this.beattimer > this.interval) {
this.tick();
while(this.beattimer > this.interval) {
this.beattimer -= this.interval;
}
}
}
Player.prototype.play = function() {
if (this.timer == null) {
console.log('bpm = '+ this.bpm);
this.lasttime = 0;
this.timer = setInterval(this.subtimer.bind(this), 25);
}
}
Player.prototype.stop = function() {
if (this.timer != null) {
clearInterval(this.timer);
this.timer = null;
}
}
Player.prototype.setBPM = function(bpm) {
console.log('set bpm', bpm);
this.bpm = bpm;
this.interval = 1000.0 / (this.bpm * 4.0 / 60.0);
console.log('set interval', this.interval);
}
exports.Player = Player;
})(window || this);