■ WavWriter 구조체의 write_sample 메소드를 사용해 사인파 WAV 파일을 생성하는 방법을 보여준다.
▶ Cargo.toml
1 2 3 4 5 6 7 8 9 10 11 |
[package] name = "test_project" version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] hound = "3.4.0" |
▶ src/main.rs
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 |
use std::f32::consts; use std::fs; use std::io; use hound; const SAMPLE_RATE : u32 = 44100; const TONE : f32 = 440.0; // 440Hz = A fn main() { let wav_spec : hound::WavSpec = hound::WavSpec { channels : 1, sample_rate : SAMPLE_RATE, bits_per_sample : 16, sample_format : hound::SampleFormat::Int }; let mut wav_writer : hound::WavWriter<io::BufWriter<fs::File>> = hound::WavWriter::create("d:/target.wav", wav_spec).unwrap(); let sample_count : u32 = SAMPLE_RATE * 3; // 3초 for t in 0..sample_count { let v : f32 = ((t as f32 / SAMPLE_RATE as f32) * TONE * 2.0 * consts::PI).sin(); wav_writer.write_sample((v * i16::MAX as f32) as i16).unwrap(); } } |