Microsoft DirectX 8.1 (Visual Basic) |
Capturing a sound consists of the following steps:
To make sure you are not attempting to read a portion of memory that is about to be used for capture, you can first obtain the position of the read cursor by calling DirectSoundCaptureBuffer8.GetCurrentPosition. For an explanation of the read cursor, see Capture Buffer Information.
By default, the buffer stops capturing automatically when the capture cursor reaches the end of the buffer. However, if the DSCBSTART_LOOPING flag was set in the flags parameter to the DirectSoundCaptureBuffer8.Start method, the capture continues until the application calls the DirectSoundCaptureBuffer8.Stop method.
The following procedure in a form that implements DirectXEvent8 is called each time the capture cursor reaches a notification position. It calculates the amount of new data available, copies it to an intermediate buffer, and then writes it to a file.
Private Sub DirectXEvent8_DXCallback(ByVal eventid As Long)
' Global variables:
' lastPos As Long initialized to 0 each time capture starts
' BytesWritten As Long also initialized to 0
' dscb As DirectSoundCaptureBuffer8
' dscbDesc As DSCBUFFERDESC
' EventStop As Long
Static curPos As Long
Static curs As DSCURSORS
Dim dataBuf() As Byte
Dim dataSize As Long
dscb.GetCurrentPosition curs
curPos = curs.lWrite ' Position up to which data is valid
' Create an intermediate, temporary data buffer.
' lastPos is a global Long that is initialized to 0 each time
' capture begins.
dataSize = curPos - lastPos
If dataSize < 0 Then ' curPos wrapped around.
dataSize = dscbDesc.lBufferBytes - lastPos + curPos
End If
ReDim dataBuf(dataSize - 1)
dscb.ReadBuffer lastPos, dataSize, dataBuf(0), DSCBLOCK_DEFAULT
Put #1, , dataBuf
BytesWritten = BytesWritten + dataSize
lastPos = curPos
If (eventid = EventStop) Then
CloseFile
End If
End Sub
The EventStop event was associated with the offset DSBPN_OFFSETSTOP when notification positions were set. This event is signaled when the capture buffer stops. In response, the application calls a procedure that adds information to the file header and closes the file. For information on how to write file headers for wave files, see Writing to a Wave File.