Skip to content

Commit f931702

Browse files
authored
Merge pull request #20 from wizenink/feature/impl-traits
Implement recommended traits on public structs
2 parents 184c8f5 + 7f8bcad commit f931702

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

src/fft_backend.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub use rustfft::num_complex::Complex;
4343

4444
// For microfft backend, we define our own Complex type
4545
#[cfg(feature = "microfft-backend")]
46-
#[derive(Debug, Clone, Copy, PartialEq)]
46+
#[derive(Debug, Clone, Copy, PartialEq, Default)]
4747
pub struct Complex<T> {
4848
pub re: T,
4949
pub im: T,
@@ -144,7 +144,7 @@ impl FftNum for f32 {}
144144
impl FftNum for f64 {}
145145

146146
/// Trait abstracting FFT operations for both forward and inverse transforms
147-
pub trait FftBackend<T: FftNum>: Send + Sync {
147+
pub trait FftBackend<T: FftNum>: Send + Sync + core::fmt::Debug {
148148
/// Process FFT in-place
149149
fn process(&self, buffer: &mut [Complex<T>]);
150150

@@ -183,6 +183,14 @@ mod rustfft_impl {
183183
fft: Arc<dyn Fft<T>>,
184184
}
185185

186+
impl<T: rustfft::FftNum> core::fmt::Debug for RustFftWrapper<T> {
187+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
188+
f.debug_struct("RustFftWrapper")
189+
.field("fft_size", &self.fft.len())
190+
.finish()
191+
}
192+
}
193+
186194
impl<T: FftNum> FftBackend<T> for RustFftWrapper<T> {
187195
fn process(&self, buffer: &mut [Complex<T>]) {
188196
// Safety: rustfft::num_complex::Complex and our re-exported Complex
@@ -243,10 +251,12 @@ mod microfft_impl {
243251

244252
/// Wrapper around microfft that implements our FftBackend trait
245253
/// Note: microfft only supports f32 and power-of-2 sizes up to 4096
254+
#[derive(Debug, Clone)]
246255
struct MicroFftForward {
247256
size: usize,
248257
}
249258

259+
#[derive(Debug, Clone)]
250260
struct MicroFftInverse {
251261
size: usize,
252262
}

src/lib.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub mod prelude {
6666
};
6767
}
6868

69-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
69+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7070
pub enum ReconstructionMode {
7171
/// Overlap-Add: normalize by sum(w), requires COLA condition
7272
Ola,
@@ -75,7 +75,7 @@ pub enum ReconstructionMode {
7575
Wola,
7676
}
7777

78-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
78+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7979
pub enum WindowType {
8080
Hann,
8181
Hamming,
@@ -122,14 +122,14 @@ impl<T: Float + fmt::Display + fmt::Debug> fmt::Display for ConfigError<T> {
122122
#[cfg(feature = "std")]
123123
impl<T: Float + fmt::Display + fmt::Debug> std::error::Error for ConfigError<T> {}
124124

125-
#[derive(Debug, Clone, Copy)]
125+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
126126
pub enum PadMode {
127127
Reflect,
128128
Zero,
129129
Edge,
130130
}
131131

132-
#[derive(Clone)]
132+
#[derive(Debug, Clone, PartialEq)]
133133
pub struct StftConfig<T: Float> {
134134
pub fft_size: usize,
135135
pub hop_size: usize,
@@ -286,6 +286,7 @@ impl<T: Float + FromPrimitive + fmt::Debug> StftConfig<T> {
286286
}
287287

288288
/// Builder for StftConfig with fluent API
289+
#[derive(Debug, Clone, PartialEq)]
289290
pub struct StftConfigBuilder<T: Float> {
290291
fft_size: Option<usize>,
291292
hop_size: Option<usize>,
@@ -384,7 +385,7 @@ fn generate_window<T: Float + FromPrimitive>(window_type: WindowType, size: usiz
384385
}
385386
}
386387

387-
#[derive(Clone)]
388+
#[derive(Debug, Clone, PartialEq)]
388389
pub struct SpectrumFrame<T: Float> {
389390
pub freq_bins: usize,
390391
pub data: Vec<Complex<T>>,
@@ -474,7 +475,7 @@ impl<T: Float> SpectrumFrame<T> {
474475
}
475476
}
476477

477-
#[derive(Clone)]
478+
#[derive(Debug, Clone, PartialEq)]
478479
pub struct Spectrum<T: Float> {
479480
pub num_frames: usize,
480481
pub freq_bins: usize,
@@ -609,6 +610,7 @@ impl<T: Float> Spectrum<T> {
609610
}
610611
}
611612

613+
#[derive(Debug, Clone)]
612614
pub struct BatchStft<T: Float + FftNum> {
613615
config: StftConfig<T>,
614616
window: Vec<T>,
@@ -821,6 +823,7 @@ impl<T: Float + FftNum + FromPrimitive + fmt::Debug> BatchStft<T> {
821823
}
822824
}
823825

826+
#[derive(Debug, Clone)]
824827
pub struct BatchIstft<T: Float + FftNum> {
825828
config: StftConfig<T>,
826829
window: Vec<T>,
@@ -1085,6 +1088,7 @@ impl<T: Float + FftNum + FromPrimitive + fmt::Debug> BatchIstft<T> {
10851088
}
10861089
}
10871090

1091+
#[derive(Debug, Clone)]
10881092
pub struct StreamingStft<T: Float + FftNum> {
10891093
config: StftConfig<T>,
10901094
window: Vec<T>,
@@ -1235,6 +1239,7 @@ impl<T: Float + FftNum + FromPrimitive + fmt::Debug> StreamingStft<T> {
12351239
}
12361240

12371241
/// Multi-channel streaming STFT processor with independent state per channel.
1242+
#[derive(Debug, Clone)]
12381243
pub struct MultiChannelStreamingStft<T: Float + FftNum> {
12391244
processors: Vec<StreamingStft<T>>,
12401245
}
@@ -1333,6 +1338,7 @@ where
13331338
}
13341339
}
13351340

1341+
#[derive(Debug, Clone)]
13361342
pub struct StreamingIstft<T: Float + FftNum> {
13371343
config: StftConfig<T>,
13381344
window: Vec<T>,
@@ -1568,6 +1574,7 @@ impl<T: Float + FftNum + FromPrimitive + fmt::Debug> StreamingIstft<T> {
15681574
}
15691575

15701576
/// Multi-channel streaming iSTFT processor with independent state per channel.
1577+
#[derive(Debug, Clone)]
15711578
pub struct MultiChannelStreamingIstft<T: Float + FftNum> {
15721579
processors: Vec<StreamingIstft<T>>,
15731580
}

src/mel.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use core::fmt;
1414
use num_traits::Float;
1515

1616
/// Mel scale variant for frequency conversion.
17-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
17+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
1818
pub enum MelScale {
1919
/// HTK mel scale formula: 2595 * log10(1 + hz/700)
2020
Htk,
@@ -25,7 +25,7 @@ pub enum MelScale {
2525
}
2626

2727
/// Normalization method for mel filterbank.
28-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
28+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
2929
pub enum MelNorm {
3030
/// No normalization
3131
None,
@@ -35,7 +35,7 @@ pub enum MelNorm {
3535
}
3636

3737
/// Configuration for mel spectrogram computation.
38-
#[derive(Debug, Clone)]
38+
#[derive(Debug, Clone, PartialEq)]
3939
pub struct MelConfig<T: Float> {
4040
/// Number of mel bands (default: 80 for speech/Whisper)
4141
pub n_mels: usize,
@@ -138,7 +138,7 @@ pub fn mel_to_hz<T: Float>(mel: T, scale: MelScale) -> T {
138138
///
139139
/// Stores triangular filters as a sparse matrix where each mel bin
140140
/// has weights for the relevant STFT frequency bins.
141-
#[derive(Clone)]
141+
#[derive(Debug, Clone, PartialEq)]
142142
pub struct MelFilterbank<T: Float> {
143143
/// Number of mel bands
144144
pub n_mels: usize,
@@ -302,7 +302,7 @@ impl<T: Float + fmt::Debug> MelFilterbank<T> {
302302
/// Mel-scale spectrum data structure.
303303
///
304304
/// Stores mel spectrogram as (num_frames x n_mels) in row-major order.
305-
#[derive(Clone)]
305+
#[derive(Debug, Clone, PartialEq)]
306306
pub struct MelSpectrum<T: Float> {
307307
/// Number of time frames
308308
pub num_frames: usize,
@@ -498,6 +498,7 @@ impl<T: Float> MelSpectrum<T> {
498498
/// Batch mel spectrogram processor.
499499
///
500500
/// Converts STFT Spectrum to mel-scale representation.
501+
#[derive(Debug, Clone)]
501502
pub struct BatchMelSpectrogram<T: Float> {
502503
filterbank: MelFilterbank<T>,
503504
use_power: bool,
@@ -596,6 +597,7 @@ impl<T: Float + fmt::Debug> BatchMelSpectrogram<T> {
596597
/// Streaming mel spectrogram processor.
597598
///
598599
/// Processes individual STFT frames into mel-scale frames.
600+
#[derive(Debug, Clone)]
599601
pub struct StreamingMelSpectrogram<T: Float> {
600602
filterbank: MelFilterbank<T>,
601603
use_power: bool,

0 commit comments

Comments
 (0)