WaveinProc not returning data

412 views Asked by At

I was following a tutorial on waveIn to capture sound from my microphone and came up with this:

 Private Delegate Sub WaveInProc(hwnd As IntPtr, ByVal uMsg As WIMMessages, ByVal dwInstance As UIntPtr, ByVal dwParam1 As IntPtr, ByVal dwParam2 As IntPtr)

Private WAVE_MAPPER As Int32 = -1

Const MEM_COMMIT As Integer = 4096
Const PAGE_READWRITE As Integer = 4
Const WAVE_FORMAT_PCM As Integer = 1

Private result As MMRESULT
Private bufferPointer As IntPtr
Private waveinHandle As IntPtr = IntPtr.Zero

 Public Sub Start_Record()
    Dim waveInCaps As New WAVEINCAPS

    Debug.WriteLine(waveInGetDevCaps(0, waveInCaps, Marshal.SizeOf(waveInCaps)))

    Dim waveFormat As New WAVEFORMATEX
    waveFormat.wFormatTag = WAVE_FORMAT_PCM
    waveFormat.wBitsPerSample = 16
    waveFormat.nSamplesPerSec = 22050
    waveFormat.nChannels = 2
    waveFormat.nAvgBytesPerSec = CUInt(waveFormat.nSamplesPerSec * waveFormat.nChannels * (waveFormat.wBitsPerSample / 8))
    waveFormat.nBlockAlign = CUShort(waveFormat.wBitsPerSample * waveFormat.nChannels / 8)
    waveFormat.cbSize = 0

    result = waveInOpen(waveinHandle, WAVE_MAPPER, waveFormat, AddressOf callback, IntPtr.Zero, WaveInOpenFlags.CALLBACK_FUNCTION)

    MessageBox.Show(result.ToString)

    Dim bufferHeader As WAVEHDR

    bufferPointer = Marshal.AllocHGlobal(Marshal.SizeOf(bufferHeader))
    Marshal.StructureToPtr(bufferHeader, bufferPointer, True)

    result = waveInPrepareHeader(waveinHandle, bufferPointer, Marshal.SizeOf(bufferHeader))

    Debug.WriteLine(result.ToString)

    result = waveInAddBuffer(waveinHandle, bufferPointer, Marshal.SizeOf(bufferHeader))

    Debug.WriteLine(result.ToString)

    result = waveInStart(waveinHandle)

    Debug.WriteLine(result.ToString)
End Sub

And the waveInProc callback:

 Private Sub callback(ByVal hwnd As IntPtr, ByVal uMsg As WIMMessages, ByVal dwInstance As UIntPtr, ByVal dwParam1 As IntPtr, ByVal dwParam2 As IntPtr)
    Select Case uMsg
        Case WIMMessages.WIM_CLOSE
            Marshal.FreeHGlobal(bufferPointer)
        Case WIMMessages.WIM_DATA
            Dim newStruct As WAVEHDR

            newStruct = CType(Marshal.PtrToStructure(dwParam1, GetType(WAVEHDR)), WAVEHDR)

            Debug.WriteLine(waveInUnPrepareHeader(waveinHandle, dwParam1, Marshal.SizeOf(newStruct)))

            'Prepare another buffer
            Dim newbuffer As New WAVEHDR
            Dim newpointer As IntPtr

            newpointer = Marshal.AllocHGlobal(Marshal.SizeOf(newbuffer))

            Marshal.StructureToPtr(newbuffer, newpointer, True)

            Debug.WriteLine(waveInPrepareHeader(waveinHandle, newpointer, Marshal.SizeOf(newbuffer)))

            Debug.WriteLine(waveInAddBuffer(waveinHandle, newpointer, Marshal.SizeOf(newbuffer)))
    End Select
End Sub

The problem is, everytime the callback is called, newStruct only gets populated on the dwflags field with the values WaveHdrFlags.WHDR_DONE Or WaveHdrFlags.WHDR_PREPARED and the rest of the structure remains empty, which means, no microphone data is being returned by the dwParam1 which is the buffer.

What did i do wrong? The callback is called every second or so, is it some buffer size problem? I can't figure it out.

0

There are 0 answers