-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
155 lines (132 loc) · 4.33 KB
/
index.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
var React = require('react');
var CanvasGrid = require('canvas-grid');
var clock = require('audio-clock');
module.exports = function(emitter) {
var Track = require('./components/track')(emitter);
var Sequencer = React.createClass({
getInitialState: function() {
var bpm = 60000 / 180;
return {bpm: bpm, activeIndex: 16, crate:{}};
},
updateSequence: function() {
console.warn('Not Implemented');
},
setBPM: function(bpm) {
var bpm = 1000 / (parseInt(bpm, 10) / 60);
clock.setBPM(bpm);
this.setState({bpm: bpm});
},
drawRowNumber: function(idx) {
var d = idx > 8 ? 2.75 : 2.25;
var x = idx*this.canvasGrid.rowWidth + (this.canvasGrid.rowWidth / d);
this.canvasGrid.ctx.fillStyle = '#000000';
this.canvasGrid.ctx.fillText(idx+1, x, this.canvasGrid.columnHeight / 1.75);
},
renderStep: function() {
var index = this.state.activeIndex,
lastIndex = index === 0 ? 16 : index - 1;
this.canvasGrid.fillSection(index, 0, this.props.activeColor);
this.drawRowNumber(index);
this.canvasGrid.clearSection(lastIndex, 0);
this.drawRowNumber(lastIndex);
},
updateStep: function(reset) {
if (this.state.activeIndex === 16 || reset) {
this.setState({activeIndex: 0});
} else {
this.setState({activeIndex: this.state.activeIndex + 1});
}
console.log('update: ', this.state.activeIndex);
this.renderStep();
emitter.emit('sequence:step', {x: this.state.activeIndex});
},
play: function() {
clock.start();
},
pause: function() {
var currentIndex = this.state.activeIndex;
clock.stop();
this.setState({activeIndex: currentIndex});
},
stop: function() {
this.setState({activeIndex: -1});
clock.stop();
},
componentWillUnmount: function() {
if (this.interval) clearInterval(this.interval);
this.interval = undefined;
},
componentDidMount: function() {
if (this.props.bpm) this.setBPM(this.props.bpm);
this.canvasEl = this.refs.grid.getDOMNode();
var grid = this.canvasGrid = new CanvasGrid(this.canvasEl);
this.canvasGrid.drawMatrix({y: this.props.tracks.length, x: 16});
var activeColor = this.props.activeColor || 'pink';
this.canvasEl.addEventListener('click', function(ev) {
if (ev.gridInfo.y === 0) return;
if (ev.gridInfo.color.hex !== this.props.activeColor) {
grid.fillSection(ev.gridInfo.x, ev.gridInfo.y, this.props.activeColor);
} else {
grid.clearSection(ev.gridInfo.x, ev.gridInfo.y);
}
emitter.emit('sequence:set', ev.gridInfo);
}.bind(this));
// draw numbers for top row
var i = 0;
grid.ctx.font = '14px sans-serif';
while (i < 16) {
this.drawRowNumber(i);
i++;
}
clock.onTick(this.updateStep);
emitter.on('sequence:bpm', function(ev) {
this.setBPM(ev.bpm);
}, this);
emitter.on('sequence:play', function(ev) {
this.play();
}, this);
emitter.on('sequence:pause', function(ev) {
this.pause();
}, this);
emitter.on('sequence:stop', function(ev) {
this.stop();
}, this);
emitter.on('sequence:remove', function() {
if (this.interval) clearInterval(this.interval);
this.interval = undefined;
}, this);
emitter.on('__sequence:track:remove', function(ev) {
grid.removeRow(ev.index);
});
},
drawRow: function() {
// debugger;
},
componentWillUnmount: function() {
clearInterval(this.interval);
},
render: function() {
var addTrack = function(track) {
var initial = track.header ? true : false;
this.drawRow(initial);
return Track({track: track, initial: initial});
};
var tracks = this.props.tracks.map(addTrack, this);
var tracklist = React.DOM.ul({
className: 'tracks'
}, tracks);
var container = React.DOM.div(null, tracklist, React.DOM.canvas({
width: 700,
ref: 'grid',
className: 'grid'
}));
var sequence = React.DOM.section({
className: 'sequencer-component',
bpm: this.props.bpm,
muted: this.props.muted || false
}, container);
return sequence;
}
});
return Sequencer;
}