-
-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathAudio.c
423 lines (328 loc) · 11.7 KB
/
Audio.c
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
* Copyright (c) scott.cgi All Rights Reserved.
*
* This source code belongs to project Mojoc, which is a pure C Game Engine hosted on GitHub.
* The Mojoc Game Engine is licensed under the MIT License, and will continue to be iterated with coding passion.
*
* License : https://github.com/scottcgi/Mojoc/blob/master/LICENSE
* GitHub : https://github.com/scottcgi/Mojoc
* CodeStyle: https://github.com/scottcgi/Mojoc/blob/master/Docs/CodeStyle.md
*
* Since : 2017-2-16
* Update : 2019-1-7
* Author : scott.cgi
*/
#include "Engine/Toolkit/Platform/Platform.h"
//------------------------
#ifdef IS_PLATFORM_ANDROID
//------------------------
#include <stdlib.h>
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
#include "Engine/Toolkit/Utils/ArrayList.h"
#include "Engine/Toolkit/Utils/ArrayStrSet.h"
#include "Engine/Toolkit/Platform/File.h"
#include "Engine/Toolkit/Platform/Log.h"
#include "Engine/Audio/Platform/Audio.h"
// engine interfaces
static SLObjectItf engineObject = NULL;
static SLEngineItf engineEngine = NULL;
// output mix interfaces
static SLObjectItf outputMixObject = NULL;
/**
* Defined in Audio.h
*/
struct AudioPlayer
{
SLObjectItf object;
SLPlayItf play;
SLSeekItf seek;
SLVolumeItf volume;
const char* filePath;
int waitCallbackCount;
};
static ArrayList (AudioPlayer*) cacheList [1] = AArrayList_Init (AudioPlayer*, 20);
static ArrayList (AudioPlayer*) destroyList [1] = AArrayList_Init (AudioPlayer*, 20);
static ArrayList (AudioPlayer*) loopList [1] = AArrayList_Init (AudioPlayer*, 5);
static ArrayList (AudioPlayer*) testErrorList[1] = AArrayList_Init (AudioPlayer*, 5);
static ArrayStrSet(filePath) filePathSet [1] = ArrayStrSet_Init(filePath, 20);
enum
{
AudioPlayer_WaitMax = 120,
AudioPlayer_WaitOver = -1,
};
#define CheckError(tag, filePath) \
ALog_A \
( \
result == SL_RESULT_SUCCESS, \
"Audio " tag " failed, " \
"error = %x, file path = %s", \
result, \
filePath \
)
static void Update(float deltaSeconds)
{
while (destroyList->size > 0)
{
AudioPlayer* player = AArrayList_Pop(destroyList, AudioPlayer*);
// remove from loopList
AAudio->SetLoop(player, false);
(*player->object)->Destroy(player->object);
player->play = NULL;
player->volume = NULL;
player->seek = NULL;
player->object = NULL;
AArrayList_Add(cacheList, player);
}
for (int i = testErrorList->size - 1; i > -1 ; --i)
{
AudioPlayer* player = AArrayList_Get(testErrorList, i, AudioPlayer*);
if (player->waitCallbackCount == AudioPlayer_WaitOver)
{
AArrayList->RemoveByLast(testErrorList, i);
}
else if (player->waitCallbackCount >= AudioPlayer_WaitMax)
{
// player callback not called, maybe E/libOpenSLES: Error after prepare: 1
AArrayList->RemoveByLast(testErrorList, i);
(*player->object)->Destroy(player->object);
AArrayList_Add(cacheList, player);
ALog_E("AAudio player = %s not callback normal", player->filePath);
}
else
{
++player->waitCallbackCount;
}
}
}
static void SetLoopPause()
{
for (int i = 0; i < loopList->size; ++i)
{
AAudio->Pause(AArrayList_Get(loopList, i, AudioPlayer*));
}
}
static void SetLoopResume()
{
for (int i = 0; i < loopList->size; ++i)
{
AAudio->Play(AArrayList_Get(loopList, i, AudioPlayer*));
}
}
static void Init()
{
SLresult result;
// create engine
result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
CheckError("Init slCreateEngine", "");
// realize the engine
result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_TRUE);
CheckError("Init Realize engineObject", "");
// get the engine interface, which is needed in order to create other objects
result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);
CheckError("Init GetInterface", "");
result = (*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject, 0, NULL, NULL);
CheckError("Init CreateOutputMix", "");
// realize the output mix
result = (*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE);
CheckError("Init Realize outputMixObject", "");
}
static SLmillisecond inline GetDuration(AudioPlayer* player)
{
SLresult result;
SLmillisecond msec;
result = (*player->play)->GetDuration(player->play, &msec);
CheckError("GetDuration", "");
return msec;
}
static void PlayerCallback(SLPlayItf caller, void* pContext, SLuint32 event)
{
AudioPlayer* player = (AudioPlayer*) pContext;
switch (event)
{
case SL_PLAYEVENT_HEADATEND:
// play finish
AArrayList_Add(destroyList, player);
(*player->play)->SetPlayState(player->play, SL_PLAYSTATE_PAUSED);
player->waitCallbackCount = AudioPlayer_WaitOver;
break;
case SL_PLAYEVENT_HEADATNEWPOS:
// play normal
player->waitCallbackCount = AudioPlayer_WaitOver;
// remove SL_PLAYEVENT_HEADATNEWPOS for reduce the number of callback
(*player->play)->SetCallbackEventsMask(player->play, SL_PLAYEVENT_HEADATEND);
break;
default:
break;
}
}
static inline void InitPlayer(const char* relativeFilePath, AudioPlayer* player)
{
off_t start;
off_t length;
int fd = AFile->OpenFileDescriptor(relativeFilePath, &start, &length);
// configure audio source
SLDataLocator_AndroidFD locFD = {SL_DATALOCATOR_ANDROIDFD, fd, start, length};
SLDataFormat_MIME formatMME = {SL_DATAFORMAT_MIME, NULL, SL_CONTAINERTYPE_UNSPECIFIED};
SLDataSource audioSrc = {&locFD, &formatMME};
// configure audio sink
SLDataLocator_OutputMix locOutMix = {SL_DATALOCATOR_OUTPUTMIX, outputMixObject};
SLDataSink audioSnk = {&locOutMix, NULL};
// create audio player
SLInterfaceID ids[3] = {SL_IID_SEEK, SL_IID_PLAY, SL_IID_VOLUME};
SLboolean req[3] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
SLresult result = (*engineEngine)->CreateAudioPlayer
(
engineEngine,
&player->object,
&audioSrc,
&audioSnk,
3, ids, req
);
CheckError("CreatePlayer CreateAudioPlayer", relativeFilePath);
// realize the player
result = (*player->object)->Realize(player->object, SL_BOOLEAN_FALSE);
CheckError("CreatePlayer player Realize", relativeFilePath);
// get the player interface
result = (*player->object)->GetInterface(player->object, SL_IID_PLAY, &player->play);
CheckError("CreatePlayer player GetInterface play", relativeFilePath);
// player callback
result = (*player->play)->RegisterCallback(player->play, PlayerCallback, player);
CheckError("CreatePlayer player RegisterCallback", relativeFilePath);
result = (*player->play)->SetCallbackEventsMask(player->play, SL_PLAYEVENT_HEADATEND | SL_PLAYEVENT_HEADATNEWPOS);
CheckError("CreatePlayer player SetCallbackEventsMask", relativeFilePath);
// get the seek interface
result = (*player->object)->GetInterface(player->object, SL_IID_SEEK, &player->seek);
CheckError("CreatePlayer player GetInterface seek", relativeFilePath);
// disable looping
result = (*player->seek)->SetLoop(player->seek, SL_BOOLEAN_FALSE, 0, SL_TIME_UNKNOWN);
CheckError("CreatePlayer SetLoop", relativeFilePath);
// get the volume interface
result = (*player->object)->GetInterface(player->object, SL_IID_VOLUME, &player->volume);
CheckError("CreatePlayer player GetInterface volume", relativeFilePath);
player->waitCallbackCount = 0;
player->filePath = AArrayStrSet->Get(filePathSet, relativeFilePath);
}
static void SetLoop(AudioPlayer* player, bool isLoop)
{
SLboolean isLoopEnabled;
SLmillisecond startPos;
SLmillisecond endPos;
(*player->seek)->GetLoop(player->seek, &isLoopEnabled, &startPos, &endPos);
if (isLoopEnabled == (SLboolean) isLoop)
{
return;
}
SLresult result = (*player->seek)->SetLoop(player->seek, (SLboolean) isLoop, 0, GetDuration(player));
CheckError("SetLoop", player->filePath);
if (isLoop)
{
AArrayList_Add(loopList, player);
}
else
{
for (int i = 0; i < loopList->size; ++i)
{
if (player == AArrayList_Get(loopList, i, AudioPlayer*))
{
AArrayList->RemoveByLast(loopList, i);
break;
}
}
}
}
static void SetVolume(AudioPlayer* player, float volume)
{
ALog_A
(
volume >= 0.0f && volume <= 1.0f,
"AAudio SetVolume volume %f not in [0.0, 1.0], file path = %s",
volume,
player->filePath
);
SLresult result = (*player->volume)->SetVolumeLevel(player->volume, (SLmillibel) ((1.0f - volume) * -5000.0f));
CheckError("SetVolume", player->filePath);
}
static void Play(AudioPlayer* player)
{
// set the player's state
SLresult result = (*player->play)->SetPlayState(player->play, SL_PLAYSTATE_PLAYING);
CheckError("Play", player->filePath);
AArrayList_Add(testErrorList, player);
}
static void Pause(AudioPlayer* player)
{
// set the player's state
SLresult result = (*player->play)->SetPlayState(player->play, SL_PLAYSTATE_PAUSED);
CheckError("Pause", player->filePath);
}
static void Stop(AudioPlayer* player)
{
// set the player's state
SLresult result = (*player->play)->SetPlayState(player->play, SL_PLAYSTATE_STOPPED);
CheckError("Stop", player->filePath);
AArrayList_Add(destroyList, player);
player->waitCallbackCount = AudioPlayer_WaitOver;
}
static bool IsPlaying(AudioPlayer* player)
{
SLuint32 state;
(*player->play)->GetPlayState(player->play, &state);
if (state == SL_PLAYSTATE_PLAYING)
{
return true;
}
else
{
return false;
}
}
static AudioPlayer* GetPlayer(const char* relativeFilePath)
{
AudioPlayer* player = AArrayList_Pop(cacheList, AudioPlayer*);
if (player == NULL)
{
player = malloc(sizeof(AudioPlayer));
}
InitPlayer(relativeFilePath, player);
return player;
}
static void Destroy(ArrayList(AudioPlayer*)* playerList)
{
while (playerList->size > 0)
{
AudioPlayer* player = AArrayList_Pop(playerList, AudioPlayer*);
(*player->object)->Destroy(player->object);
}
}
static void Release()
{
Destroy(destroyList);
Destroy(loopList);
Destroy(testErrorList);
AArrayList ->Release(cacheList);
AArrayList ->Release(destroyList);
AArrayList ->Release(loopList);
AArrayList ->Release(testErrorList);
AArrayStrSet->Release(filePathSet);
(*outputMixObject)->Destroy(outputMixObject);
(*engineObject) ->Destroy(engineObject);
}
struct AAudio AAudio[1] =
{{
Init,
Release,
Update,
SetLoopPause,
SetLoopResume,
GetPlayer,
SetVolume,
SetLoop,
Play,
Pause,
Stop,
IsPlaying,
}};
//---------------------------
#endif // IS_PLATFORM_ANDROID
//---------------------------