19
19
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
20
// THE SOFTWARE.
21
21
22
- trait Interpolator : std:: fmt:: Debug {
22
+ pub ( crate ) trait Interpolator : std:: fmt:: Debug {
23
23
fn process ( & mut self , src : & [ f32 ] , dst : & mut [ f32 ] ) ;
24
24
fn reset ( & mut self ) ;
25
25
fn get_factor ( & self ) -> usize ;
@@ -29,21 +29,8 @@ trait Interpolator: std::fmt::Debug {
29
29
pub struct Interp ( Box < dyn Interpolator > ) ;
30
30
31
31
impl Interp {
32
- pub fn new ( taps : usize , factor : usize , channels : u32 ) -> Self {
33
- let imp: Box < dyn Interpolator > = match ( taps, factor, channels) {
34
- ( 49 , 2 , 1 ) => Box :: new ( specialized:: Interp2F :: < [ f32 ; 1 ] > :: new ( ) ) ,
35
- ( 49 , 2 , 2 ) => Box :: new ( specialized:: Interp2F :: < [ f32 ; 2 ] > :: new ( ) ) ,
36
- ( 49 , 2 , 4 ) => Box :: new ( specialized:: Interp2F :: < [ f32 ; 4 ] > :: new ( ) ) ,
37
- ( 49 , 2 , 6 ) => Box :: new ( specialized:: Interp2F :: < [ f32 ; 6 ] > :: new ( ) ) ,
38
- ( 49 , 2 , 8 ) => Box :: new ( specialized:: Interp2F :: < [ f32 ; 8 ] > :: new ( ) ) ,
39
- ( 49 , 4 , 1 ) => Box :: new ( specialized:: Interp4F :: < [ f32 ; 1 ] > :: new ( ) ) ,
40
- ( 49 , 4 , 2 ) => Box :: new ( specialized:: Interp4F :: < [ f32 ; 2 ] > :: new ( ) ) ,
41
- ( 49 , 4 , 4 ) => Box :: new ( specialized:: Interp4F :: < [ f32 ; 4 ] > :: new ( ) ) ,
42
- ( 49 , 4 , 6 ) => Box :: new ( specialized:: Interp4F :: < [ f32 ; 6 ] > :: new ( ) ) ,
43
- ( 49 , 4 , 8 ) => Box :: new ( specialized:: Interp4F :: < [ f32 ; 8 ] > :: new ( ) ) ,
44
- ( taps, factor, channels) => Box :: new ( generic:: Interp :: new ( taps, factor, channels) ) ,
45
- } ;
46
- Self ( imp)
32
+ pub fn new ( _taps : usize , _factor : usize , _channels : u32 ) -> Self {
33
+ unimplemented ! ( )
47
34
}
48
35
49
36
pub fn process ( & mut self , src : & [ f32 ] , dst : & mut [ f32 ] ) {
@@ -59,7 +46,7 @@ impl Interp {
59
46
}
60
47
}
61
48
62
- mod generic {
49
+ pub mod generic {
63
50
use smallvec:: SmallVec ;
64
51
use std:: f64;
65
52
/// Data structure for polyphase FIR interpolator
@@ -209,161 +196,8 @@ mod generic {
209
196
}
210
197
}
211
198
212
- /// A trait to be generic over number of channels in a of frame
213
- ///
214
- /// TODO: Might want to use dasp-frame instead here, but needs
215
- /// coordination with `Samples` trait
216
- trait Frame : Sized + Copy {
217
- const CHANNELS : usize ;
218
-
219
- fn scale_add ( & mut self , other : & Self , coeff : f32 ) ;
220
- fn from_planar ( slice : & [ f32 ] , stride : usize ) -> Self ;
221
- }
222
-
223
- type MonoFrame32 = [ f32 ; 1 ] ;
224
- type StereoFrame32 = [ f32 ; 2 ] ;
225
- type QuadFrame32 = [ f32 ; 4 ] ;
226
- type SurroundFrame32 = [ f32 ; 6 ] ;
227
- type Surround8Frame32 = [ f32 ; 8 ] ;
228
-
229
- impl Frame for MonoFrame32 {
230
- const CHANNELS : usize = 1 ;
231
-
232
- #[ inline( always) ]
233
- fn scale_add ( & mut self , other : & Self , coeff : f32 ) {
234
- for i in 0 ..Self :: CHANNELS {
235
- #[ cfg( feature = "precision-true-peak" ) ]
236
- {
237
- self [ i] = other[ i] . mul_add ( coeff, self [ i] ) ;
238
- }
239
- #[ cfg( not( feature = "precision-true-peak" ) ) ]
240
- {
241
- self [ i] += other[ i] * coeff;
242
- }
243
- }
244
- }
245
-
246
- #[ inline( always) ]
247
- fn from_planar ( slice : & [ f32 ] , _stride : usize ) -> Self {
248
- [ slice[ 0 ] ]
249
- }
250
- }
251
-
252
- impl Frame for StereoFrame32 {
253
- const CHANNELS : usize = 2 ;
254
-
255
- #[ inline( always) ]
256
- fn scale_add ( & mut self , other : & Self , coeff : f32 ) {
257
- for i in 0 ..Self :: CHANNELS {
258
- #[ cfg( feature = "precision-true-peak" ) ]
259
- {
260
- self [ i] = other[ i] . mul_add ( coeff, self [ i] ) ;
261
- }
262
- #[ cfg( not( feature = "precision-true-peak" ) ) ]
263
- {
264
- self [ i] += other[ i] * coeff;
265
- }
266
- }
267
- }
268
-
269
- #[ inline( always) ]
270
- fn from_planar ( slice : & [ f32 ] , stride : usize ) -> Self {
271
- [ slice[ 0 ] , slice[ stride] ]
272
- }
273
- }
274
-
275
- impl Frame for QuadFrame32 {
276
- const CHANNELS : usize = 4 ;
277
-
278
- #[ inline( always) ]
279
- fn scale_add ( & mut self , other : & Self , coeff : f32 ) {
280
- for i in 0 ..Self :: CHANNELS {
281
- #[ cfg( feature = "precision-true-peak" ) ]
282
- {
283
- self [ i] = other[ i] . mul_add ( coeff, self [ i] ) ;
284
- }
285
- #[ cfg( not( feature = "precision-true-peak" ) ) ]
286
- {
287
- self [ i] += other[ i] * coeff;
288
- }
289
- }
290
- }
291
-
292
- #[ inline( always) ]
293
- fn from_planar ( slice : & [ f32 ] , stride : usize ) -> Self {
294
- [
295
- slice[ 0 ] ,
296
- slice[ stride] ,
297
- slice[ 2 * stride] ,
298
- slice[ 3 * stride] ,
299
- ]
300
- }
301
- }
302
-
303
- impl Frame for SurroundFrame32 {
304
- const CHANNELS : usize = 6 ;
305
-
306
- #[ inline( always) ]
307
- fn scale_add ( & mut self , other : & Self , coeff : f32 ) {
308
- for i in 0 ..Self :: CHANNELS {
309
- #[ cfg( feature = "precision-true-peak" ) ]
310
- {
311
- self [ i] = other[ i] . mul_add ( coeff, self [ i] ) ;
312
- }
313
- #[ cfg( not( feature = "precision-true-peak" ) ) ]
314
- {
315
- self [ i] += other[ i] * coeff;
316
- }
317
- }
318
- }
319
-
320
- #[ inline( always) ]
321
- fn from_planar ( slice : & [ f32 ] , stride : usize ) -> Self {
322
- [
323
- slice[ 0 ] ,
324
- slice[ stride] ,
325
- slice[ 2 * stride] ,
326
- slice[ 3 * stride] ,
327
- slice[ 4 * stride] ,
328
- slice[ 5 * stride] ,
329
- ]
330
- }
331
- }
332
-
333
- impl Frame for Surround8Frame32 {
334
- const CHANNELS : usize = 8 ;
335
-
336
- #[ inline( always) ]
337
- fn scale_add ( & mut self , other : & Self , coeff : f32 ) {
338
- for i in 0 ..Self :: CHANNELS {
339
- #[ cfg( feature = "precision-true-peak" ) ]
340
- {
341
- self [ i] = other[ i] . mul_add ( coeff, self [ i] ) ;
342
- }
343
- #[ cfg( not( feature = "precision-true-peak" ) ) ]
344
- {
345
- self [ i] += other[ i] * coeff;
346
- }
347
- }
348
- }
349
-
350
- #[ inline( always) ]
351
- fn from_planar ( slice : & [ f32 ] , stride : usize ) -> Self {
352
- [
353
- slice[ 0 ] ,
354
- slice[ stride] ,
355
- slice[ 2 * stride] ,
356
- slice[ 3 * stride] ,
357
- slice[ 4 * stride] ,
358
- slice[ 5 * stride] ,
359
- slice[ 6 * stride] ,
360
- slice[ 7 * stride] ,
361
- ]
362
- }
363
- }
364
-
365
- mod specialized {
366
- use super :: Frame ;
199
+ pub mod specialized {
200
+ use crate :: utils:: FrameAccumulator ;
367
201
use std:: f64:: consts:: PI ;
368
202
369
203
const ALMOST_ZERO : f64 = 0.000001 ;
@@ -374,18 +208,18 @@ mod specialized {
374
208
const FACTOR2 : usize = 2 ;
375
209
const FACTOR2_INPUT_LENGTH : usize = TAPS / FACTOR2 ;
376
210
377
- #[ derive( Debug ) ]
378
- pub ( super ) struct Interp4F < F : Frame > {
211
+ #[ derive( Debug , Clone ) ]
212
+ pub ( crate ) struct Interp4F < F : FrameAccumulator > {
379
213
filter : [ [ f32 ; FACTOR4 ] ; FACTOR4_INPUT_LENGTH ] ,
380
214
buffer : [ F ; FACTOR4_INPUT_LENGTH ] ,
381
215
buffer_pos : usize ,
382
216
}
383
217
384
218
impl < F > Interp4F < F >
385
219
where
386
- F : Frame + Default ,
220
+ F : FrameAccumulator + Default ,
387
221
{
388
- pub ( super ) fn new ( ) -> Self {
222
+ pub ( crate ) fn new ( ) -> Self {
389
223
let mut filter: [ [ _ ; FACTOR4 ] ; FACTOR4_INPUT_LENGTH ] = Default :: default ( ) ;
390
224
for ( j, coeff) in filter
391
225
. iter_mut ( )
@@ -415,7 +249,7 @@ mod specialized {
415
249
}
416
250
}
417
251
418
- pub ( super ) fn push ( & mut self , frame : & F ) -> [ F ; FACTOR4 ] {
252
+ pub ( crate ) fn push ( & mut self , frame : & F ) -> [ F ; FACTOR4 ] {
419
253
// Write in Frames in reverse, to enable forward-scanning with filter
420
254
self . buffer_pos = ( self . buffer_pos + self . buffer . len ( ) - 1 ) % self . buffer . len ( ) ;
421
255
self . buffer [ self . buffer_pos ] = * frame;
@@ -441,48 +275,24 @@ mod specialized {
441
275
442
276
output
443
277
}
444
- }
445
278
446
- impl < F > super :: Interpolator for Interp4F < F >
447
- where
448
- F : Frame + std:: fmt:: Debug + Default + AsRef < [ f32 ] > ,
449
- {
450
- fn process ( & mut self , src : & [ f32 ] , dst : & mut [ f32 ] ) {
451
- assert_eq ! ( 0 , src. len( ) % F :: CHANNELS ) ;
452
- assert_eq ! ( src. len( ) * FACTOR4 , dst. len( ) ) ;
453
- let frames = src. len ( ) / F :: CHANNELS ;
454
-
455
- for i in 0 ..frames {
456
- let res = self . push ( & F :: from_planar ( & src[ i..] , frames) ) ;
457
- for c in 0 ..F :: CHANNELS {
458
- for ( f, frame) in res. iter ( ) . enumerate ( ) {
459
- dst[ c * frames * FACTOR4 + i * FACTOR4 + f] = frame. as_ref ( ) [ c] ;
460
- }
461
- }
462
- }
463
- }
464
-
465
- fn reset ( & mut self ) {
279
+ pub ( crate ) fn reset ( & mut self ) {
466
280
self . buffer = Default :: default ( ) ;
467
281
}
468
-
469
- fn get_factor ( & self ) -> usize {
470
- 4
471
- }
472
282
}
473
283
474
- #[ derive( Debug ) ]
475
- pub ( super ) struct Interp2F < F : Frame > {
284
+ #[ derive( Debug , Clone ) ]
285
+ pub ( crate ) struct Interp2F < F : FrameAccumulator > {
476
286
filter : [ [ f32 ; FACTOR2 ] ; FACTOR2_INPUT_LENGTH ] ,
477
287
buffer : [ F ; FACTOR2_INPUT_LENGTH ] ,
478
288
buffer_pos : usize ,
479
289
}
480
290
481
291
impl < F > Interp2F < F >
482
292
where
483
- F : Frame + Default ,
293
+ F : FrameAccumulator + Default ,
484
294
{
485
- pub ( super ) fn new ( ) -> Self {
295
+ pub ( crate ) fn new ( ) -> Self {
486
296
let mut filter: [ [ _ ; FACTOR2 ] ; FACTOR2_INPUT_LENGTH ] = Default :: default ( ) ;
487
297
for ( j, coeff) in filter
488
298
. iter_mut ( )
@@ -512,7 +322,7 @@ mod specialized {
512
322
}
513
323
}
514
324
515
- pub ( super ) fn push ( & mut self , frame : & F ) -> [ F ; FACTOR2 ] {
325
+ pub ( crate ) fn push ( & mut self , frame : & F ) -> [ F ; FACTOR2 ] {
516
326
// Write in Frames in reverse, to enable forward-scanning with filter
517
327
self . buffer_pos = ( self . buffer_pos + self . buffer . len ( ) - 1 ) % self . buffer . len ( ) ;
518
328
self . buffer [ self . buffer_pos ] = * frame;
@@ -538,34 +348,10 @@ mod specialized {
538
348
539
349
output
540
350
}
541
- }
542
-
543
- impl < F > super :: Interpolator for Interp2F < F >
544
- where
545
- F : Frame + std:: fmt:: Debug + Default + AsRef < [ f32 ] > ,
546
- {
547
- fn process ( & mut self , src : & [ f32 ] , dst : & mut [ f32 ] ) {
548
- assert_eq ! ( 0 , src. len( ) % F :: CHANNELS ) ;
549
- assert_eq ! ( src. len( ) * FACTOR2 , dst. len( ) ) ;
550
- let frames = src. len ( ) / F :: CHANNELS ;
551
-
552
- for i in 0 ..frames {
553
- let res = self . push ( & F :: from_planar ( & src[ i..] , frames) ) ;
554
- for c in 0 ..F :: CHANNELS {
555
- for ( f, frame) in res. iter ( ) . enumerate ( ) {
556
- dst[ c * frames * FACTOR2 + i * FACTOR2 + f] = frame. as_ref ( ) [ c] ;
557
- }
558
- }
559
- }
560
- }
561
351
562
- fn reset ( & mut self ) {
352
+ pub ( crate ) fn reset ( & mut self ) {
563
353
self . buffer = Default :: default ( ) ;
564
354
}
565
-
566
- fn get_factor ( & self ) -> usize {
567
- 2
568
- }
569
355
}
570
356
}
571
357
0 commit comments