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
|
//========= Copyright 1996-2009, Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
// This module implements the voice record and compression functions
//#include "audio_pch.h"
//#include "voice.h"
#include "tier0/platform.h"
#include "ivoicerecord.h"
#include <assert.h>
#ifndef POSIX
class VoiceRecord_OpenAL : public IVoiceRecord
{
public:
VoiceRecord_OpenAL() {}
virtual ~VoiceRecord_OpenAL() {}
virtual void Release() {}
virtual bool RecordStart() { return true; }
virtual void RecordStop() {}
virtual bool Init(int sampleRate) { return true; }
virtual void Idle() {}
virtual int GetRecordedData(short *pOut, int nSamplesWanted ) { return 0; }
};
IVoiceRecord* CreateVoiceRecord_DSound(int sampleRate) { return new VoiceRecord_OpenAL; }
#else
#define min(a,b) (((a) < (b)) ? (a) : (b))
#ifdef OSX
#include <Carbon/Carbon.h>
#include <OpenAL/al.h>
#else
#include <AL/al.h>
#endif
#include "openal/alc.h"
// ------------------------------------------------------------------------------
// VoiceRecord_OpenAL
// ------------------------------------------------------------------------------
class VoiceRecord_OpenAL : public IVoiceRecord
{
protected:
virtual ~VoiceRecord_OpenAL();
// IVoiceRecord.
public:
VoiceRecord_OpenAL();
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:
bool InitalizeInterfaces(); // Initialize the openal capture buffers and other interfaces
void ReleaseInterfaces(); // Release openal buffers and other interfaces
void ClearInterfaces(); // Clear members.
private:
ALCdevice *m_Device;
int m_nSampleRate;
};
VoiceRecord_OpenAL::VoiceRecord_OpenAL() :
m_nSampleRate( 0 ), m_Device( NULL )
{
ClearInterfaces();
}
VoiceRecord_OpenAL::~VoiceRecord_OpenAL()
{
ReleaseInterfaces();
}
void VoiceRecord_OpenAL::Release()
{
delete this;
}
bool VoiceRecord_OpenAL::RecordStart()
{
// Re-initialize the capture buffer if neccesary (should always be)
if ( !m_Device )
{
InitalizeInterfaces();
}
if ( !m_Device )
return false;
alcGetError(m_Device);
alcCaptureStart(m_Device);
const ALenum error = alcGetError(m_Device);
return error == AL_NO_ERROR;
}
void VoiceRecord_OpenAL::RecordStop()
{
// Stop capturing.
if ( m_Device )
{
alcCaptureStop( m_Device );
}
// Release the capture buffer interface and any other resources that are no
// longer needed
ReleaseInterfaces();
}
bool VoiceRecord_OpenAL::InitalizeInterfaces()
{
m_Device = alcCaptureOpenDevice( NULL, m_nSampleRate, AL_FORMAT_MONO16, m_nSampleRate * 10 * 2);
const ALenum error = alcGetError(m_Device);
const bool result = error == AL_NO_ERROR;
return m_Device != NULL && result;
}
bool VoiceRecord_OpenAL::Init(int sampleRate)
{
m_nSampleRate = sampleRate;
ReleaseInterfaces();
return true;
}
void VoiceRecord_OpenAL::ReleaseInterfaces()
{
alcCaptureCloseDevice(m_Device);
ClearInterfaces();
}
void VoiceRecord_OpenAL::ClearInterfaces()
{
m_Device = NULL;
}
void VoiceRecord_OpenAL::Idle()
{
}
int VoiceRecord_OpenAL::GetRecordedData(short *pOut, int nSamples )
{
int frameCount = 0;
alcGetIntegerv( m_Device,ALC_CAPTURE_SAMPLES,1,&frameCount );
if ( frameCount > 0 )
{
frameCount = min( nSamples, frameCount );
alcCaptureSamples( m_Device, pOut, frameCount );
if ( alcGetError(m_Device) != ALC_NO_ERROR )
{
return 0;
}
return frameCount;
}
return 0;
}
IVoiceRecord* CreateVoiceRecord_OpenAL(int sampleRate)
{
VoiceRecord_OpenAL *pRecord = new VoiceRecord_OpenAL;
if ( pRecord && pRecord->Init(sampleRate) )
{
return pRecord;
}
else
{
if ( pRecord )
{
pRecord->Release();
}
return NULL;
}
}
#endif
|