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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
//========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
// This module implements the voice record and compression functions
#include "audio_pch.h"
#if !defined( _X360 )
#include "dsound.h"
#endif
#include <assert.h>
#include "voice.h"
#include "tier0/vcrmode.h"
#include "ivoicerecord.h"
#if defined( _X360 )
#include "xbox/xbox_win32stubs.h"
#endif
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
// ------------------------------------------------------------------------------
// Globals.
// ------------------------------------------------------------------------------
typedef HRESULT (WINAPI *DirectSoundCaptureCreateFn)(const GUID FAR *lpGUID, LPDIRECTSOUNDCAPTURE *pCapture, LPUNKNOWN pUnkOuter);
// ------------------------------------------------------------------------------
// Static helpers
// ------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
// VoiceRecord_DSound
// ------------------------------------------------------------------------------
class VoiceRecord_DSound : public IVoiceRecord
{
protected:
virtual ~VoiceRecord_DSound();
// IVoiceRecord.
public:
VoiceRecord_DSound();
virtual void Release();
virtual bool RecordStart();
virtual void RecordStop();
// Initialize. The format of the data we expect from the provider is
// 8-bit signed mono at the specified sample rate.
virtual bool Init(int sampleRate);
virtual void Idle();
// Get the most recent N samples.
virtual int GetRecordedData(short *pOut, int nSamplesWanted);
private:
void Term(); // Delete members.
void Clear(); // Clear members.
void UpdateWrapping();
inline DWORD NumCaptureBufferBytes() {return m_nCaptureBufferBytes;}
private:
HINSTANCE m_hInstDS;
LPDIRECTSOUNDCAPTURE m_pCapture;
LPDIRECTSOUNDCAPTUREBUFFER m_pCaptureBuffer;
// How many bytes our capture buffer has.
DWORD m_nCaptureBufferBytes;
// We need to know when the capture buffer loops, so we install an event and
// update this in the event.
DWORD m_WrapOffset;
HANDLE m_hWrapEvent;
// This is our (unwrapped) position that tells how much data we've given to the app.
DWORD m_LastReadPos;
};
VoiceRecord_DSound::VoiceRecord_DSound()
{
Clear();
}
VoiceRecord_DSound::~VoiceRecord_DSound()
{
Term();
}
void VoiceRecord_DSound::Release()
{
delete this;
}
bool VoiceRecord_DSound::RecordStart()
{
//When we start recording we want to make sure we don't provide any audio
//that occurred before now. So set m_LastReadPos to the current
//read position of the audio device
if (m_pCaptureBuffer == NULL)
{
return false;
}
Idle();
DWORD dwStatus;
HRESULT hr = m_pCaptureBuffer->GetStatus(&dwStatus);
if (FAILED(hr) || !(dwStatus & DSCBSTATUS_CAPTURING))
return false;
DWORD dwReadPos;
hr = m_pCaptureBuffer->GetCurrentPosition(NULL, &dwReadPos);
if (!FAILED(hr))
{
m_LastReadPos = dwReadPos + m_WrapOffset;
}
return true;
}
void VoiceRecord_DSound::RecordStop()
{
}
static bool IsRunningWindows7()
{
if ( IsPC() )
{
OSVERSIONINFOEX osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
if ( GetVersionEx ((OSVERSIONINFO *)&osvi) )
{
if ( osvi.dwMajorVersion > 6 || (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion >= 1) )
return true;
}
}
return false;
}
bool VoiceRecord_DSound::Init(int sampleRate)
{
HRESULT hr;
DSCBUFFERDESC dscDesc;
DirectSoundCaptureCreateFn createFn;
Term();
WAVEFORMATEX recordFormat =
{
WAVE_FORMAT_PCM, // wFormatTag
1, // nChannels
(uint32)sampleRate, // nSamplesPerSec
(uint32)sampleRate*2, // nAvgBytesPerSec
2, // nBlockAlign
16, // wBitsPerSample
sizeof(WAVEFORMATEX) // cbSize
};
// Load the DSound DLL.
m_hInstDS = LoadLibrary("dsound.dll");
if(!m_hInstDS)
goto HandleError;
createFn = (DirectSoundCaptureCreateFn)GetProcAddress(m_hInstDS, "DirectSoundCaptureCreate");
if(!createFn)
goto HandleError;
const GUID FAR *pGuid = &DSDEVID_DefaultVoiceCapture;
if ( IsRunningWindows7() )
{
pGuid = NULL;
}
hr = createFn(pGuid, &m_pCapture, NULL);
if(FAILED(hr))
goto HandleError;
// Create the capture buffer.
memset(&dscDesc, 0, sizeof(dscDesc));
dscDesc.dwSize = sizeof(dscDesc);
dscDesc.dwFlags = 0;
dscDesc.dwBufferBytes = recordFormat.nAvgBytesPerSec;
dscDesc.lpwfxFormat = &recordFormat;
hr = m_pCapture->CreateCaptureBuffer(&dscDesc, &m_pCaptureBuffer, NULL);
if(FAILED(hr))
goto HandleError;
// Figure out how many bytes we got in our capture buffer.
DSCBCAPS caps;
memset(&caps, 0, sizeof(caps));
caps.dwSize = sizeof(caps);
hr = m_pCaptureBuffer->GetCaps(&caps);
if(FAILED(hr))
goto HandleError;
m_nCaptureBufferBytes = caps.dwBufferBytes;
// Set it up so we get notification when the buffer wraps.
m_hWrapEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if(!m_hWrapEvent)
goto HandleError;
DSBPOSITIONNOTIFY dsbNotify;
dsbNotify.dwOffset = dscDesc.dwBufferBytes - 1;
dsbNotify.hEventNotify = m_hWrapEvent;
// Get the IDirectSoundNotify interface.
LPDIRECTSOUNDNOTIFY pNotify;
hr = m_pCaptureBuffer->QueryInterface(IID_IDirectSoundNotify, (void**)&pNotify);
if(FAILED(hr))
goto HandleError;
hr = pNotify->SetNotificationPositions(1, &dsbNotify);
pNotify->Release();
if(FAILED(hr))
goto HandleError;
// Start capturing.
hr = m_pCaptureBuffer->Start(DSCBSTART_LOOPING);
if(FAILED(hr))
return false;
return true;
HandleError:;
Term();
return false;
}
void VoiceRecord_DSound::Term()
{
if(m_pCaptureBuffer)
m_pCaptureBuffer->Release();
if(m_pCapture)
m_pCapture->Release();
if(m_hWrapEvent)
DeleteObject(m_hWrapEvent);
if(m_hInstDS)
{
FreeLibrary(m_hInstDS);
m_hInstDS = NULL;
}
Clear();
}
void VoiceRecord_DSound::Clear()
{
m_pCapture = NULL;
m_pCaptureBuffer = NULL;
m_WrapOffset = 0;
m_LastReadPos = 0;
m_hWrapEvent = NULL;
m_hInstDS = NULL;
}
void VoiceRecord_DSound::Idle()
{
UpdateWrapping();
}
int VoiceRecord_DSound::GetRecordedData( short *pOut, int nSamples )
{
if(!m_pCaptureBuffer)
{
assert(false);
return 0;
}
DWORD dwStatus;
HRESULT hr = m_pCaptureBuffer->GetStatus(&dwStatus);
if(FAILED(hr) || !(dwStatus & DSCBSTATUS_CAPTURING))
return 0;
Idle(); // Update wrapping..
DWORD nBytesWanted = (DWORD)( nSamples << 1 );
DWORD dwReadPos;
hr = m_pCaptureBuffer->GetCurrentPosition( NULL, &dwReadPos);
if(FAILED(hr))
return 0;
dwReadPos += m_WrapOffset;
// Read the range (dwReadPos-nSamplesWanted, dwReadPos), but don't re-read data we've already read.
DWORD readStart = Max( dwReadPos - nBytesWanted, (DWORD)0u );
if ( readStart < m_LastReadPos )
{
readStart = m_LastReadPos;
}
// Lock the buffer.
LPVOID pData[2];
DWORD dataLen[2];
hr = m_pCaptureBuffer->Lock(
readStart % NumCaptureBufferBytes(), // Offset.
dwReadPos - readStart, // Number of bytes to lock.
&pData[0], // Buffer 1.
&dataLen[0], // Buffer 1 length.
&pData[1], // Buffer 2.
&dataLen[1], // Buffer 2 length.
0 // Flags.
);
if(FAILED(hr))
return 0;
// Hopefully we didn't get too much data back!
if((dataLen[0]+dataLen[1]) > nBytesWanted )
{
assert(false);
m_pCaptureBuffer->Unlock(pData[0], dataLen[0], pData[1], dataLen[1]);
return 0;
}
// Copy the data to the output.
memcpy(pOut, pData[0], dataLen[0]);
memcpy(&pOut[dataLen[0]/2], pData[1], dataLen[1]);
m_pCaptureBuffer->Unlock(pData[0], dataLen[0], pData[1], dataLen[1]);
// Last Read Position
m_LastReadPos = dwReadPos;
// Return sample count (not bytes)
return (dataLen[0] + dataLen[1]) >> 1;
}
void VoiceRecord_DSound::UpdateWrapping()
{
if(!m_pCaptureBuffer)
return;
// Has the buffer wrapped?
if ( VCRHook_WaitForSingleObject(m_hWrapEvent, 0) == WAIT_OBJECT_0 )
{
m_WrapOffset += m_nCaptureBufferBytes;
}
}
IVoiceRecord* CreateVoiceRecord_DSound(int sampleRate)
{
VoiceRecord_DSound *pRecord = new VoiceRecord_DSound;
if(pRecord && pRecord->Init(sampleRate))
{
return pRecord;
}
else
{
if(pRecord)
pRecord->Release();
return NULL;
}
}
|