Search code examples
c#naudiowave

Wav converting with NAudio


I have a problem with converting the regular wav to a-law sound. I getting the bad quality when doing like that:

WaveStream stream = new WaveFileReader(in.wav);
var s = new RawSourceWaveStream(new WaveFormatConversionStream(new WaveFormat(8000, 16, 1), stream), new WaveFormat(8000, 16, 1));
var c = new WaveFormatConversionStream(WaveFormat.CreateALawFormat(8000, 1), s);
WaveFileWriter.CreateWaveFile(AppDomain.CurrentDomain.BaseDirectory + "\\out.alaw", c);

Or rhe twise slowed sound:

WaveStream stream = new WaveFileReader(in.wav);
 var s = new RawSourceWaveStream(stream, new WaveFormat(8000, 16, 1));
 var c = new WaveFormatConversionStream(WaveFormat.CreateALawFormat(8000, 1), s);
WaveFileWriter.CreateWaveFile(AppDomain.CurrentDomain.BaseDirectory + "\\out.alaw", c);

How convert the wav to a-law with good quality?


Solution

  • You don't need the RawSourceStream in either example, and the second example will not work unless in.wav happens to have the right format.

    Try this:

    var stream = new WaveFileReader(in.wav);
    var s = new WaveFormatConversionStream(new WaveFormat(8000, 16, 1), stream);
    var c = new WaveFormatConversionStream(WaveFormat.CreateALawFormat(8000, 1), s);
    WaveFileWriter.CreateWaveFile(AppDomain.CurrentDomain.BaseDirectory + "\\out.alaw", c);