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

I am trying to convert a WAV file (PCM, 48kHz, 4-Channel, 16 bit) to single-channel WAV files.

I tried splitting the WAV file into 4 byte arrays, like, and created a WaveMemoryStream, as shown below, but it doesn't work.

byte[] chan1ByteArray = new byte[channel1Buffer.Length]; Buffer.BlockCopy(channel1Buffer, 0, chan1ByteArray, 0, chan1ByteArray.Length); WaveMemoryStream chan1 = new WaveMemoryStream(chan1ByteArray, sampleRate, (ushort)bitsPerSample, 1); 

Am I missing something in creating WAVE headers? Or is there more to split WAV into files with mono-channel WAV?

+4
source share
1 answer

The basic idea is that the original wave file contains alternating patterns. One for the first channel, the second for the second, etc. Here are some unverified code examples 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(); 
+4
source

All Articles