-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
517 lines (481 loc) · 16.7 KB
/
Copy pathmain.rs
File metadata and controls
517 lines (481 loc) · 16.7 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
use std::env;
use std::f32::consts::TAU;
use std::io::Write;
use std::process;
use std::vec::Vec;
static SUPPORTED_SAMPLE_RATES: [u32; 3] = [
16_000, // 16 kHz is commonly used for speech and telephony applications
44_100, // 44.1 kHz is the standard sample rate for audio CDs and is widely used in music production
48_000, // 48 kHz is commonly used in professional audio and video production, as well as in some high-quality consumer audio formats
];
/// Audio sample width.
///
/// Stored in number of bytes per sample.
#[repr(u8)]
#[derive(Clone, Copy, Debug)]
pub enum SampleWidth {
/// 16 bit audio
Width2Byte = 2,
/// 24 bit audio
Width3Byte = 3,
/// 32 bit audio
Width4Byte = 4,
}
impl SampleWidth {
/// Parse from string (16, 24, 32)
fn from_str(s: &str) -> Option<Self> {
match s {
"16" => Some(SampleWidth::Width2Byte),
"24" => Some(SampleWidth::Width3Byte),
"32" => Some(SampleWidth::Width4Byte),
_ => None,
}
}
/// Get string representation
fn to_str(&self) -> &'static str {
match self {
SampleWidth::Width2Byte => "16",
SampleWidth::Width3Byte => "24",
SampleWidth::Width4Byte => "32",
}
}
}
// https://ccrma.stanford.edu/courses/422-winter-2014/projects/WaveFormat/
#[repr(C, packed)]
#[allow(dead_code)]
struct WavHeader {
chunk_id: [u8; 4], // 0
chunk_size: u32, //4
format: [u8; 4], //8
subchunk_1_id: [u8; 4], //12
subchunk_1_size: u32, // 16
audio_format: u16, // 20
num_channels: u16, // 22
sample_rate: u32, // 24
byte_rate: u32, // 28
block_align: u16, // 32
bits_per_sample: u16, // 34
subchunk_2_id: [u8; 4], //36
subchunk_2_size: u32, //40
}
impl WavHeader {
pub fn new() -> Self {
Self {
chunk_id: *b"RIFF",
chunk_size: 0,
format: *b"WAVE",
subchunk_1_id: *b"fmt ",
subchunk_1_size: 16,
audio_format: 0x0001, //WINDOWS PCM
num_channels: 1,
sample_rate: 44_100,
byte_rate: 176_400,
block_align: 2,
bits_per_sample: 16,
subchunk_2_id: *b"data",
subchunk_2_size: 0,
}
}
}
// Get the maximum absolute value for a given sample width.
// Digital Audio Representation:
/*
|----------|-----------------------------|------------------|
| Format | Integer Type | Max Positive |
|----------|-----------------------------|------------------|
| 16-bit | int16_t | 32767 |
| 24-bit | int32_t (in 24 bits) | 8,388,607 |
| 32-bit | int32_t | 2,147,483,647 |
|----------|-----------------------------|------------------|
*/
fn get_range(sample_width: SampleWidth) -> f32 {
match sample_width {
SampleWidth::Width2Byte => 32767.0,
SampleWidth::Width3Byte => 8388607.0,
SampleWidth::Width4Byte => 2147483647.0,
}
}
struct Config {
frequency: f32,
sample_rate: u32,
channels: u8,
sample_width: SampleWidth,
duration_ms: f32,
output_format: OutputFormat,
analyze_only: bool,
}
#[derive(Clone, Copy)]
enum OutputFormat {
Hex,
CArray,
RustArray,
RawBytes,
Info,
WavFile,
}
impl OutputFormat {
fn from_str(s: &str) -> Option<Self> {
match s.to_lowercase().as_str() {
"hex" => Some(OutputFormat::Hex),
"carray" | "c" => Some(OutputFormat::CArray),
"rustarray" | "rust" => Some(OutputFormat::RustArray),
"raw" | "bytes" => Some(OutputFormat::RawBytes),
"info" => Some(OutputFormat::Info),
"wav" => Some(OutputFormat::WavFile),
_ => None,
}
}
}
fn print_usage() {
println!("Usage: singen [OPTIONS]");
println!();
println!("Options:");
println!(" -f, --frequency FREQ Sine wave frequency in Hz (default: 440.0)");
println!(" -r, --rate RATE Sample rate in Hz (default: 16000)");
println!(" Supported: 16000, 44100, 48000");
println!(" -c, --channels CH Number of channels (1=mono, 2=stereo, default: 2)");
println!(" -b, --bits BITS Bit depth: 16, 24, or 32 (default: 16)");
println!(" -d, --duration MS Duration in milliseconds (default: 1.0)");
println!(" -o, --output FORMAT Output format:");
println!(" hex - Hexadecimal values (default)");
println!(" carray - C-style array declaration");
println!(" rustarray - Rust array declaration");
println!(" raw - Raw binary bytes (stdout)");
println!(" wav - Windows audio file format (stdout)");
println!(" info - Only show buffer info, no data");
println!(" -a, --analyze Analyze only (don't generate data)");
println!(" -h, --help Show this help message");
println!();
println!("Examples:");
println!(" singen -f 1000 -r 48000 -b 16 -d 10 -o carray");
println!(" singen --frequency 440 --rate 44100 --channels 1 --bits 24");
println!(" singen -r 16000 -d 1 -o rustarray -p");
}
fn parse_args() -> Config {
let args: Vec<String> = env::args().collect();
let mut config = Config {
frequency: 440.0,
sample_rate: 16_000,
channels: 2,
sample_width: SampleWidth::Width2Byte,
duration_ms: 1.0,
output_format: OutputFormat::Hex,
analyze_only: false,
};
let mut i = 1;
while i < args.len() {
match args[i].as_str() {
"-h" | "--help" => {
print_usage();
process::exit(0);
}
"-f" | "--frequency" => {
i += 1;
if i < args.len() {
config.frequency = args[i].parse().unwrap_or_else(|_| {
eprintln!("Error: Invalid frequency value");
process::exit(1);
});
}
}
"-r" | "--rate" => {
i += 1;
if i < args.len() {
config.sample_rate = args[i].parse().unwrap_or_else(|_| {
eprintln!("Error: Invalid sample rate");
process::exit(1);
});
if !SUPPORTED_SAMPLE_RATES.contains(&config.sample_rate) {
eprintln!(
"Warning: {} Hz is not in the standard supported rates list",
config.sample_rate
);
}
}
}
"-c" | "--channels" => {
i += 1;
if i < args.len() {
let ch = args[i].parse().unwrap_or_else(|_| {
eprintln!("Error: Invalid channel count");
process::exit(1);
});
if ch != 1 && ch != 2 {
eprintln!("Error: Channel count must be 1 or 2");
process::exit(1);
}
config.channels = ch;
}
}
"-b" | "--bits" => {
i += 1;
if i < args.len() {
config.sample_width = SampleWidth::from_str(&args[i]).unwrap_or_else(|| {
eprintln!("Error: Invalid bit depth. Must be 16, 24, or 32");
process::exit(1);
});
}
}
"-d" | "--duration" => {
i += 1;
if i < args.len() {
config.duration_ms = args[i].parse().unwrap_or_else(|_| {
eprintln!("Error: Invalid duration");
process::exit(1);
});
}
}
"-o" | "--output" => {
i += 1;
if i < args.len() {
config.output_format = OutputFormat::from_str(&args[i]).unwrap_or_else(|| {
eprintln!("Error: Invalid output format");
process::exit(1);
});
}
}
"-a" | "--analyze" => {
config.analyze_only = true;
config.output_format = OutputFormat::Info;
}
_ => {
eprintln!("Error: Unknown option: {}", args[i]);
print_usage();
process::exit(1);
}
}
i += 1;
}
config
}
/// Generate a linear chirp from `f0` Hz to `f1` Hz over `duration_secs`.
/// Returns a vector of floating‑point samples in the range [-1.0, 1.0].
fn generate_linear_chirp(
f0: f32, // start frequency (Hz)
f1: f32, // end frequency (Hz)
sample_rate: f32, // samples per second
duration_secs: f32, // total duration in seconds
) -> Vec<f32> {
let dt = 1.0 / sample_rate;
let num_samples = (duration_secs * sample_rate).round() as usize;
let mut samples = Vec::with_capacity(num_samples);
let mut phase = 0.0;
for i in 0..num_samples {
let t = i as f32 * dt;
// Instantaneous frequency at time t (linear interpolation)
let freq = f0 + (f1 - f0) * (t / duration_secs);
// Phase increment for this sample
phase += TAU * freq * dt;
// Keep phase in [-π, π] range to avoid floating-point drift (optional)
phase = phase.rem_euclid(TAU);
samples.push(phase.sin());
}
samples
}
fn float_samples_to_bytes(samples: &[f32], channels: u8, sample_width: SampleWidth) -> Vec<u8> {
let max_val = get_range(sample_width);
let mut buffer = Vec::with_capacity(samples.len() * channels as usize * sample_width as usize);
for &sample in samples {
let scaled = (sample * max_val).round() as i32;
let bytes = scaled.to_le_bytes();
for _ in 0..channels {
for b in &bytes[0..sample_width as usize] {
buffer.push(*b);
}
}
}
buffer
}
fn print_buffer_info(config: &Config, total_samples: usize, total_bytes: usize) {
println!("Sine Wave Generator - Configuration");
println!("=====================================");
println!("Frequency: {} Hz", config.frequency);
println!("Sample Rate: {} Hz", config.sample_rate);
println!(
"Channels: {} ({})",
config.channels,
if config.channels == 1 {
"mono"
} else {
"stereo"
}
);
println!("Bit Depth: {}-bit", config.sample_width.to_str());
println!("Duration: {} ms", config.duration_ms);
println!();
println!("Buffer Analysis:");
println!(" Samples: {}", total_samples);
println!(" Total bytes: {}", total_bytes);
// Calculate frequency info
let period_samples = config.sample_rate as f32 / config.frequency;
println!("\nFrequency Analysis:");
println!(" Period: {:.2} samples", period_samples);
println!(
" Full cycles: {:.2}",
total_samples as f32 / period_samples
);
}
fn print_buffer_hex(buffer: &[u8], bytes_per_line: usize) {
let mut i: usize = 0;
print!("[");
while i < buffer.len() {
if i > 0 {
print!("\n ");
}
for j in 0..bytes_per_line {
if i + j < buffer.len() {
print!("0x{:02X}", buffer[i + j]);
if i + j + 1 < buffer.len() && j < bytes_per_line - 1 {
print!(", ");
}
}
}
i += bytes_per_line;
}
println!("]");
}
fn print_c_array(buffer: &[u8], config: &Config) {
let name = format!(
"sine_{}hz_{}ms_{}bit_{}ch",
config.sample_rate,
config.duration_ms as u32,
config.sample_width.to_str(),
config.channels
);
println!(
"// Sine wave: {} Hz, {} ms, {}-bit, {} channel{}",
config.frequency,
config.duration_ms,
config.sample_width.to_str(),
config.channels,
if config.channels > 1 { "s" } else { "" }
);
println!("// Sample rate: {} Hz", config.sample_rate);
println!("// Total bytes: {}", buffer.len());
println!(
"const uint8_t {}[{}] = {{",
name.to_uppercase(),
buffer.len()
);
for (i, chunk) in buffer.chunks(16).enumerate() {
print!(" ");
for (j, byte) in chunk.iter().enumerate() {
print!("0x{:02X}", byte);
if i * 16 + j < buffer.len() - 1 {
print!(", ");
}
}
if i * 16 < buffer.len() {
println!();
}
}
println!("}};");
}
fn print_rust_array(buffer: &[u8], config: &Config) {
let name = format!(
"SINE_{}HZ_{}MS_{}BIT_{}CH",
config.sample_rate,
config.duration_ms as u32,
config.sample_width.to_str(),
config.channels
);
println!(
"// Sine wave: {} Hz, {} ms, {}-bit, {} channel{}",
config.frequency,
config.duration_ms,
config.sample_width.to_str(),
config.channels,
if config.channels > 1 { "s" } else { "" }
);
println!("// Sample rate: {} Hz", config.sample_rate);
println!("// Total bytes: {}", buffer.len());
println!("pub const {}: [u8; {}] = [", name, buffer.len());
for (i, chunk) in buffer.chunks(16).enumerate() {
print!(" ");
for (j, byte) in chunk.iter().enumerate() {
print!("0x{:02X}", byte);
if i * 16 + j < buffer.len() - 1 {
print!(", ");
}
}
if i * 16 < buffer.len() {
println!();
}
}
println!("];");
}
fn print_raw_bytes(buffer: &[u8]) {
use std::io::{self, Write};
let stdout = io::stdout();
let mut handle = stdout.lock();
handle.write_all(buffer).unwrap();
}
fn create_wav_file_array(
buffer: &[u8],
sample_rate: u32,
channels: u16,
sample_width: SampleWidth,
) -> Vec<u8> {
let wav_header_len = std::mem::size_of::<WavHeader>();
let buffer_len = buffer.len();
let mut wav_hdr = WavHeader::new();
wav_hdr.chunk_size = (36 + buffer_len) as u32; // 4 + (24) + 8 + buffer_len
wav_hdr.num_channels = channels;
wav_hdr.sample_rate = sample_rate;
wav_hdr.byte_rate = sample_rate as u32 * channels as u32 * sample_width as u32;
wav_hdr.block_align = channels * sample_width as u16; // fixed formula
wav_hdr.bits_per_sample = sample_width as u16 * 8;
wav_hdr.subchunk_2_size = buffer_len as u32;
let mut file = Vec::with_capacity(wav_header_len + buffer_len);
let ptr = &wav_hdr as *const WavHeader as *const u8;
// SAFETY: WavHeader is repr(C, packed) so it has no padding.
file.write_all(unsafe { std::slice::from_raw_parts(ptr, wav_header_len) })
.unwrap();
file.write_all(buffer).unwrap();
file
}
fn main() {
let config = parse_args();
let total_samples =
((config.duration_ms * config.sample_rate as f32) / 1000.0).round() as usize;
let total_bytes = total_samples * (config.sample_width as u8 * config.channels) as usize;
let float_samples = generate_linear_chirp(
config.frequency,
config.frequency,
config.sample_rate as f32,
config.duration_ms / 1000.0,
);
let buffer = float_samples_to_bytes(&float_samples, config.channels, config.sample_width);
match config.output_format {
OutputFormat::Info => {
print_buffer_info(&config, total_samples, total_bytes);
}
OutputFormat::Hex => {
print_buffer_info(&config, total_samples, total_bytes);
println!("\nBuffer data (hexadecimal):");
print_buffer_hex(&buffer, 16);
}
OutputFormat::CArray => {
print_buffer_info(&config, total_samples, total_bytes);
println!("\nC array declaration:");
print_c_array(&buffer, &config);
}
OutputFormat::RustArray => {
print_buffer_info(&config, total_samples, total_bytes);
println!("\nRust array declaration:");
print_rust_array(&buffer, &config);
}
OutputFormat::RawBytes => {
print_raw_bytes(&buffer);
}
OutputFormat::WavFile => {
let file = create_wav_file_array(
&buffer,
config.sample_rate,
config.channels as u16,
config.sample_width,
);
print_raw_bytes(file.as_ref());
}
}
}