@@ -26,15 +26,13 @@ pub struct FFTProcessor {
26
26
window_size : usize ,
27
27
batch_size : usize ,
28
28
overlap_size : usize ,
29
- batch_fft : wgpu_radix4:: FFTCompute ,
29
+ gpu_batch_fft : wgpu_radix4:: FFTCompute ,
30
30
cpu_batch_fft : Arc < dyn Fft < f32 > > ,
31
31
batch_fft_buffer : Vec < Complex < f32 > > ,
32
32
avg_spectrum : Vec < Complex < f32 > > ,
33
33
hanning_window : Vec < f32 > ,
34
34
sample_buffer : Vec < f32 > ,
35
- batch_sample_buffer : Vec < f32 > ,
36
35
batch_cache_buffer_count : usize ,
37
- buffer : Vec < Complex < f32 > > ,
38
36
fn_is_cancelled : Box < dyn Fn ( ) -> bool > ,
39
37
is_cancelled : bool ,
40
38
// Processing state
@@ -68,7 +66,6 @@ macro_rules! check_cancellation {
68
66
}
69
67
70
68
impl FFTProcessor {
71
- // batch_size is only associated with variables starting with batch
72
69
pub fn new (
73
70
computing_device : ComputingDevice ,
74
71
window_size : usize ,
@@ -78,13 +75,11 @@ impl FFTProcessor {
78
75
overlap_size : usize ,
79
76
cancel_token : Option < CancellationToken > ,
80
77
) -> Self {
81
- let batch_fft = pollster:: block_on ( wgpu_radix4:: FFTCompute :: new ( window_size * batch_size) ) ;
78
+ let gpu_batch_fft = pollster:: block_on ( wgpu_radix4:: FFTCompute :: new ( window_size * batch_size) ) ;
82
79
let batch_fft_buffer = vec ! [ Complex :: new( 0.0 , 0.0 ) ; window_size * batch_size] ;
83
80
let avg_spectrum = vec ! [ Complex :: new( 0.0 , 0.0 ) ; window_size] ;
84
81
let hanning_window = build_hanning_window ( window_size) ;
85
82
let sample_buffer = Vec :: with_capacity ( window_size) ;
86
- let batch_sample_buffer = Vec :: with_capacity ( window_size * batch_size) ;
87
- let buffer = vec ! [ Complex :: new( 0.0 , 0.0 ) ; window_size] ;
88
83
let mut planner = FftPlanner :: < f32 > :: new ( ) ;
89
84
let cpu_batch_fft = planner. plan_fft_forward ( window_size) ;
90
85
@@ -100,14 +95,12 @@ impl FFTProcessor {
100
95
window_size,
101
96
batch_size,
102
97
overlap_size,
103
- batch_fft ,
98
+ gpu_batch_fft ,
104
99
cpu_batch_fft,
105
100
batch_fft_buffer,
106
101
avg_spectrum,
107
102
hanning_window,
108
103
sample_buffer,
109
- batch_sample_buffer,
110
- buffer,
111
104
batch_cache_buffer_count : 0 ,
112
105
fn_is_cancelled,
113
106
is_cancelled,
@@ -345,7 +338,7 @@ impl FFTProcessor {
345
338
// Only process the valid portion of the batch buffer
346
339
// let valid_size = self.batch_cache_buffer_count * self.window_size;
347
340
// let mut batch_vec = self.batch_fft_buffer[..valid_size].to_vec();
348
- pollster:: block_on ( self . batch_fft . compute_fft ( & mut self . batch_fft_buffer ) ) ;
341
+ pollster:: block_on ( self . gpu_batch_fft . compute_fft ( & mut self . batch_fft_buffer ) ) ;
349
342
// self.batch_fft_buffer[..valid_size].copy_from_slice(&batch_vec);
350
343
351
344
// Accumulate spectrums for the valid batches
@@ -358,7 +351,7 @@ impl FFTProcessor {
358
351
359
352
self . batch_cache_buffer_count = 0 ;
360
353
} else if self . batch_cache_buffer_count >= self . batch_size {
361
- pollster:: block_on ( self . batch_fft . compute_fft ( & mut self . batch_fft_buffer ) ) ;
354
+ pollster:: block_on ( self . gpu_batch_fft . compute_fft ( & mut self . batch_fft_buffer ) ) ;
362
355
363
356
// Split batch_fft_buffer into batches and accumulate into avg_spectrum
364
357
for batch_idx in 0 ..self . batch_size {
@@ -432,14 +425,13 @@ pub fn gpu_fft(
432
425
overlap_size : usize ,
433
426
cancel_token : Option < CancellationToken > ,
434
427
) -> Option < AudioDescription > {
435
- let mut processor = FFTProcessor :: new (
428
+ FFTProcessor :: new (
436
429
ComputingDevice :: Gpu ,
437
430
window_size,
438
431
batch_size,
439
432
overlap_size,
440
433
cancel_token,
441
- ) ;
442
- processor. process_file ( file_path)
434
+ ) . process_file ( file_path)
443
435
}
444
436
445
437
pub fn cpu_fft (
@@ -448,24 +440,19 @@ pub fn cpu_fft(
448
440
overlap_size : usize ,
449
441
cancel_token : Option < CancellationToken > ,
450
442
) -> Option < AudioDescription > {
451
- let mut processor = FFTProcessor :: new (
443
+ FFTProcessor :: new (
452
444
ComputingDevice :: Cpu ,
453
445
window_size,
454
446
1 ,
455
447
overlap_size,
456
448
cancel_token,
457
- ) ;
458
-
459
- processor. process_file ( file_path)
449
+ ) . process_file ( file_path)
460
450
}
461
451
462
452
#[ cfg( test) ]
463
453
mod tests {
464
454
use crate :: measure_time;
465
- use std:: path:: PathBuf ;
466
-
467
455
use crate :: legacy_fft;
468
-
469
456
use super :: * ;
470
457
471
458
#[ test]
@@ -518,13 +505,13 @@ mod tests {
518
505
) ;
519
506
520
507
// Compare spectrum values
521
- // for (i, (gpu_val, cpu_val)) in gpu_result.spectrum.iter()
522
- // .zip(cpu_result.spectrum.iter())
523
- // .enumerate()
524
- // {
525
- // assert!((gpu_val.norm() - cpu_val.norm()).abs() < 0.001,
526
- // "Spectrum difference too large at index {}: {} vs {}",
527
- // i, gpu_val.norm(), cpu_val.norm());
528
- // }
508
+ for ( i, ( gpu_val, cpu_val) ) in gpu_result. spectrum . iter ( )
509
+ . zip ( cpu_result. spectrum . iter ( ) )
510
+ . enumerate ( )
511
+ {
512
+ assert ! ( ( gpu_val. norm( ) - cpu_val. norm( ) ) . abs( ) < 0.001 ,
513
+ "Spectrum difference too large at index {}: {} vs {}" ,
514
+ i, gpu_val. norm( ) , cpu_val. norm( ) ) ;
515
+ }
529
516
}
530
517
}
0 commit comments