AudioEngine-linux.cpp
11.1 KB
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
/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
/**
* @author cesarpachon
*/
#include <cstring>
#include <cstdint>
#include "audio/linux/AudioEngine-linux.h"
#include "base/CCDirector.h"
#include "base/CCScheduler.h"
#include "platform/CCFileUtils.h"
using namespace cocos2d;
AudioEngineImpl * g_AudioEngineImpl = nullptr;
void ERRCHECKWITHEXIT(FMOD_RESULT result)
{
if (result != FMOD_OK) {
printf("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
}
}
bool ERRCHECK(FMOD_RESULT result)
{
if (result != FMOD_OK) {
printf("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
return true;
}
return false;
}
FMOD_RESULT F_CALLBACK channelCallback(FMOD_CHANNELCONTROL *channelcontrol,
FMOD_CHANNELCONTROL_TYPE controltype,
FMOD_CHANNELCONTROL_CALLBACK_TYPE callbacktype,
void *commandData1, void *commandData2)
{
if (controltype == FMOD_CHANNELCONTROL_CHANNEL && callbacktype == FMOD_CHANNELCONTROL_CALLBACK_END) {
g_AudioEngineImpl->onSoundFinished((FMOD::Channel *)channelcontrol);
}
return FMOD_OK;
}
AudioEngineImpl::AudioEngineImpl()
{
}
AudioEngineImpl::~AudioEngineImpl()
{
FMOD_RESULT result;
result = pSystem->release();
ERRCHECKWITHEXIT(result);
}
bool AudioEngineImpl::init()
{
FMOD_RESULT result;
/*
Create a System object and initialize.
*/
result = FMOD::System_Create(&pSystem);
ERRCHECKWITHEXIT(result);
result = pSystem->setOutput(FMOD_OUTPUTTYPE_AUTODETECT);
ERRCHECKWITHEXIT(result);
result = pSystem->init(32, FMOD_INIT_NORMAL, 0);
ERRCHECKWITHEXIT(result);
mapChannelInfo.clear();
mapSound.clear();
auto scheduler = cocos2d::Director::getInstance()->getScheduler();
scheduler->schedule(CC_SCHEDULE_SELECTOR(AudioEngineImpl::update), this, 0.05f, false);
g_AudioEngineImpl = this;
return true;
}
int AudioEngineImpl::play2d(const std::string &fileFullPath, bool loop, float volume)
{
int id = preload(fileFullPath, nullptr);
if (id >= 0) {
mapChannelInfo[id].loop=loop;
// channel is null here. Don't dereference it. It's only set in resume(id).
//mapChannelInfo[id].channel->setPaused(true);
mapChannelInfo[id].volume = volume;
AudioEngine::_audioIDInfoMap[id].state = AudioEngine::AudioState::PAUSED;
resume(id);
}
return id;
}
void AudioEngineImpl::setVolume(int audioID, float volume)
{
try {
mapChannelInfo[audioID].channel->setVolume(volume);
}
catch (const std::out_of_range& oor) {
printf("AudioEngineImpl::setVolume: invalid audioID: %d\n", audioID);
}
}
void AudioEngineImpl::setLoop(int audioID, bool loop)
{
try {
mapChannelInfo[audioID].channel->setLoopCount(loop ? -1 : 0);
}
catch (const std::out_of_range& oor) {
printf("AudioEngineImpl::setLoop: invalid audioID: %d\n", audioID);
}
}
bool AudioEngineImpl::pause(int audioID)
{
try {
mapChannelInfo[audioID].channel->setPaused(true);
AudioEngine::_audioIDInfoMap[audioID].state = AudioEngine::AudioState::PAUSED;
return true;
}
catch (const std::out_of_range& oor) {
printf("AudioEngineImpl::pause: invalid audioID: %d\n", audioID);
return false;
}
}
bool AudioEngineImpl::resume(int audioID)
{
try {
if (!mapChannelInfo[audioID].channel) {
FMOD::Channel *channel = nullptr;
FMOD::ChannelGroup *channelgroup = nullptr;
//starts the sound in pause mode, use the channel to unpause
FMOD_RESULT result = pSystem->playSound(mapChannelInfo[audioID].sound, channelgroup, true, &channel);
if (ERRCHECK(result)) {
return false;
}
channel->setMode(mapChannelInfo[audioID].loop ? FMOD_LOOP_NORMAL : FMOD_LOOP_OFF);
channel->setLoopCount(mapChannelInfo[audioID].loop ? -1 : 0);
channel->setVolume(mapChannelInfo[audioID].volume);
channel->setUserData(reinterpret_cast<void *>(static_cast<std::intptr_t>(mapChannelInfo[audioID].id)));
mapChannelInfo[audioID].channel = channel;
}
mapChannelInfo[audioID].channel->setPaused(false);
AudioEngine::_audioIDInfoMap[audioID].state = AudioEngine::AudioState::PLAYING;
return true;
}
catch (const std::out_of_range& oor) {
printf("AudioEngineImpl::resume: invalid audioID: %d\n", audioID);
return false;
}
}
bool AudioEngineImpl::stop(int audioID)
{
try {
mapChannelInfo[audioID].channel->stop();
mapChannelInfo[audioID].channel = nullptr;
return true;
}
catch (const std::out_of_range& oor) {
printf("AudioEngineImpl::stop: invalid audioID: %d\n", audioID);
return false;
}
}
void AudioEngineImpl::stopAll()
{
for (auto& it : mapChannelInfo) {
ChannelInfo & audioRef = it.second;
audioRef.channel->stop();
audioRef.channel = nullptr;
}
}
float AudioEngineImpl::getDuration(int audioID)
{
try {
FMOD::Sound * sound = mapChannelInfo[audioID].sound;
unsigned int length;
FMOD_RESULT result = sound->getLength(&length, FMOD_TIMEUNIT_MS);
ERRCHECK(result);
float duration = (float)length / 1000.0f;
return duration;
}
catch (const std::out_of_range& oor) {
printf("AudioEngineImpl::getDuration: invalid audioID: %d\n", audioID);
return AudioEngine::TIME_UNKNOWN;
}
}
float AudioEngineImpl::getCurrentTime(int audioID)
{
try {
unsigned int position;
FMOD_RESULT result = mapChannelInfo[audioID].channel->getPosition(&position, FMOD_TIMEUNIT_MS);
ERRCHECK(result);
float currenttime = position /1000.0f;
return currenttime;
}
catch (const std::out_of_range& oor) {
printf("AudioEngineImpl::getCurrentTime: invalid audioID: %d\n", audioID);
return AudioEngine::TIME_UNKNOWN;
}
}
bool AudioEngineImpl::setCurrentTime(int audioID, float time)
{
bool ret = false;
try {
unsigned int position = (unsigned int)(time * 1000.0f);
FMOD_RESULT result = mapChannelInfo[audioID].channel->setPosition(position, FMOD_TIMEUNIT_MS);
ret = !ERRCHECK(result);
}
catch (const std::out_of_range& oor) {
printf("AudioEngineImpl::setCurrentTime: invalid audioID: %d\n", audioID);
}
return ret;
}
void AudioEngineImpl::setFinishCallback(int audioID, const std::function<void (int, const std::string &)> &callback)
{
try {
FMOD::Channel * channel = mapChannelInfo[audioID].channel;
mapChannelInfo[audioID].callback = callback;
FMOD_RESULT result = channel->setCallback(channelCallback);
ERRCHECK(result);
}
catch (const std::out_of_range& oor) {
printf("AudioEngineImpl::setFinishCallback: invalid audioID: %d\n", audioID);
}
}
void AudioEngineImpl::onSoundFinished(FMOD::Channel * channel)
{
int id = 0;
try {
void * data;
channel->getUserData(&data);
id = static_cast<int>(reinterpret_cast<std::intptr_t>(data));
if (mapChannelInfo[id].callback) {
mapChannelInfo[id].callback(id, mapChannelInfo[id].path);
}
mapChannelInfo[id].channel = nullptr;
}
catch (const std::out_of_range& oor) {
printf("AudioEngineImpl::onSoundFinished: invalid audioID: %d\n", id);
}
}
void AudioEngineImpl::uncache(const std::string& path)
{
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(path);
std::map<std::string, FMOD::Sound *>::const_iterator it = mapSound.find(fullPath);
if (it!=mapSound.end()) {
FMOD::Sound * sound = it->second;
if (sound) {
sound->release();
}
mapSound.erase(it);
}
mapId.erase(path);
}
void AudioEngineImpl::uncacheAll()
{
for (const auto& it : mapSound) {
auto sound = it.second;
if (sound) {
sound->release();
}
}
mapSound.clear();
mapId.clear();
}
int AudioEngineImpl::preload(const std::string& filePath, std::function<void(bool isSuccess)> callback)
{
FMOD::Sound * sound = findSound(filePath);
if (!sound) {
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filePath);
FMOD_RESULT result = pSystem->createSound(fullPath.c_str(), FMOD_LOOP_OFF, 0, &sound);
if (ERRCHECK(result)) {
printf("sound effect in %s could not be preload\n", filePath.c_str());
if (callback) {
callback(false);
}
return -1;
}
mapSound[fullPath] = sound;
}
int id = static_cast<int>(mapChannelInfo.size()) + 1;
// std::map::insert returns std::pair<iter, bool>
auto channelInfoIter = mapId.insert({filePath, id});
id = channelInfoIter.first->second;
auto& chanelInfo = mapChannelInfo[id];
chanelInfo.sound = sound;
chanelInfo.id = id;
chanelInfo.channel = nullptr;
chanelInfo.callback = nullptr;
chanelInfo.path = filePath;
//we are going to use UserData to store pointer to Channel when playing
chanelInfo.sound->setUserData(reinterpret_cast<void *>(static_cast<std::intptr_t>(id)));
if (callback) {
callback(true);
}
return id;
}
void AudioEngineImpl::update(float dt)
{
pSystem->update();
}
FMOD::Sound * AudioEngineImpl::findSound(const std::string &path)
{
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(path);
std::map<std::string, FMOD::Sound *>::const_iterator it = mapSound.find(fullPath);
return (it != mapSound.end()) ? (it->second) : nullptr;
}
FMOD::Channel * AudioEngineImpl::getChannel(FMOD::Sound *sound)
{
void * data;
sound->getUserData(&data);
int id = static_cast<int>(reinterpret_cast<std::intptr_t>(data));
return mapChannelInfo[id].channel;
}