This project has moved. For the latest updates, please go here.

Adpcm to WAV

Feb 17, 2013 at 11:59 PM

I'm trying to convert an IMA Adpcm stream into a WAV stream.
This is my latest attempt:
            var bytes = File.ReadAllBytes(@"c:\temp\bytes.bin");
            using (var stream = new MemoryStream(bytes))
                RawSourceWaveStream rawStream = new RawSourceWaveStream(stream, new ImaAdpcmWaveFormat(8000, 1, 4));
                WaveFormatConversionStream convertStream = new WaveFormatConversionStream(new WaveFormat(8000, 16, 1), rawStream);
                var buffer = new byte[640];
                convertStream.Read(buffer, 0, buffer.Length);
This attempt has failed because the WaveFormatConversionStream is trying to create an AcmStream which in turn doing sourceBufferSize -= (sourceBufferSize % sourceFormat.BlockAlign);
Since ImaAdpcmWafeFormat initializes BlockAlign to be 0, I get DivisionByZeroException.
I fixed that by doing this: sourceBufferSize -= sourceFormat.BlockAlign != 0 ? (sourceBufferSize % sourceFormat.BlockAlign) : 0;
So I passed this line.
Now it fails while calling acmStreamOpen with the error: "AcmNotPossible calling acmStreamOpen".
I don't understand why. When I enumerate my codecs I see the Microsoft IMA ADPCM CODEC and the following:
Format 0: 8.000 kHz, 4 Bit, Mono
  FormatTag: DviAdpcm, Support Flags: Codec
  WaveFormat: DviAdpcm 8000Hz Channels: 1 Bits: 4 Block Align: 256, AverageBytesPerSecond: 4055 (32.4 kbps), Extra Size: 2
  Extra Bytes:
  F9 01 
Which is exactly the format of my stream.

How can I make this conversion to work?

Thank you,