-
Notifications
You must be signed in to change notification settings - Fork 0
/
capturesound.bas
226 lines (171 loc) · 7.7 KB
/
capturesound.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#pragma once
'capturesound声音捕捉
' Copyright (c) 2023 CM.Wang
' Freeware. Use at your own risk.
#include once "capturesound.bi"
Private Function WaveFileCreate(WaveFile As ZString Ptr, ByVal sample As Long = 44100, ByVal bits As Long = 16, ByVal channel As Long = 1) As Boolean
wavHeader.fmt.nSamplesPerSec = sample
wavHeader.fmt.wBitsPerSample = IIf(bits = 8, 8, 16)
wavHeader.fmt.nChannels = IIf(channel = 1, 1, 2)
wavHeader.fmt.nBlockAlign = wavHeader.fmt.nChannels * wavHeader.fmt.wBitsPerSample / 8
wavHeader.fmt.nAvgBytesPerSec = wavHeader.fmt.nSamplesPerSec * wavHeader.fmt.nBlockAlign
Dim As FILE Ptr fileHandle = fopen(WaveFile, @Str("wb"))
If fileHandle = NULL Then
Print "WaveFileCreate: Failed to open file for writing"
Return False
End If
Dim As DWORD bytesWritten = fwrite(@wavHeader, 1, SizeOf(wavHeader), fileHandle)
fclose(fileHandle)
If bytesWritten <> SizeOf(wavHeader) Then
Print "WaveFileCreate: Failed to write all data to file"
Return False
End If
Return True
End Function
Private Function WaveFileWriteData(WaveFile As ZString Ptr, bufferData As LPBYTE, bufferSize As DWORD) As Boolean
Dim As FILE Ptr fileHandle
fileHandle = fopen(WaveFile, @Str("ab"))
If fileHandle = NULL Then
Print "WaveFileWriteData: Failed to open file for writing"
Return False
End If
Dim As DWORD bytesWritten = fwrite(bufferData, 1, bufferSize, fileHandle)
fclose(fileHandle)
If bytesWritten <> bufferSize Then
Print "WaveFileWriteData: Failed to write all data to file"
Return False
End If
Return True
End Function
Private Function WaveFileClose(WaveFile As ZString Ptr) As Boolean
Dim As FILE Ptr fileHandle = fopen(WaveFile, @Str("r+"))
If fileHandle = NULL Then
Print "WaveFileClose: Failed to open file for writing"
Return False
End If
Dim As DWORD filesize = ftell(fileHandle)
wavHeader.riff.riffBlockSize = filesize - SizeOf(WAVEHEADER_DATA)
wavHeader.data.dataBlockSize = filesize - SizeOf(WAVEHEADER)
fseek(fileHandle, 0, SEEK_SET)
Dim As DWORD bytesWritten = fwrite(@wavHeader, 1, SizeOf(wavHeader), fileHandle)
fclose(fileHandle)
If bytesWritten <> SizeOf(wavHeader) Then
Print "WaveFileClose: Failed to write all data to file"
Return False
End If
Return True
End Function
Private Function InitNotifications() As HRESULT
Dim As HRESULT hr
' Create a notification event, for when the sound stops playing
hr = g_pDSBCapture->lpVtbl->QueryInterface(g_pDSBCapture, @IID_IDirectSoundNotify, @g_pDSNotify)
DXTRACE_MSG("InitNotifications()g_pDSBCapture->lpVtbl->QueryInterface", hr)
' Setup the notification positions
g_aPosNotify = New DSBPOSITIONNOTIFY[NUM_REC_NOTIFICATIONS]
For i As Integer = 0 To NUM_REC_NOTIFICATIONS - 1
g_aPosNotify[i].dwOffset = (g_dwNotifySize * i) + g_dwNotifySize - 1
g_aPosNotify[i].hEventNotify = g_hNotificationEvent
Next
' Tell DirectSound when to notify us. the notification will come in the from
' of signaled events that are handled in WinMain()
hr = g_pDSNotify->lpVtbl->SetNotificationPositions(g_pDSNotify, NUM_REC_NOTIFICATIONS, g_aPosNotify)
DXTRACE_MSG("InitNotifications()g_pDSNotify->lpVtbl->SetNotificationPositions", hr)
Return hr
End Function
Private Function CreateCaptureBuffer(pwfxInput As WAVEFORMATEX Ptr) As HRESULT
Dim As HRESULT hr
Dim As DSCBUFFERDESC dscbd
'Set the notification size
g_dwNotifySize = Max(1024, pwfxInput->nAvgBytesPerSec / 8)
g_dwNotifySize -= g_dwNotifySize Mod pwfxInput->nBlockAlign
DXTRACE_MSG("CreateCaptureBuffer()g_dwNotifySize", g_dwNotifySize)
'Set the buffer sizes
g_dwCaptureBufferSize = g_dwNotifySize * NUM_REC_NOTIFICATIONS
DXTRACE_MSG("CreateCaptureBuffer()g_dwCaptureBufferSize", g_dwCaptureBufferSize)
'Create the capture buffer
dscbd.dwSize = SizeOf(DSCBUFFERDESC)
dscbd.dwBufferBytes = g_dwCaptureBufferSize
dscbd.lpwfxFormat = pwfxInput ' Set the format during creatation
hr = g_pDSCapture->lpVtbl->CreateCaptureBuffer(g_pDSCapture, @dscbd, @g_pDSBCapture, NULL)
DXTRACE_MSG("CreateCaptureBuffer()g_pDSCapture->lpVtbl->CreateCaptureBuffer", hr)
g_dwNextCaptureOffset = 0
Return hr
End Function
Private Function RecordCapturedData(WaveFile As ZString Ptr) As DWORD
Dim As HRESULT hr
Dim As VOID Ptr pbCaptureData = NULL
Dim As DWORD dwCaptureLength
Dim As VOID Ptr pbCaptureData2 = NULL
Dim As DWORD dwCaptureLength2
Dim As VOID Ptr pbPlayData = NULL
Dim As UINT dwDataWrote
Dim As DWORD dwReadPos
Dim As DWORD dwCapturePos
Dim As Long lLockSize
If (NULL = g_pDSBCapture) Then DXTRACE_MSG("RecordCapturedData()g_pDSBCapture", S_FALSE)
hr = g_pDSBCapture->lpVtbl->GetCurrentPosition(g_pDSBCapture, @dwCapturePos, @dwReadPos)
DXTRACE_MSG("RecordCapturedData()g_pDSBCapture->lpVtbl->GetCurrentPosition", hr)
DXTRACE_MSG("RecordCapturedData()dwCapturePos", dwCapturePos)
DXTRACE_MSG("RecordCapturedData()dwReadPos", dwReadPos)
lLockSize = dwReadPos - g_dwNextCaptureOffset
If (lLockSize < 0) Then lLockSize += g_dwCaptureBufferSize
' Block align lock size so that we are always write on a boundary
lLockSize -= (lLockSize Mod g_dwNotifySize)
If (lLockSize = 0) Then DXTRACE_MSG("RecordCapturedData()lLockSize = 0", S_FALSE)
' Lock the capture buffer down
hr = g_pDSBCapture->lpVtbl->Lock(g_pDSBCapture, g_dwNextCaptureOffset, lLockSize, @pbCaptureData, @dwCaptureLength, @pbCaptureData2, @dwCaptureLength2, 0L)
DXTRACE_MSG("RecordCapturedData()g_pDSBCapture->lpVtbl->Lock", hr)
' Write the data into the wav file
WaveFileWriteData(WaveFile, pbCaptureData, dwCaptureLength)
' Move the capture offset along
g_dwNextCaptureOffset += dwCaptureLength
g_dwNextCaptureOffset = (g_dwNextCaptureOffset Mod g_dwCaptureBufferSize) ' Circular buffer
If (pbCaptureData2 <> NULL) Then
' Write the data into the wav file
WaveFileWriteData(WaveFile, pbCaptureData2, dwCaptureLength2)
' Move the capture offset along
g_dwNextCaptureOffset += dwCaptureLength2
g_dwNextCaptureOffset = (g_dwNextCaptureOffset Mod g_dwCaptureBufferSize) ' Circular buffer
End If
' Unlock the capture buffer
hr = g_pDSBCapture->lpVtbl->Unlock(g_pDSBCapture, pbCaptureData, dwCaptureLength, pbCaptureData2, dwCaptureLength2)
DXTRACE_MSG("RecordCapturedData()g_pDSBCapture->lpVtbl->Unlock", hr)
Return dwCaptureLength
End Function
Private Function RecordStart() As HRESULT
Dim As HRESULT hr
hr = g_pDSBCapture->lpVtbl->Start(g_pDSBCapture, DSCBSTART_LOOPING)
DXTRACE_MSG("RecordStart()g_pDSBCapture->lpVtbl->Start", hr)
Return hr
End Function
Private Function RecordStop() As HRESULT
Dim As HRESULT hr
' Stop the capture and read any data that
' was not caught by a notification
If (NULL = g_pDSBCapture) Then Return S_OK
' Stop the buffer, and read any data that was not
' caught by a notification
hr = g_pDSBCapture->lpVtbl->Stop(g_pDSBCapture)
DXTRACE_MSG("RecordStop()g_pDSBCapture->lpVtbl->Stop", hr)
Return hr
End Function
Private Function InitDirectSound(hDlg As HWND, pDeviceGuid As GUID Ptr) As HRESULT
Dim As HRESULT hr
FreeDirectSound()
g_dwCaptureBufferSize = 0
g_dwNotifySize = 0
' Create IDirectSoundCapture Using the preferred capture device
hr = DirectSoundCaptureCreate(pDeviceGuid, @g_pDSCapture, NULL)
DXTRACE_MSG("InitDirectSound()DirectSoundCaptureCreate", hr)
Return hr
End Function
Private Function FreeDirectSound() As HRESULT
' Release DirectSound interfaces
SAFE_RELEASE(g_pDSNotify)
SAFE_RELEASE(g_pDSBCapture)
SAFE_RELEASE(g_pDSCapture)
SAFE_DELETE_ARRAY(g_aPosNotify)
g_dwCaptureBufferSize = 0
g_dwNotifySize = 0
Return S_OK
End Function