Saving each WAV channel as a mono-channel WAV file using Naudio

可紊 提交于 2019-11-29 07:29:26

The basic idea is that the source wave file contains the samples interleaved. One for the first channel, one for the second, and so on. Here's some untested example code to give you an idea of how to do this.

var reader = new WaveFileReader("fourchannel.wav");
var buffer = new byte[2 * reader.WaveFormat.SampleRate * reader.WaveFormat.Channels];
var writers = new WaveFileWriter[reader.WaveFormat.Channels];
for (int n = 0; n < writers.Length; n++) 
{
    var format = new WaveFormat(reader.WaveFormat.SampleRate,16,1);
    writers[n] = new WaveFileWriter(String.Format("channel{0}.wav",n+1), format);
}
int bytesRead;
while((bytesRead = reader.Read(buffer,0, buffer.Length)) > 0) 
{
    int offset=  0;
    while (offset < bytesRead) 
    {
        for (int n = 0; n < writers.Length; n++) 
        {
            // write one sample
            writers[n].Write(buffer,offset,2);
            offset += 2;
        }           
    }
}
for (int n = 0; n < writers.Length; n++) 
{
    writers[n].Dispose();
}
reader.Dispose();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!