[RESOLVED] DirectSound reading data from capture buffer
Can anyone see where I'm going wrong with my circular DirectSound Capture buffer here? If I just capture sound into a big non-circular buffer it works fine, but with a 1 second (44100 bytes) buffer and a 100ms timer to poll the read position it goes haywire. No error messages, just long blocks of zeros with short bursts of valid data. I can't help thinking I'm doing something silly with my calculation of the number of bytes to read and/or the read position.
Here is the code. gBuffer is defined elsewhere as a big byte array. In this example I am just trying to copy the captured sound data from the small temporary byte array (tempBuffer) into the big gBuffer byte array. Obviously this must be done so that every byte is read and copied into the byte array with no gaps or duplication of values. Timer1 is set to 100mS:
Code:
Dim McaptureBuffer As CaptureBuffer
Dim MiLastReadPos As Integer
Dim MiBufferOffset As Integer
Dim MiBufferSize As Integer = 44100
Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click
Dim waveFormat As New WaveFormat
waveFormat.SamplesPerSecond = 44100
waveFormat.Channels = 1
waveFormat.BitsPerSample = 8
waveFormat.FormatTag = WaveFormatTag.Pcm
waveFormat.AverageBytesPerSecond = 44100
waveFormat.BlockAlign = 1
Dim captureBufferDesc = New CaptureBufferDescription
captureBufferDesc.BufferBytes = 44100
captureBufferDesc.ControlEffects = False
captureBufferDesc.WaveMapped = True
captureBufferDesc.Format = waveFormat
Dim capture As New Capture
McaptureBuffer = New CaptureBuffer(captureBufferDesc, capture)
McaptureBuffer.Start(True) ' True = looping
MiBufferOffset = 0
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim iCap As Integer
Dim iRead As Integer
Dim iBytes As Integer
Try
McaptureBuffer.GetCurrentPosition(iCap, iRead)
If iRead < MiLastReadPos Then
' "wrap-around" of circular buffer has occurred
iBytes = (MiBufferSize - MiLastReadPos) + iRead
Else
iBytes = iRead - MiLastReadPos
End If
Dim tempBuffer(iBytes) As Byte
McaptureBuffer.Read(iRead, New IO.MemoryStream(tempBuffer), iBytes, LockFlag.None)
MiLastReadPos = iRead
lstData.Items.Add(iBytes & " : " & MiBufferOffset)
For n As Integer = 0 To tempBuffer.Length - 1
Gbuffer(MiBufferOffset) = tempBuffer(n)
MiBufferOffset += 1
Next n
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Any help would be muich appreciated.
Re: DirectSound reading data from capture buffer
I found the first obvious mistake - I should be starting my Read from the last read position, not the present one. It's still not quite right though (sorry to be vague - there's a "glitch" value getting into the buffer on every Timer1 timeout). I'll keep working on it.
Re: DirectSound reading data from capture buffer
OK, got it sorted out. My tempBuffer array was one byte bigger than it needed to be so I was always copying a 0 from it to the global buffer (gBuffer). Sorry to have wasted your time ...