Converting to Wave (CCITT aLaw 8Khz mono)

Jun 22, 2008 at 3:36 PM
hello,
it's me again..like I told you  I'm trying to convert from MP3(or wave ) to Wave(CCitt Alaw )..
I'm trying this using NAudio:
anyway here is it:

     class Program
    {
        static void Main(string[] args)
        {
            string inputfile = "test1.wav";
            string outputfile = "test1conv.wav";

    
            NAudio.Wave.WaveFormat frt = NAudio.Wave.WaveFormat.CreateCustomFormat(
               NAudio.Wave.WaveFormatEncoding.ALaw, 1,8000, 8000, 1, 8);
            using (NAudio.Wave.WaveFileReader srcFile = new NAudio.Wave.WaveFileReader(inputfile))
            {

                using (NAudio.Wave.WaveFormatConversionStream conv = new NAudio.Wave.WaveFormatConversionStream(frt, (NAudio.Wave.WaveStream)srcFile))//MmException here, it said: AcmNotPossible calling acmStreamOpen???
                {

                    using (NAudio.Wave.WaveFileWriter write = new NAudio.Wave.WaveFileWriter(outputfile, frt))
                    {
                        byte[] buffer = new byte[1024];
                        int byteRead;
                        do
                        {
                            byteRead = conv.Read(buffer, 0, buffer.Length);
                            write.WriteData(buffer, 0, byteRead);
                        } while (byteRead > 0);


                    }
                }
            }
        }
    }

do you have any idea what's could be the problem??
thanks.



Coordinator
Jun 22, 2008 at 4:32 PM
these type of conversions usually require multiple stages. First the MP3 needs to be converted to PCM. I would use WaveFormatConversionStream.CreatePcmStream for this. With a typical MP3 file this would come out as PCM 16 bit stereo 44.1kHz. Next use a second WaveFormatConversionStream to convert to PCM with the sample rate and channels you require - i.e. PCM 16 bit mono 8kHz. Now finally you can use a third WaveFormatConversionStream to get to the A-law format you want.

hope this helps

Mark
Jun 23, 2008 at 10:33 AM
Hi,
I'm trying your way...I got an MP3 file and I'm trying to conversion to PCM ...I read my MP3  MP3FileReader, then I use the 
WaveFormatConversionStream.CreatePcmStream to get a Wave Stream ..then I instantiate a Wave Writer and try to read form the PCMStream and Write...but It's not Working ..
this the part of the code:
        public static string inputfile = "Test2.mp3";
        public static string outputfile = "Testconv1.wav";
        public static void convertToPcm()
        {
            using (NAudio.Wave.Mp3FileReader mp3Reader = new NAudio.Wave.Mp3FileReader(inputfile))
            {
          

     
                using (NAudio.Wave.WaveStream pcmStream = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(mp3Reader))
                {

                    using (NAudio.Wave.WaveFileWriter writer = new NAudio.Wave.WaveFileWriter(outputfile, pcmStream.WaveFormat))
                    {
                        writer.Flush();   
                        byte[] buffer = new byte[1024];
                        int byteRead;
                        do
                        {
                            byteRead = pcmStream.Read(buffer, 0, buffer.Length);///Here is the Problem...It return 0 ..always!!!
                            writer.WriteData(buffer,0, byteRead);

                        } while (byteRead > 0);
                    }
                }
            }
        }


byteRead = pcmStream.Read(buffer, 0, buffer.Length);///Here is the Problem...It return 0 ..always!!!
even the first time I read the file!!!
do You have any idea !!
thanks a lot..
cheers,
Walid
Coordinator
Jun 23, 2008 at 2:12 PM
Hi Walid,
I'm not sure exactly what is going wrong. Perhaps you could try reading with a larger buffer (say 8KB). Also, is your MP3 file using CBR or VBR?
I have to admit that the Mp3FileReader class is still in its early stages, and probably needs to be made more robust. It currently uses whatever MP3 ACM decoder it can find on your system. Let me know how you get on.
Mark

Jun 23, 2008 at 3:37 PM
hello,
I'm trying to fix this..but do you think that for the moment..I can just convert from normal wave to CCITT ALaw 8Khz Mono..???
thanks..
Coordinator
Jun 23, 2008 at 6:22 PM
Walid, are you using the latest code from CodePlex? I have made some improvements to the ACM conversion code recently. If you can build it, you can run the NAudioDemo application and use it to perform a WAVE conversion to A-law. (You might need to do it in two stages though, depending on the sample-rate of your original file).

Mark
Jun 23, 2008 at 9:50 PM
Thanks man, I'll give it a shot..I'm under a lot of pressure to make this work..
when you say two stage: from wave -->PCM 16 bit stereo 44.1kHz --> PCM 8 bit Mono 8kHz--> then CCITT (with CustomWaveFormat Class)??
and just a last thing , I still have this MmException saying "AcmNotPossible calling acmStreamOpen"!!!
do you where this is coming from !!
thanks a lot
cheers,
Walid
Jun 25, 2008 at 2:43 PM
hello,
I got the Demo working now!! :D
what is the stage to go from a wave (PCM 44Khz Stereo)--> to a Wave (CCITT A-Law 8Khz MOnO )
please???


cheers,
Walid
Coordinator
Jun 25, 2008 at 4:29 PM


what is the stage to go from a wave (PCM 44Khz Stereo)--> to a Wave (CCITT A-Law 8Khz MOnO )
please???

Hi Walid

You need to go first from PCM 44kHz Stereo to PCM 8kHZ mono. Then you can convert to A-law

Mark