-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
172 lines (123 loc) · 3.82 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
var fs = require('fs');
var setterGetterify = require('setter-getterify');
var SamplePlayer = require('openmusic-sample-player');
var Promise = require('es6-promise').Promise;
module.exports = function(context) {
var node = context.createGain();
var nodeProperties = {
tracks: 0,
steps: 16,
resolution: 16, // although it's actually the inverse 1/16
bpm: 125,
currentPattern: []
};
setterGetterify(node, nodeProperties);
var patterns = [
[
[ 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 ],
[ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 ],
[ 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1 ]
]
];
var currentPatternIndex = 0; // TODO not used yet
var currentStep = 0;
var stepTime;
var startTime;
var scheduleTimeout = null;
var samplePlayers = [];
// Sigh that we need to do it this way but it's the best we can do with
// browserify brfs transforms
var bassDrum = fs.readFileSync(__dirname + '/samples/bassdrum.wav');
var clap = fs.readFileSync(__dirname + '/samples/clap.wav');
var closedHat = fs.readFileSync(__dirname + '/samples/hihat_closed.wav');
var samples = [
bassDrum,
clap,
closedHat
];
// Makes sure the machine is ready to play
node.ready = function() {
var samplesLoaded = [];
// disconnect existing samplers just in case
samplePlayers.forEach(function(s) {
s.disconnect();
});
// dump them, and let's start again
samplePlayers = [];
samples.forEach(function(sample, index) {
var samplePlayer = new SamplePlayer(context);
var arrayBuffer = sample.toArrayBuffer();
samplePlayers.push(samplePlayer);
samplePlayer.connect(node);
var sampleLoaded = new Promise(function(resolve, reject) {
context.decodeAudioData(arrayBuffer, function(buffer) {
samplePlayer.buffer = buffer;
resolve(buffer);
}, function(err) {
reject(err);
});
});
samplesLoaded.push(sampleLoaded);
});
// Kinda hacks for the time being
nodeProperties.currentPattern = patterns[currentPatternIndex];
nodeProperties.tracks = samplePlayers.length;
return Promise.all(samplesLoaded);
};
node.start = function() {
stepTime = 0.0;
startTime = context.currentTime + 0.005;
samplePlayers.forEach(function(sampler) {
sampler.stop();
});
schedule();
};
node.stop = function(when) {
clearTimeout(scheduleTimeout);
};
node.cancelScheduledEvents = function(when) {
// TODO cancel scheduled events on the 'child' sample players
};
node.setStep = function(track, step, trigger) {
var currentPattern = this.currentPattern;
currentPattern[track][step] = trigger;
};
function schedule() {
var currentPattern = patterns[currentPatternIndex];
var numTracks = samplePlayers.length;
var currentTime = context.currentTime;
currentTime -= startTime;
// TODO also why 0.2
while(stepTime < currentTime + 0.2) {
var contextPlayTime = stepTime + startTime;
for(var track = 0; track < numTracks; track++) {
var sampler = samplePlayers[track];
var trigger = currentPattern[track][currentStep];
if(trigger) {
sampler.start(contextPlayTime);
}
}
var oldStep = currentStep;
advanceStep();
// Dispatch event for drawing if step != oldStep
if(oldStep !== currentStep) {
var ev = new CustomEvent('step', { detail: { value: currentStep } });
node.dispatchEvent(ev);
}
}
// TODO: Chris's example has the timeout at 0 but it seems excessive?
scheduleTimeout = setTimeout(schedule, 10);
}
function advanceStep() {
// Advance time by a 16th note...
var secondsPerBeat = 60.0 / nodeProperties.bpm;
currentStep++;
if(currentStep === nodeProperties.steps) {
currentStep = 0;
}
// TODO something something swing which I'm ignoring
// TODO also why 0.25 - maybe because it's a black note so 1/4 of bar?
stepTime += 0.25 * secondsPerBeat;
}
return node;
};