-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathengine.js
330 lines (293 loc) · 12.1 KB
/
engine.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// an engine which triggers midi events on some MIDIOutput device
// it has built in midi effects:
// arpeggiation and delay (which run in that order)
class Engine {
constructor(midiOutput) {
// where to send midi data
this.midiOutput = midiOutput;
// which note events are active (currently pressed)
this.noteEvents = [];
// arpeggiator mode
this.arpeggiatorMode = ArpeggiatorMode.OFF;
// arpeggiator step sequencer
this.arpeggiatorStepSequencer = new StepSequencer(midiOutput);
// how many delay repeats
this.delayRepeats = 0;
// how much delay time
this.delayTimeInMilliseconds = 500;
// user set midi logging callback (initialized to an empty callback)
this.loggingCallback = () => {};
}
// --------------------------------------------------------------- private
// updates the arpeggiator sequence according to
// the provided arpeggiator mode and existing note events
updateArpeggiatorSequence() {
// get the notes which are active from the engine's note events
let notes = this.noteEvents.map(noteEvent => noteEvent.getNote());
let playbackMode = PlaybackMode.FORWARD;
// temp variable
let s = [];
// calculate order of notes to set into the arp step sequence
switch (this.arpeggiatorMode) {
case ArpeggiatorMode.OFF:
// do nothing
break;
case ArpeggiatorMode.UP:
notes.sort((noteA, noteB) => noteA.pitch - noteB.pitch);
break;
case ArpeggiatorMode.DOWN:
notes.sort((noteA, noteB) => noteB.pitch - noteA.pitch);
break;
case ArpeggiatorMode.UP_DOWN:
notes.sort((noteA, noteB) => noteA.pitch - noteB.pitch);
notes.forEach(note => s.push(note.copySelf()));
s.pop();
s.reverse();
s.pop();
notes.push(...s);
break;
case ArpeggiatorMode.ORDER:
// do nothing
break;
case ArpeggiatorMode.REVERSE:
notes.reverse();
break;
case ArpeggiatorMode.RANDOM:
playbackMode = PlaybackMode.RANDOM;
break;
case ArpeggiatorMode.RANDOM_2:
notes.forEach(note => s.push(note.copySelf()));
s.forEach(note => note.pitch += 12);
notes.push(...s);
playbackMode = PlaybackMode.RANDOM;
break;
case ArpeggiatorMode.UP_2:
notes.forEach(note => s.push(note.copySelf()));
s.forEach(note => note.pitch += 12);
notes.push(...s);
notes.sort((noteA, noteB) => noteA.pitch - noteB.pitch);
break;
case ArpeggiatorMode.DOWN_2:
notes.forEach(note => s.push(note.copySelf()));
s.forEach(note => note.pitch += 12);
notes.push(...s);
notes.sort((noteA, noteB) => noteB.pitch - noteA.pitch);
break;
}
// set arpeggiator's sequence to conjured notes
// and set its playback mode and length
let stepIndex = 0;
this.arpeggiatorStepSequencer.setSteps(stepIndex, notes);
this.arpeggiatorStepSequencer.setPlaybackMode(playbackMode);
this.arpeggiatorStepSequencer.setLength(notes.length);
}
// release (and remove from active note events) *only* the latched notes
releaseLatchedNotes() {
let unlatchedNoteEvents = [];
for (let i = 0; i < this.noteEvents.length; ++i) {
if (this.noteEvents[i].latched) {
this.noteEvents[i].release();
} else {
unlatchedNoteEvents.push(this.noteEvents[i]);
}
}
this.noteEvents = unlatchedNoteEvents;
this.updateArpeggiatorSequence();
// if there are no remaining note events, stop the arpeggiator
if (this.noteEvents.length == 0) {
this.arpeggiatorStepSequencer.stop();
}
}
// ---------------------------------------------------------------- public
noteOn(pitch, velocity = 96, channel = 0) {
// check that some pitch was passed in and return otherwise
if (isNaN(pitch) || (typeof(pitch) !== "number")) {
return;
}
// create a note object
let note = new Note(pitch, velocity, channel);
// if this note is already pressed (and we're not in latch mode)
if (this.noteEvents.some(noteEvent => noteEvent.getNote().equals(note)) &&
!this.latchOn) {
return;
}
// create the new note event
let noteEvent = new NoteEvent(note, this.delayRepeats,
this.delayTimeInMilliseconds, this.midiOutput, this.loggingCallback);
// append the new note event
this.noteEvents.push(noteEvent);
// if latch is on
if (this.latchOn) {
// find latched note events and release/remove them
this.releaseLatchedNotes();
}
// determine how to trigger this new note
if (this.arpeggiatorMode == ArpeggiatorMode.OFF) {
// if there's no arpeggiation active
// fire a note on
// this will not automatically release as it's unlimited duration
noteEvent.attack();
} else {
// else arpeggiation is active
// update the arpeggiator sequence
this.updateArpeggiatorSequence();
// start the arpeggiator (which fires its own note events)
this.arpeggiatorStepSequencer.start() // will not restart if already on
}
}
noteOff(pitch, velocity = 0, channel = 0) {
// check that some pitch was passed in and return otherwise
if (isNaN(pitch) || (typeof(pitch) !== "number")) {
return;
}
// create a note object
let note = new Note(pitch, velocity, channel);
// if there isn't an existing note event corresponding to this note
// then just return
let noteEvent = this.noteEvents.find(
noteEvent => noteEvent.getNote().equals(note));
if (noteEvent === undefined) {
return;
}
// if latch is on
if (this.latchOn) {
// flag note event as latched and return
// in latch mode, note off events occur only
// with future note on events
noteEvent.latched = true;
return;
}
// otherwise
// find note event index
let noteEventIndex = this.noteEvents.findIndex(
noteEvent => noteEvent.getNote().equals(note));
// remove it from engine's active note events
this.noteEvents.splice(noteEventIndex, 1);
// determine how to release this (now removed) note event
if (this.arpeggiatorMode == ArpeggiatorMode.OFF) {
// if there's no arpeggiation active
// release this note event
noteEvent.release();
} else {
// else arpeggiation is active
this.updateArpeggiatorSequence();
// if there are no remaining note events, stop the arpeggiator
if (this.noteEvents.length == 0) {
this.arpeggiatorStepSequencer.stop();
}
}
}
// send a program change value [0-127]
programChange(programChangeValue, channel = 0) {
// clamp channel between 0 and 15 inclusive
// and clamp program change value between 0 and 127 inclusive
channel = Math.max(0, Math.min(channel, 15));
programChangeValue = Math.max(0, Math.min(programChangeValue, 127));
// bitwise or with the channel to produce the correct status byte
let statusByte = 0b11000000 | channel;
let midiBytesArray = [statusByte, programChangeValue];
this.midiOutput.send(midiBytesArray);
this.loggingCallback(midiBytesArray);
}
// set the type of arpeggiation
setArpeggiatorMode(arpeggiatorMode) {
let oldMode = this.arpeggiatorMode;
let newMode = arpeggiatorMode;
this.arpeggiatorMode = arpeggiatorMode;
// update the arpeggiator sequence with the new mode
this.updateArpeggiatorSequence();
if (oldMode !== ArpeggiatorMode.OFF &&
newMode === ArpeggiatorMode.OFF) {
// if we're turning the arpeggiator from on to off
// stop the arpeggiator
this.arpeggiatorStepSequencer.stop();
// and turn on existing note events
this.noteEvents.forEach(noteEvent => {
noteEvent.attack();
});
} else if (oldMode === ArpeggiatorMode.OFF &&
newMode !== ArpeggiatorMode.OFF) {
// else if the arpeggiator was off and we're turning it on
// if there are existing (active) note events turn them all off
this.noteEvents.forEach(noteEvent => {
noteEvent.release();
});
// start the arpeggiator IF there are note events
if (this.noteEvents.length > 0) {
this.arpeggiatorStepSequencer.start();
}
}
}
setBPM(bpm) {
this.arpeggiatorStepSequencer.setBPM(bpm);
}
getBPM() {
return this.arpeggiatorStepSequencer.getBPM();
}
// set the arpeggiation trigger rate
setArpeggiatorTimeDivision(timeDivision) {
// you better pass in a valid TimeDivision enum value lest explosion
this.arpeggiatorStepSequencer.setStepsPerBeat(timeDivision);
}
// set the arpeggiation gate time
setArpeggiatorGateTime(percentage) {
this.arpeggiatorStepSequencer.setGateTime(percentage);
}
// sets the # of repeats in the delay effect, may be 0 to 9 where 0 means
// delay effect is off and greater than 9 is ignored
setDelayRepeats(numberOfRepeats) {
this.delayRepeats = Math.max(0, Math.min(numberOfRepeats, 9));
// update arpeggiator delay repeats
this.arpeggiatorStepSequencer.setDelayRepeats(numberOfRepeats);
}
// sets delay time in milliseconds
setDelayTimeInMilliseconds(delayTimeInMilliseconds) {
// make sure it's greater than or equal to 0
delayTimeInMilliseconds = Math.max(0.0, delayTimeInMilliseconds);
this.delayTimeInMilliseconds = delayTimeInMilliseconds;
// update arpeggiator delay time
this.arpeggiatorStepSequencer.setDelayTimeInMilliseconds(delayTimeInMilliseconds);
}
// sets latch
setLatch(latchOn) {
if (this.latchOn && !latchOn) {
// if we're turning latch from on to off
// release everything that was latched
this.releaseLatchedNotes();
}
this.latchOn = latchOn;
}
// release every note event
releaseEverything() {
this.noteEvents.forEach(noteEvent => noteEvent.release());
this.noteEvents = [];
this.updateArpeggiatorSequence();
}
// send death wall
hailMary() {
this.releaseEverything();
let messages = [];
for (let channel = 0; channel < 16; ++channel) {
for (let pitch = 0; pitch < 128; ++pitch) {
messages.push(0b10000000 | channel, pitch, 0);
}
}
// should we call the logging callback here to log this nuke?
this.midiOutput.send(messages);
}
// sets the midi output (where midi data is sent)
setMidiOutput(midiOutput) {
// we have to shutdown *everything* before we change the midi output
// otherwise this could cause a sticky note (note off never sent)
this.releaseEverything();
this.midiOutput = midiOutput;
// update the arpeggiator's midi output
this.arpeggiatorStepSequencer.setMidiOutput(midiOutput);
}
// sets a callback to trigger when midi messages are sent out
// intended as a logging mechanism
setLoggingCallback(callback) {
this.loggingCallback = callback;
this.arpeggiatorStepSequencer.setLoggingCallback(callback);
}
}