mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-16 15:22:43 +00:00
31 lines
1.2 KiB
Rust
31 lines
1.2 KiB
Rust
//! Helpers shared by the integration test modules.
|
|
|
|
use std::path::Path;
|
|
|
|
/// 16-bit stereo 48 kHz WAV: a 440 Hz sine at 0.5 on the left, silence on the right.
|
|
pub(crate) fn write_tone(path: &Path, secs: f64) {
|
|
let sr = 48_000u32;
|
|
let n = (secs * sr as f64) as usize;
|
|
let mut data = Vec::with_capacity(n * 4);
|
|
for i in 0..n {
|
|
let s = (0.5 * (2.0 * std::f64::consts::PI * 440.0 * i as f64 / sr as f64).sin() * 32767.0).round() as i16;
|
|
data.extend_from_slice(&s.to_le_bytes());
|
|
data.extend_from_slice(&0i16.to_le_bytes());
|
|
}
|
|
let mut w = Vec::new();
|
|
w.extend_from_slice(b"RIFF");
|
|
w.extend_from_slice(&(36 + data.len() as u32).to_le_bytes());
|
|
w.extend_from_slice(b"WAVEfmt ");
|
|
w.extend_from_slice(&16u32.to_le_bytes());
|
|
w.extend_from_slice(&1u16.to_le_bytes());
|
|
w.extend_from_slice(&2u16.to_le_bytes());
|
|
w.extend_from_slice(&sr.to_le_bytes());
|
|
w.extend_from_slice(&(sr * 4).to_le_bytes());
|
|
w.extend_from_slice(&4u16.to_le_bytes());
|
|
w.extend_from_slice(&16u16.to_le_bytes());
|
|
w.extend_from_slice(b"data");
|
|
w.extend_from_slice(&(data.len() as u32).to_le_bytes());
|
|
w.extend_from_slice(&data);
|
|
std::fs::write(path, w).expect("write wav");
|
|
}
|