Skip to content

Commit 5857ea4

Browse files
authored
refactor: Clean analyze code (#67)
1 parent e891e42 commit 5857ea4

File tree

7 files changed

+20
-553
lines changed

7 files changed

+20
-553
lines changed

analysis/src/analysis.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
use anyhow::Result;
22
use tokio_util::sync::CancellationToken;
3-
use log::info;
43

54
use crate::computing_device::ComputingDevice;
65
use crate::features::*;
7-
use crate::legacy_fft;
86
use crate::fft_processor;
97
use crate::measure_time;
108

analysis/src/compute_device.rs

-26
This file was deleted.

analysis/src/fft_processor.rs

+17-30
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@ pub struct FFTProcessor {
2626
window_size: usize,
2727
batch_size: usize,
2828
overlap_size: usize,
29-
batch_fft: wgpu_radix4::FFTCompute,
29+
gpu_batch_fft: wgpu_radix4::FFTCompute,
3030
cpu_batch_fft: Arc<dyn Fft<f32>>,
3131
batch_fft_buffer: Vec<Complex<f32>>,
3232
avg_spectrum: Vec<Complex<f32>>,
3333
hanning_window: Vec<f32>,
3434
sample_buffer: Vec<f32>,
35-
batch_sample_buffer: Vec<f32>,
3635
batch_cache_buffer_count: usize,
37-
buffer: Vec<Complex<f32>>,
3836
fn_is_cancelled: Box<dyn Fn() -> bool>,
3937
is_cancelled: bool,
4038
// Processing state
@@ -68,7 +66,6 @@ macro_rules! check_cancellation {
6866
}
6967

7068
impl FFTProcessor {
71-
// batch_size is only associated with variables starting with batch
7269
pub fn new(
7370
computing_device: ComputingDevice,
7471
window_size: usize,
@@ -78,13 +75,11 @@ impl FFTProcessor {
7875
overlap_size: usize,
7976
cancel_token: Option<CancellationToken>,
8077
) -> 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));
8279
let batch_fft_buffer = vec![Complex::new(0.0, 0.0); window_size * batch_size];
8380
let avg_spectrum = vec![Complex::new(0.0, 0.0); window_size];
8481
let hanning_window = build_hanning_window(window_size);
8582
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];
8883
let mut planner = FftPlanner::<f32>::new();
8984
let cpu_batch_fft = planner.plan_fft_forward(window_size);
9085

@@ -100,14 +95,12 @@ impl FFTProcessor {
10095
window_size,
10196
batch_size,
10297
overlap_size,
103-
batch_fft,
98+
gpu_batch_fft,
10499
cpu_batch_fft,
105100
batch_fft_buffer,
106101
avg_spectrum,
107102
hanning_window,
108103
sample_buffer,
109-
batch_sample_buffer,
110-
buffer,
111104
batch_cache_buffer_count: 0,
112105
fn_is_cancelled,
113106
is_cancelled,
@@ -345,7 +338,7 @@ impl FFTProcessor {
345338
// Only process the valid portion of the batch buffer
346339
// let valid_size = self.batch_cache_buffer_count * self.window_size;
347340
// 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));
349342
// self.batch_fft_buffer[..valid_size].copy_from_slice(&batch_vec);
350343

351344
// Accumulate spectrums for the valid batches
@@ -358,7 +351,7 @@ impl FFTProcessor {
358351

359352
self.batch_cache_buffer_count = 0;
360353
} 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));
362355

363356
// Split batch_fft_buffer into batches and accumulate into avg_spectrum
364357
for batch_idx in 0..self.batch_size {
@@ -432,14 +425,13 @@ pub fn gpu_fft(
432425
overlap_size: usize,
433426
cancel_token: Option<CancellationToken>,
434427
) -> Option<AudioDescription> {
435-
let mut processor = FFTProcessor::new(
428+
FFTProcessor::new(
436429
ComputingDevice::Gpu,
437430
window_size,
438431
batch_size,
439432
overlap_size,
440433
cancel_token,
441-
);
442-
processor.process_file(file_path)
434+
).process_file(file_path)
443435
}
444436

445437
pub fn cpu_fft(
@@ -448,24 +440,19 @@ pub fn cpu_fft(
448440
overlap_size: usize,
449441
cancel_token: Option<CancellationToken>,
450442
) -> Option<AudioDescription> {
451-
let mut processor = FFTProcessor::new(
443+
FFTProcessor::new(
452444
ComputingDevice::Cpu,
453445
window_size,
454446
1,
455447
overlap_size,
456448
cancel_token,
457-
);
458-
459-
processor.process_file(file_path)
449+
).process_file(file_path)
460450
}
461451

462452
#[cfg(test)]
463453
mod tests {
464454
use crate::measure_time;
465-
use std::path::PathBuf;
466-
467455
use crate::legacy_fft;
468-
469456
use super::*;
470457

471458
#[test]
@@ -518,13 +505,13 @@ mod tests {
518505
);
519506

520507
// 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+
}
529516
}
530517
}

0 commit comments

Comments
 (0)