-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserde_example.rs
More file actions
247 lines (210 loc) · 8.32 KB
/
serde_example.rs
File metadata and controls
247 lines (210 loc) · 8.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//! Example demonstrating serialization of spectrograms and related result types.
//!
//! This example shows how to:
//! - Compute spectrograms and other features
//! - Serialize them to JSON and binary formats (bincode)
//! - Deserialize them back
//! - Compare serialization sizes
//!
//! Run with: cargo run --example serde_example --features serde,realfft
#[cfg(feature = "serde")]
use spectrograms::*;
#[cfg(feature = "serde")]
use num_format::{Locale, ToFormattedString};
#[cfg(feature = "serde")]
fn run_example() -> Result<(), Box<dyn std::error::Error>> {
use non_empty_slice::NonEmptyVec;
println!("Spectrogram Serialization Example\n");
println!("==================================\n");
// Generate a test signal: 440 Hz sine wave
let sample_rate = 16000.0;
let duration = 2.0;
let n_samples = (sample_rate * duration) as usize;
let signal: Vec<f64> = (0..n_samples)
.map(|i| {
let t = i as f64 / sample_rate;
(2.0 * std::f64::consts::PI * 440.0 * t).sin()
})
.collect();
let signal = NonEmptyVec::new(signal).unwrap();
println!("Generated {} samples at {} Hz", n_samples, sample_rate);
println!();
// 1. Linear Power Spectrogram
println!("1. Linear Power Spectrogram");
println!("----------------------------");
let stft = StftParams::new(nzu!(512), nzu!(256), WindowType::Hanning, true)?;
let params = SpectrogramParams::new(stft, sample_rate)?;
let spec = LinearPowerSpectrogram::compute(&signal, ¶ms, None)?;
println!(
"Spectrogram shape: {} bins x {} frames",
spec.n_bins(),
spec.n_frames()
);
// Serialize to JSON
let json = serde_json::to_string_pretty(&spec)?;
let json_size = json.len();
println!("JSON size: {} bytes", json_size);
// Serialize to bincode
let bincode_bytes = bincode2::serialize(&spec)?;
let bincode_size = bincode_bytes.len();
println!("Bincode size: {} bytes", bincode_size);
println!(
"Bincode is {:.1}x smaller than JSON",
json_size as f64 / bincode_size as f64
);
// Deserialize from JSON
let deserialized: LinearPowerSpectrogram = serde_json::from_str(&json)?;
println!(
"Successfully deserialized from JSON: {} bins x {} frames",
deserialized.n_bins(),
deserialized.n_frames()
);
// Deserialize from bincode
let deserialized_bin: LinearPowerSpectrogram = bincode2::deserialize(&bincode_bytes)?;
println!(
"Successfully deserialized from bincode: {} bins x {} frames",
deserialized_bin.n_bins(),
deserialized_bin.n_frames()
);
println!();
// 2. Mel Spectrogram
println!("2. Mel Spectrogram");
println!("------------------");
let mel_params = MelParams::new(nzu!(80), 0.0, 8000.0)?;
let mel_spec = MelPowerSpectrogram::compute(&signal, ¶ms, &mel_params, None)?;
println!(
"Mel spectrogram shape: {} bins x {} frames",
mel_spec.n_bins(),
mel_spec.n_frames()
);
let mel_json = serde_json::to_string_pretty(&mel_spec)?;
let mel_json_size = mel_json.len();
println!("JSON size: {} bytes", mel_json_size);
let mel_bincode = bincode2::serialize(&mel_spec)?;
let mel_bincode_size = mel_bincode.len();
println!("Bincode size: {} bytes", mel_bincode_size);
let mel_deserialized: MelPowerSpectrogram = serde_json::from_str(&mel_json)?;
println!(
"Successfully deserialized: {} mel bins, {} frames",
mel_deserialized.n_bins(),
mel_deserialized.n_frames()
);
println!();
// 3. MFCC
println!("3. MFCC Features");
println!("----------------");
let mfcc_stft = StftParams::new(nzu!(512), nzu!(256), WindowType::Hanning, true)?;
let mfcc_params = MfccParams::new(nzu!(13));
let mfcc = mfcc(&signal, &mfcc_stft, sample_rate, nzu!(40), &mfcc_params)?;
println!(
"MFCC shape: {} coefficients x {} frames",
mfcc.n_coefficients(),
mfcc.n_frames()
);
let mfcc_json = serde_json::to_string_pretty(&mfcc)?;
let mfcc_json_size = mfcc_json.len();
println!("JSON size: {} bytes", mfcc_json_size);
let mfcc_bincode = bincode2::serialize(&mfcc)?;
let mfcc_bincode_size = mfcc_bincode.len();
println!("Bincode size: {} bytes", mfcc_bincode_size);
let mfcc_deserialized: Mfcc = bincode2::deserialize(&mfcc_bincode)?;
println!(
"Successfully deserialized: {} coefficients x {} frames",
mfcc_deserialized.n_coefficients(),
mfcc_deserialized.n_frames()
);
println!();
// 4. Chromagram
println!("4. Chromagram");
println!("-------------");
let chroma_stft = StftParams::new(nzu!(2048), nzu!(512), WindowType::Hanning, true)?;
let chroma_params = ChromaParams::new(440.0, 55.0, 4200.0, ChromaNorm::L2)?;
let chroma = chromagram(&signal, &chroma_stft, sample_rate, &chroma_params)?;
println!(
"Chromagram shape: {} pitch classes x {} frames",
chroma.n_bins(),
chroma.n_frames()
);
let chroma_json = serde_json::to_string_pretty(&chroma)?;
let chroma_json_size = chroma_json.len();
println!("JSON size: {} bytes", chroma_json_size);
let chroma_bincode = bincode2::serialize(&chroma)?;
let chroma_bincode_size = chroma_bincode.len();
println!("Bincode size: {} bytes", chroma_bincode_size);
let chroma_deserialized: Chromagram = serde_json::from_str(&chroma_json)?;
println!(
"Successfully deserialized: {} pitch classes x {} frames",
chroma_deserialized.n_bins(),
chroma_deserialized.n_frames()
);
println!();
// 5. CQT Result
println!("5. Constant-Q Transform");
println!("-----------------------");
let cqt_params = CqtParams::new(nzu!(12), nzu!(6), 55.0)?;
let cqt = cqt(&signal, sample_rate, &cqt_params, nzu!(256))?;
println!("CQT shape: {:?}", cqt.data.shape());
println!("Frequency bins: {}", cqt.frequencies.len());
let cqt_json = serde_json::to_string_pretty(&cqt)?;
let cqt_json_size = cqt_json.len();
println!("JSON size: {} bytes", cqt_json_size);
let cqt_bincode = bincode2::serialize(&cqt)?;
let cqt_bincode_size = cqt_bincode.len();
println!("Bincode size: {} bytes", cqt_bincode_size);
let cqt_deserialized: CqtResult = bincode2::deserialize(&cqt_bincode)?;
println!(
"Successfully deserialized: shape {:?}",
cqt_deserialized.data.shape(),
);
println!();
// Summary
println!("=======");
println!();
println!("All result types successfully serialized and deserialized!");
println!();
println!("Size comparison (JSON vs Bincode):");
println!(
" Linear Spectrogram: {} bytes vs {} bytes ({:.1}x)",
json_size.to_formatted_string(&Locale::en),
bincode_size.to_formatted_string(&Locale::en),
json_size as f64 / bincode_size as f64
);
println!(
" Mel Spectrogram: {} bytes vs {} bytes ({:.1}x)",
mel_json_size.to_formatted_string(&Locale::en),
mel_bincode_size.to_formatted_string(&Locale::en),
mel_json_size as f64 / mel_bincode_size as f64
);
println!(
" MFCC: {} bytes vs {} bytes ({:.1}x)",
mfcc_json_size.to_formatted_string(&Locale::en),
mfcc_bincode_size.to_formatted_string(&Locale::en),
mfcc_json_size as f64 / mfcc_bincode_size as f64
);
println!(
" Chromagram: {} bytes vs {} bytes ({:.1}x)",
chroma_json_size.to_formatted_string(&Locale::en),
chroma_bincode_size.to_formatted_string(&Locale::en),
chroma_json_size as f64 / chroma_bincode_size as f64
);
println!(
" CQT: {} bytes vs {} bytes ({:.1}x)",
cqt_json_size.to_formatted_string(&Locale::en),
cqt_bincode_size.to_formatted_string(&Locale::en),
cqt_json_size as f64 / cqt_bincode_size as f64
);
Ok(())
}
#[cfg(feature = "serde")]
fn main() -> Result<(), Box<dyn std::error::Error>> {
run_example()
}
#[cfg(not(feature = "serde"))]
fn main() {
eprintln!("This example requires the 'serde' feature to be enabled.");
eprintln!("Run with: cargo run --example serde_example --features serde,realfft");
eprintln!(
"If erroring due to missing 'num-format' dependency, make sure to run in development/debug mode. It is a dev dependency only."
);
std::process::exit(1);
}