diff --git a/cocos/audio/AudioEngine.cpp b/cocos/audio/AudioEngine.cpp index 423b6c84f9b1..417daee53ae0 100644 --- a/cocos/audio/AudioEngine.cpp +++ b/cocos/audio/AudioEngine.cpp @@ -35,10 +35,8 @@ #include "audio/android/AudioEngine-inl.h" #elif CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC #include "audio/apple/AudioEngine-inl.h" -#elif CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 -#include "audio/win32/AudioEngine-win32.h" -#elif CC_TARGET_PLATFORM == CC_PLATFORM_LINUX -#include "audio/linux/AudioEngine-linux.h" +#elif CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX +#include "audio/desktop/AudioEngine-desktop.h" #endif #define TIME_DELAY_PRECISION 0.0001 diff --git a/cocos/audio/CMakeLists.txt b/cocos/audio/CMakeLists.txt index b9be171836a0..c95229d25e4d 100644 --- a/cocos/audio/CMakeLists.txt +++ b/cocos/audio/CMakeLists.txt @@ -3,30 +3,41 @@ set(COCOS_AUDIO_SRC audio/AudioEngine.cpp ) +if(WINDOWS OR LINUX) + + set(COCOS_AUDIO_PLATFORM_HEADER + audio/desktop/AudioDecoderManager.h + audio/desktop/AudioDecoder.h + audio/desktop/AudioPlayer.h + audio/desktop/AudioDecoderOgg.h + audio/desktop/AudioMacros.h + audio/desktop/AudioEngine-desktop.h + audio/desktop/AudioDecoderMp3.h + audio/desktop/AudioCache.h + ) + + set(COCOS_AUDIO_PLATFORM_SRC + audio/desktop/AudioEngine-desktop.cpp + audio/desktop/AudioCache.cpp + audio/desktop/AudioPlayer.cpp + audio/desktop/AudioDecoder.cpp + audio/desktop/AudioDecoderManager.cpp + audio/desktop/AudioDecoderMp3.cpp + audio/desktop/AudioDecoderOgg.cpp + ) + +endif() + if(WINDOWS) set(COCOS_AUDIO_PLATFORM_HEADER - audio/win32/AudioDecoderManager.h - audio/win32/AudioDecoder.h + ${COCOS_AUDIO_PLATFORM_HEADER} audio/win32/MciPlayer.h - audio/win32/AudioPlayer.h - audio/win32/AudioDecoderOgg.h - audio/win32/AudioMacros.h - audio/win32/AudioEngine-win32.h - audio/win32/AudioDecoderMp3.h - audio/win32/AudioCache.h ) set(COCOS_AUDIO_PLATFORM_SRC + ${COCOS_AUDIO_PLATFORM_SRC} audio/win32/MciPlayer.cpp - audio/win32/MciPlayer.h - audio/win32/AudioEngine-win32.cpp - audio/win32/AudioCache.cpp - audio/win32/AudioPlayer.cpp - audio/win32/AudioDecoder.cpp - audio/win32/AudioDecoderManager.cpp - audio/win32/AudioDecoderMp3.cpp - audio/win32/AudioDecoderOgg.cpp ) elseif(ANDROID) @@ -105,16 +116,6 @@ elseif(ANDROID) audio/android/tinysndfile.cpp ) -elseif(LINUX) - set(COCOS_AUDIO_PLATFORM_HEADER - audio/linux/AudioEngine-linux.h - ) - - set(COCOS_AUDIO_PLATFORM_SRC - audio/linux/AudioEngine-linux.h - audio/linux/AudioEngine-linux.cpp - ) - elseif(APPLE) # common set(COCOS_AUDIO_PLATFORM_HEADER diff --git a/cocos/audio/win32/AudioCache.cpp b/cocos/audio/desktop/AudioCache.cpp similarity index 98% rename from cocos/audio/win32/AudioCache.cpp rename to cocos/audio/desktop/AudioCache.cpp index 702b6b62de8e..81244c13a83d 100644 --- a/cocos/audio/win32/AudioCache.cpp +++ b/cocos/audio/desktop/AudioCache.cpp @@ -27,15 +27,15 @@ #include "platform/CCPlatformConfig.h" -#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 +#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX -#include "audio/win32/AudioCache.h" +#include "audio/desktop/AudioCache.h" #include #include "base/CCDirector.h" #include "base/CCScheduler.h" -#include "audio/win32/AudioDecoderManager.h" -#include "audio/win32/AudioDecoder.h" +#include "audio/desktop/AudioDecoderManager.h" +#include "audio/desktop/AudioDecoder.h" #define VERY_VERY_VERBOSE_LOGGING #ifdef VERY_VERY_VERBOSE_LOGGING diff --git a/cocos/audio/win32/AudioCache.h b/cocos/audio/desktop/AudioCache.h similarity index 96% rename from cocos/audio/win32/AudioCache.h rename to cocos/audio/desktop/AudioCache.h index cb75fe918c5c..34d8d0425925 100644 --- a/cocos/audio/win32/AudioCache.h +++ b/cocos/audio/desktop/AudioCache.h @@ -27,7 +27,7 @@ #include "platform/CCPlatformConfig.h" -#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 +#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX #include #include @@ -40,7 +40,7 @@ #include #endif #include "platform/CCPlatformMacros.h" -#include "audio/win32/AudioMacros.h" +#include "audio/desktop/AudioMacros.h" NS_CC_BEGIN diff --git a/cocos/audio/win32/AudioDecoder.cpp b/cocos/audio/desktop/AudioDecoder.cpp similarity index 97% rename from cocos/audio/win32/AudioDecoder.cpp rename to cocos/audio/desktop/AudioDecoder.cpp index e05d1f9ec958..271efbf4fe9b 100644 --- a/cocos/audio/win32/AudioDecoder.cpp +++ b/cocos/audio/desktop/AudioDecoder.cpp @@ -23,8 +23,8 @@ THE SOFTWARE. ****************************************************************************/ -#include "audio/win32/AudioDecoder.h" -#include "audio/win32/AudioMacros.h" +#include "audio/desktop/AudioDecoder.h" +#include "audio/desktop/AudioMacros.h" #include "platform/CCFileUtils.h" #define LOG_TAG "AudioDecoder" diff --git a/cocos/audio/win32/AudioDecoder.h b/cocos/audio/desktop/AudioDecoder.h similarity index 100% rename from cocos/audio/win32/AudioDecoder.h rename to cocos/audio/desktop/AudioDecoder.h diff --git a/cocos/audio/win32/AudioDecoderManager.cpp b/cocos/audio/desktop/AudioDecoderManager.cpp similarity index 92% rename from cocos/audio/win32/AudioDecoderManager.cpp rename to cocos/audio/desktop/AudioDecoderManager.cpp index 950c30535523..cb52e31d3658 100644 --- a/cocos/audio/win32/AudioDecoderManager.cpp +++ b/cocos/audio/desktop/AudioDecoderManager.cpp @@ -25,10 +25,10 @@ THE SOFTWARE. #define LOG_TAG "AudioDecoderManager" -#include "audio/win32/AudioDecoderManager.h" -#include "audio/win32/AudioDecoderOgg.h" -#include "audio/win32/AudioDecoderMp3.h" -#include "audio/win32/AudioMacros.h" +#include "audio/desktop/AudioDecoderManager.h" +#include "audio/desktop/AudioDecoderOgg.h" +#include "audio/desktop/AudioDecoderMp3.h" +#include "audio/desktop/AudioMacros.h" #include "platform/CCFileUtils.h" #include "base/CCConsole.h" #include "mpg123.h" diff --git a/cocos/audio/win32/AudioDecoderManager.h b/cocos/audio/desktop/AudioDecoderManager.h similarity index 100% rename from cocos/audio/win32/AudioDecoderManager.h rename to cocos/audio/desktop/AudioDecoderManager.h diff --git a/cocos/audio/win32/AudioDecoderMp3.cpp b/cocos/audio/desktop/AudioDecoderMp3.cpp similarity index 98% rename from cocos/audio/win32/AudioDecoderMp3.cpp rename to cocos/audio/desktop/AudioDecoderMp3.cpp index e6d5b3a94dc4..a3e359821dc0 100644 --- a/cocos/audio/win32/AudioDecoderMp3.cpp +++ b/cocos/audio/desktop/AudioDecoderMp3.cpp @@ -23,8 +23,8 @@ THE SOFTWARE. ****************************************************************************/ -#include "audio/win32/AudioDecoderMp3.h" -#include "audio/win32/AudioMacros.h" +#include "audio/desktop/AudioDecoderMp3.h" +#include "audio/desktop/AudioMacros.h" #include "platform/CCFileUtils.h" #include "base/CCConsole.h" diff --git a/cocos/audio/win32/AudioDecoderMp3.h b/cocos/audio/desktop/AudioDecoderMp3.h similarity index 98% rename from cocos/audio/win32/AudioDecoderMp3.h rename to cocos/audio/desktop/AudioDecoderMp3.h index 41b642b7d929..d4e122f86765 100644 --- a/cocos/audio/win32/AudioDecoderMp3.h +++ b/cocos/audio/desktop/AudioDecoderMp3.h @@ -25,7 +25,7 @@ #pragma once -#include "audio/win32/AudioDecoder.h" +#include "audio/desktop/AudioDecoder.h" struct mpg123_handle_struct; diff --git a/cocos/audio/win32/AudioDecoderOgg.cpp b/cocos/audio/desktop/AudioDecoderOgg.cpp similarity index 97% rename from cocos/audio/win32/AudioDecoderOgg.cpp rename to cocos/audio/desktop/AudioDecoderOgg.cpp index a5c5c7c6ab66..9c52244ba07e 100644 --- a/cocos/audio/win32/AudioDecoderOgg.cpp +++ b/cocos/audio/desktop/AudioDecoderOgg.cpp @@ -23,8 +23,8 @@ THE SOFTWARE. ****************************************************************************/ -#include "audio/win32/AudioDecoderOgg.h" -#include "audio/win32/AudioMacros.h" +#include "audio/desktop/AudioDecoderOgg.h" +#include "audio/desktop/AudioMacros.h" #include "platform/CCFileUtils.h" #define LOG_TAG "AudioDecoderOgg" diff --git a/cocos/audio/win32/AudioDecoderOgg.h b/cocos/audio/desktop/AudioDecoderOgg.h similarity index 98% rename from cocos/audio/win32/AudioDecoderOgg.h rename to cocos/audio/desktop/AudioDecoderOgg.h index 13150677b1f5..59123f712b0b 100644 --- a/cocos/audio/win32/AudioDecoderOgg.h +++ b/cocos/audio/desktop/AudioDecoderOgg.h @@ -25,7 +25,7 @@ #pragma once -#include "audio/win32/AudioDecoder.h" +#include "audio/desktop/AudioDecoder.h" #include "vorbis/vorbisfile.h" diff --git a/cocos/audio/win32/AudioEngine-win32.cpp b/cocos/audio/desktop/AudioEngine-desktop.cpp similarity index 97% rename from cocos/audio/win32/AudioEngine-win32.cpp rename to cocos/audio/desktop/AudioEngine-desktop.cpp index 24605942da2a..3388901de0ae 100644 --- a/cocos/audio/win32/AudioEngine-win32.cpp +++ b/cocos/audio/desktop/AudioEngine-desktop.cpp @@ -22,7 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ -#include "audio/win32/AudioEngine-win32.h" +#include "audio/desktop/AudioEngine-desktop.h" #ifdef OPENAL_PLAIN_INCLUDES #include "alc.h" @@ -35,18 +35,20 @@ #include "base/CCDirector.h" #include "base/CCScheduler.h" #include "platform/CCFileUtils.h" -#include "audio/win32/AudioDecoderManager.h" +#include "audio/desktop/AudioDecoderManager.h" +#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 #include +#endif -#define LOG_TAG "AudioEngine-Win32" +#define LOG_TAG "AudioEngine-Desktop" // 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 void _desktopLog(const char *format, va_list args) { +#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 static const int MAX_LOG_LENGTH = 16 * 1024; int bufferSize = MAX_LOG_LENGTH; char* buf = nullptr; @@ -90,13 +92,17 @@ static void _winLog(const char *format, va_list args) } while (pos < len); delete[] buf; + +#elif CC_TARGET_PLATFORM == CC_PLATFORM_LINUX + // TODO: Implement log output +#endif } void audioLog(const char * format, ...) { va_list args; va_start(args, format); - _winLog(format, args); + _desktopLog(format, args); va_end(args); } diff --git a/cocos/audio/win32/AudioEngine-win32.h b/cocos/audio/desktop/AudioEngine-desktop.h similarity index 97% rename from cocos/audio/win32/AudioEngine-win32.h rename to cocos/audio/desktop/AudioEngine-desktop.h index 9cfd0f51b8e8..06577737d6e6 100644 --- a/cocos/audio/win32/AudioEngine-win32.h +++ b/cocos/audio/desktop/AudioEngine-desktop.h @@ -27,8 +27,8 @@ #include #include "base/CCRef.h" -#include "audio/win32/AudioCache.h" -#include "audio/win32/AudioPlayer.h" +#include "audio/desktop/AudioCache.h" +#include "audio/desktop/AudioPlayer.h" NS_CC_BEGIN diff --git a/cocos/audio/win32/AudioMacros.h b/cocos/audio/desktop/AudioMacros.h similarity index 100% rename from cocos/audio/win32/AudioMacros.h rename to cocos/audio/desktop/AudioMacros.h diff --git a/cocos/audio/win32/AudioPlayer.cpp b/cocos/audio/desktop/AudioPlayer.cpp similarity index 98% rename from cocos/audio/win32/AudioPlayer.cpp rename to cocos/audio/desktop/AudioPlayer.cpp index 370c015982f6..8d510df220af 100644 --- a/cocos/audio/win32/AudioPlayer.cpp +++ b/cocos/audio/desktop/AudioPlayer.cpp @@ -22,11 +22,11 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ -#include "audio/win32/AudioPlayer.h" -#include "audio/win32/AudioCache.h" +#include "audio/desktop/AudioPlayer.h" +#include "audio/desktop/AudioCache.h" #include "platform/CCFileUtils.h" -#include "audio/win32/AudioDecoderManager.h" -#include "audio/win32/Audiodecoder.h" +#include "audio/desktop/AudioDecoderManager.h" +#include "audio/desktop/AudioDecoder.h" #define VERY_VERY_VERBOSE_LOGGING #ifdef VERY_VERY_VERBOSE_LOGGING diff --git a/cocos/audio/win32/AudioPlayer.h b/cocos/audio/desktop/AudioPlayer.h similarity index 100% rename from cocos/audio/win32/AudioPlayer.h rename to cocos/audio/desktop/AudioPlayer.h diff --git a/cocos/audio/linux/AudioEngine-linux.cpp b/cocos/audio/linux/AudioEngine-linux.cpp deleted file mode 100644 index 80b886d894da..000000000000 --- a/cocos/audio/linux/AudioEngine-linux.cpp +++ /dev/null @@ -1,357 +0,0 @@ -/**************************************************************************** - 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 -#include -#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(static_cast(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 &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(reinterpret_cast(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::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 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(mapChannelInfo.size()) + 1; - // std::map::insert returns std::pair - 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(static_cast(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::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(reinterpret_cast(data)); - return mapChannelInfo[id].channel; -} diff --git a/cocos/audio/linux/AudioEngine-linux.h b/cocos/audio/linux/AudioEngine-linux.h deleted file mode 100644 index 06c5659826ba..000000000000 --- a/cocos/audio/linux/AudioEngine-linux.h +++ /dev/null @@ -1,109 +0,0 @@ -/**************************************************************************** - Copyright (c) 2015-2016 Chukong Technologies Inc. - 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. - ****************************************************************************/ -#include "platform/CCPlatformConfig.h" - -#if CC_TARGET_PLATFORM == CC_PLATFORM_LINUX - -#ifndef __AUDIO_ENGINE_LINUX_H_ -#define __AUDIO_ENGINE_LINUX_H_ - -#include -#include -#include -#include "fmod.hpp" -#include "fmod_errors.h" -#include "audio/include/AudioEngine.h" - -#include "base/CCRef.h" - -NS_CC_BEGIN - -#define MAX_AUDIOINSTANCES 32 - -class CC_DLL AudioEngineImpl : public cocos2d::Ref -{ -public: - AudioEngineImpl(); - ~AudioEngineImpl(); - - bool init(); - int play2d(const std::string &fileFullPath ,bool loop ,float volume); - void setVolume(int audioID,float volume); - void setLoop(int audioID, bool loop); - bool pause(int audioID); - bool resume(int audioID); - bool stop(int audioID); - void stopAll(); - float getDuration(int audioID); - float getCurrentTime(int audioID); - bool setCurrentTime(int audioID, float time); - void setFinishCallback(int audioID, const std::function &callback); - - void uncache(const std::string& filePath); - void uncacheAll(); - - - int preload(const std::string& filePath, std::function callback); - - void update(float dt); - - /** - * used internally by ffmod callback - */ - void onSoundFinished(FMOD::Channel * channel); - -private: - - /** - * returns null if a sound with the given path is not found - */ - FMOD::Sound * findSound(const std::string &path); - - FMOD::Channel * getChannel(FMOD::Sound *); - - struct ChannelInfo{ - int id; - std::string path; - FMOD::Sound * sound; - FMOD::Channel * channel; - bool loop; - float volume; - std::function callback; - }; - - std::map mapChannelInfo; - - std::map mapId; - - std::map mapSound; - - FMOD::System* pSystem; - -}; - -NS_CC_END -#endif // __AUDIO_ENGINE_LINUX_H_ -#endif - diff --git a/templates/cocos2dx_files.json b/templates/cocos2dx_files.json index 89ed04734bab..e9706343ea0a 100644 --- a/templates/cocos2dx_files.json +++ b/templates/cocos2dx_files.json @@ -2754,22 +2754,47 @@ "external/json/stream.h", "external/json/stringbuffer.h", "external/json/writer.h", - "external/linux-specific/fmod/CMakeLists.txt", - "external/linux-specific/fmod/include/fmod.h", - "external/linux-specific/fmod/include/fmod.hpp", - "external/linux-specific/fmod/include/fmod_codec.h", - "external/linux-specific/fmod/include/fmod_common.h", - "external/linux-specific/fmod/include/fmod_dsp.h", - "external/linux-specific/fmod/include/fmod_dsp_effects.h", - "external/linux-specific/fmod/include/fmod_errors.h", - "external/linux-specific/fmod/include/fmod_output.h", - "external/linux-specific/fmod/prebuilt/64-bit/libfmod.so", - "external/linux-specific/fmod/prebuilt/64-bit/libfmod.so.6", - "external/linux-specific/fmod/prebuilt/64-bit/libfmodL.so", - "external/linux-specific/fmod/prebuilt/64-bit/libfmodL.so.6", "external/md5/CMakeLists.txt", "external/md5/md5.c", "external/md5/md5.h", + "external/MP3Decoder/CMakeLists.txt", + "external/MP3Decoder/include/mpg123.h", + "external/MP3Decoder/prebuilt/linux/64-bit/libmpg123.a", + "external/MP3Decoder/prebuilt/win32/libmpg123.dll", + "external/MP3Decoder/prebuilt/win32/libmpg123.lib", + "external/OggDecoder/CMakeLists.txt", + "external/OggDecoder/include/ogg/ogg.h", + "external/OggDecoder/include/ogg/os_types.h", + "external/OggDecoder/include/vorbis/codec.h", + "external/OggDecoder/include/vorbis/vorbisenc.h", + "external/OggDecoder/include/vorbis/vorbisfile.h", + "external/OggDecoder/prebuilt/linux/64-bit/libogg.a", + "external/OggDecoder/prebuilt/linux/64-bit/libvorbis.a", + "external/OggDecoder/prebuilt/linux/64-bit/libvorbisfile.a", + "external/OggDecoder/prebuilt/win32/libogg.dll", + "external/OggDecoder/prebuilt/win32/libogg.lib", + "external/OggDecoder/prebuilt/win32/libvorbis.dll", + "external/OggDecoder/prebuilt/win32/libvorbis.lib", + "external/OggDecoder/prebuilt/win32/libvorbisfile.dll", + "external/OggDecoder/prebuilt/win32/libvorbisfile.lib", + "external/OpenalSoft/CMakeLists.txt", + "external/OpenalSoft/include/linux/AL/al.h", + "external/OpenalSoft/include/linux/AL/alc.h", + "external/OpenalSoft/include/linux/AL/alext.h", + "external/OpenalSoft/include/linux/AL/efx-creative.h", + "external/OpenalSoft/include/linux/AL/efx-presets.h", + "external/OpenalSoft/include/linux/AL/efx.h", + "external/OpenalSoft/prebuilt/linux/64-bit/libopenal.so", + "external/OpenalSoft/prebuilt/linux/64-bit/libopenal.so.1", + "external/OpenalSoft/prebuilt/linux/64-bit/libopenal.so.1.17.0", + "external/OpenalSoft/include/win32/AL/al.h", + "external/OpenalSoft/include/win32/AL/alc.h", + "external/OpenalSoft/include/win32/AL/alext.h", + "external/OpenalSoft/include/win32/AL/efx-creative.h", + "external/OpenalSoft/include/win32/AL/efx-presets.h", + "external/OpenalSoft/include/win32/AL/efx.h", + "external/OpenalSoft/prebuilt/win32/OpenAL32.dll", + "external/OpenalSoft/prebuilt/win32/OpenAL32.lib", "external/openssl/CMakeLists.txt", "external/openssl/include/android/openssl/aes.h", "external/openssl/include/android/openssl/asn1.h", @@ -3690,31 +3715,6 @@ "external/win10-specific/zlib/prebuilt/x64/zlib.lib", "external/win10-specific/zlib/prebuilt/x64/zlib1.dll", "external/win10-specific/zlib/prebuilt/x64/zlibstatic.lib", - "external/win32-specific/MP3Decoder/CMakeLists.txt", - "external/win32-specific/MP3Decoder/include/mpg123.h", - "external/win32-specific/MP3Decoder/prebuilt/libmpg123.dll", - "external/win32-specific/MP3Decoder/prebuilt/libmpg123.lib", - "external/win32-specific/OggDecoder/CMakeLists.txt", - "external/win32-specific/OggDecoder/include/ogg/ogg.h", - "external/win32-specific/OggDecoder/include/ogg/os_types.h", - "external/win32-specific/OggDecoder/include/vorbis/codec.h", - "external/win32-specific/OggDecoder/include/vorbis/vorbisenc.h", - "external/win32-specific/OggDecoder/include/vorbis/vorbisfile.h", - "external/win32-specific/OggDecoder/prebuilt/libogg.dll", - "external/win32-specific/OggDecoder/prebuilt/libogg.lib", - "external/win32-specific/OggDecoder/prebuilt/libvorbis.dll", - "external/win32-specific/OggDecoder/prebuilt/libvorbis.lib", - "external/win32-specific/OggDecoder/prebuilt/libvorbisfile.dll", - "external/win32-specific/OggDecoder/prebuilt/libvorbisfile.lib", - "external/win32-specific/OpenalSoft/CMakeLists.txt", - "external/win32-specific/OpenalSoft/include/AL/al.h", - "external/win32-specific/OpenalSoft/include/AL/alc.h", - "external/win32-specific/OpenalSoft/include/AL/alext.h", - "external/win32-specific/OpenalSoft/include/AL/efx-creative.h", - "external/win32-specific/OpenalSoft/include/AL/efx-presets.h", - "external/win32-specific/OpenalSoft/include/AL/efx.h", - "external/win32-specific/OpenalSoft/prebuilt/OpenAL32.dll", - "external/win32-specific/OpenalSoft/prebuilt/OpenAL32.lib", "external/win32-specific/gles/CMakeLists.txt", "external/win32-specific/gles/include/OGLES/GL/glew.h", "external/win32-specific/gles/include/OGLES/GL/glxew.h",