@@ -7,33 +7,35 @@ import Panner from './panner';
7
7
// SIGNAL MATH FOR MODULATION //
8
8
// ========================== //
9
9
10
- // return sigChain(this, scale, thisChain, nextChain, Scale);
11
- function sigChain ( o , mathObj , thisChain , nextChain , type ) {
12
- var chainSource = o . oscillator ;
13
- // if this type of math already exists in the chain, replace it
14
- for ( var i in o . mathOps ) {
15
- if ( o . mathOps [ i ] instanceof type ) {
16
- chainSource . disconnect ( ) ;
17
- o . mathOps [ i ] . dispose ( ) ;
18
- thisChain = i ;
19
- // assume nextChain is output gain node unless...
20
- if ( thisChain < o . mathOps . length - 2 ) {
21
- nextChain = o . mathOps [ i + 1 ] ;
22
- }
10
+ function sigChain ( nodes , newNode , nodeType , input , output ) {
11
+ var prevNode = null ;
12
+ var nextNode = null ;
13
+ var replacedNode = null ;
14
+ // If nodes already contains an node of type nodeType, replace that node
15
+ // with newNode.
16
+ for ( var i = 0 ; i < nodes . length ; i ++ ) {
17
+ if ( nodes [ i ] instanceof nodeType ) {
18
+ prevNode = i === 0 ? input : nodes [ i - 1 ] ;
19
+ nextNode = i === nodes . length - 1 ? output : nodes [ i + 1 ] ;
20
+ replacedNode = nodes [ i ] ;
21
+ nodes [ i ] = newNode ;
22
+ break ;
23
23
}
24
24
}
25
- if ( thisChain === o . mathOps . length - 1 ) {
26
- o . mathOps . push ( nextChain ) ;
25
+ // Otherwise, add newMathOp to the end of mathOps.
26
+ if ( replacedNode === null ) {
27
+ prevNode = nodes . length === 0 ? input : nodes [ nodes . length - 1 ] ;
28
+ nextNode = output ;
29
+ nodes . push ( newNode ) ;
27
30
}
28
- // assume source is the oscillator unless i > 0
29
- if ( i > 0 ) {
30
- chainSource = o . mathOps [ i - 1 ] ;
31
+ // Connect the newMathOp to the previous and next nodes.
32
+ prevNode . disconnect ( ) ;
33
+ if ( replacedNode !== null ) {
34
+ replacedNode . disconnect ( ) ;
35
+ replacedNode . dispose ( ) ;
31
36
}
32
- chainSource . disconnect ( ) ;
33
- chainSource . connect ( mathObj ) ;
34
- mathObj . connect ( nextChain ) ;
35
- o . mathOps [ thisChain ] = mathObj ;
36
- return o ;
37
+ prevNode . connect ( newNode ) ;
38
+ newNode . connect ( nextNode ) ;
37
39
}
38
40
39
41
/**
@@ -137,8 +139,9 @@ class Oscillator {
137
139
138
140
this . panner = new Panner ( ) ;
139
141
this . output . connect ( this . panner ) ;
140
- //array of math operation signal chaining
141
- this . mathOps = [ this . output ] ;
142
+
143
+ // array of math operation signal chaining
144
+ this . mathOps = [ ] ;
142
145
143
146
// add to the soundArray so we can dispose of the osc later
144
147
p5sound . soundArray . push ( this ) ;
@@ -496,9 +499,8 @@ class Oscillator {
496
499
*/
497
500
add ( num ) {
498
501
var add = new Add ( num ) ;
499
- var thisChain = this . mathOps . length - 1 ;
500
- var nextChain = this . output ;
501
- return sigChain ( this , add , thisChain , nextChain , Add ) ;
502
+ sigChain ( this . mathOps , add , Add , this . oscillator , this . output ) ;
503
+ return this ;
502
504
}
503
505
/**
504
506
* Multiply the p5.Oscillator's output amplitude
@@ -513,9 +515,8 @@ class Oscillator {
513
515
*/
514
516
mult ( num ) {
515
517
var mult = new Mult ( num ) ;
516
- var thisChain = this . mathOps . length - 1 ;
517
- var nextChain = this . output ;
518
- return sigChain ( this , mult , thisChain , nextChain , Mult ) ;
518
+ sigChain ( this . mathOps , mult , Mult , this . oscillator , this . output ) ;
519
+ return this ;
519
520
}
520
521
521
522
/**
@@ -535,19 +536,15 @@ class Oscillator {
535
536
scale ( inMin , inMax , outMin , outMax ) {
536
537
var mapOutMin , mapOutMax ;
537
538
if ( arguments . length === 4 ) {
538
- mapOutMin = p5 . prototype . map ( outMin , inMin , inMax , 0 , 1 ) - 0.5 ; //find a way to get rid of p5.prototype ?
539
- mapOutMax = p5 . prototype . map ( outMax , inMin , inMax , 0 , 1 ) - 0.5 ; //find a way to get rid of p5.prototype ?
539
+ mapOutMin = p5 . prototype . map ( 0 , inMin , inMax , outMin , outMax ) ;
540
+ mapOutMax = p5 . prototype . map ( 1 , inMin , inMax , outMin , outMax ) ;
540
541
} else {
541
542
mapOutMin = arguments [ 0 ] ;
542
543
mapOutMax = arguments [ 1 ] ;
543
544
}
544
545
var scale = new Scale ( mapOutMin , mapOutMax ) ;
545
- var thisChain = this . mathOps . length - 1 ;
546
- var nextChain = this . output ;
547
- return sigChain ( this , scale , thisChain , nextChain , Scale ) ;
548
-
549
- // this.output.disconnect();
550
- // this.output.connect(scale)
546
+ sigChain ( this . mathOps , scale , Scale , this . oscillator , this . output ) ;
547
+ return this ;
551
548
}
552
549
}
553
550
0 commit comments