-
-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathAudio.m
463 lines (359 loc) · 13.3 KB
/
Audio.m
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
* 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-5-4
* Update : 2019-1-7
* Author : scott.cgi
*/
#include "Engine/Toolkit/Platform/Platform.h"
//--------------------
#ifdef IS_PLATFORM_IOS
//--------------------
#include <stddef.h>
#include <Foundation/Foundation.h>
#include <AudioToolbox/AudioToolbox.h>
#include <OpenAL/OpenAL.h>
#include "Engine/Audio/Platform/Audio.h"
#include "Engine/Toolkit/Platform/Log.h"
#include "Engine/Toolkit/Utils/ArrayStrMap.h"
#include "Engine/Toolkit/Utils/ArrayStrSet.h"
typedef struct
{
ALsizei size;
ALenum format;
ALsizei sampleRate;
void* data;
}
AudioData;
static ArrayStrMap(filePath, AudioData*) audioDataMap[1] = AArrayStrMap_Init(AudioData*, 20);
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 ArrayStrSet(filePath) filePathSet[1] = ArrayStrSet_Init (filePath, 20);
#define CheckAudioDataError(tag) \
ALog_A \
( \
error == noErr, \
"AAudio GetAudioData " tag " failed, " \
"OSStatus error = %x, file path = %s", \
(int) error, \
relativeFilePath \
)
#define CheckAudioError(tag, filePath) \
ALog_A \
( \
(error = alGetError()) == AL_NO_ERROR,\
"AAudio " tag " failed, " \
"alGetError = %x, file path = %s", \
error, \
filePath \
)
/**
* The return data will cached in AudioDataMap.
*/
static inline AudioData* GetAudioData(const char* relativeFilePath)
{
AudioData* audioData = NULL;
int index = AArrayStrMap->GetIndex(audioDataMap, relativeFilePath);
if (index < 0)
{
AudioStreamBasicDescription fileFormat;
AudioStreamBasicDescription outputFormat;
SInt64 fileLengthInFrames = 0;
UInt32 propertySize = sizeof(fileFormat);
ExtAudioFileRef audioFileRef = NULL;
NSString* path = [[NSBundle mainBundle] pathForResource:
[NSString stringWithUTF8String:relativeFilePath] ofType:nil];
CFURLRef fileUrl = CFURLCreateWithString(kCFAllocatorDefault, (CFStringRef) path, NULL);
OSStatus error = ExtAudioFileOpenURL(fileUrl, &audioFileRef);
CheckAudioDataError("ExtAudioFileOpenURL");
CFRelease(fileUrl);
// get the audio data format
error = ExtAudioFileGetProperty(audioFileRef, kExtAudioFileProperty_FileDataFormat, &propertySize, &fileFormat);
CheckAudioDataError("ExtAudioFileGetProperty kExtAudioFileProperty_FileDataFormat");
if (fileFormat.mChannelsPerFrame > 2)
{
ALog_E
(
"AAudio GetAudioData unsupported format,"
"channel count = %u is greater than stereo, relativeFilePath = %s",
(unsigned int) fileFormat.mChannelsPerFrame,
relativeFilePath
);
}
// set the client format to 16 bit signed integer (native-endian) data
// maintain the channel count and sample rate of the original source format
outputFormat.mSampleRate = fileFormat.mSampleRate;
outputFormat.mChannelsPerFrame = fileFormat.mChannelsPerFrame;
outputFormat.mFormatID = kAudioFormatLinearPCM;
outputFormat.mBytesPerPacket = outputFormat.mChannelsPerFrame * 2;
outputFormat.mFramesPerPacket = 1;
outputFormat.mBytesPerFrame = outputFormat.mChannelsPerFrame * 2;
outputFormat.mBitsPerChannel = 16;
outputFormat.mFormatFlags = kAudioFormatFlagsNativeEndian |
kAudioFormatFlagIsPacked |
kAudioFormatFlagIsSignedInteger;
// set the desired client (output) data format
error = ExtAudioFileSetProperty
(
audioFileRef,
kExtAudioFileProperty_ClientDataFormat,
sizeof(outputFormat),
&outputFormat
);
CheckAudioDataError("ExtAudioFileSetProperty kExtAudioFileProperty_ClientDataFormat");
// get the total frame count
propertySize = sizeof(fileLengthInFrames);
error = ExtAudioFileGetProperty
(
audioFileRef,
kExtAudioFileProperty_FileLengthFrames,
&propertySize,
&fileLengthInFrames
);
CheckAudioDataError("ExtAudioFileGetProperty kExtAudioFileProperty_FileLengthFrames");
// read all the data into memory
UInt32 framesToRead = (UInt32) fileLengthInFrames;
UInt32 dataSize = framesToRead * outputFormat.mBytesPerFrame;
//----------------------------------------------------------------------------------------------------------------------
audioData = malloc(sizeof(AudioData) + dataSize);
audioData->size = (ALsizei) dataSize;
audioData->format = outputFormat.mChannelsPerFrame > 1 ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16;
audioData->sampleRate = (ALsizei) outputFormat.mSampleRate;
audioData->data = (char*) audioData + sizeof(AudioData);
AudioBufferList dataBuffer;
dataBuffer.mNumberBuffers = 1;
dataBuffer.mBuffers[0].mDataByteSize = dataSize;
dataBuffer.mBuffers[0].mNumberChannels = outputFormat.mChannelsPerFrame;
dataBuffer.mBuffers[0].mData = audioData->data;
// read the data into an AudioBufferList
error = ExtAudioFileRead(audioFileRef, &framesToRead, &dataBuffer);
CheckAudioDataError("ExtAudioFileRead");
// dispose the ExtAudioFileRef, it is no longer needed
ExtAudioFileDispose(audioFileRef);
AArrayStrMap_InsertAt(audioDataMap, relativeFilePath, -index - 1, audioData);
}
else
{
audioData = AArrayStrMap_GetAt(audioDataMap, index, AudioData*);
}
return audioData;
}
#undef CheckAudioDataError
//----------------------------------------------------------------------------------------------------------------------
static ALCdevice* device = NULL;
static ALCcontext* context = NULL;
static alBufferDataStaticProcPtr alBufferDataStaticProc = NULL;
/**
* Defined in Audio.h
*/
struct AudioPlayer
{
ALuint sourceId;
ALuint bufferId;
const char* filePath;
};
static void Update(float deltaSeconds)
{
for (int i = destroyList->size - 1; i > -1; --i)
{
AudioPlayer* player = AArrayList_Get(destroyList, i, AudioPlayer*);
ALint state;
alGetSourcei(player->sourceId, AL_SOURCE_STATE, &state);
if (state == AL_STOPPED)
{
alDeleteSources(1, &player->sourceId);
alDeleteBuffers(1, &player->bufferId);
player->sourceId = 0;
player->bufferId = 0;
player->filePath = NULL;
AArrayList->Remove(destroyList, i);
AArrayList_Add(cacheList, player);
}
}
}
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()
{
// get static buffer data API
alBufferDataStaticProc = alcGetProcAddress(NULL, "alBufferDataStatic");
// create a new OpenAL Device
// pass NULL to specify the system’s default output device
device = alcOpenDevice(NULL);
if (device != NULL)
{
// create a new OpenAL Context
// the new context will render to the OpenAL Device just created
context = alcCreateContext(device, 0);
if (context != NULL)
{
// make the new context the Current OpenAL Context
alcMakeContextCurrent(context);
}
}
else
{
ALog_E("AAudio Init failed, OpenAL cannot open device");
}
// clear any errors
ALenum error;
CheckAudioError("Init", "");
}
static inline void InitPlayer(const char* relativeFilePath, AudioPlayer* player)
{
ALenum error;
AudioData* audioData = GetAudioData(relativeFilePath);
alGenBuffers(1, &player->bufferId);
CheckAudioError("InitPlayer generate buffer", relativeFilePath);
// use the static buffer data API
// the data will not copy in buffer so cannot free data until buffer deleted
alBufferDataStaticProc(player->bufferId, audioData->format, audioData->data, audioData->size, audioData->sampleRate);
CheckAudioError("InitPlayer attach audio data to buffer", relativeFilePath);
alGenSources(1, &player->sourceId);
CheckAudioError("InitPlayer generate source", relativeFilePath);
// turn Looping off
alSourcei(player->sourceId, AL_LOOPING, AL_FALSE);
// set Source Position
alSourcefv(player->sourceId, AL_POSITION, (const ALfloat[3]) {0.0f, 0.0f, 0.0f});
// set source reference distance
alSourcef(player->sourceId, AL_REFERENCE_DISTANCE, 0.0f);
// attach OpenAL buffer to OpenAL Source
alSourcei(player->sourceId, AL_BUFFER, player->bufferId);
CheckAudioError("InitPlayer attach buffer to source", relativeFilePath);
// set player name
player->filePath = AArrayStrSet->Get(filePathSet, relativeFilePath);
}
static void SetLoop(AudioPlayer* player, bool isLoop)
{
ALint isLoopEnabled;
alGetSourcei(player->sourceId, AL_LOOPING, &isLoopEnabled);
if (isLoopEnabled == isLoop)
{
return;
}
alSourcei(player->sourceId, AL_LOOPING, (ALint) isLoop);
// move player from removeList to addList
ArrayList(AudioPlayer*)* addList;
ArrayList(AudioPlayer*)* removeList;
if (isLoop)
{
addList = loopList;
removeList = destroyList;
}
else
{
addList = destroyList;
removeList = loopList;
}
for (int i = 0; i < removeList->size; ++i)
{
if (player == AArrayList_Get(removeList, i, AudioPlayer*))
{
AArrayList->RemoveByLast(removeList, i);
break;
}
}
AArrayList_Add(addList, player);
}
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], audio file path = %s",
volume,
player->filePath
);
alSourcef(player->sourceId, AL_GAIN, volume);
ALenum error;
CheckAudioError("SetVolume", player->filePath);
}
static void Play(AudioPlayer* player)
{
alSourcePlay(player->sourceId);
ALenum error;
CheckAudioError("Play", player->filePath);
}
static void Pause(AudioPlayer* player)
{
alSourcePause(player->sourceId);
ALenum error;
CheckAudioError("Pause", player->filePath);
}
static void Stop(AudioPlayer* player)
{
// after alSourceStop call alDeleteSources will cause OpenAL leak OALSource:AddPlaybackMessage
alSourceStop(player->sourceId);
ALenum error;
CheckAudioError("Stop", player->filePath);
// put player into destroyList
SetLoop(player, false);
}
static bool IsPlaying(AudioPlayer* player)
{
ALint state;
alGetSourcei(player->sourceId, AL_SOURCE_STATE, &state);
return state == AL_PLAYING;
}
static AudioPlayer* GetPlayer(const char* relativeFilePath)
{
AudioPlayer* player = AArrayList_Pop(cacheList, AudioPlayer*);
if (player == NULL)
{
player = malloc(sizeof(AudioPlayer));
}
InitPlayer(relativeFilePath, player);
AArrayList_Add(destroyList, player);
return player;
}
#undef CheckAudioError
static void Release()
{
// release context
alcDestroyContext(context);
// close device
alcCloseDevice(device);
AArrayStrMap->Release(audioDataMap);
AArrayList ->Release(cacheList);
AArrayList ->Release(destroyList);
AArrayList ->Release(loopList);
AArrayStrSet->Release(filePathSet);
}
struct AAudio AAudio[1] =
{{
Init,
Release,
Update,
SetLoopPause,
SetLoopResume,
GetPlayer,
SetVolume,
SetLoop,
Play,
Pause,
Stop,
IsPlaying,
}};
//-----------------------
#endif // IS_PLATFORM_IOS
//-----------------------