2014-10-15 05:13:14 +08:00
|
|
|
/*
|
|
|
|
* cocos2d-x http://www.cocos2d-x.org
|
|
|
|
*
|
|
|
|
* Copyright (c) 2010-2011 - cocos2d-x community
|
2018-01-29 16:25:32 +08:00
|
|
|
* Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2014-10-15 05:13:14 +08:00
|
|
|
*
|
|
|
|
* Portions Copyright (c) Microsoft Open Technologies, Inc.
|
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "audio/include/SimpleAudioEngine.h"
|
|
|
|
#include "audio/winrt/Audio.h"
|
2014-03-29 03:53:35 +08:00
|
|
|
|
|
|
|
#include <map>
|
2016-04-18 15:09:21 +08:00
|
|
|
#include "platform/CCPlatformMacros.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
|
2014-03-29 03:53:35 +08:00
|
|
|
using namespace std;
|
2015-05-21 02:18:54 +08:00
|
|
|
USING_NS_CC;
|
2014-03-29 03:53:35 +08:00
|
|
|
|
|
|
|
namespace CocosDenshion {
|
|
|
|
|
2016-06-23 11:39:23 +08:00
|
|
|
Audio* s_audioController = nullptr;
|
2014-10-15 05:13:14 +08:00
|
|
|
bool s_initialized = false;
|
2014-03-29 03:53:35 +08:00
|
|
|
|
2014-10-15 05:13:14 +08:00
|
|
|
SimpleAudioEngine* SimpleAudioEngine::getInstance()
|
|
|
|
{
|
|
|
|
static SimpleAudioEngine s_SharedEngine;
|
|
|
|
return &s_SharedEngine;
|
|
|
|
}
|
2014-03-29 03:53:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
static Audio* sharedAudioController()
|
|
|
|
{
|
2014-10-15 05:13:14 +08:00
|
|
|
if (! s_audioController || !s_initialized)
|
2014-03-29 03:53:35 +08:00
|
|
|
{
|
2016-06-23 11:39:23 +08:00
|
|
|
if (s_audioController == nullptr)
|
2014-10-15 05:13:14 +08:00
|
|
|
{
|
|
|
|
s_audioController = new Audio;
|
|
|
|
}
|
2014-03-29 03:53:35 +08:00
|
|
|
s_audioController->Initialize();
|
|
|
|
s_audioController->CreateResources();
|
2014-10-15 05:13:14 +08:00
|
|
|
s_initialized = true;
|
2014-03-29 03:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return s_audioController;
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleAudioEngine::SimpleAudioEngine()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleAudioEngine::~SimpleAudioEngine()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SimpleAudioEngine::end()
|
|
|
|
{
|
|
|
|
sharedAudioController()->StopBackgroundMusic(true);
|
2014-10-15 05:13:14 +08:00
|
|
|
sharedAudioController()->StopAllSoundEffects(true);
|
2014-03-29 03:53:35 +08:00
|
|
|
sharedAudioController()->ReleaseResources();
|
2014-10-15 05:13:14 +08:00
|
|
|
s_initialized = false;
|
2014-03-29 03:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// BackgroundMusic
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
|
|
|
|
{
|
|
|
|
if (! pszFilePath)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-18 15:09:21 +08:00
|
|
|
string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
2015-05-21 02:18:54 +08:00
|
|
|
sharedAudioController()->PlayBackgroundMusic(fullPath.c_str(), bLoop);
|
2014-03-29 03:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::stopBackgroundMusic(bool bReleaseData)
|
|
|
|
{
|
|
|
|
sharedAudioController()->StopBackgroundMusic(bReleaseData);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::pauseBackgroundMusic()
|
|
|
|
{
|
|
|
|
sharedAudioController()->PauseBackgroundMusic();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::resumeBackgroundMusic()
|
|
|
|
{
|
|
|
|
sharedAudioController()->ResumeBackgroundMusic();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::rewindBackgroundMusic()
|
|
|
|
{
|
|
|
|
sharedAudioController()->RewindBackgroundMusic();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SimpleAudioEngine::willPlayBackgroundMusic()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SimpleAudioEngine::isBackgroundMusicPlaying()
|
|
|
|
{
|
|
|
|
return sharedAudioController()->IsBackgroundMusicPlaying();
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// effect function
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-10-15 05:13:14 +08:00
|
|
|
unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath, bool bLoop,float pitch, float pan, float gain)
|
2014-03-29 03:53:35 +08:00
|
|
|
{
|
|
|
|
unsigned int sound;
|
2016-04-18 15:09:21 +08:00
|
|
|
string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
2015-05-21 02:18:54 +08:00
|
|
|
sharedAudioController()->PlaySoundEffect(fullPath.c_str(), bLoop, sound); // TODO: need to support playEffect parameters
|
2014-03-29 03:53:35 +08:00
|
|
|
return sound;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::stopEffect(unsigned int nSoundId)
|
|
|
|
{
|
|
|
|
sharedAudioController()->StopSoundEffect(nSoundId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::preloadEffect(const char* pszFilePath)
|
|
|
|
{
|
2016-04-18 15:09:21 +08:00
|
|
|
string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
2015-05-21 02:18:54 +08:00
|
|
|
sharedAudioController()->PreloadSoundEffect(fullPath.c_str());
|
2014-03-29 03:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::pauseEffect(unsigned int nSoundId)
|
|
|
|
{
|
|
|
|
sharedAudioController()->PauseSoundEffect(nSoundId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::resumeEffect(unsigned int nSoundId)
|
|
|
|
{
|
|
|
|
sharedAudioController()->ResumeSoundEffect(nSoundId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::pauseAllEffects()
|
|
|
|
{
|
|
|
|
sharedAudioController()->PauseAllSoundEffects();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::resumeAllEffects()
|
|
|
|
{
|
|
|
|
sharedAudioController()->ResumeAllSoundEffects();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::stopAllEffects()
|
|
|
|
{
|
2014-10-15 05:13:14 +08:00
|
|
|
sharedAudioController()->StopAllSoundEffects(false);
|
2014-03-29 03:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath)
|
|
|
|
{
|
2014-10-15 05:13:14 +08:00
|
|
|
UNUSED_PARAM(pszFilePath);
|
2014-03-29 03:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::unloadEffect(const char* pszFilePath)
|
|
|
|
{
|
2016-04-18 15:09:21 +08:00
|
|
|
string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
2015-05-21 02:18:54 +08:00
|
|
|
sharedAudioController()->UnloadSoundEffect(fullPath.c_str());
|
2014-03-29 03:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// volume interface
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
float SimpleAudioEngine::getBackgroundMusicVolume()
|
|
|
|
{
|
|
|
|
return sharedAudioController()->GetBackgroundVolume();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::setBackgroundMusicVolume(float volume)
|
|
|
|
{
|
|
|
|
sharedAudioController()->SetBackgroundVolume((volume<=0.0f)? 0.0f : volume);
|
|
|
|
}
|
|
|
|
|
|
|
|
float SimpleAudioEngine::getEffectsVolume()
|
|
|
|
{
|
|
|
|
return sharedAudioController()->GetSoundEffectVolume();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::setEffectsVolume(float volume)
|
|
|
|
{
|
|
|
|
sharedAudioController()->SetSoundEffectVolume((volume<=0.0f)? 0.0f : volume);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end of namespace CocosDenshion
|