2
2
use std:: any:: Any ;
3
3
use std:: f64:: consts:: { PI , SQRT_2 } ;
4
4
5
+ use arrayvec:: ArrayVec ;
5
6
use num_complex:: Complex ;
6
7
7
8
use crate :: context:: { AudioContextRegistration , AudioParamId , BaseAudioContext } ;
@@ -396,10 +397,7 @@ impl BiquadFilterNode {
396
397
frequency : f_proc,
397
398
q : q_proc,
398
399
type_,
399
- x1 : Vec :: with_capacity ( MAX_CHANNELS ) ,
400
- x2 : Vec :: with_capacity ( MAX_CHANNELS ) ,
401
- y1 : Vec :: with_capacity ( MAX_CHANNELS ) ,
402
- y2 : Vec :: with_capacity ( MAX_CHANNELS ) ,
400
+ xy : ArrayVec :: new ( ) ,
403
401
} ;
404
402
405
403
let node = Self {
@@ -549,10 +547,7 @@ struct BiquadFilterRenderer {
549
547
/// `BiquadFilterType`
550
548
type_ : BiquadFilterType ,
551
549
// keep filter state for each channel
552
- x1 : Vec < f64 > ,
553
- x2 : Vec < f64 > ,
554
- y1 : Vec < f64 > ,
555
- y2 : Vec < f64 > ,
550
+ xy : ArrayVec < [ f64 ; 4 ] , MAX_CHANNELS > ,
556
551
}
557
552
558
553
impl AudioProcessor for BiquadFilterRenderer {
@@ -572,10 +567,10 @@ impl AudioProcessor for BiquadFilterRenderer {
572
567
if input. is_silent ( ) {
573
568
let mut ended = true ;
574
569
575
- if self . x1 . iter ( ) . any ( | & v| v . is_normal ( ) )
576
- || self . x2 . iter ( ) . any ( | & v| v . is_normal ( ) )
577
- || self . y1 . iter ( ) . any ( | & v| v . is_normal ( ) )
578
- || self . y2 . iter ( ) . any ( | & v| v . is_normal ( ) )
570
+ if self
571
+ . xy
572
+ . iter ( )
573
+ . any ( |v| v . iter ( ) . copied ( ) . any ( f64 :: is_normal ) )
579
574
{
580
575
ended = false ;
581
576
}
@@ -595,16 +590,16 @@ impl AudioProcessor for BiquadFilterRenderer {
595
590
// see https://webaudio.github.io/web-audio-api/#channels-tail-time
596
591
let num_channels = input. number_of_channels ( ) ;
597
592
598
- if num_channels != self . x1 . len ( ) {
599
- self . x1 . resize ( num_channels, 0. ) ;
600
- self . x2 . resize ( num_channels, 0. ) ;
601
- self . y1 . resize ( num_channels , 0. ) ;
602
- self . y2 . resize ( num_channels , 0. ) ;
593
+ if num_channels != self . xy . len ( ) {
594
+ self . xy . truncate ( num_channels) ;
595
+ for _ in self . xy . len ( ) .. num_channels {
596
+ self . xy . push ( [ 0. ; 4 ] ) ;
597
+ }
603
598
}
604
599
605
600
output. set_number_of_channels ( num_channels) ;
606
601
} else {
607
- let num_channels = self . x1 . len ( ) ;
602
+ let num_channels = self . xy . len ( ) ;
608
603
output. set_number_of_channels ( num_channels) ;
609
604
}
610
605
@@ -655,10 +650,9 @@ impl AudioProcessor for BiquadFilterRenderer {
655
650
input. channel_data ( channel_number)
656
651
} ;
657
652
// retrieve state from previous block
658
- let mut x1 = self . x1 [ channel_number] ;
659
- let mut x2 = self . x2 [ channel_number] ;
660
- let mut y1 = self . y1 [ channel_number] ;
661
- let mut y2 = self . y2 [ channel_number] ;
653
+ let ( mut x1, mut x2, mut y1, mut y2) = match self . xy [ channel_number] {
654
+ [ x1, x2, y1, y2] => ( x1, x2, y1, y2) ,
655
+ } ;
662
656
663
657
output_channel
664
658
. iter_mut ( )
@@ -680,10 +674,7 @@ impl AudioProcessor for BiquadFilterRenderer {
680
674
} ) ;
681
675
682
676
// store channel state for next block
683
- self . x1 [ channel_number] = x1;
684
- self . x2 [ channel_number] = x2;
685
- self . y1 [ channel_number] = y1;
686
- self . y2 [ channel_number] = y2;
677
+ self . xy [ channel_number] = [ x1, x2, y1, y2] ;
687
678
}
688
679
689
680
true
0 commit comments