728x90
728x170
■ WavWriter 구조체의 write_sample 메소드를 사용해 사인파 WAV 파일을 생성하는 방법을 보여준다.
▶ Cargo.toml
[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
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();
}
}
728x90
그리드형(광고전용)