This project has moved and is read-only. For the latest updates, please go here.

Converting to A-law wave / Playing all types of mp3, wave

Feb 3, 2010 at 9:54 PM

I am wanting to convert from an mp3 to a wave and store the result in a byte stream and send it over a network.

In read one of your previous posts and you said to convert from "PCM 44kHz Stereo to PCM 8kHZ mono. Then you can convert to A-law"

How do I do the last step? Converting to A-law?


using (NAudio.Wave.Mp3FileReader mp3Reader = new NAudio.Wave.Mp3FileReader(fileName))
     using (NAudio.Wave.WaveStream pcmStream = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(mp3Reader))
         using (NAudio.Wave.WaveStream alawStream = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(pcmStream))
             buffer = new byte[alawStream.Length];
             int byteRead = pcmStream.Read(buffer, 0, alawStream.Length);

The reason I am doing this is because I can't get NAudio to play every kind of mp3 and wave file (with varying sample rates). I need to store the byte streams of these audio files in a database and then send the stream across the network to play on a different computer. In a different implementation I managed to play all sorts of wave files but was only able to play 44khz mp3 files. I don't know why... I would prefer to not do any converting of files if possible...

Thanks for your help!


Mar 11, 2010 at 2:32 PM

you create a WaveFormatConversionStream like this

new WaveFormatConversionStream(WaveFormat.CreateALawFormat(sourceStream.SampleRate, sourceStream.Channels), sourceStream)


ps sorry for the delayed reply, I have been swamped with requests for NAudio help recently


Mar 12, 2010 at 6:17 PM

No problem. Thanks for the reply.

How can I get the SampleRate and Channels from a sourceStream though?

Mar 12, 2010 at 7:15 PM

oops, sorry. these are properties of the WaveFormat property of the source stream