axmol/cocos/audio/win32/AudioEngine-win32.cpp

501 lines
14 KiB
C++
Raw Normal View History

2014-09-29 10:15:41 +08:00
/****************************************************************************
Copyright (c) 2014-2016 Chukong Technologies Inc.
2014-09-29 10:15:41 +08:00
http://www.cocos2d-x.org
2014-09-29 10:15:41 +08:00
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:
2014-09-29 10:15:41 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2014-09-29 10:15:41 +08:00
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.
****************************************************************************/
#define LOG_TAG "AudioEngine-Win32"
2014-09-30 09:57:28 +08:00
#include "platform/CCPlatformConfig.h"
2014-09-29 10:15:41 +08:00
#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
2014-09-30 09:57:28 +08:00
#include "audio/win32/AudioEngine-win32.h"
2015-07-09 14:31:03 +08:00
#ifdef OPENAL_PLAIN_INCLUDES
#include "alc.h"
#include "alext.h"
#else
2014-09-29 10:15:41 +08:00
#include "AL/alc.h"
#include "AL/alext.h"
#endif
2014-09-29 10:15:41 +08:00
#include "audio/include/AudioEngine.h"
#include "base/CCDirector.h"
#include "base/CCScheduler.h"
#include "platform/CCFileUtils.h"
#include "audio/win32/AudioDecoderManager.h"
#include <windows.h>
// log, CCLOG aren't threadsafe, since we uses sub threads for parsing pcm data, threadsafe log output
// is needed. Define the following macros (ALOGV, ALOGD, ALOGI, ALOGW, ALOGE) for threadsafe log output.
//FIXME: Move _winLog, winLog to a separated file
static void _winLog(const char *format, va_list args)
{
static const int MAX_LOG_LENGTH = 16 * 1024;
int bufferSize = MAX_LOG_LENGTH;
char* buf = nullptr;
do
{
buf = new (std::nothrow) char[bufferSize];
if (buf == nullptr)
return; // not enough memory
int ret = vsnprintf(buf, bufferSize - 3, format, args);
if (ret < 0)
{
bufferSize *= 2;
delete[] buf;
}
else
break;
} while (true);
strcat(buf, "\n");
int pos = 0;
int len = strlen(buf);
char tempBuf[MAX_LOG_LENGTH + 1] = { 0 };
WCHAR wszBuf[MAX_LOG_LENGTH + 1] = { 0 };
do
{
std::copy(buf + pos, buf + pos + MAX_LOG_LENGTH, tempBuf);
tempBuf[MAX_LOG_LENGTH] = 0;
MultiByteToWideChar(CP_UTF8, 0, tempBuf, -1, wszBuf, sizeof(wszBuf));
OutputDebugStringW(wszBuf);
pos += MAX_LOG_LENGTH;
} while (pos < len);
delete[] buf;
}
void audioLog(const char * format, ...)
{
va_list args;
va_start(args, format);
_winLog(format, args);
va_end(args);
}
2014-09-29 10:15:41 +08:00
using namespace cocos2d;
using namespace cocos2d::experimental;
static ALCdevice *s_ALDevice = nullptr;
static ALCcontext *s_ALContext = nullptr;
AudioEngineImpl::AudioEngineImpl()
: _lazyInitLoop(true)
, _currentAudioID(0)
{
2014-09-29 10:15:41 +08:00
}
AudioEngineImpl::~AudioEngineImpl()
{
if (s_ALContext) {
alDeleteSources(MAX_AUDIOINSTANCES, _alSources);
2014-09-29 10:15:41 +08:00
_audioCaches.clear();
2014-09-29 10:15:41 +08:00
alcMakeContextCurrent(nullptr);
alcDestroyContext(s_ALContext);
s_ALContext = nullptr;
2014-09-29 10:15:41 +08:00
}
2014-09-29 10:15:41 +08:00
if (s_ALDevice) {
alcCloseDevice(s_ALDevice);
s_ALDevice = nullptr;
2014-09-29 10:15:41 +08:00
}
AudioDecoderManager::destroy();
2014-09-29 10:15:41 +08:00
}
bool AudioEngineImpl::init()
{
bool ret = false;
do{
s_ALDevice = alcOpenDevice(nullptr);
2014-09-29 10:15:41 +08:00
if (s_ALDevice) {
auto alError = alGetError();
s_ALContext = alcCreateContext(s_ALDevice, nullptr);
2014-09-29 10:15:41 +08:00
alcMakeContextCurrent(s_ALContext);
2014-09-29 10:15:41 +08:00
alGenSources(MAX_AUDIOINSTANCES, _alSources);
alError = alGetError();
if(alError != AL_NO_ERROR)
{
ALOGE("%s:generating sources failed! error = %x\n", __FUNCTION__, alError);
2014-09-29 10:15:41 +08:00
break;
}
2014-09-29 10:15:41 +08:00
for (int i = 0; i < MAX_AUDIOINSTANCES; ++i) {
_alSourceUsed[_alSources[i]] = false;
}
_scheduler = Director::getInstance()->getScheduler();
ret = AudioDecoderManager::init();
ALOGI("OpenAL was initialized successfully!");
2014-09-29 10:15:41 +08:00
}
}while (false);
2014-09-29 10:15:41 +08:00
return ret;
}
AudioCache* AudioEngineImpl::preload(const std::string& filePath, std::function<void(bool)> callback)
2014-09-29 10:15:41 +08:00
{
AudioCache* audioCache = nullptr;
auto it = _audioCaches.find(filePath);
if (it == _audioCaches.end()) {
audioCache = &_audioCaches[filePath];
2014-09-29 10:15:41 +08:00
audioCache->_fileFullPath = FileUtils::getInstance()->fullPathForFilename(filePath);
unsigned int cacheId = audioCache->_id;
auto isCacheDestroyed = audioCache->_isDestroyed;
AudioEngine::addTask([audioCache, cacheId, isCacheDestroyed](){
if (*isCacheDestroyed)
{
ALOGV("AudioCache (id=%u) was destroyed, no need to launch readDataTask.", cacheId);
audioCache->setSkipReadDataTask(true);
return;
}
audioCache->readDataTask(cacheId);
});
}
else {
audioCache = &it->second;
}
if (audioCache && callback)
{
audioCache->addLoadCallback(callback);
}
return audioCache;
}
int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume)
{
if (s_ALDevice == nullptr) {
return AudioEngine::INVALID_AUDIO_ID;
}
bool sourceFlag = false;
ALuint alSource = 0;
for (int i = 0; i < MAX_AUDIOINSTANCES; ++i) {
alSource = _alSources[i];
if ( !_alSourceUsed[alSource]) {
sourceFlag = true;
break;
}
}
if(!sourceFlag){
return AudioEngine::INVALID_AUDIO_ID;
2014-09-29 10:15:41 +08:00
}
auto player = new (std::nothrow) AudioPlayer;
if (player == nullptr) {
return AudioEngine::INVALID_AUDIO_ID;
2014-09-29 10:15:41 +08:00
}
2014-09-29 10:15:41 +08:00
player->_alSource = alSource;
player->_loop = loop;
player->_volume = volume;
auto audioCache = preload(filePath, nullptr);
if (audioCache == nullptr) {
delete player;
return AudioEngine::INVALID_AUDIO_ID;
}
player->setCache(audioCache);
_threadMutex.lock();
_audioPlayers[_currentAudioID] = player;
_threadMutex.unlock();
2014-09-29 10:15:41 +08:00
_alSourceUsed[alSource] = true;
audioCache->addPlayCallback(std::bind(&AudioEngineImpl::_play2d,this,audioCache,_currentAudioID));
2014-09-29 10:15:41 +08:00
if (_lazyInitLoop) {
_lazyInitLoop = false;
_scheduler->schedule(CC_SCHEDULE_SELECTOR(AudioEngineImpl::update), this, 0.05f, false);
2014-09-29 10:15:41 +08:00
}
2014-09-29 10:15:41 +08:00
return _currentAudioID++;
}
void AudioEngineImpl::_play2d(AudioCache *cache, int audioID)
{
//Note: It may bn in sub thread or main thread :(
if (!*cache->_isDestroyed && cache->_state == AudioCache::State::READY)
{
_threadMutex.lock();
2014-09-29 10:15:41 +08:00
auto playerIt = _audioPlayers.find(audioID);
if (playerIt != _audioPlayers.end() && playerIt->second->play2d()) {
_scheduler->performFunctionInCocosThread([audioID](){
if (AudioEngine::_audioIDInfoMap.find(audioID) != AudioEngine::_audioIDInfoMap.end()) {
AudioEngine::_audioIDInfoMap[audioID].state = AudioEngine::AudioState::PLAYING;
}
});
2014-09-29 10:15:41 +08:00
}
_threadMutex.unlock();
}
else
{
ALOGD("AudioEngineImpl::_play2d, cache was destroyed or not ready!");
auto iter = _audioPlayers.find(audioID);
if (iter != _audioPlayers.end())
{
iter->second->_removeByAudioEngine = true;
}
}
2014-09-29 10:15:41 +08:00
}
void AudioEngineImpl::setVolume(int audioID,float volume)
{
auto player = _audioPlayers[audioID];
player->_volume = volume;
if (player->_ready) {
alSourcef(_audioPlayers[audioID]->_alSource, AL_GAIN, volume);
2014-09-29 10:15:41 +08:00
auto error = alGetError();
if (error != AL_NO_ERROR) {
ALOGE("%s: audio id = %d, error = %x", __FUNCTION__,audioID,error);
2014-09-29 10:15:41 +08:00
}
}
}
void AudioEngineImpl::setLoop(int audioID, bool loop)
{
auto player = _audioPlayers[audioID];
if (player->_ready) {
if (player->_streamingSource) {
player->setLoop(loop);
2014-09-29 10:15:41 +08:00
} else {
if (loop) {
alSourcei(player->_alSource, AL_LOOPING, AL_TRUE);
2014-09-29 10:15:41 +08:00
} else {
alSourcei(player->_alSource, AL_LOOPING, AL_FALSE);
2014-09-29 10:15:41 +08:00
}
2014-09-29 10:15:41 +08:00
auto error = alGetError();
if (error != AL_NO_ERROR) {
ALOGE("%s: audio id = %d, error = %x", __FUNCTION__,audioID,error);
2014-09-29 10:15:41 +08:00
}
}
}
else {
player->_loop = loop;
2014-09-29 10:15:41 +08:00
}
}
bool AudioEngineImpl::pause(int audioID)
{
bool ret = true;
alSourcePause(_audioPlayers[audioID]->_alSource);
2014-09-29 10:15:41 +08:00
auto error = alGetError();
if (error != AL_NO_ERROR) {
ret = false;
ALOGE("%s: audio id = %d, error = %x\n", __FUNCTION__,audioID,error);
2014-09-29 10:15:41 +08:00
}
2014-09-29 10:15:41 +08:00
return ret;
}
bool AudioEngineImpl::resume(int audioID)
{
bool ret = true;
alSourcePlay(_audioPlayers[audioID]->_alSource);
2014-09-29 10:15:41 +08:00
auto error = alGetError();
if (error != AL_NO_ERROR) {
ret = false;
ALOGE("%s: audio id = %d, error = %x\n", __FUNCTION__,audioID,error);
2014-09-29 10:15:41 +08:00
}
2014-09-29 10:15:41 +08:00
return ret;
}
void AudioEngineImpl::stop(int audioID)
2014-09-29 10:15:41 +08:00
{
auto player = _audioPlayers[audioID];
player->destroy();
//Note: Don't set the flag to false here, it should be set in 'update' function.
// Otherwise, the state got from alSourceState may be wrong
// _alSourceUsed[player->_alSource] = false;
2014-09-29 10:15:41 +08:00
}
void AudioEngineImpl::stopAll()
{
for(auto&& player : _audioPlayers)
2014-09-29 10:15:41 +08:00
{
player.second->destroy();
2015-05-20 17:13:04 +08:00
}
//Note: Don't set the flag to false here, it should be set in 'update' function.
// Otherwise, the state got from alSourceState may be wrong
// for(int index = 0; index < MAX_AUDIOINSTANCES; ++index)
// {
// _alSourceUsed[_alSources[index]] = false;
// }
2014-09-29 10:15:41 +08:00
}
float AudioEngineImpl::getDuration(int audioID)
{
auto player = _audioPlayers[audioID];
if(player->_ready){
return player->_audioCache->_duration;
2014-09-29 10:15:41 +08:00
} else {
return AudioEngine::TIME_UNKNOWN;
}
}
float AudioEngineImpl::getCurrentTime(int audioID)
{
float ret = 0.0f;
auto player = _audioPlayers[audioID];
if(player->_ready){
if (player->_streamingSource) {
ret = player->getTime();
2014-09-29 10:15:41 +08:00
} else {
alGetSourcef(player->_alSource, AL_SEC_OFFSET, &ret);
2014-09-29 10:15:41 +08:00
auto error = alGetError();
if (error != AL_NO_ERROR) {
ALOGE("%s, audio id:%d,error code:%x", __FUNCTION__,audioID,error);
2014-09-29 10:15:41 +08:00
}
}
}
2014-09-29 10:15:41 +08:00
return ret;
}
bool AudioEngineImpl::setCurrentTime(int audioID, float time)
{
bool ret = false;
auto player = _audioPlayers[audioID];
2014-09-29 10:15:41 +08:00
do {
if (!player->_ready) {
2014-09-29 10:15:41 +08:00
break;
}
if (player->_streamingSource) {
ret = player->setTime(time);
2014-09-29 10:15:41 +08:00
break;
}
else {
if (player->_audioCache->_framesRead != player->_audioCache->_totalFrames &&
(time * player->_audioCache->_sampleRate) > player->_audioCache->_framesRead) {
ALOGE("%s: audio id = %d", __FUNCTION__,audioID);
break;
}
alSourcef(player->_alSource, AL_SEC_OFFSET, time);
2014-09-29 10:15:41 +08:00
auto error = alGetError();
if (error != AL_NO_ERROR) {
ALOGE("%s: audio id = %d, error = %x", __FUNCTION__,audioID,error);
2014-09-29 10:15:41 +08:00
}
ret = true;
}
} while (0);
2014-09-29 10:15:41 +08:00
return ret;
}
void AudioEngineImpl::setFinishCallback(int audioID, const std::function<void (int, const std::string &)> &callback)
{
_audioPlayers[audioID]->_finishCallbak = callback;
2014-09-29 10:15:41 +08:00
}
void AudioEngineImpl::update(float dt)
{
ALint sourceState;
int audioID;
AudioPlayer* player;
// ALOGV("AudioPlayer count: %d", (int)_audioPlayers.size());
2014-09-29 10:15:41 +08:00
for (auto it = _audioPlayers.begin(); it != _audioPlayers.end(); ) {
audioID = it->first;
player = it->second;
alGetSourcei(player->_alSource, AL_SOURCE_STATE, &sourceState);
if (player->_removeByAudioEngine)
2015-05-20 17:13:04 +08:00
{
_alSourceUsed[player->_alSource] = false;
AudioEngine::remove(audioID);
_threadMutex.lock();
2015-05-20 17:13:04 +08:00
it = _audioPlayers.erase(it);
_threadMutex.unlock();
delete player;
2015-05-20 17:13:04 +08:00
}
else if (player->_ready && sourceState == AL_STOPPED) {
_alSourceUsed[player->_alSource] = false;
if (player->_finishCallbak) {
2014-09-29 10:15:41 +08:00
auto& audioInfo = AudioEngine::_audioIDInfoMap[audioID];
player->_finishCallbak(audioID, *audioInfo.filePath); //FIXME: callback will delay 50ms
2014-09-29 10:15:41 +08:00
}
2014-09-29 10:15:41 +08:00
AudioEngine::remove(audioID);
delete player;
_threadMutex.lock();
it = _audioPlayers.erase(it);
_threadMutex.unlock();
2014-09-29 10:15:41 +08:00
}
else{
++it;
}
}
2014-09-29 10:15:41 +08:00
if(_audioPlayers.empty()){
_lazyInitLoop = true;
_scheduler->unschedule(CC_SCHEDULE_SELECTOR(AudioEngineImpl::update), this);
2014-09-29 10:15:41 +08:00
}
}
void AudioEngineImpl::uncache(const std::string &filePath)
{
_audioCaches.erase(filePath);
}
void AudioEngineImpl::uncacheAll()
{
_audioCaches.clear();
}
#endif