-
-
Notifications
You must be signed in to change notification settings - Fork 683
/
Copy pathlooper.js
513 lines (479 loc) · 13.4 KB
/
looper.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
'use strict';
define(function(require) {
var p5sound = require('master');
var BPM = 120;
/**
* Set the global tempo, in beats per minute, for all
* p5.Parts. This method will impact all active p5.Parts.
*
* @method setBPM
* @for p5
* @param {Number} BPM Beats Per Minute
* @param {Number} rampTime Seconds from now
*/
p5.prototype.setBPM = function(bpm, rampTime) {
BPM = bpm;
for (var i in p5sound.parts) {
if (p5sound.parts[i]) {
p5sound.parts[i].setBPM(bpm, rampTime);
}
}
};
/**
* <p>A phrase is a pattern of musical events over time, i.e.
* a series of notes and rests.</p>
*
* <p>Phrases must be added to a p5.Part for playback, and
* each part can play multiple phrases at the same time.
* For example, one Phrase might be a kick drum, another
* could be a snare, and another could be the bassline.</p>
*
* <p>The first parameter is a name so that the phrase can be
* modified or deleted later. The callback is a a function that
* this phrase will call at every step—for example it might be
* called <code>playNote(value){}</code>. The array determines
* which value is passed into the callback at each step of the
* phrase. It can be numbers, an object with multiple numbers,
* or a zero (0) indicates a rest so the callback won't be called).</p>
*
* @class p5.Phrase
* @constructor
* @param {String} name Name so that you can access the Phrase.
* @param {Function} callback The name of a function that this phrase
* will call. Typically it will play a sound,
* and accept two parameters: a time at which
* to play the sound (in seconds from now),
* and a value from the sequence array. The
* time should be passed into the play() or
* start() method to ensure precision.
* @param {Array} sequence Array of values to pass into the callback
* at each step of the phrase.
* @example
* <div><code>
* let mySound, myPhrase, myPart;
* let pattern = [1,0,0,2,0,2,0,0];
*
* function preload() {
* mySound = loadSound('assets/beatbox.mp3');
* }
*
* function setup() {
* let cnv = createCanvas(100, 100);
* cnv.mousePressed(playMyPart);
* background(220);
* text('tap to play', width/2, height/2);
* textAlign(CENTER, CENTER);
*
* myPhrase = new p5.Phrase('bbox', onEachStep, pattern);
* myPart = new p5.Part();
* myPart.addPhrase(myPhrase);
* myPart.setBPM(60);
* }
*
* function onEachStep(time, playbackRate) {
* mySound.rate(playbackRate);
* mySound.play(time);
* }
*
* function playMyPart() {
* userStartAudio();
* myPart.start();
* }
* </code></div>
*/
p5.Phrase = function(name, callback, sequence) {
this.phraseStep = 0;
this.name = name;
this.callback = callback;
/**
* Array of values to pass into the callback
* at each step of the phrase. Depending on the callback
* function's requirements, these values may be numbers,
* strings, or an object with multiple parameters.
* Zero (0) indicates a rest.
*
* @property {Array} sequence
*/
this.sequence = sequence;
};
/**
* <p>A p5.Part plays back one or more p5.Phrases. Instantiate a part
* with steps and tatums. By default, each step represents a 1/16th note.</p>
*
* <p>See p5.Phrase for more about musical timing.</p>
*
* @class p5.Part
* @constructor
* @param {Number} [steps] Steps in the part
* @param {Number} [tatums] Divisions of a beat, e.g. use 1/4, or 0.25 for a quater note (default is 1/16, a sixteenth note)
* @example
* <div><code>
* let box, drum, myPart;
* let boxPat = [1,0,0,2,0,2,0,0];
* let drumPat = [0,1,1,0,2,0,1,0];
*
* function preload() {
* box = loadSound('assets/beatbox.mp3');
* drum = loadSound('assets/drum.mp3');
* }
*
* function setup() {
* let cnv = createCanvas(100, 100);
* cnv.mousePressed(playMyPart);
* background(220);
* textAlign(CENTER, CENTER);
* text('tap to play', width/2, height/2);
*
* let boxPhrase = new p5.Phrase('box', playBox, boxPat);
* let drumPhrase = new p5.Phrase('drum', playDrum, drumPat);
* myPart = new p5.Part();
* myPart.addPhrase(boxPhrase);
* myPart.addPhrase(drumPhrase);
* myPart.setBPM(60);
* }
*
* function playBox(time, playbackRate) {
* box.rate(playbackRate);
* box.play(time);
* }
*
* function playDrum(time, playbackRate) {
* drum.rate(playbackRate);
* drum.play(time);
* }
*
* function playMyPart() {
* userStartAudio();
*
* myPart.start();
* }
* </code></div>
*/
p5.Part = function(steps, bLength) {
this.length = steps || 0; // how many beats
this.partStep = 0;
this.phrases = [];
this.isPlaying = false;
this.noLoop();
this.tatums = bLength || 0.0625; // defaults to quarter note
this.metro = new p5.Metro();
this.metro._init();
this.metro.beatLength(this.tatums);
this.metro.setBPM(BPM);
p5sound.parts.push(this);
this.callback = function() {};
};
/**
* Set the tempo of this part, in Beats Per Minute.
*
* @method setBPM
* @for p5.Part
* @param {Number} BPM Beats Per Minute
* @param {Number} [rampTime] Seconds from now
*/
p5.Part.prototype.setBPM = function(tempo, rampTime) {
this.metro.setBPM(tempo, rampTime);
};
/**
* Returns the tempo, in Beats Per Minute, of this part.
*
* @method getBPM
* @for p5.Part
* @return {Number}
*/
p5.Part.prototype.getBPM = function() {
return this.metro.getBPM();
};
/**
* Start playback of this part. It will play
* through all of its phrases at a speed
* determined by setBPM.
*
* @method start
* @for p5.Part
* @param {Number} [time] seconds from now
*/
p5.Part.prototype.start = function(time) {
if (!this.isPlaying) {
this.isPlaying = true;
this.metro.resetSync(this);
var t = time || 0;
this.metro.start(t);
}
};
/**
* Loop playback of this part. It will begin
* looping through all of its phrases at a speed
* determined by setBPM.
*
* @method loop
* @for p5.Part
* @param {Number} [time] seconds from now
*/
p5.Part.prototype.loop = function(time) {
this.looping = true;
// rest onended function
this.onended = function() {
this.partStep = 0;
};
var t = time || 0;
this.start(t);
};
/**
* Tell the part to stop looping.
*
* @method noLoop
* @for p5.Part
*/
p5.Part.prototype.noLoop = function() {
this.looping = false;
// rest onended function
this.onended = function() {
this.stop();
};
};
/**
* Stop the part and cue it to step 0. Playback will resume from the begining of the Part when it is played again.
*
* @method stop
* @for p5.Part
* @param {Number} [time] seconds from now
*/
p5.Part.prototype.stop = function(time) {
this.partStep = 0;
this.pause(time);
};
/**
* Pause the part. Playback will resume
* from the current step.
*
* @method pause
* @for p5.Part
* @param {Number} time seconds from now
*/
p5.Part.prototype.pause = function(time) {
this.isPlaying = false;
var t = time || 0;
this.metro.stop(t);
};
/**
* Add a p5.Phrase to this Part.
*
* @method addPhrase
* @for p5.Part
* @param {p5.Phrase} phrase reference to a p5.Phrase
*/
p5.Part.prototype.addPhrase = function(name, callback, array) {
var p;
if (arguments.length === 3) {
p = new p5.Phrase(name, callback, array);
} else if (arguments[0] instanceof p5.Phrase) {
p = arguments[0];
} else {
throw 'invalid input. addPhrase accepts name, callback, array or a p5.Phrase';
}
this.phrases.push(p);
// reset the length if phrase is longer than part's existing length
if (p.sequence.length > this.length) {
this.length = p.sequence.length;
}
};
/**
* Remove a phrase from this part, based on the name it was
* given when it was created.
*
* @method removePhrase
* @for p5.Part
* @param {String} phraseName
*/
p5.Part.prototype.removePhrase = function(name) {
for (var i in this.phrases) {
if (this.phrases[i].name === name) {
this.phrases.splice(i, 1);
}
}
};
/**
* Get a phrase from this part, based on the name it was
* given when it was created. Now you can modify its array.
*
* @method getPhrase
* @for p5.Part
* @param {String} phraseName
*/
p5.Part.prototype.getPhrase = function(name) {
for (var i in this.phrases) {
if (this.phrases[i].name === name) {
return this.phrases[i];
}
}
};
/**
* Find all sequences with the specified name, and replace their patterns with the specified array.
*
* @method replaceSequence
* @for p5.Part
* @param {String} phraseName
* @param {Array} sequence Array of values to pass into the callback
* at each step of the phrase.
*/
p5.Part.prototype.replaceSequence = function(name, array) {
for (var i in this.phrases) {
if (this.phrases[i].name === name) {
this.phrases[i].sequence = array;
}
}
};
p5.Part.prototype.incrementStep = function(time) {
if (this.partStep < this.length - 1) {
this.callback(time);
this.partStep += 1;
} else {
if (!this.looping && this.partStep === this.length - 1) {
// this.callback(time);
this.onended();
}
}
};
/**
* Set the function that will be called at every step. This will clear the previous function.
*
* @method onStep
* @for p5.Part
* @param {Function} callback The name of the callback
* you want to fire
* on every beat/tatum.
*/
p5.Part.prototype.onStep = function(callback) {
this.callback = callback;
};
// ===============
// p5.Score
// ===============
/**
* A Score consists of a series of Parts. The parts will
* be played back in order. For example, you could have an
* A part, a B part, and a C part, and play them back in this order
* <code>new p5.Score(a, a, b, a, c)</code>
*
* @class p5.Score
* @constructor
* @param {p5.Part} [...parts] One or multiple parts, to be played in sequence.
*/
p5.Score = function() {
// for all of the arguments
this.parts = [];
this.currentPart = 0;
var thisScore = this;
for (var i in arguments) {
if (arguments[i]) {
this.parts[i] = arguments[i];
this.parts[i].nextPart = this.parts[i + 1];
this.parts[i].onended = function() {
thisScore.resetPart(i);
playNextPart(thisScore);
};
}
}
this.looping = false;
};
p5.Score.prototype.onended = function() {
if (this.looping) {
// this.resetParts();
this.parts[0].start();
}
this.currentPart = 0;
};
/**
* Start playback of the score.
*
* @method start
* @for p5.Score
*/
p5.Score.prototype.start = function() {
this.parts[this.currentPart].start();
this.scoreStep = 0;
};
/**
* Stop playback of the score.
*
* @method stop
* @for p5.Score
*/
p5.Score.prototype.stop = function() {
this.parts[this.currentPart].stop();
this.currentPart = 0;
this.scoreStep = 0;
};
/**
* Pause playback of the score.
*
* @method pause
* @for p5.Score
*/
p5.Score.prototype.pause = function() {
this.parts[this.currentPart].stop();
};
/**
* Loop playback of the score.
*
* @method loop
* @for p5.Score
*/
p5.Score.prototype.loop = function() {
this.looping = true;
this.start();
};
/**
* Stop looping playback of the score. If it
* is currently playing, this will go into effect
* after the current round of playback completes.
*
* @method noLoop
* @for p5.Score
*/
p5.Score.prototype.noLoop = function() {
this.looping = false;
};
p5.Score.prototype.resetParts = function() {
var self = this;
this.parts.forEach(function(part) {
self.resetParts[part];
});
};
p5.Score.prototype.resetPart = function(i) {
this.parts[i].stop();
this.parts[i].partStep = 0;
for (var p in this.parts[i].phrases) {
if (this.parts[i]) {
this.parts[i].phrases[p].phraseStep = 0;
}
}
};
/**
* Set the tempo for all parts in the score
*
* @method setBPM
* @for p5.Score
* @param {Number} BPM Beats Per Minute
* @param {Number} rampTime Seconds from now
*/
p5.Score.prototype.setBPM = function(bpm, rampTime) {
for (var i in this.parts) {
if (this.parts[i]) {
this.parts[i].setBPM(bpm, rampTime);
}
}
};
function playNextPart(aScore) {
aScore.currentPart++;
if (aScore.currentPart >= aScore.parts.length) {
aScore.scoreStep = 0;
aScore.onended();
} else {
aScore.scoreStep = 0;
aScore.parts[aScore.currentPart - 1].stop();
aScore.parts[aScore.currentPart].start();
}
}
});