@@ -229,7 +229,7 @@ const MAX_DELAY_TIME = 10;
229229export class Delay {
230230 writeIdx = 0 ;
231231 readIdx = 0 ;
232- buffer = new Float32Array ( MAX_DELAY_TIME * SAMPLE_RATE ) ; // .fill(0)
232+ buffer = new Float32Array ( MAX_DELAY_TIME * SAMPLE_RATE ) ; //.fill(0)
233233 write ( s , delayTime ) {
234234 this . writeIdx = ( this . writeIdx + 1 ) % this . buffer . length ;
235235 this . buffer [ this . writeIdx ] = s ;
@@ -479,19 +479,13 @@ export class DoughVoice {
479479 this . density = this . density ?? getDefaultValue ( 'density' ) ;
480480 this . fanchor = this . fanchor ?? getDefaultValue ( 'fanchor' ) ;
481481 this . drive = this . drive ?? 0.69 ;
482- this . resonance = this . resonance ?? getDefaultValue ( 'resonance' ) ;
483- this . hresonance = this . hresonance ?? getDefaultValue ( 'hresonance' ) ;
484- this . bandq = this . bandq ?? getDefaultValue ( 'bandq' ) ;
485482 this . phaserdepth = this . phaserdepth ?? getDefaultValue ( 'phaserdepth' ) ;
486483 this . shapevol = this . shapevol ?? getDefaultValue ( 'shapevol' ) ;
487484 this . distortvol = this . distortvol ?? getDefaultValue ( 'distortvol' ) ;
488- this . delay = this . delay ?? getDefaultValue ( 'delay' ) ;
489- this . delayfeedback = this . delayfeedback ?? getDefaultValue ( 'delayfeedback' ) ;
490- this . delaytime = this . delaytime ?? getDefaultValue ( 'delaytime' ) ;
491- this . orbit = this . orbit ?? getDefaultValue ( 'orbit' ) ;
492485 this . i = this . i ?? getDefaultValue ( 'i' ) ;
493486 this . fft = this . fft ?? getDefaultValue ( 'fft' ) ;
494487 this . pan = this . pan ?? getDefaultValue ( 'pan' ) ;
488+ this . orbit = this . orbit ?? getDefaultValue ( 'orbit' ) ;
495489
496490 [ this . attack , this . decay , this . sustain , this . release ] = getADSRValues ( [
497491 this . attack ,
@@ -508,6 +502,7 @@ export class DoughVoice {
508502
509503 // filter setup
510504 this . _lpf = this . cutoff ? new TwoPoleFilter ( ) : null ;
505+ this . resonance = this . resonance ?? getDefaultValue ( 'resonance' ) ;
511506 if ( this . lpenv ) {
512507 this . _lpenv = new ADSR ( ) ;
513508 [ this . lpattack , this . lpdecay , this . lpsustain , this . lprelease ] = getADSRValues ( [
@@ -519,6 +514,7 @@ export class DoughVoice {
519514 }
520515
521516 this . _hpf = this . hcutoff ? new TwoPoleFilter ( ) : null ;
517+ this . hresonance = this . hresonance ?? getDefaultValue ( 'hresonance' ) ;
522518 if ( this . hpenv ) {
523519 this . _hpenv = new ADSR ( ) ;
524520 [ this . hpattack , this . hpdecay , this . hpsustain , this . hprelease ] = getADSRValues ( [
@@ -529,6 +525,7 @@ export class DoughVoice {
529525 ] ) ;
530526 }
531527 this . _bpf = this . bandf ? new TwoPoleFilter ( ) : null ;
528+ this . bandq = this . bandq ?? getDefaultValue ( 'bandq' ) ;
532529 if ( this . bpenv ) {
533530 this . _bpenv = new ADSR ( ) ;
534531 [ this . bpattack , this . bpdecay , this . bpsustain , this . bprelease ] = getADSRValues ( [
@@ -547,6 +544,12 @@ export class DoughVoice {
547544 this . _crush = this . crush ? new Crush ( ) : null ;
548545 this . _distort = this . distort ? new Distort ( ) : null ;
549546
547+ // delay
548+ this . delay = this . delay ?? getDefaultValue ( 'delay' ) ;
549+ this . delayfeedback = this . delayfeedback ?? getDefaultValue ( 'delayfeedback' ) ;
550+ this . delaytime = this . delaytime ?? getDefaultValue ( 'delaytime' ) ;
551+
552+ // precalculated values
550553 this . piOverSr = Math . PI / value . sampleRate ;
551554 this . eighthOverLogHalf = 0.125 / Math . log ( 0.5 ) ;
552555 }
@@ -631,12 +634,17 @@ export class Dough {
631634 vid = 0 ;
632635 q = [ ] ;
633636 channels = [ 0 , 0 ] ;
637+ delaysend = [ 0 , 0 ] ;
638+ delaytime = getDefaultValue ( 'delaytime' ) ;
639+ delayfeedback = getDefaultValue ( 'delayfeedback' ) ;
634640 t = 0 ;
635641 // sampleRate: number, currentTime: number (seconds)
636642 constructor ( sampleRate = 48000 , currentTime = 0 ) {
637643 this . sampleRate = sampleRate ;
638644 this . t = Math . floor ( currentTime * sampleRate ) ; // samples
639645 // console.log('init dough', this.sampleRate, this.t);
646+ this . _delayL = new Delay ( ) ;
647+ this . _delayR = new Delay ( ) ;
640648 }
641649 scheduleSpawn ( value ) {
642650 if ( value . _begin === undefined ) {
@@ -696,7 +704,18 @@ export class Dough {
696704 this . voices [ v ] . update ( this . t / this . sampleRate ) ;
697705 this . channels [ 0 ] += this . voices [ v ] . l ;
698706 this . channels [ 1 ] += this . voices [ v ] . r ;
707+ if ( this . voices [ v ] . delay ) {
708+ this . delaysend [ 0 ] += this . voices [ v ] . l * this . voices [ v ] . delay ;
709+ this . delaysend [ 1 ] += this . voices [ v ] . r * this . voices [ v ] . delay ;
710+ }
699711 }
712+ // todo: how to change delaytime / delayfeedback from a voice?
713+ const delayL = this . _delayL . update ( this . delaysend [ 0 ] , this . delaytime ) ;
714+ const delayR = this . _delayR . update ( this . delaysend [ 1 ] , this . delaytime ) ;
715+ this . delaysend [ 0 ] = delayL * this . delayfeedback ;
716+ this . delaysend [ 1 ] = delayR * this . delayfeedback ;
717+ this . channels [ 0 ] += delayL ;
718+ this . channels [ 1 ] += delayR ;
700719 this . t ++ ;
701720 }
702721}
0 commit comments