Fix name of variables and coding style.

This commit is contained in:
Dhilan007 2014-09-10 17:12:46 +08:00
parent 61b316e12f
commit 1d43c6ec82
8 changed files with 73 additions and 64 deletions

View File

@ -44,7 +44,7 @@ const float AudioEngine::TIME_UNKNOWN = -1.0f;
std::unordered_map<std::string,std::list<int>> AudioEngine::_audioPathIDMap;
//profileName,ProfileHelper
std::unordered_map<std::string, AudioEngine::ProfileHelper> AudioEngine::_audioPathProfileHelperMap;
int AudioEngine::_maxInstances = kMaxSources;
int AudioEngine::_maxInstances = MAX_AUDIOINSTANCES;
AudioEngine::ProfileHelper* AudioEngine::_defaultProfileHelper;
std::unordered_map<int, AudioEngine::AudioInfo> AudioEngine::_audioIDInfoMap;
AudioEngineImpl* AudioEngine::_audioEngineImpl = nullptr;
@ -318,7 +318,7 @@ void AudioEngine::setFinishCallback(int audioID, const std::function<void (int,
bool AudioEngine::setMaxAudioInstance(int maxInstances)
{
if (maxInstances > 0 && maxInstances <= kMaxSources) {
if (maxInstances > 0 && maxInstances <= MAX_AUDIOINSTANCES) {
_maxInstances = maxInstances;
return true;
}

View File

@ -32,7 +32,7 @@
#include "audio/include/AudioEngine.h"
#include "base/CCDirector.h"
#include "platform/android/CCFileUtilsAndroid.h"
#include "platform/android/CCFileUtils-android.h"
#include "platform/android/jni/JniHelper.h"
#include <android/log.h>
@ -120,23 +120,23 @@ bool AudioPlayer::init(SLEngineItf engineEngine, SLObjectItf outputMixObject,con
const SLInterfaceID ids[3] = {SL_IID_SEEK, SL_IID_PREFETCHSTATUS, SL_IID_VOLUME};
const SLboolean req[3] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
auto result = (*engineEngine)->CreateAudioPlayer(engineEngine, &_fdPlayerObject, &audioSrc, &audioSnk, 3, ids, req);
if(SL_RESULT_SUCCESS != result){ LOG_FUN break; }
if(SL_RESULT_SUCCESS != result){ ERRORLOG("create audio player fail"); break; }
// realize the player
result = (*_fdPlayerObject)->Realize(_fdPlayerObject, SL_BOOLEAN_FALSE);
if(SL_RESULT_SUCCESS != result){ LOG_FUN break; }
if(SL_RESULT_SUCCESS != result){ ERRORLOG("realize the player fail"); break; }
// get the play interface
result = (*_fdPlayerObject)->GetInterface(_fdPlayerObject, SL_IID_PLAY, &_fdPlayerPlay);
if(SL_RESULT_SUCCESS != result){ LOG_FUN break; }
if(SL_RESULT_SUCCESS != result){ ERRORLOG("get the play interface fail"); break; }
// get the seek interface
result = (*_fdPlayerObject)->GetInterface(_fdPlayerObject, SL_IID_SEEK, &_fdPlayerSeek);
if(SL_RESULT_SUCCESS != result){ LOG_FUN break; }
if(SL_RESULT_SUCCESS != result){ ERRORLOG("get the seek interface fail"); break; }
// get the volume interface
result = (*_fdPlayerObject)->GetInterface(_fdPlayerObject, SL_IID_VOLUME, &_fdPlayerVolume);
if(SL_RESULT_SUCCESS != result){ LOG_FUN break; }
if(SL_RESULT_SUCCESS != result){ ERRORLOG("get the volume interface fail"); break; }
if (loop){
(*_fdPlayerSeek)->SetLoop(_fdPlayerSeek, SL_BOOLEAN_TRUE, 0, SL_TIME_UNKNOWN);
@ -149,7 +149,7 @@ bool AudioPlayer::init(SLEngineItf engineEngine, SLObjectItf outputMixObject,con
(*_fdPlayerVolume)->SetVolumeLevel(_fdPlayerVolume, dbVolume);
result = (*_fdPlayerPlay)->SetPlayState(_fdPlayerPlay, SL_PLAYSTATE_PLAYING);
if(SL_RESULT_SUCCESS != result){ LOG_FUN break; }
if(SL_RESULT_SUCCESS != result){ ERRORLOG("SetPlayState fail"); break; }
ret = true;
} while (0);
@ -159,7 +159,7 @@ bool AudioPlayer::init(SLEngineItf engineEngine, SLObjectItf outputMixObject,con
//====================================================
AudioEngineImpl::AudioEngineImpl()
: nextAudioID(0)
: currentAudioID(0)
, _engineObject(nullptr)
, _engineEngine(nullptr)
, _outputMixObject(nullptr)
@ -185,25 +185,25 @@ bool AudioEngineImpl::init()
do{
// create engine
auto result = slCreateEngine(&_engineObject, 0, nullptr, 0, nullptr, nullptr);
if(SL_RESULT_SUCCESS != result){ LOG_FUN break; }
if(SL_RESULT_SUCCESS != result){ ERRORLOG("create opensl engine fail"); break; }
// realize the engine
result = (*_engineObject)->Realize(_engineObject, SL_BOOLEAN_FALSE);
if(SL_RESULT_SUCCESS != result){ LOG_FUN break; }
if(SL_RESULT_SUCCESS != result){ ERRORLOG("realize the engine fail"); break; }
// get the engine interface, which is needed in order to create other objects
result = (*_engineObject)->GetInterface(_engineObject, SL_IID_ENGINE, &_engineEngine);
if(SL_RESULT_SUCCESS != result){ LOG_FUN break; }
if(SL_RESULT_SUCCESS != result){ ERRORLOG("get the engine interface fail"); break; }
// create output mix
const SLInterfaceID outputMixIIDs[] = {};
const SLboolean outputMixReqs[] = {};
result = (*_engineEngine)->CreateOutputMix(_engineEngine, &_outputMixObject, 0, outputMixIIDs, outputMixReqs);
if(SL_RESULT_SUCCESS != result){ LOG_FUN break; }
if(SL_RESULT_SUCCESS != result){ ERRORLOG("create output mix fail"); break; }
// realize the output mix
result = (*_outputMixObject)->Realize(_outputMixObject, SL_BOOLEAN_FALSE);
if(SL_RESULT_SUCCESS != result){ LOG_FUN break; }
if(SL_RESULT_SUCCESS != result){ ERRORLOG("realize the output mix fail"); break; }
ret = true;
}while (false);
@ -220,15 +220,15 @@ int AudioEngineImpl::play2d(const std::string &fileFullPath ,bool loop ,float vo
if (_engineEngine == nullptr)
break;
auto& player = _audioPlayers[nextAudioID];
auto& player = _audioPlayers[currentAudioID];
auto initPlayer = player.init( _engineEngine, _outputMixObject, fileFullPath, volume, loop);
if (!initPlayer){
_audioPlayers.erase(nextAudioID);
_audioPlayers.erase(currentAudioID);
log("%s,%d message:create player for %s fail", __func__, __LINE__, fileFullPath.c_str());
break;
}
audioId = nextAudioID++;
audioId = currentAudioID++;
player._audioID = audioId;
(*(player._fdPlayerPlay))->RegisterCallback(player._fdPlayerPlay, PlayOverEvent, (void*)this);

View File

@ -32,9 +32,9 @@
#include <unordered_map>
#include "base/ccUtils.h"
#define kMaxSources 24
#define MAX_AUDIOINSTANCES 24
#define LOG_FUN log("error: %s,%d",__func__,__LINE__);
#define ERRORLOG(msg) log("fun:%s,line:%d,msg:%s",__func__,__LINE__,#msg)
NS_CC_BEGIN
@ -98,7 +98,7 @@ private:
//audioID,AudioInfo
std::unordered_map<int, AudioPlayer> _audioPlayers;
int nextAudioID;
int currentAudioID;
};
#endif // __AUDIO_ENGINE_INL_H_

View File

@ -33,7 +33,7 @@
#include <mutex>
#include <vector>
#include "base/CCPlatformMacros.h"
#include "CCPlatformMacros.h"
#define QUEUEBUFFER_NUM 3

View File

@ -26,19 +26,20 @@
#import <OpenAL/alc.h>
#import <AudioToolbox/ExtendedAudioFile.h>
#define PCMDATA_CACHEMAXSIZE 1048576//1MB
#define PCMDATA_CACHEMAXSIZE 1048576
typedef ALvoid AL_APIENTRY (*alBufferDataStaticProcPtr) (const ALint bid, ALenum format, ALvoid* data, ALsizei size, ALsizei freq);
ALvoid alBufferDataStaticProc(const ALint bid, ALenum format, ALvoid* data, ALsizei size, ALsizei freq)
static ALvoid alBufferDataStaticProc(const ALint bid, ALenum format, ALvoid* data, ALsizei size, ALsizei freq)
{
static alBufferDataStaticProcPtr proc = NULL;
if (proc == NULL) {
if (proc == NULL){
proc = (alBufferDataStaticProcPtr) alcGetProcAddress(NULL, (const ALCchar*) "alBufferDataStatic");
}
if (proc)
if (proc){
proc(bid, format, data, size, freq);
}
return;
}
@ -60,9 +61,9 @@ AudioCache::~AudioCache()
{
_release = true;
if(_pcmData){
if (_alBufferReady) {
if (_alBufferReady)
alDeleteBuffers(1, &_alBufferId);
}
_readThreadMutex.lock();
_readThreadMutex.unlock();
free(_pcmData);
@ -79,8 +80,8 @@ void AudioCache::readDataThread()
{
_readThreadMutex.lock();
AudioStreamBasicDescription theFileFormat;
UInt32 thePropertySize = sizeof(theFileFormat);
AudioStreamBasicDescription theFileFormat;
UInt32 thePropertySize = sizeof(theFileFormat);
SInt64 theFileLengthInFrames;
SInt64 readInFrames;
@ -130,7 +131,10 @@ void AudioCache::readDataThread()
// Get the total frame count
thePropertySize = sizeof(theFileLengthInFrames);
error = ExtAudioFileGetProperty(extRef, kExtAudioFileProperty_FileLengthFrames, &thePropertySize, &theFileLengthInFrames);
if(error) { printf("initOpenALAudioData: ExtAudioFileGetProperty(kExtAudioFileProperty_FileLengthFrames) FAILED, Error = %d\n", (int)error); goto ExitThread; }
if(error) {
printf("initOpenALAudioData: ExtAudioFileGetProperty(kExtAudioFileProperty_FileLengthFrames) FAILED, Error = %d\n", (int)error);
goto ExitThread;
}
_dataSize = (ALsizei)(theFileLengthInFrames * outputFormat.mBytesPerFrame);
_format = (outputFormat.mChannelsPerFrame > 1) ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16;
@ -142,7 +146,8 @@ void AudioCache::readDataThread()
alGenBuffers(1, &_alBufferId);
auto alError = alGetError();
if (alError != AL_NO_ERROR) {
printf("error attaching audio to buffer: %x\n", alError); goto ExitThread;
printf("error attaching audio to buffer: %x\n", alError);
goto ExitThread;
}
alBufferDataStaticProc(_alBufferId, _format, _pcmData, _dataSize, _sampleRate);
@ -199,16 +204,14 @@ void AudioCache::readDataThread()
ExitThread:
CFRelease(fileURL);
if (extRef) ExtAudioFileDispose(extRef);
if (extRef)
ExtAudioFileDispose(extRef);
_readThreadMutex.unlock();
if (_queBufferFrames > 0) {
if (_queBufferFrames > 0)
_alBufferReady = true;
invokingCallbacks();
}
else {
invokingCallbacks();
}
invokingCallbacks();
}
void AudioCache::invokingCallbacks()

View File

@ -34,7 +34,7 @@
NS_CC_BEGIN
#define kMaxSources 32
#define MAX_AUDIOINSTANCES 32
class AudioEngineThreadPool;
@ -67,7 +67,7 @@ private:
AudioEngineThreadPool* _threadPool;
ALuint _alSources[kMaxSources];
ALuint _alSources[MAX_AUDIOINSTANCES];
//source,used
std::unordered_map<ALuint, bool> _alSourceUsed;
@ -80,12 +80,12 @@ private:
std::mutex _threadMutex;
std::vector<AudioCache*> _removeCaches;
std::vector<int> _removeAudioIDs;
std::vector<AudioCache*> _toRemoveCaches;
std::vector<int> _toRemoveAudioIDs;
bool _lazyInitLoop;
int nextAudioID;
int _currentAudioID;
};
NS_CC_END

View File

@ -84,7 +84,7 @@ namespace cocos2d {
_sleepCondition.notify_all();
}
void stop()
void destroy()
{
_running = false;
_sleepCondition.notify_all();
@ -109,7 +109,6 @@ namespace cocos2d {
if (nullptr == task)
{
// Wait for http request tasks from main thread
std::unique_lock<std::mutex> lk(_sleepMutex);
_sleepCondition.wait(lk);
continue;
@ -134,7 +133,7 @@ namespace cocos2d {
AudioEngineImpl::AudioEngineImpl()
: _lazyInitLoop(true)
, nextAudioID(0)
, _currentAudioID(0)
, _threadPool(nullptr)
{
@ -143,7 +142,7 @@ AudioEngineImpl::AudioEngineImpl()
AudioEngineImpl::~AudioEngineImpl()
{
if (s_ALContext) {
alDeleteSources(kMaxSources, _alSources);
alDeleteSources(MAX_AUDIOINSTANCES, _alSources);
_audioCaches.clear();
@ -153,7 +152,7 @@ AudioEngineImpl::~AudioEngineImpl()
alcCloseDevice(s_ALDevice);
}
if (_threadPool) {
_threadPool->stop();
_threadPool->destroy();
delete _threadPool;
}
}
@ -172,7 +171,7 @@ bool AudioEngineImpl::init()
s_ALContext = alcCreateContext(s_ALDevice, nullptr);
alcMakeContextCurrent(s_ALContext);
alGenSources(kMaxSources, _alSources);
alGenSources(MAX_AUDIOINSTANCES, _alSources);
alError = alGetError();
if(alError != AL_NO_ERROR)
{
@ -180,7 +179,7 @@ bool AudioEngineImpl::init()
break;
}
for (int i = 0; i < kMaxSources; ++i) {
for (int i = 0; i < MAX_AUDIOINSTANCES; ++i) {
_alSourceUsed[_alSources[i]] = false;
}
@ -200,7 +199,7 @@ int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume
bool sourceFlag = false;
ALuint alSource = 0;
for (int i = 0; i < kMaxSources; ++i) {
for (int i = 0; i < MAX_AUDIOINSTANCES; ++i) {
alSource = _alSources[i];
if ( !_alSourceUsed[alSource]) {
@ -224,11 +223,11 @@ int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume
audioCache = &it->second;
}
auto player = &_audioPlayers[nextAudioID];
auto player = &_audioPlayers[_currentAudioID];
player->_alSource = alSource;
player->_loop = loop;
player->_volume = volume;
audioCache->addCallbacks(std::bind(&AudioEngineImpl::_play2d,this,audioCache,nextAudioID));
audioCache->addCallbacks(std::bind(&AudioEngineImpl::_play2d,this,audioCache,_currentAudioID));
_alSourceUsed[alSource] = true;
@ -239,7 +238,7 @@ int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume
scheduler->schedule(schedule_selector(AudioEngineImpl::update), this, 0.05f, false);
}
return nextAudioID++;
return _currentAudioID++;
}
void AudioEngineImpl::_play2d(AudioCache *cache, int audioID)
@ -252,15 +251,15 @@ void AudioEngineImpl::_play2d(AudioCache *cache, int audioID)
}
else{
_threadMutex.lock();
_removeAudioIDs.push_back(audioID);
_toRemoveAudioIDs.push_back(audioID);
_threadMutex.unlock();
}
}
}
else {
_threadMutex.lock();
_removeCaches.push_back(cache);
_removeAudioIDs.push_back(audioID);
_toRemoveCaches.push_back(cache);
_toRemoveAudioIDs.push_back(audioID);
_threadMutex.unlock();
}
}
@ -357,7 +356,7 @@ bool AudioEngineImpl::stop(int audioID)
void AudioEngineImpl::stopAll()
{
for(int index = 0; index < kMaxSources; ++index)
for(int index = 0; index < MAX_AUDIOINSTANCES; ++index)
{
alSourceStop(_alSources[index]);
alSourcei(_alSources[index], AL_BUFFER, NULL);
@ -442,9 +441,9 @@ void AudioEngineImpl::update(float dt)
int audioID;
if (_threadMutex.try_lock()) {
size_t removeAudioCount = _removeAudioIDs.size();
size_t removeAudioCount = _toRemoveAudioIDs.size();
for (size_t index = 0; index < removeAudioCount; ++index) {
audioID = _removeAudioIDs[index];
audioID = _toRemoveAudioIDs[index];
auto playerIt = _audioPlayers.find(audioID);
if (playerIt != _audioPlayers.end()) {
_alSourceUsed[playerIt->second._alSource] = false;
@ -452,11 +451,11 @@ void AudioEngineImpl::update(float dt)
AudioEngine::remove(audioID);
}
}
size_t removeCacheCount = _removeCaches.size();
size_t removeCacheCount = _toRemoveCaches.size();
for (size_t index = 0; index < removeCacheCount; ++index) {
auto itEnd = _audioCaches.end();
for (auto it = _audioCaches.begin(); it != itEnd; ++it) {
if (&it->second == _removeCaches[index]) {
if (&it->second == _toRemoveCaches[index]) {
_audioCaches.erase(it);
break;
}
@ -485,6 +484,13 @@ void AudioEngineImpl::update(float dt)
++it;
}
}
if(_audioPlayers.empty()){
_lazyInitLoop = true;
auto scheduler = cocos2d::Director::getInstance()->getScheduler();
scheduler->unschedule(schedule_selector(AudioEngineImpl::update), this);
}
}
void AudioEngineImpl::uncache(const std::string &filePath)

View File

@ -29,7 +29,7 @@
#import <OpenAL/al.h>
#include <string>
#include <mutex>
#include "base/CCPlatformMacros.h"
#include "CCPlatformMacros.h"
NS_CC_BEGIN
class AudioCache;