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

read sound from Serial port as bytes then save it to wav file

Nov 11, 2013 at 10:15 PM
dear Sir,
First , thanks for the great support in this forum. and thanks for the great control as well.

Currently , i am trying to get sound from serial port as byte. the format of the coming sound is 8KHz , 16 bit , 1 channel. I need to collect the sound and after serial port data is complete , it will save the data to a wav file (and to MP3 file later).
Currently , i used this code
' 1---------------------------------------- initial declaration -------------------------
Private Mywriter As WaveFileWriter ' into class delclaration section
' 2----------------------------------------when need to start recording data from com port --
 Mywriter = New WaveFileWriter("c:\y1.wav", New WaveFormat())
                AddHandler sPortSound.DataReceived, AddressOf PhoneSoundReceived
' -3----------------------------------------------------- reading com port data ----------------
Private Sub COMPortSoundReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
    Dim n As Integer = sPortSound.BytesToRead
    Dim comBuffer As Byte() = New Byte(n - 1) {}
sPortSound.Read(comBuffer, 0, n)
    If comBuffer.Length > 0 Then
        Mywriter.Write(comBuffer, 0, n)
    End If
end sub
' 4------------------------------- need to close recording and build file -------------------
                If Not IsNothing(Mywriter) Then
                    Mywriter = Nothing
                End If

problem is that is did not make part 4 , when closing the application , system crashes showing message ""Wavefilewriter was not disposed at wavefilewriter.finalize() " , then this message disappears . However , the file is stored as pcm file (without header) ,

if I include section 4 , ie to close the wavefilewriter before close application , the program terminates easily without error , stored file is in wav format , , but the length is to much small , eg 0.5 second.

please advice.

Nov 12, 2013 at 4:38 PM
You must dispose the WaveFileWriter only after you've received all your audio from the COM port and just before your program exits. Your WaveFormat in the WaveFileWriter constructor also needs to have the correct sample rate and channel count set.
Nov 12, 2013 at 6:12 PM
Great ... Thanks man, works like charm
Thanks for great control , and for the excellent support. :)