11'use strict' ;
22define ( function ( require ) {
33
4-
54 var p5sound = require ( 'master' ) ;
65 var TimelineSignal = require ( 'Tone/signal/TimelineSignal' ) ;
7- require ( 'sndcore' ) ;
8-
6+ var noteToFreq = require ( 'helpers' ) . noteToFreq ;
97
108 /**
119 * An AudioVoice is used as a single voice for sound synthesis.
@@ -247,30 +245,27 @@ define(function (require) {
247245 * </code></div>
248246 */
249247 p5 . PolySynth . prototype . noteAttack = function ( _note , _velocity , secondsFromNow ) {
250- var now = p5sound . audiocontext . currentTime ;
251-
252248 //this value goes to the audiovoices which handle their own scheduling
253- var tFromNow = secondsFromNow || 0 ;
249+ var secondsFromNow = ~ ~ secondsFromNow ;
254250
255251 //this value is used by this._voicesInUse
256- var t = now + tFromNow ;
252+ var acTime = p5sound . audiocontext . currentTime + secondsFromNow ;
257253
258254 //Convert note to frequency if necessary. This is because entries into this.notes
259255 //should be based on frequency for the sake of consistency.
260- var note = typeof _note === 'string' ? this . AudioVoice . prototype . _setNote ( _note )
261- : typeof _note === 'number' ? _note : 440 ;
262- var velocity = _velocity === undefined ? 1 : _velocity ;
256+ var note = noteToFreq ( _note ) ;
257+ var velocity = _velocity || 0.1 ;
263258
264259 var currentVoice ;
265260
266261 //Release the note if it is already playing
267- if ( this . notes [ note ] !== undefined && this . notes [ note ] . getValueAtTime ( t ) !== null ) {
268- this . noteRelease ( note , 0 ) ;
262+ if ( this . notes [ note ] && this . notes [ note ] . getValueAtTime ( acTime ) !== null ) {
263+ this . noteRelease ( note , 0 ) ;
269264 }
270265
271266 //Check to see how many voices are in use at the time the note will start
272- if ( this . _voicesInUse . getValueAtTime ( t ) < this . maxVoices ) {
273- currentVoice = Math . max ( ~ ~ this . _voicesInUse . getValueAtTime ( t ) , 0 ) ;
267+ if ( this . _voicesInUse . getValueAtTime ( acTime ) < this . maxVoices ) {
268+ currentVoice = Math . max ( ~ ~ this . _voicesInUse . getValueAtTime ( acTime ) , 0 ) ;
274269 }
275270 //If we are exceeding the polyvalue, bump off the oldest notes and replace
276271 //with a new note
@@ -285,23 +280,23 @@ define(function (require) {
285280 //Overrite the entry in the notes object. A note (frequency value)
286281 //corresponds to the index of the audiovoice that is playing it
287282 this . notes [ note ] = new TimelineSignal ( ) ;
288- this . notes [ note ] . setValueAtTime ( currentVoice , t ) ;
283+ this . notes [ note ] . setValueAtTime ( currentVoice , acTime ) ;
289284
290285 //Find the scheduled change in this._voicesInUse that will be previous to this new note
291286 //Add 1 and schedule this value at time 't', when this note will start playing
292- var previousVal = this . _voicesInUse . _searchBefore ( t ) === null ? 0 : this . _voicesInUse . _searchBefore ( t ) . value ;
293- this . _voicesInUse . setValueAtTime ( previousVal + 1 , t ) ;
287+ var previousVal = this . _voicesInUse . _searchBefore ( acTime ) === null ? 0 : this . _voicesInUse . _searchBefore ( acTime ) . value ;
288+ this . _voicesInUse . setValueAtTime ( previousVal + 1 , acTime ) ;
294289
295290 //Then update all scheduled values that follow to increase by 1
296- this . _updateAfter ( t , 1 ) ;
291+ this . _updateAfter ( acTime , 1 ) ;
297292
298293 this . _newest = currentVoice ;
299294 //The audiovoice handles the actual scheduling of the note
300295 if ( typeof velocity === 'number' ) {
301- var maxRange = 1 / this . _voicesInUse . getValueAtTime ( t ) * 2 ;
296+ var maxRange = 1 / this . _voicesInUse . getValueAtTime ( acTime ) * 2 ;
302297 velocity = velocity > maxRange ? maxRange : velocity ;
303298 }
304- this . audiovoices [ currentVoice ] . triggerAttack ( note , velocity , tFromNow ) ;
299+ this . audiovoices [ currentVoice ] . triggerAttack ( note , velocity , secondsFromNow ) ;
305300 } ;
306301
307302 /**
@@ -368,16 +363,16 @@ define(function (require) {
368363 } ) ;
369364 this . _voicesInUse . setValueAtTime ( 0 , t ) ;
370365 for ( var n in this . notes ) {
371- this . notes [ n ] . setValueAtTime ( null , t )
366+ this . notes [ n ] . dispose ( ) ;
367+ delete this . notes [ n ] ;
372368 }
373369 return ;
374370 }
375371
376372 //Make sure note is in frequency inorder to query the this.notes object
377- var note = typeof _note === 'string' ? this . AudioVoice . prototype . _setNote ( _note )
378- : typeof _note === 'number' ? _note : this . audiovoices [ this . _newest ] . oscillator . freq ( ) . value ;
373+ var note = noteToFreq ( _note ) ;
379374
380- if ( this . notes [ note ] . getValueAtTime ( t ) === null ) {
375+ if ( ! this . notes [ note ] || this . notes [ note ] . getValueAtTime ( t ) === null ) {
381376 console . warn ( 'Cannot release a note that is not already playing' ) ;
382377 } else {
383378 //Find the scheduled change in this._voicesInUse that will be previous to this new note
@@ -390,7 +385,8 @@ define(function (require) {
390385 }
391386
392387 this . audiovoices [ this . notes [ note ] . getValueAtTime ( t ) ] . triggerRelease ( tFromNow ) ;
393- this . notes [ note ] . setValueAtTime ( null , t ) ;
388+ this . notes [ note ] . dispose ( ) ;
389+ delete this . notes [ note ] ;
394390
395391 this . _newest = this . _newest === 0 ? 0 : ( this . _newest - 1 ) % ( this . maxVoices - 1 ) ;
396392 }
0 commit comments