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

playing MP3 and sending PCM bytes over serial port

Nov 18, 2013 at 6:29 PM
Dear Sir ,

I am trying to send audio file to serial port ( GSM ). I need to send the bytes in sequence in order to be heared by the receipient phone. the needed format is signed 16bit PCM 8000 Hz file. I have made some success by sending the PCM itself

Dim fileByte() As Byte = My.Computer.FileSystem.ReadAllBytes("c:\k29.pcm")
Dim n As Integer = 16 / 2
For x As Integer = 0 To fileByte.Length - n - 1 Step n
sPortSound.Write(fileByte, x, n)
Next

no , I need to play MP3 directly to serial port. I need to get the PCM bytes , and send it to the serial port. I guess that MP3 file reader class will open the MP3 file and decompress to PCM. Am I right , in this case how to loop through bytes to send it to the serial port.

Please assume that the recorded MP3 is compressed from PCM file that is 16 bit,8KHz, so we do not need to convert file to the required sampling & bit rate.

Thanks for support
Coordinator
Nov 18, 2013 at 6:44 PM
you can just call Read on the Mp3FileReader to read into a byte array, and this will be filled with PCM.
Nov 18, 2013 at 8:12 PM
I tried the below code , but I am hearing a very strange sound on the other side. I know I am missing something like sample rate
Dim sf As New Mp3FileReader("c:\play.mp3")
Dim fileByte(sf.Length) As Byte
sf.Read(fileByte, 0, sf.Length)
    Dim n As Integer = 2
    For x As Integer = 0 To fileByte.Length - n - 1 Step n
        sPortSound.Write(fileByte, x, n)
    Next
Coordinator
Nov 19, 2013 at 12:33 PM
not possible to tell from your code what is wrong. I'd usually recommend reading out of the MP3 file in smaller chunks (e.g. a second at a time).
Most MP3s decompress to 16 bit, stereo, 44.1kHz
Nov 19, 2013 at 10:21 PM
Edited Nov 19, 2013 at 10:22 PM
Dear markheath ,

Thanks for your feedback.. I will struggling/fighting to get it resolved. I still want to confirm with you that :-
  • the code above , the MP3FileReader will read the MP3 file and converts it to PCM data ?
  • how do i determine the chunk size ? and on which interval I should send it to the serial port. when i determine the MPRFilerReader properties , it shows the the loaded file format is {16 bit PCM: 16kHz 2 channels}. May be it is 2 channel and I am send both channels. How can I split data for one channel , and read the other , or can i combine both into one channel and send to the serial port ?
Can I send you the MP3 file to play with ?

Thanks
Coordinator
Nov 21, 2013 at 3:47 PM
Yes, MP3FileReader emits PCM from its Read method
The chunk size will depend entirely on what the device on the other end of your serial port is expecting.
To convert stereo to mono, you can discard a channel or mix them together. There are StereoToMono classes in NAudio that will help you do this. If you have access to Pluralsight, watch the "Changing Audio Formats" of this course, in which I go into a lot of detail about how to do this.
Dec 18, 2013 at 4:16 PM
Thanks so much , works great. I am really surprised.

Thanks,
Waleed
Dec 27, 2013 at 11:43 AM
hi waleed makram i m seen your code and trying work with same as u do please help me how u fix this i m successfuly dial number to gsm modem after call established i want to play mp3 file like u do

Dim sf As New Mp3FileReader("c:\play.mp3")
Dim fileByte(sf.Length) As Byte
sf.Read(fileByte, 0, sf.Length)
Dim n As Integer = 2
For x As Integer = 0 To fileByte.Length - n - 1 Step n
    sPortSound.Write(fileByte, x, n)
Next
Thanks
Zahid
Apr 26, 2014 at 6:28 PM
Hi Zahid,

Any Progress , I am still have the same issue , and not work for me. Please advice if you made any progress.

Regards,
Waleed
Jun 24, 2014 at 5:43 AM
yes waleed i m successfully send wave or mp3 file data to serial port please email me at zahid.mahmood2010@gmail.com i will send u code