如何使用n音讯加入3 WAV文件转换成单一的文件有3个频道在C#中? [英] How to use NAudio to join 3 wav files into single file with 3 channels in C#?

查看:123
本文介绍了如何使用n音讯加入3 WAV文件转换成单一的文件有3个频道在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C#和n音讯,我也有三个波文件,我想加入到具有三个信道的单波文件,分别对应三个输入文件之一。此外,我想每个波形文件的音频剪辑,开始在不同的点在最后流。例如,如果三个声音文件的长度是3秒,5秒。和2秒,分别输出波形文件将在10秒之久,并...

Using C# and NAudio, I have have three wave files I would like to join into a single wave file having three channels, each corresponding to one of the three input files. Furthermore, I would like the audio clip from each wave file to begin at a different point in the final stream. For example, if the lengths of the three wave files are 3 sec., 5 sec. and 2 sec., respectively, the output wave file would be 10 seconds long and...

  • 频道1包含声音的3秒内从文件1后跟7 沉默秒。

  • channel 1 contains the 3 seconds of sound from file 1 followed by 7 seconds of silence.

频道2包含沉默3秒其次是5秒 从文件2,然后在2秒钟无声胜有声。

channel 2 contains 3 seconds of silence followed by the 5 seconds of sound from file 2 followed by 2 seconds of silence.

频道3含8秒的沉默之后的2秒 从文件3声。

channel 3 contains 8 seconds of silence followed by the 2 seconds of sound from file 3.

我一直在尝试通过为每个文件创建WaveChannel32实例,然后使用WaveOffsetStream类,但我是新来的这样的事情,我没有太多的成功。

I have been experimenting by creating a WaveChannel32 instance for each file and then using the WaveOffsetStream class, but I'm new to this sort of thing and I'm not having much success.

任何人有什么建议?

推荐答案

您将通过编写自己的自定义IWaveProvider /的WaveStream其输出WAVEFORMAT有三个渠道做到这一点。它拥有一个引用您的每三个输入文件。 然后,在读取方法,它的工作原理出请求的样本数,并读出从每个源文件的合适数量。样品然后根据需要进行交织 - 一个来自第一个文件,一个来自第二,一个来自第三方。要耽误你只是把零点研究。

You would do this by writing your own custom IWaveProvider/WaveStream whose output WaveFormat has three channels. It holds a reference to each of your three input files. Then, in the Read method, it works out the number of samples requested, and reads the appropriate number from each source file. The samples then need to be interleaved - one from first file, one from second, one from third. To delay you simply put zeroes in.

下面是一些未经检验的例子$​​ C $ C,从不同的源文件交错(必须是相同的比特深度和采样率),希望这将指向您在正确的方向:

Here's some untested example code that interleaves from various source files (must all be of same bit depth and sample rate) which will hopefully point you in the right direction:

int Read(byte[] buffer, int offset, int bytesRequired)
{
    int bytesPerSample = this.WaveFormat.BitsPerSample / 8;
    int samplesRequired = bytesRequired / bytesPerSample;
    int channels = this.WaveFormat.Channels; // 3
    int samplesPerChannel = samplesRequired / channels;
    byte[] readBuffer = new byte[samplesPerChannel * bytesPerSample];
    for (int channel = 0; channel < channels; channel++)
    {
        int read = inputs[channel].Read(readBuffer, 0, samplesPerChannel * bytesPerSample);
        int outOffset = offset + channel * bytesPerSample;
        for (int i = 0; i < read; i += bytesPerSample)
        {
             Array.Copy(readBuffer, i, buffer, outOffset, bytesPerSample);
             outOffset += channels * bytesPerSample;
        } 
    }
}

要保持code变得过于复杂,你可以通过创建另一个衍生IWaveProvider /的WaveStream,其Read方法返回的沉默适量,在此之前,从输入文件返回真实的数据做你的沉默插入。这可以被用来作为一个输入您的交织的WaveStream

To keep the code from becoming overly complicated, you could do your silence insertion by creating another derived IWaveProvider / WaveStream, whose Read method returned the appropriate amount of silence, before then returning the real data from the input file. This can then be used as an input to your interleaving WaveStream.

这篇关于如何使用n音讯加入3 WAV文件转换成单一的文件有3个频道在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆