-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequencer.js
65 lines (62 loc) · 1.6 KB
/
sequencer.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
"use strict";
var MidiClock = require('midi-clock');
var _ = require('underscore');
var Backbone = require("backbone");
var cycle = require('./utils').cycle;
var Sequencer = function(bpm){
_.extend(this, Backbone.Events);
this.ticks = cycle(_.range(8));
this.tempo = bpm || 94;
this.clock = MidiClock();
this.clock.setTempo(this.tempo);
this.muted = [];
this.skipped = [];
var that = this;
this.clock.on('position', function(position){
that.trigger('position', {'position': position});
var microPosition = position % 24;
if (microPosition === 0){
var tick = that.ticks.next();
while (_.contains(that.skipped, tick)){
tick = that.ticks.next();
}
that.trigger('tick', {
'muted': _.contains(that.muted, tick),
'position': tick,
'microtick': microPosition
});
}
});
this.mute = function(step){
this.muted.push(step);
};
this.skip = function(step){
this.skipped.push(step);
};
this.restore = function(step){
this.muted = _.without(this.muted, step);
this.skipped = _.without(this.skipped, step);
};
this.changeTempo = function(bpm){
this.tempo = bpm;
this.clock.setTempo(this.tempo);
};
this.play = function(){
this.clock.start();
this.trigger('seq:play');
};
this.continue = function(){
this.clock.start();
this.trigger('seq:continue');
};
this.pause = function(){
this.clock.stop();
this.trigger('seq:pause');
};
this.stop = function(){
this.clock.stop();
this.ticks = cycle(_.range(8));
this.trigger('seq:stop');
};
}
exports.Sequencer = Sequencer;