@@ -9,31 +9,76 @@ define(function (require) {
9
9
*
10
10
* @class p5.SoundLoop
11
11
* @constructor
12
+ *
13
+ * @param {Function } callback this function will be called on each iteration of theloop
14
+ * @param {Number or String } [interval] amount of time or beats for each iteration of the loop
15
+ * defaults to 1
16
+ *
17
+ * @example
18
+ * <div><code>
19
+ * var click;
20
+ * var looper1;
21
+ *
22
+ * function preload() {
23
+ * click = loadSound('assets/drum.mp3'
24
+ * }
25
+ *
26
+ * function setup() {
27
+ * //the looper's callback is passed the timeFromNow
28
+ * //this value should be used as a reference point from
29
+ * //which to schedule sounds
30
+ * looper1 = new p5.SoundLoop(function(timeFromNow){
31
+ * click.play(timeFromNow);
32
+ * background(255 * (looper1.iterations % 2));
33
+ * }, 2);
34
+ *
35
+ * //stop after 10 iteratios;
36
+ * looper1.maxIterations = 10;
37
+ * //start the loop
38
+ * looper1.start();
39
+ * }
40
+ * </code></div>
12
41
*/
13
- p5 . SoundLoop = function ( callback , interval , BPM ) {
14
-
42
+ p5 . SoundLoop = function ( callback , interval ) {
15
43
this . callback = callback ;
16
-
44
+ /**
45
+ * musicalTimeMode uses <a href = "https://github.com/Tonejs/Tone.js/wiki/Time">Tone.Time</a> convention
46
+ * @property {Boolean } musicalTimeMode true if string, false if number
47
+ */
17
48
this . musicalTimeMode = typeof this . _interval === 'number' ? false : true ;
18
- this . _interval = interval ;
19
49
20
- //These variables should only be modified if using musicalTimeMode
21
- //If these variables are modified when interval is measured in seconds,
22
- //the interval will become inaccurate.
23
- //ex. 8 second interval at 60BPM in 4/4 time will be 8 seconds long
24
- //8 second interval at 120BPM in 5/4 time will be 3.2 seconds long
50
+ this . _interval = interval || 1 ;
51
+
52
+ /**
53
+ * musicalTimeMode variables
54
+ * modify these only when the interval is specified in musicalTime format as a string
55
+ * @property {Number } [BPM] beats per minute (defaults to 60)
56
+ * @property {Number } [timeSignature] number of quarter notes in a measure (defaults to 4)
57
+ */
25
58
this . _timeSignature = 4 ;
26
- this . _bpm = BPM || 60 ;
59
+ this . _bpm = 60 ;
27
60
28
61
this . isPlaying = false ;
62
+
63
+ /**
64
+ * Set a limit to the number of loops to play
65
+ * @property {Number } [maxIterations] defaults to Infinity
66
+ */
67
+ this . maxIterations = Infinity ;
29
68
var self = this ;
69
+
30
70
this . clock = new Clock ( {
31
71
'callback' : function ( time ) {
32
72
var timeFromNow = time - p5sound . audiocontext . currentTime ;
33
- //Do not initiate the callback if timeFromNow is < 0
34
- //This ususually occurs for a few milliseconds when the page
35
- //is not fully loaded
36
- if ( timeFromNow > 0 ) { self . callback ( timeFromNow ) ; }
73
+ /**
74
+ * Do not initiate the callback if timeFromNow is < 0
75
+ * This ususually occurs for a few milliseconds when the page
76
+ * is not fully loaded
77
+ *
78
+ * The callback should only be called until maxIterations is reached
79
+ */
80
+ if ( timeFromNow > 0 && self . iterations <= self . maxIterations ) {
81
+ self . callback ( timeFromNow ) ; }
37
82
} ,
38
83
'frequency' : this . _calcFreq ( )
39
84
} ) ;
@@ -67,7 +112,7 @@ define(function (require) {
67
112
}
68
113
} ;
69
114
/**
70
- * Start the loop
115
+ * Pause the loop
71
116
* @method pause
72
117
* @param {Number } [timeFromNow] schedule a pausing time
73
118
*/
@@ -79,14 +124,7 @@ define(function (require) {
79
124
}
80
125
} ;
81
126
82
- //use synced start to start 2 loops at the same time
83
- //OR
84
- //sync the start of one loop with one that is already playing
85
- //loop.syncedStart(someother loop)
86
- //
87
- //
88
- //loopToStart.syncedStart( loopToSyncWtih );
89
- //
127
+
90
128
/**
91
129
* Synchronize loops. Use this method to start two more more loops in synchronization
92
130
* or to start a loop in synchronization with a loop that is already playing
@@ -183,19 +221,20 @@ define(function (require) {
183
221
* frequency, that will be reflected after the next callback
184
222
* @param {Number } bpm
185
223
* @param {Number } timeSignature
186
- * @param {Number/String } interval [description]
224
+ * @param {Number/String } interval length of the loops interval
225
+ * @param @readOnly {Number} iteations how many times the callback has been called so far
226
+ *
187
227
*/
188
228
Object . defineProperty ( p5 . SoundLoop . prototype , 'bpm' , {
189
229
get : function ( ) {
190
230
return this . _bpm ;
191
231
} ,
192
232
set : function ( bpm ) {
193
233
if ( ! this . musicalTimeMode ) {
194
- console . warn ( 'Changing the BPM in "seconds" mode is not advised. ' +
195
- 'This will make the specified time interval inaccurate. ' +
196
- '8 second interval at 60BPM in 4/4 time will be 8 seconds long ' +
197
- '8 second interval at 120BPM in 5/4 time will be 3.2 seconds long. ' +
198
- 'Use musical timing notation ("2n", "4n", "1m"...etc' ) ;
234
+ console . warn ( 'Changing the BPM in "seconds" mode has no effect. ' +
235
+ 'BPM is only relevant in musicalTimeMode ' +
236
+ 'when the interval is specified as a string ' +
237
+ '("2n", "4n", "1m"...etc)' ) ;
199
238
}
200
239
this . _bpm = bpm ;
201
240
this . _update ( ) ;
@@ -208,11 +247,10 @@ define(function (require) {
208
247
} ,
209
248
set : function ( timeSig ) {
210
249
if ( ! this . musicalTimeMode ) {
211
- console . warn ( 'Changing the time signature in "seconds" mode is not advised. ' +
212
- 'This will make the specified time interval inaccurate. ' +
213
- '8 second interval at 60BPM in 4/4 time will be 8 seconds long ' +
214
- '8 second interval at 120BPM in 5/4 time will be 3.2 seconds long. ' +
215
- 'Use musical timing notation ("2n", "4n", "1m"...etc' ) ;
250
+ console . warn ( 'Changing the timeSignature in "seconds" mode has no effect. ' +
251
+ 'BPM is only relevant in musicalTimeMode ' +
252
+ 'when the interval is specified as a string ' +
253
+ '("2n", "4n", "1m"...etc)' ) ;
216
254
}
217
255
this . _timeSignature = timeSig ;
218
256
this . _update ( ) ;
@@ -224,10 +262,17 @@ define(function (require) {
224
262
return this . _interval ;
225
263
} ,
226
264
set : function ( interval ) {
265
+ this . musicalTimeMode = typeof interval === 'Number' ? false : true ;
227
266
this . _interval = interval ;
228
267
this . _update ( ) ;
229
268
}
230
269
} ) ;
231
270
271
+ Object . defineProperty ( p5 . SoundLoop . prototype , 'iterations' , {
272
+ get : function ( ) {
273
+ return this . clock . ticks ;
274
+ }
275
+ } ) ;
276
+
232
277
return p5 . SoundLoop ;
233
278
} ) ;
0 commit comments