-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern.js
392 lines (370 loc) · 11.3 KB
/
pattern.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
"use strict";
// A pattern is a series of intervals with probabilities, a scale, and a
// function to play the notes of the scale with the probabilities assigned
var teoria = require('teoria');
var _ = require('underscore');
var utils = require('./utils');
var probabilities = {
'low': 0.5,
'medium': 0.75,
'high': 1
}
var invertedcolors = {};
var colors = _.each(_.filter(_.map(require('midi-launchpad').colors, function(colour){
if (_.isObject(colour)){
return _.invert(colour);
}
})), function(obj){_.extend(this, obj)}, invertedcolors);
var Pattern = function(pattern){
var defaults = {
voices: 8,
scale: _.range(60, 68),
probability: 100,
type: "Pattern",
name: "Pattern"
}
this.repr = _.extend(defaults, pattern || {});
this.stackMe = function(){
if (this.repr.stack){
// If a pattern has a stack of other pattern types extend it here, omitting
// the stack to avoid recursive death.
_.each(this.repr.stack, function(patternType){
var stackee = _.omit(this.repr, "stack");
// the type becomes a list
var originalType = _.flatten([this.repr.type]);
stackee.type = patternType;
var x = _.extend(this, patternFactory(stackee));
this.repr.type = originalType;
this.repr.type.push(patternType);
}, this);
}
};
this.midiMessages = {
'note_on': 143,
'note_off': 127
}
this.notes = [[], [], [], [], [], [], [], []];
this._makenote = function(button, color, probability, accented, name){
return {
button: button,
midi: this.repr.scale[button.y],
accented: accented || false,
colour: color,
probability: probability
};
};
this.toJSON = function(){
var jsonMe = {notes: this.notes}
_.extend(jsonMe, this.repr);
return jsonMe;
};
this.addnote = function(button, color, probability, accented, name){
// Add a note to the pattern
if (button.y < this.repr.scale.length){
var note = this._makenote(
button,
color,
probability * this.repr.probability,
accented
);
if (this.notes[button.x].length == this.repr.voices){
// Patterns can only have as many notes as voices
this.dropnote(this.notes[button.x].shift().button);
}
this.notes[button.x].push(note);
button.light(note.colour);
}
};
this.dropnote = function(button){
this.notes[button.x] = _.filter(
this.notes[button.x],
function(note){return note.button != button;}
);
button.dark();
};
this.velocity = function(note){
return note.accented ? 120 : 90;
};
var pattern = this;
this.play = function(arg){
// gets called in the context of the output from the sequencer event
if (!arg.muted){
var output = this;
// stop the previous note(s)
if (arg.position > 0){
pattern.sendmidi(arg.position - 1, 'note_off', output);
} else {
pattern.sendmidi(7, 'note_off', output);
}
// play the current note(s)
pattern.sendmidi(arg.position, 'note_on', output);
}
}
this.sendmidi = function(beat, message, output){
var ticknotes = this.notes[beat];
_.each(ticknotes, function(note){
var play = _.has(this.midiMessages, message);
var chance = _.random(0, 100);
if (message == 'note_on' && chance > note.probability){
play = false
}
if (play){
var packet = [
this.midiMessages[message] + output.midiChannel,
note.midi,
this.velocity(note)
];
output.midiOutput.sendMessage(packet);
}
}, this);
};
// Now that everything is initialised, deal with the stacking
this.stackMe();
};
function createscale(pattern){
var key = pattern.key || "g4";
var scale = teoria.note(key).scale(pattern.scale || "dorian").notes();
// TODO: repeat this to make 8 note scales?
scale.push(scale[0].interval('P8'));
// Keeping 'natural' numbering of keypads means reversing the scale so
// highest note is highest pad
scale.reverse();
return _.last(scale, 8);
}
var PingPongPattern = function(pattern){
var defaults = {name: 'PingPongPattern', type: 'PingPongPattern'};
_.extend(
this,
new Pattern(_.extend(defaults, pattern))
);
var pattern = this;
var normalPlay = this.play;
this.play = function(arg){
var output = this;
var p = _.bind(normalPlay, output);
p(arg);
if (arg.position == 7){
pattern.notes.reverse();
console.log('flipped');
// TODO: fire a render event here - to be caught by the layout
}
}
};
var ScalePattern = function(pattern){
var defaults = {name: 'ScalePattern', type: 'ScalePattern'};
_.extend(
this,
new Pattern(_.extend(defaults, pattern))
);
this.repr.scale = createscale(this.repr);
console.log(this.repr.scale.toString());
this._makenote = function(button, color, probability, accented){
// Add a note to the pattern
return {
button: button,
midi: this.repr.scale[button.y].midi(),
accented: accented || false,
colour: color,
probability: probability
};
};
};
var ChordPattern = function(pattern){
var defaults = {name: 'ChordPattern', type: 'ChordPattern'};
_.extend(
this,
new Pattern(_.extend(defaults, pattern))
);
this.repr.chords = this.repr.chords.reverse();
this._makenote = function(button, color, probability, accented){
// Add a chord to the pattern
if (button.y <= this.repr.chords.length){
var notes = [];
if(this.repr.chords[button.y].chord){
var chord = teoria.chord(
this.repr.chords[button.y].chord,
this.repr.chords[button.y].octave
)
notes = _.map(
chord.notes(),
function(note){return note.midi()}
);
} else if (this.repr.chords[button.y].notes){
notes = _.map(
this.repr.chords[button.y].notes,
function(name){
return teoria.note(name).midi()
}
);
} else if (this.repr.chords[button.y].midi){
notes = this.repr.chords[button.y].midi;
}
return {
button: button,
chord: chord,
midi: notes,
accented: accented || false,
colour: color,
probability: probability
};
}
};
var pattern = this;
this.play = function(arg){
// gets called in the context of the output from the sequencer event
if (!arg.muted){
var output = this;
// stop the previous note(s)
if (arg.position > 0){
pattern.sendmidi(arg.position - 1, 'note_off', output);
} else {
pattern.sendmidi(7, 'note_off', output);
}
// play the current note(s)
pattern.sendmidi(arg.position, 'note_on', output);
}
};
this.sendmidi = function(beat, message, output){
var ticknotes = this.notes[beat];
_.each(ticknotes, function(note, idx, list){
var play = _.has(this.midiMessages, message);
var chance = _.random(0, 100);
if (message == 'note_on' && chance > note.probability){
play = false
}
if (play){
_.each(note.midi, function(note){
var packet = [
this.midiMessages[message] + output.midiChannel,
note,
this.velocity(note)
];
output.midiOutput.sendMessage(packet);
}, this);
}
}, this);
};
};
var VolcaDrumPattern = function(pattern){
// Midi notes for the drums on the volca, missing out the toms
// midi implemenation http://media.aadl.org/files/catalog_guides/1445131_chart.pdf
// http://www.midi.org/techspecs/midimessages.php
var defaults = {
name: 'VolcaDrumPattern',
type: 'VolcaDrumPattern',
scale:[75, 67, 49, 39, 46, 42, 38, 36]
}
_.extend(
this,
new Pattern(_.extend(defaults, pattern))
);
var pattern = this;
this.play = function(arg){
if (!arg.muted){
// gets called in the context of the output from the sequencer event
var output = this;
// don't need to stop previous notes on the volca, so only send note_on
pattern.sendmidi(arg.position, 'note_on', output);
}
}
};
var VolcaSamplePattern = function(pattern){
// The Sample plays the same sample, regardless of note. It uses midi channels
// for different notes
var defaults = {name: 'VolcaSamplePattern', type: 'VolcaSamplePattern'};
_.extend(
this,
new Pattern(_.extend(defaults, pattern))
);
var pattern = this;
this.play = function(arg){
if (!arg.muted){
// gets called in the context of the output from the sequencer event
var output = this;
// don't need to stop previous notes on the volca, so only send note_on
pattern.sendmidi(arg.position, 'note_on', output);
}
}
this.sendmidi = function(beat, message, output){
var ticknotes = this.notes[beat];
_.each(ticknotes, function(note){
var play = _.has(this.midiMessages, message);
var chance = _.random(0, 100);
if (message == 'note_on' && chance > note.probability){
play = false
}
if (play){
var packet = [
this.midiMessages[message] + output.midiChannel + (7 - note.button.y),
note.midi,
this.velocity(note)
];
output.midiOutput.sendMessage(packet);
}
}, this);
};
};
var TestPattern = function(pattern){
var defaults = {name: 'TestPattern', type: 'TestPattern', foo: 'bar'};
_.extend(
this,
new Pattern(_.extend(defaults, pattern || {}))
);
this._makenote = 'test pattern makes a note, and loves life';
this.play = function(arg){
return 'test pattern plays';
};
};
var LockingPattern = function(pattern){
var defaults = {name: 'LockingPattern', type: 'LockingPattern', foo: 'bar'};
_.extend(
this,
new Pattern(_.extend(defaults, pattern || {}))
);
this.playing = -1;
var pattern = this;
this.play = function(arg){
// gets called in the context of the output from the sequencer event
if (!arg.muted){
var output = this;
if (pattern.notes[arg.position].length > 0 && pattern.playing >= 0){
pattern.sendmidi(pattern.playing, 'note_off', output);
}
if (pattern.notes[arg.position].length > 0){
pattern.playing = arg.position;
// play the current note(s)
pattern.sendmidi(arg.position, 'note_on', output);
}
}
};
};
function patternFactory(pattern){
if (_.isUndefined(pattern)){
return new Pattern({});
} else if (pattern.type == 'ScalePattern'){
return new ScalePattern(pattern);
} else if (pattern.type == 'ChordPattern'){
return new ChordPattern(pattern);
} else if (pattern.type == 'PingPongPattern'){
return new PingPongPattern(pattern);
} else if (pattern.type == 'LockingPattern'){
return new LockingPattern(pattern);
} else if (pattern.type == 'VolcaDrumPattern'){
return new VolcaDrumPattern(pattern);
} else if (pattern.type == 'VolcaSamplePattern'){
return new VolcaSamplePattern(pattern);
} else if (pattern.type == 'TestPattern'){
return new TestPattern(pattern);
} else {
return new Pattern(pattern);
};
};
exports.Pattern = Pattern;
exports.ScalePattern = ScalePattern;
exports.ChordPattern = ChordPattern;
exports.PingPongPattern = PingPongPattern;
exports.LockingPattern = LockingPattern;
exports.VolcaDrumPattern = VolcaDrumPattern;
exports.VolcaSamplePattern = VolcaSamplePattern;
exports.TestPattern = TestPattern;
exports.patternFactory = patternFactory;