2014-09-03 18:19:21 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2014-09-30 01:07:11 +08:00
|
|
|
|
|
|
|
#include "platform/CCPlatformConfig.h"
|
2014-09-22 15:38:12 +08:00
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC
|
2014-09-30 01:07:11 +08:00
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
#include "AudioEngine-inl.h"
|
|
|
|
|
|
|
|
#import <OpenAL/alc.h>
|
2015-02-11 15:34:08 +08:00
|
|
|
#import <AVFoundation/AVFoundation.h>
|
|
|
|
|
|
|
|
#include "audio/include/AudioEngine.h"
|
2014-09-03 18:19:21 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/CCScheduler.h"
|
|
|
|
#include "base/ccUtils.h"
|
|
|
|
|
|
|
|
using namespace cocos2d;
|
2014-09-16 10:22:25 +08:00
|
|
|
using namespace cocos2d::experimental;
|
2014-09-03 18:19:21 +08:00
|
|
|
|
|
|
|
static ALCdevice *s_ALDevice = nullptr;
|
|
|
|
static ALCcontext *s_ALContext = nullptr;
|
|
|
|
|
|
|
|
namespace cocos2d {
|
2014-09-16 14:57:58 +08:00
|
|
|
namespace experimental {
|
|
|
|
class AudioEngineThreadPool
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
2014-09-16 14:57:58 +08:00
|
|
|
public:
|
|
|
|
AudioEngineThreadPool()
|
|
|
|
: _running(true)
|
|
|
|
, _numThread(6)
|
|
|
|
{
|
|
|
|
_threads.reserve(_numThread);
|
|
|
|
_tasks.reserve(_numThread);
|
|
|
|
|
|
|
|
for (int index = 0; index < _numThread; ++index) {
|
|
|
|
_tasks.push_back(nullptr);
|
|
|
|
_threads.push_back( std::thread( std::bind(&AudioEngineThreadPool::threadFunc,this,index) ) );
|
|
|
|
}
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
2014-09-16 14:57:58 +08:00
|
|
|
|
|
|
|
void addTask(const std::function<void()> &task){
|
|
|
|
_taskMutex.lock();
|
|
|
|
int targetIndex = -1;
|
|
|
|
for (int index = 0; index < _numThread; ++index) {
|
|
|
|
if (_tasks[index] == nullptr) {
|
|
|
|
targetIndex = index;
|
|
|
|
_tasks[index] = task;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (targetIndex == -1) {
|
|
|
|
_tasks.push_back(task);
|
|
|
|
_threads.push_back( std::thread( std::bind(&AudioEngineThreadPool::threadFunc,this,_numThread) ) );
|
|
|
|
|
|
|
|
_numThread++;
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
2014-09-16 14:57:58 +08:00
|
|
|
_taskMutex.unlock();
|
|
|
|
|
|
|
|
_sleepCondition.notify_all();
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
2014-09-16 14:57:58 +08:00
|
|
|
|
|
|
|
void destroy()
|
|
|
|
{
|
|
|
|
_running = false;
|
|
|
|
_sleepCondition.notify_all();
|
2014-09-03 18:19:21 +08:00
|
|
|
|
2014-09-16 14:57:58 +08:00
|
|
|
for (int index = 0; index < _numThread; ++index) {
|
|
|
|
_threads[index].join();
|
|
|
|
}
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
2014-09-16 14:57:58 +08:00
|
|
|
private:
|
|
|
|
bool _running;
|
|
|
|
std::vector<std::thread> _threads;
|
|
|
|
std::vector< std::function<void ()> > _tasks;
|
2014-09-03 18:19:21 +08:00
|
|
|
|
2014-09-16 14:57:58 +08:00
|
|
|
void threadFunc(int index)
|
|
|
|
{
|
|
|
|
while (_running) {
|
|
|
|
std::function<void ()> task = nullptr;
|
|
|
|
_taskMutex.lock();
|
|
|
|
task = _tasks[index];
|
|
|
|
_taskMutex.unlock();
|
|
|
|
|
|
|
|
if (nullptr == task)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lk(_sleepMutex);
|
|
|
|
_sleepCondition.wait(lk);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
task();
|
|
|
|
|
|
|
|
_taskMutex.lock();
|
|
|
|
_tasks[index] = nullptr;
|
|
|
|
_taskMutex.unlock();
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
}
|
2014-09-16 14:57:58 +08:00
|
|
|
|
|
|
|
int _numThread;
|
|
|
|
|
|
|
|
std::mutex _taskMutex;
|
|
|
|
std::mutex _sleepMutex;
|
|
|
|
std::condition_variable _sleepCondition;
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
2015-02-11 15:34:08 +08:00
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
|
|
|
|
@interface AudioEngineSessionHandler : NSObject
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
-(id) init;
|
|
|
|
-(void)handleInterruption:(NSNotification*)notification;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation AudioEngineSessionHandler
|
|
|
|
|
2015-04-04 06:57:47 +08:00
|
|
|
void AudioEngineInterruptionListenerCallback(void* user_data, UInt32 interruption_state)
|
|
|
|
{
|
|
|
|
if (kAudioSessionBeginInterruption == interruption_state)
|
|
|
|
{
|
|
|
|
alcMakeContextCurrent(nullptr);
|
|
|
|
}
|
|
|
|
else if (kAudioSessionEndInterruption == interruption_state)
|
|
|
|
{
|
|
|
|
OSStatus result = AudioSessionSetActive(true);
|
|
|
|
if (result) NSLog(@"Error setting audio session active! %d\n", result);
|
|
|
|
|
|
|
|
alcMakeContextCurrent(s_ALContext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-11 15:34:08 +08:00
|
|
|
-(id) init
|
|
|
|
{
|
2015-05-28 11:49:27 +08:00
|
|
|
if (self = [super init])
|
2015-02-11 15:34:08 +08:00
|
|
|
{
|
2015-04-04 06:57:47 +08:00
|
|
|
if ([[[UIDevice currentDevice] systemVersion] intValue] > 5) {
|
2015-02-11 15:34:08 +08:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleInterruption:) name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]];
|
2015-05-29 21:41:54 +08:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleInterruption:) name:UIApplicationDidBecomeActiveNotification object:[AVAudioSession sharedInstance]];
|
2015-04-04 06:57:47 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
AudioSessionInitialize(NULL, NULL, AudioEngineInterruptionListenerCallback, self);
|
|
|
|
}
|
2015-02-11 15:34:08 +08:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)handleInterruption:(NSNotification*)notification
|
|
|
|
{
|
2015-05-29 21:41:54 +08:00
|
|
|
static bool resumeOnBecomingActive = false;
|
|
|
|
|
2015-02-11 15:34:08 +08:00
|
|
|
if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) {
|
|
|
|
NSInteger reason = [[[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey] integerValue];
|
|
|
|
if (reason == AVAudioSessionInterruptionTypeBegan) {
|
|
|
|
alcMakeContextCurrent(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reason == AVAudioSessionInterruptionTypeEnded) {
|
2015-05-29 21:41:54 +08:00
|
|
|
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
|
|
|
|
NSError *error = nil;
|
|
|
|
[[AVAudioSession sharedInstance] setActive:YES error:&error];
|
|
|
|
alcMakeContextCurrent(s_ALContext);
|
|
|
|
} else {
|
|
|
|
resumeOnBecomingActive = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([notification.name isEqualToString:UIApplicationDidBecomeActiveNotification] && resumeOnBecomingActive) {
|
|
|
|
resumeOnBecomingActive = false;
|
|
|
|
NSError *error = nil;
|
|
|
|
BOOL success = [[AVAudioSession sharedInstance]
|
|
|
|
setCategory: AVAudioSessionCategoryAmbient
|
|
|
|
error: &error];
|
|
|
|
if (!success) {
|
|
|
|
printf("Fail to set audio session.\n");
|
|
|
|
return;
|
2015-02-11 15:34:08 +08:00
|
|
|
}
|
2015-05-29 21:41:54 +08:00
|
|
|
[[AVAudioSession sharedInstance] setActive:YES error:&error];
|
|
|
|
alcMakeContextCurrent(s_ALContext);
|
2015-02-11 15:34:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVAudioSessionInterruptionNotification object:nil];
|
2015-05-29 21:41:54 +08:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
|
2015-02-11 15:34:08 +08:00
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
static id s_AudioEngineSessionHandler = nullptr;
|
|
|
|
#endif
|
|
|
|
|
2014-09-05 14:34:30 +08:00
|
|
|
AudioEngineImpl::AudioEngineImpl()
|
2014-11-26 09:53:52 +08:00
|
|
|
: _threadPool(nullptr)
|
|
|
|
, _lazyInitLoop(true)
|
2014-11-15 05:07:34 +08:00
|
|
|
, _currentAudioID(0)
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
2015-02-11 15:34:08 +08:00
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioEngineImpl::~AudioEngineImpl()
|
|
|
|
{
|
|
|
|
if (s_ALContext) {
|
2014-09-10 17:12:46 +08:00
|
|
|
alDeleteSources(MAX_AUDIOINSTANCES, _alSources);
|
2014-09-03 18:19:21 +08:00
|
|
|
|
|
|
|
_audioCaches.clear();
|
|
|
|
|
2014-09-22 15:38:12 +08:00
|
|
|
alcMakeContextCurrent(nullptr);
|
2014-09-03 18:19:21 +08:00
|
|
|
alcDestroyContext(s_ALContext);
|
|
|
|
}
|
|
|
|
if (s_ALDevice) {
|
|
|
|
alcCloseDevice(s_ALDevice);
|
|
|
|
}
|
|
|
|
if (_threadPool) {
|
2014-09-10 17:12:46 +08:00
|
|
|
_threadPool->destroy();
|
2014-09-03 18:19:21 +08:00
|
|
|
delete _threadPool;
|
|
|
|
}
|
2015-02-11 15:34:08 +08:00
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
|
|
|
|
[s_AudioEngineSessionHandler release];
|
|
|
|
#endif
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioEngineImpl::init()
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
do{
|
2015-02-11 15:34:08 +08:00
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
|
|
|
|
s_AudioEngineSessionHandler = [[AudioEngineSessionHandler alloc] init];
|
|
|
|
#endif
|
|
|
|
|
2014-09-03 18:19:21 +08:00
|
|
|
s_ALDevice = alcOpenDevice(nullptr);
|
|
|
|
|
|
|
|
if (s_ALDevice) {
|
|
|
|
s_ALContext = alcCreateContext(s_ALDevice, nullptr);
|
|
|
|
alcMakeContextCurrent(s_ALContext);
|
|
|
|
|
2014-09-10 17:12:46 +08:00
|
|
|
alGenSources(MAX_AUDIOINSTANCES, _alSources);
|
2015-05-28 11:49:27 +08:00
|
|
|
auto alError = alGetError();
|
2014-09-03 18:19:21 +08:00
|
|
|
if(alError != AL_NO_ERROR)
|
|
|
|
{
|
2014-09-11 10:53:01 +08:00
|
|
|
printf("%s:generating sources fail! error = %x\n", __PRETTY_FUNCTION__, alError);
|
2014-09-03 18:19:21 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-09-10 17:12:46 +08:00
|
|
|
for (int i = 0; i < MAX_AUDIOINSTANCES; ++i) {
|
2014-09-03 18:19:21 +08:00
|
|
|
_alSourceUsed[_alSources[i]] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_threadPool = new (std::nothrow) AudioEngineThreadPool();
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}while (false);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-09-05 14:34:30 +08:00
|
|
|
int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume)
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
|
|
|
if (s_ALDevice == nullptr) {
|
2015-01-01 08:51:45 +08:00
|
|
|
return AudioEngine::INVALID_AUDIO_ID;
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool sourceFlag = false;
|
|
|
|
ALuint alSource = 0;
|
2014-09-10 17:12:46 +08:00
|
|
|
for (int i = 0; i < MAX_AUDIOINSTANCES; ++i) {
|
2014-09-03 18:19:21 +08:00
|
|
|
alSource = _alSources[i];
|
|
|
|
|
|
|
|
if ( !_alSourceUsed[alSource]) {
|
|
|
|
sourceFlag = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!sourceFlag){
|
2015-01-01 08:51:45 +08:00
|
|
|
return AudioEngine::INVALID_AUDIO_ID;
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioCache* audioCache = nullptr;
|
|
|
|
auto it = _audioCaches.find(filePath);
|
|
|
|
if (it == _audioCaches.end()) {
|
|
|
|
audioCache = &_audioCaches[filePath];
|
|
|
|
audioCache->_fileFullPath = FileUtils::getInstance()->fullPathForFilename(filePath);
|
|
|
|
|
2014-09-11 10:53:01 +08:00
|
|
|
_threadPool->addTask(std::bind(&AudioCache::readDataTask, audioCache));
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
audioCache = &it->second;
|
|
|
|
}
|
|
|
|
|
2014-09-10 17:12:46 +08:00
|
|
|
auto player = &_audioPlayers[_currentAudioID];
|
2014-09-03 18:19:21 +08:00
|
|
|
player->_alSource = alSource;
|
|
|
|
player->_loop = loop;
|
|
|
|
player->_volume = volume;
|
2014-09-10 17:12:46 +08:00
|
|
|
audioCache->addCallbacks(std::bind(&AudioEngineImpl::_play2d,this,audioCache,_currentAudioID));
|
2014-09-03 18:19:21 +08:00
|
|
|
|
|
|
|
_alSourceUsed[alSource] = true;
|
|
|
|
|
|
|
|
if (_lazyInitLoop) {
|
|
|
|
_lazyInitLoop = false;
|
|
|
|
|
|
|
|
auto scheduler = cocos2d::Director::getInstance()->getScheduler();
|
2014-10-04 00:38:36 +08:00
|
|
|
scheduler->schedule(CC_SCHEDULE_SELECTOR(AudioEngineImpl::update), this, 0.05f, false);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
2014-09-10 17:12:46 +08:00
|
|
|
return _currentAudioID++;
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngineImpl::_play2d(AudioCache *cache, int audioID)
|
|
|
|
{
|
|
|
|
if(cache->_alBufferReady){
|
|
|
|
auto playerIt = _audioPlayers.find(audioID);
|
|
|
|
if (playerIt != _audioPlayers.end()) {
|
|
|
|
if (playerIt->second.play2d(cache)) {
|
2014-09-10 13:47:17 +08:00
|
|
|
AudioEngine::_audioIDInfoMap[audioID].state = AudioEngine::AudioState::PLAYING;
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
_threadMutex.lock();
|
2014-09-10 17:12:46 +08:00
|
|
|
_toRemoveAudioIDs.push_back(audioID);
|
2014-09-03 18:19:21 +08:00
|
|
|
_threadMutex.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_threadMutex.lock();
|
2014-09-10 17:12:46 +08:00
|
|
|
_toRemoveCaches.push_back(cache);
|
|
|
|
_toRemoveAudioIDs.push_back(audioID);
|
2014-09-03 18:19:21 +08:00
|
|
|
_threadMutex.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngineImpl::setVolume(int audioID,float volume)
|
|
|
|
{
|
|
|
|
auto& player = _audioPlayers[audioID];
|
|
|
|
player._volume = volume;
|
|
|
|
|
|
|
|
if (player._ready) {
|
|
|
|
alSourcef(_audioPlayers[audioID]._alSource, AL_GAIN, volume);
|
|
|
|
|
|
|
|
auto error = alGetError();
|
|
|
|
if (error != AL_NO_ERROR) {
|
2014-09-11 10:53:01 +08:00
|
|
|
printf("%s: audio id = %d, error = %x\n", __PRETTY_FUNCTION__,audioID,error);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngineImpl::setLoop(int audioID, bool loop)
|
|
|
|
{
|
|
|
|
auto& player = _audioPlayers[audioID];
|
|
|
|
|
|
|
|
if (player._ready) {
|
2014-09-11 10:53:01 +08:00
|
|
|
if (player._streamingSource) {
|
2014-09-03 18:19:21 +08:00
|
|
|
player.setLoop(loop);
|
|
|
|
} else {
|
|
|
|
if (loop) {
|
|
|
|
alSourcei(player._alSource, AL_LOOPING, AL_TRUE);
|
|
|
|
} else {
|
|
|
|
alSourcei(player._alSource, AL_LOOPING, AL_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto error = alGetError();
|
|
|
|
if (error != AL_NO_ERROR) {
|
2014-09-11 10:53:01 +08:00
|
|
|
printf("%s: audio id = %d, error = %x\n", __PRETTY_FUNCTION__,audioID,error);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
player._loop = loop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioEngineImpl::pause(int audioID)
|
|
|
|
{
|
|
|
|
bool ret = true;
|
|
|
|
alSourcePause(_audioPlayers[audioID]._alSource);
|
|
|
|
|
|
|
|
auto error = alGetError();
|
|
|
|
if (error != AL_NO_ERROR) {
|
|
|
|
ret = false;
|
2014-09-11 10:53:01 +08:00
|
|
|
printf("%s: audio id = %d, error = %x\n", __PRETTY_FUNCTION__,audioID,error);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioEngineImpl::resume(int audioID)
|
|
|
|
{
|
|
|
|
bool ret = true;
|
|
|
|
alSourcePlay(_audioPlayers[audioID]._alSource);
|
|
|
|
|
|
|
|
auto error = alGetError();
|
|
|
|
if (error != AL_NO_ERROR) {
|
|
|
|
ret = false;
|
2014-09-11 10:53:01 +08:00
|
|
|
printf("%s: audio id = %d, error = %x\n", __PRETTY_FUNCTION__,audioID,error);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioEngineImpl::stop(int audioID)
|
|
|
|
{
|
|
|
|
bool ret = true;
|
|
|
|
auto& player = _audioPlayers[audioID];
|
|
|
|
if (player._ready) {
|
|
|
|
alSourceStop(player._alSource);
|
|
|
|
|
|
|
|
auto error = alGetError();
|
|
|
|
if (error != AL_NO_ERROR) {
|
|
|
|
ret = false;
|
2014-09-11 10:53:01 +08:00
|
|
|
printf("%s: audio id = %d, error = %x\n", __PRETTY_FUNCTION__,audioID,error);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-15 05:07:34 +08:00
|
|
|
alSourcei(player._alSource, AL_BUFFER, 0);
|
2014-09-03 18:19:21 +08:00
|
|
|
|
|
|
|
_alSourceUsed[player._alSource] = false;
|
|
|
|
_audioPlayers.erase(audioID);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngineImpl::stopAll()
|
|
|
|
{
|
2014-09-10 17:12:46 +08:00
|
|
|
for(int index = 0; index < MAX_AUDIOINSTANCES; ++index)
|
2014-09-03 18:19:21 +08:00
|
|
|
{
|
|
|
|
alSourceStop(_alSources[index]);
|
2014-11-15 05:07:34 +08:00
|
|
|
alSourcei(_alSources[index], AL_BUFFER, 0);
|
2014-09-03 18:19:21 +08:00
|
|
|
_alSourceUsed[_alSources[index]] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_audioPlayers.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
float AudioEngineImpl::getDuration(int audioID)
|
|
|
|
{
|
|
|
|
auto& player = _audioPlayers[audioID];
|
|
|
|
if(player._ready){
|
|
|
|
return player._audioCache->_duration;
|
|
|
|
} else {
|
|
|
|
return AudioEngine::TIME_UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float AudioEngineImpl::getCurrentTime(int audioID)
|
|
|
|
{
|
|
|
|
float ret = 0.0f;
|
|
|
|
auto& player = _audioPlayers[audioID];
|
|
|
|
if(player._ready){
|
2014-09-11 10:53:01 +08:00
|
|
|
if (player._streamingSource) {
|
2014-09-03 18:19:21 +08:00
|
|
|
ret = player.getTime();
|
|
|
|
} else {
|
|
|
|
alGetSourcef(player._alSource, AL_SEC_OFFSET, &ret);
|
|
|
|
|
|
|
|
auto error = alGetError();
|
|
|
|
if (error != AL_NO_ERROR) {
|
|
|
|
printf("%s, audio id:%d,error code:%x", __PRETTY_FUNCTION__,audioID,error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioEngineImpl::setCurrentTime(int audioID, float time)
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
auto& player = _audioPlayers[audioID];
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (!player._ready) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-09-11 10:53:01 +08:00
|
|
|
if (player._streamingSource) {
|
2014-09-10 13:47:17 +08:00
|
|
|
ret = player.setTime(time);
|
2014-09-03 18:19:21 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (player._audioCache->_bytesOfRead != player._audioCache->_dataSize &&
|
|
|
|
(time * player._audioCache->_sampleRate * player._audioCache->_bytesPerFrame) > player._audioCache->_bytesOfRead) {
|
2014-09-11 10:53:01 +08:00
|
|
|
printf("%s: audio id = %d\n", __PRETTY_FUNCTION__,audioID);
|
2014-09-03 18:19:21 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
alSourcef(player._alSource, AL_SEC_OFFSET, time);
|
|
|
|
|
|
|
|
auto error = alGetError();
|
|
|
|
if (error != AL_NO_ERROR) {
|
2014-09-11 10:53:01 +08:00
|
|
|
printf("%s: audio id = %d, error = %x\n", __PRETTY_FUNCTION__,audioID,error);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngineImpl::setFinishCallback(int audioID, const std::function<void (int, const std::string &)> &callback)
|
|
|
|
{
|
|
|
|
_audioPlayers[audioID]._finishCallbak = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngineImpl::update(float dt)
|
|
|
|
{
|
|
|
|
ALint sourceState;
|
|
|
|
int audioID;
|
|
|
|
|
|
|
|
if (_threadMutex.try_lock()) {
|
2014-09-10 17:12:46 +08:00
|
|
|
size_t removeAudioCount = _toRemoveAudioIDs.size();
|
2014-09-03 18:19:21 +08:00
|
|
|
for (size_t index = 0; index < removeAudioCount; ++index) {
|
2014-09-10 17:12:46 +08:00
|
|
|
audioID = _toRemoveAudioIDs[index];
|
2014-09-03 18:19:21 +08:00
|
|
|
auto playerIt = _audioPlayers.find(audioID);
|
|
|
|
if (playerIt != _audioPlayers.end()) {
|
|
|
|
_alSourceUsed[playerIt->second._alSource] = false;
|
2014-09-22 15:38:12 +08:00
|
|
|
if(playerIt->second._finishCallbak) {
|
|
|
|
auto& audioInfo = AudioEngine::_audioIDInfoMap[audioID];
|
|
|
|
playerIt->second._finishCallbak(audioID, *audioInfo.filePath);
|
|
|
|
}
|
2014-09-03 18:19:21 +08:00
|
|
|
_audioPlayers.erase(audioID);
|
2014-09-05 14:34:30 +08:00
|
|
|
AudioEngine::remove(audioID);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
}
|
2014-09-10 17:12:46 +08:00
|
|
|
size_t removeCacheCount = _toRemoveCaches.size();
|
2014-09-03 18:19:21 +08:00
|
|
|
for (size_t index = 0; index < removeCacheCount; ++index) {
|
|
|
|
auto itEnd = _audioCaches.end();
|
|
|
|
for (auto it = _audioCaches.begin(); it != itEnd; ++it) {
|
2014-09-10 17:12:46 +08:00
|
|
|
if (&it->second == _toRemoveCaches[index]) {
|
2014-09-03 18:19:21 +08:00
|
|
|
_audioCaches.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_threadMutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto it = _audioPlayers.begin(); it != _audioPlayers.end(); ) {
|
|
|
|
audioID = it->first;
|
|
|
|
auto& player = it->second;
|
|
|
|
alGetSourcei(player._alSource, AL_SOURCE_STATE, &sourceState);
|
|
|
|
|
|
|
|
if (player._ready && sourceState == AL_STOPPED) {
|
|
|
|
_alSourceUsed[player._alSource] = false;
|
|
|
|
if (player._finishCallbak) {
|
2014-09-22 15:38:12 +08:00
|
|
|
auto& audioInfo = AudioEngine::_audioIDInfoMap[audioID];
|
2014-09-03 18:19:21 +08:00
|
|
|
player._finishCallbak(audioID, *audioInfo.filePath);
|
|
|
|
}
|
|
|
|
|
2014-09-05 14:34:30 +08:00
|
|
|
AudioEngine::remove(audioID);
|
2014-09-03 18:19:21 +08:00
|
|
|
|
|
|
|
it = _audioPlayers.erase(it);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
2014-09-10 17:12:46 +08:00
|
|
|
|
|
|
|
if(_audioPlayers.empty()){
|
|
|
|
_lazyInitLoop = true;
|
|
|
|
|
|
|
|
auto scheduler = cocos2d::Director::getInstance()->getScheduler();
|
2014-10-04 00:38:36 +08:00
|
|
|
scheduler->unschedule(CC_SCHEDULE_SELECTOR(AudioEngineImpl::update), this);
|
2014-09-10 17:12:46 +08:00
|
|
|
}
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngineImpl::uncache(const std::string &filePath)
|
|
|
|
{
|
2014-09-29 10:15:41 +08:00
|
|
|
_audioCaches.erase(filePath);
|
2014-09-03 18:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioEngineImpl::uncacheAll()
|
|
|
|
{
|
|
|
|
_audioCaches.clear();
|
|
|
|
}
|
2014-09-22 15:38:12 +08:00
|
|
|
|
|
|
|
#endif
|