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

Write mp3 file to serial port to play on other side on phone call

May 7, 2015 at 7:37 AM
Hi I am trying to write data from mp3 file to serial port so that i can play this on otherside of phone call . But i can only listen some noise. Following is my code :

bufferedWaveProviderout = new BufferedWaveProvider(new WaveFormat(8000, 16, 1));
        // try
        {
            //    dynamic audioBufferSize = 320;
            //  int offset = 0;
            //  byte[] buffer2 = new byte[320];

            Mp3FileReader sf = new Mp3FileReader("d:\\a.mp3");
            byte[] fileByte = new byte[sf.Length + 1];
            sf.Read(fileByte, 0, Convert.ToInt32(sf.Length));

            int max = fileByte.Length;
            int playingyt = 0;
            while (playingyt <= max)
            {

                byte[] buf = fileByte.Skip(playingyt).Take(1600).ToArray();

                byte[] source = buf;
                Stream byteStream12 = new MemoryStream(source);
                WaveFormat waveFormat = new WaveFormat(8000, 16, 1);

                RawSourceWaveStream rawSourceWaveStream = new RawSourceWaveStream(byteStream12, waveFormat);
                byte[] bt = ConvertNonSeekableStreamToByteArray(byteStream12);
                bufferedWaveProviderout.AddSamples(bt, 0, bt.Length);

                var resampleStream = new AcmStream(new WaveFormat(8000, 16, 1), WaveFormat.CreateCustomFormat(WaveFormatEncoding.MuLaw, 8000, 1, 8000 * 1, 1, 8));

                byte[] source1 = bt;
                Buffer.BlockCopy(source1, 0, resampleStream.SourceBuffer, 0, source1.Length);
                int sourceBytesConverted = 0;
                var convertedBytes = resampleStream.Convert(source1.Length, out sourceBytesConverted);
                if (sourceBytesConverted != source1.Length)
                {
                    Console.WriteLine("We didn't convert everything {0} bytes in, {1} bytes converted");
                }

                var converted = new byte[convertedBytes];
                Buffer.BlockCopy(resampleStream.DestBuffer, 0, converted, 0, convertedBytes);

                MemoryStream byteStream = new MemoryStream(converted);


                Stream byteStream122 = new MemoryStream(converted);

                dynamic audioBufferSize = 320;
                int offset = 0;
                //  byte[] buffer = new byte[audioBufferSize - 1];// (audioBufferSize - 1) {}
                byte[] buffer2 = new byte[320];

                while (byteStream122.Read(buffer2, offset, audioBufferSize - offset) > 0)
                {

                    {

                        {
                            _spManager.WriteVoice(buffer2, offset, buffer2.Length - offset);

                            PauseForMilliSeconds(20);
                        }

                    }
                }

                playingyt = playingyt + 1600;
            }





        }
Thanks in advance
May 8, 2015 at 11:34 AM
Hi mark

Waiting for your reply.

Thanks
May 11, 2015 at 7:18 AM
Hi

Please Guide me where i am wrong, This code is not working. If this is not possible with this library or something is missing, please give me right direction. Scratching my head from last one week, still sucked at this point.

Regards

Rakesh Kumar