关于这篇文章

工作休息后,我在学习 Rust 的同时能够用 Rust 创造声音,所以我想做一些独立的暑假研究。
我以“让战记绝杀 Symphogear 永远获胜的感觉”为主题进行了尝试。

参考视频

我通过将两者结合起来制作了这个。

这很有趣。

补充

恭喜那些赢得 Symphogear Live 的人! !

11月20日,我们期待大家的“择手”。

我羡慕你。 . .

源代码

请购买这本书以获得该代码的解释。

额外的板条箱

hound = "3.4.0"

矿。 rs

use hound;
const SAMPLE_RATE: f32 = 44100.0;

fn main() {
    let spec = hound::WavSpec {
        channels: 1,
        sample_rate: SAMPLE_RATE as u32,
        bits_per_sample: 32,
        sample_format: hound::SampleFormat::Float,
    };
    let mut fw = hound::WavWriter::create("saw.wav", spec).unwrap();
    let mut wav: Vec<f32> = vec![];
    let bpm = 120;
    wav.extend(sawtooth_wave(67, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(72, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(74, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(75, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(75, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(75, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(75, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(75, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(67, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(67, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(67, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(67, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(67, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(67, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(74, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(74, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(74, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(74, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(74, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(74, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(70, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(70, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(70, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(70, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(70, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(70, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(72, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(72, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(72, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(72, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(72, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(72, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(72, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(72, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(72, calc_len(bpm, 16), 0.4));
    wav.extend(sawtooth_wave(72, calc_len(bpm, 4), 0.4));

    for v in wav.into_iter() {
        fw.write_sample(v).unwrap();
        println!("{}", v);
    }
}

fn noteno_to_hz(no: i32) -> f32 {
    440.0 * 2.0f32.powf((no - 69) as f32 / 12.0)
}

fn calc_len(bpm: usize, n: usize) -> usize {
    let base_len = (60.0 / bpm as f32) * SAMPLE_RATE;
    ((4.0 / n as f32) * base_len) as usize
}

fn sawtooth_wave(noteno: i32, len: usize, gain: f32) -> Vec<f32> {
    let tone = noteno_to_hz(noteno);
    let form_samples = SAMPLE_RATE / tone;
    let mut wav: Vec<f32> = vec![0.0; len];
    for i in 0..len {
        let pif = (i as f32 / form_samples) % 1.0;
        wav[i] = pif * 2.0 - 1.0;
    }
    wav.into_iter().map(|v| (v * gain) as f32).collect()
}


原创声明:本文系作者授权爱码网发表,未经许可,不得转载;

原文地址:https://www.likecs.com/show-308623246.html

相关文章: