Question

I've been using NAudio for the past few day mixing two wave files with this format :

SampleRate: 8000 BitsPerSample: 8 Channels: 1 Block Align Channels: 1 Bits per Second: 8000

as I found from https://naudio.codeplex.com/discussions/251475 , first I should convert them into a 32 bit ieeefloatingpoint encoding and then add converted streams to WaveMixerStream32. after that I should convert mixed data to 16bit/sample stream using Wave32To16Stream, convert obtained stream to mono and finally convert mono 16bit/sample to mono 8bit/sample.

I wrote these lines of code according to what I explain :

    WaveFileReader r1 = new WaveFileReader(...);
    WaveFileReader r2 = new WaveFileReader(...);

    WaveFormat OutputWaveFormat = WaveFormat.CreateCustomFormat(WaveFormatEncoding.Pcm, 8000, 1, 16000, 2, 16);
    WaveFormat InputWaveFormat = WaveFormat.CreateALawFormat(8000, 1);

    WaveFormatConversionStream ConversionStream1 = new WaveFormatConversionStream(OutputWaveFormat, r1);
    WaveFormatConversionStream ConversionStream2 = new WaveFormatConversionStream(OutputWaveFormat, r2);

    WaveChannel32 WaveChannel32_1 = new WaveChannel32(ConversionStream1);
    WaveChannel32 WaveChannel32_2 = new WaveChannel32(ConversionStream2);

    WaveMixerStream32 mixer32 = new WaveMixerStream32();
    mixer32.AddInputStream(WaveChannel32_1);

    mixer32.AddInputStream(WaveChannel32_2);//(*)

    Wave32To16Stream stereo16 = new Wave32To16Stream(mixer32);
    StereoToMonoProvider16 mono16 = new StereoToMonoProvider16(m_Wave32To16Stream);

after line (*) I have a stream such : 0 0 32 60 0 0 32 60 0 0 59 126 0 0 59 126 ... but after applying Wave32To16Stream, I have a sequence of just zeros in stereo16 and so in mono16!! what should I do in this step?

Was it helpful?

Solution

Answer was very simple than I think. I just need to convert ALaw to PCM and then add two wave stream, I done that using ALawDecoder from G711 codeproject article.

r1 = new WaveFileReader(...);
r2 = new WaveFileReader(...);
r1.Read(arr1, 0, arr1.Length);    
r2.Read(arr2, 0, arr2.Length);
short[] firstPCM16 = ALawDecoder.ALawDecode(arr1);
short[] secondPCM16 = ALawDecoder.ALawDecode(arr2);
byte[] result1 = new byte[firstPCM16.Length * sizeof(short)];
byte[] result1 = new byte[secondPCM16.Length * sizeof(short)];
Buffer.BlockCopy(firstPCM16, 0, result1, 0, result1.Length);
Buffer.BlockCopy(secondPCM16, 0, result2, 0, result2.Length);
for (...)
{                        
    mixed[i] = (byte)(result1[i] + result2[i]);//No need to dividing by 2 because r1  and r2 are 8 bit ALaw and return value of ALawDecoder is 16 bit pcm
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top