string pcmFile = @"C:\AlvasAudio\obrigada.wav";
string alawFile = @"C:\AlvasAudio\obrigada.alaw";
Pcm2ALaw(pcmFile, alawFile);
//---
private static void Pcm2ALaw(string wavFile, string alawFile)
{
WaveReader wr = new WaveReader(File.OpenRead(wavFile));
IntPtr format = wr.ReadFormat();
byte[] data = wr.ReadData();
wr.Close();
WaveFormat wfAlaw = new WaveFormat();
wfAlaw.wFormatTag = AudioCompressionManager.ALawFormatTag;
wfAlaw.nChannels = 1;
wfAlaw.nSamplesPerSec = 8000;
FormatDetails[] fdArr = AudioCompressionManager.GetFormatList(wfAlaw);
WaveFormat wf = AudioCompressionManager.GetWaveFormat(format);
if (wf.wFormatTag != AudioCompressionManager.PcmFormatTag)//Decode if not PCM data
{
Decode2Pcm(ref format, ref data, ref wf);
}
if (wf.nSamplesPerSec != wfAlaw.nSamplesPerSec || wf.nChannels != wfAlaw.nChannels)
{
Resample(ref format, ref data, ref wf, wfAlaw.nSamplesPerSec, wfAlaw.nChannels);
}
byte[] buffer = AudioCompressionManager.Convert(format, fdArr[0].FormatHandle, data, false);
BinaryWriter bw = new BinaryWriter(File.Create(alawFile));
bw.Write(buffer, 0, buffer.Length);
bw.Close();
}
private static void Resample(ref IntPtr format, ref byte[] data, ref WaveFormat wf, int samplesPerSec, short channels)
{
IntPtr newFormat = AudioCompressionManager.GetPcmFormat(channels, wf.wBitsPerSample, samplesPerSec);
byte[] buffer = AudioCompressionManager.Convert(format, newFormat, data, true);
format = newFormat;
wf = AudioCompressionManager.GetWaveFormat(newFormat);
data = buffer;
}
private static void Decode2Pcm(ref IntPtr format, ref byte[] data, ref WaveFormat wf)
{
IntPtr newFormat = AudioCompressionManager.GetCompatibleFormat(format,
AudioCompressionManager.PcmFormatTag);
byte[] buffer = AudioCompressionManager.Convert(format, newFormat, data, false);
wf = AudioCompressionManager.GetWaveFormat(newFormat);
format = newFormat;
data = buffer;
}