-
Notifications
You must be signed in to change notification settings - Fork 5
/
audio.cpp
287 lines (224 loc) · 8.52 KB
/
audio.cpp
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
/*
===========================================================================
Doom 3 GPL Source Code
Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
Copyright (C) 2012 Havlena Petr <havlenapetr@gmail.com>
This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
Doom 3 Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#include <idlib/precompiled.h>
#include <sound/snd_local.h>
#include "openal_stub.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
#define CHECK_INIT(ok) \
if(!ok) { \
Sys_Printf("Can't init android audio driver[%i]", __LINE__); \
return false; \
}
class idAudioHardwareAndroid : public idAudioHardware {
public:
idAudioHardwareAndroid();
virtual ~idAudioHardwareAndroid();
virtual bool Initialize(void);
virtual bool Lock(void **pDSLockedBuffer, ulong *dwDSLockedBufferSize) {
return false;
}
virtual bool Unlock(void *pDSLockedBuffer, dword dwDSLockedBufferSize) {
return false;
}
virtual bool GetCurrentPosition(ulong *pdwCurrentWriteCursor) {
return false;
}
virtual bool Flush();
virtual void Write(bool flushing);
virtual int GetNumberOfSpeakers() {
return 2;
}
virtual int GetMixBufferSize() {
return MIXBUFFER_SAMPLES * mPcm.numChannels * GetNumberOfSpeakers();
}
virtual short* GetMixBuffer() {
if(!mInternalBuffer) {
mInternalBuffer = malloc(GetMixBufferSize());
}
return (short *) mInternalBuffer;
}
private:
bool WaitForQueue(unsigned int threshold);
static int GetSLSamplingRate(int rate);
static const int kQueueWaitTimeUs = 10000;
// engine interfaces
SLObjectItf mEngine;
SLEngineItf mIEngine;
// output mix interface
SLObjectItf mOutput;
SLDataLocator_OutputMix mOutputMix;
// buffer queue player interfaces
SLObjectItf mPlayer;
SLPlayItf mIPlayer;
SLDataFormat_PCM mPcm;
SLBufferQueueItf mBufferQueue;
SLDataLocator_BufferQueue mBufferQueueLoc;
void* mInternalBuffer;
};
idAudioHardwareAndroid::idAudioHardwareAndroid()
: mEngine(NULL),
mIEngine(NULL),
mOutput(NULL),
mPlayer(NULL),
mIPlayer(NULL),
mBufferQueue(NULL),
mInternalBuffer(NULL) {
}
idAudioHardwareAndroid::~idAudioHardwareAndroid() {
if(mIPlayer) {
(*mIPlayer)->SetPlayState(mIPlayer, SL_PLAYSTATE_STOPPED);
}
if (mPlayer) {
(*mPlayer)->Destroy(mPlayer);
mPlayer = NULL;
mIPlayer = NULL;
mBufferQueue = NULL;
}
if (mOutput) {
(*mOutput)->Destroy(mOutput);
mOutput = NULL;
}
if (mEngine) {
(*mEngine)->Destroy(mEngine);
mEngine = NULL;
mIEngine = NULL;
}
if(mInternalBuffer) {
free(mInternalBuffer);
mInternalBuffer = NULL;
}
}
/* static */
int idAudioHardwareAndroid::GetSLSamplingRate(int rate) {
switch (rate) {
case 44100:
return SL_SAMPLINGRATE_44_1;
}
return -1;
}
bool idAudioHardwareAndroid::Flush() {
return WaitForQueue(0);
}
bool idAudioHardwareAndroid::WaitForQueue(unsigned int threshold) {
assert(mBufferQueue);
// Wait until the PCM data is done playing
SLBufferQueueState state;
do {
if ((*mBufferQueue)->GetState(mBufferQueue, &state) != SL_RESULT_SUCCESS) {
Sys_Printf("Can't obtain audio buffer queue state!");
return false;
}
usleep(kQueueWaitTimeUs);
} while (state.count > threshold);
return true;
}
void idAudioHardwareAndroid::Write(bool flushing) {
assert(mBufferQueue);
WaitForQueue(mBufferQueueLoc.numBuffers - 1);
// enqueue another buffer
SLresult result = (*mBufferQueue)->Enqueue(mBufferQueue,
GetMixBuffer(), GetMixBufferSize());
if (flushing) {
Flush();
}
}
bool idAudioHardwareAndroid::Initialize() {
// create engine
SLresult result = slCreateEngine(&mEngine, 0, NULL, 0, NULL, NULL);
CHECK_INIT(SL_RESULT_SUCCESS == result);
// realize the engine
result = (*mEngine)->Realize(mEngine, SL_BOOLEAN_FALSE);
CHECK_INIT(SL_RESULT_SUCCESS == result);
// get the engine interface, which is needed in order to create other objects
result = (*mEngine)->GetInterface(mEngine, SL_IID_ENGINE, &mIEngine);
CHECK_INIT(SL_RESULT_SUCCESS == result);
// create output mix
const SLInterfaceID idsOutput[1] = {SL_IID_VOLUME};
const SLboolean reqOutput[1] = {SL_BOOLEAN_TRUE};
result = (*mIEngine)->CreateOutputMix(mIEngine, &mOutput, 0, idsOutput, reqOutput);
CHECK_INIT(SL_RESULT_SUCCESS == result);
// realize the output mix
result = (*mOutput)->Realize(mOutput, SL_BOOLEAN_FALSE);
CHECK_INIT(SL_RESULT_SUCCESS == result);
/* Setup the data source structure for the buffer queue */
mBufferQueueLoc.locatorType = SL_DATALOCATOR_BUFFERQUEUE;
mBufferQueueLoc.numBuffers = 4; /* number of buffers in our buffer queue */
/* Setup the format of the content in the buffer queue */
mPcm.formatType = SL_DATAFORMAT_PCM;
mPcm.numChannels = idSoundSystemLocal::s_numberOfSpeakers.GetInteger();
mPcm.samplesPerSec = GetSLSamplingRate(PRIMARYFREQ);
mPcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
mPcm.containerSize = 16;
mPcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
mPcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
/* Setup the data sink structure */
mOutputMix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
mOutputMix.outputMix = mOutput;
SLDataSource audioSource;
audioSource.pFormat = (void *)&mPcm;
audioSource.pLocator = (void *)&mBufferQueueLoc;
SLDataSink audioSink;
audioSink.pLocator = (void *)&mOutputMix;
audioSink.pFormat = NULL;
/* Set arrays required[] and iidArray[] for SEEK interface
(PlayItf is implicit) */
const SLInterfaceID iidPlayer[1] = {SL_IID_BUFFERQUEUE};
const SLboolean reqPlayer[1] = {SL_BOOLEAN_TRUE};
/* Create the music player */
result = (*mIEngine)->CreateAudioPlayer(mIEngine, &mPlayer,
&audioSource, &audioSink, 1, iidPlayer, reqPlayer);
CHECK_INIT(SL_RESULT_SUCCESS == result);
// realize the player
result = (*mPlayer)->Realize(mPlayer, SL_BOOLEAN_FALSE);
CHECK_INIT(SL_RESULT_SUCCESS == result);
// get the play interface
result = (*mPlayer)->GetInterface(mPlayer, SL_IID_PLAY, &mIPlayer);
CHECK_INIT(SL_RESULT_SUCCESS == result);
// get the buffer queue interface
result = (*mPlayer)->GetInterface(mPlayer, SL_IID_BUFFERQUEUE, &mBufferQueue);
CHECK_INIT(SL_RESULT_SUCCESS == result);
// set the player's state to playing
result = (*mIPlayer)->SetPlayState(mIPlayer, SL_PLAYSTATE_PLAYING);
CHECK_INIT(SL_RESULT_SUCCESS == result);
return true;
}
/*
===============
idAudioHardware::~idAudioHardware
===============
*/
idAudioHardware::~idAudioHardware() {
}
/* static */
idAudioHardware* idAudioHardware::Alloc() {
return new idAudioHardwareAndroid();
}
bool Sys_LoadOpenAL() {
return false;
}
void Sys_FreeOpenAL() {
// nothing here
}