简体   繁体   English

用猎犬创造声音

[英]Create sound on sound with the hound

How can you make one file, for example: " out.wav ", and in it add " 1.wav " 10 times, but without delays.你如何制作一个文件,例如:“ out.wav ”,并在其中添加“ 1.wav10次,但没有延迟。 That is, to superimpose the sound on another sound even if the previous one has not finished playing.即,将声音叠加到另一个声音上,即使前一个声音还没有播放完。 As I've been trying to do for about an hour and a half now, and all I get is playback one by one (ie: first the sound plays completely)正如我现在尝试做的大约一个半小时,我得到的只是一个一个地播放(即:首先声音完全播放)

Simplest code:最简单的代码:

use hound::{WavReader, WavWriter};

fn main() {
    let mut reader = WavReader::open("1.wav").unwrap();
    let spec = reader.spec();
    let mut writer = WavWriter::create("output.wav", spec).unwrap();

    for _ in 0..10 {
        for sample in reader.samples::<i16>() {
            writer.write_sample(sample.unwrap()).unwrap();
        }

        reader.seek(0).unwrap();
    }
}

You have to somehow read & mix the multiple sounds you want to play at the same time.您必须以某种方式读取和混合您想要同时播放的多种声音。

Here is an example that plays 10 times the same sound each delayed by half that sounds length, mixed by simply averaging both clips playing at the same time.这是一个示例,播放 10 次相同的声音,每次延迟一半的声音长度,通过简单地对同时播放的两个片段进行平均来混合。

use hound::{WavReader, WavWriter};

fn main() {
    let mut reader = WavReader::open("1.wav").unwrap();
    let mut reader2 = WavReader::open("1.wav").unwrap();
    let mut writer = WavWriter::create("output.wav", spec).unwrap();
    let l = reader.len();
    reader.seek(l/2).unwrap();
    for i in 0..l*10 {
        if i % (l/2) == 0 {
            reader.seek(0).unwrap();
            let tmp = reader;
            reader = reader2;
            reader2 = tmp;
        }
        let s1 = reader.samples::<i16>().next().unwrap().unwrap();
        let s2 = reader2.samples::<i16>().next().unwrap().unwrap();
        let sample = s1/2 + s2/2;
        writer.write_sample(sample).unwrap();
    }
}

Which can definitely be cleaned up a lot (ie store the samples in a Vec once and just read from there)这绝对可以清理很多(即将样本存储在Vec中一次,然后从那里读取)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM