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

Conversion PCM to Mulaw

Sep 17, 2014 at 7:14 PM
Edited Sep 17, 2014 at 7:17 PM
I am trying to convert PCM to MuLaw. I'm not changing SampleRate, or Channels.

The code works for these audio files below:
PCM - Sample Rate: 8000 - BitsPerSample: 16 - Channels: 1
PCM - Sample Rate: 11025 - BitsPerSample: 16 - Channels: 1

But not these audio files. I get "AcmNotPossible calling acmStreamOpen"
PCM - Sample Rate: 8000 - BitsPerSample: 16 - Channels: 2
PCM - Sample Rate: 11025 - BitsPerSample: 16 - Channels: 2

When I use ACM Format Conversion / NAudioDemo, I can convert all of these 4 audio files to muLaw. So the audio files are fine.

This is my code:
        WaveFormat targetFormat = WaveFormat.CreateMuLawFormat(_mainWaveStream.WaveFormat.SampleRate, _mainWaveStream.WaveFormat.Channels);

        _mainWaveStream = new WaveFormatConversionStream(targetFormat, _mainWaveStream);

I was wondering what the hell I am doing wrong. :)

Sep 17, 2014 at 7:55 PM
I compared my targetFormat, with targetFormat of ACM Format Conversion / NAudioDemo. The only difference is my BlockAlign is 1, the other BlockAlign is 2.

So I have to do this, and it works fine, but it looks ugly:
        WaveFormat targetMuLawFormat = WaveFormat.CreateMuLawFormat(_mainWaveStream.WaveFormat.SampleRat, _mainWaveStream.WaveFormat.Channels);

        WaveFormat targetFormat = WaveFormat.CreateCustomFormat(WaveFormatEncoding.MuLaw, targetMuLawFormat.SampleRate, targetMuLawFormat.Channels, targetMuLawFormat.AverageBytesPerSecond, _mainWaveStream.WaveFormat.BlockAlign, 8);

        _mainWaveStream = new WaveFormatConversionStream(targetFormat, _mainWaveStream);

Is there a smart way to do this?
Sep 17, 2014 at 8:59 PM
OK, that may be a bug in CreateMuLawFormat. BlockAlign should be the same as the number of channels. I'm surprised that the ACM codec is picky about what that value is, but I'll get a fixed checked in.