2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 Steve Oldmeadow
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2015-03-19 10:38:12 +08:00
|
|
|
Copyright (c) 2013-2015 Chukong Technologies Inc.
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef _SIMPLE_AUDIO_ENGINE_H_
|
|
|
|
#define _SIMPLE_AUDIO_ENGINE_H_
|
|
|
|
|
2012-07-31 09:38:36 +08:00
|
|
|
#include "Export.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-19 07:30:19 +08:00
|
|
|
#if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
|
|
|
|
#define CC_DEPRECATED_ATTRIBUTE __attribute__((deprecated))
|
|
|
|
#elif _MSC_VER >= 1400 //vs 2005 or higher
|
|
|
|
#define CC_DEPRECATED_ATTRIBUTE __declspec(deprecated)
|
|
|
|
#else
|
|
|
|
#define CC_DEPRECATED_ATTRIBUTE
|
|
|
|
#endif
|
|
|
|
|
2015-03-19 10:38:12 +08:00
|
|
|
/**
|
2015-03-24 14:12:58 +08:00
|
|
|
* @addtogroup core
|
2015-03-19 10:38:12 +08:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
namespace CocosDenshion {
|
2012-08-31 21:23:23 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2015-03-19 10:38:12 +08:00
|
|
|
* @class SimpleAudioEngine
|
|
|
|
*
|
|
|
|
* @brief Offers a very simple interface to play background music & sound effects.
|
|
|
|
*
|
|
|
|
* @note Make sure to call SimpleAudioEngine::end() when the sound engine is not needed anymore to release allocated resources.
|
2015-03-19 20:20:16 +08:00
|
|
|
* @js cc.audioEngine
|
2013-07-14 23:27:00 +08:00
|
|
|
*/
|
2012-08-31 21:23:23 +08:00
|
|
|
|
2013-08-22 18:16:50 +08:00
|
|
|
class EXPORT_DLL SimpleAudioEngine
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-07-19 07:30:19 +08:00
|
|
|
/**
|
2015-03-19 10:38:12 +08:00
|
|
|
* Returns a shared instance of the SimpleAudioEngine.
|
2015-03-19 18:41:11 +08:00
|
|
|
* @js NA
|
2013-07-19 07:30:19 +08:00
|
|
|
*/
|
|
|
|
static SimpleAudioEngine* getInstance();
|
2015-03-19 10:38:12 +08:00
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
2015-03-19 10:38:12 +08:00
|
|
|
* @deprecated Use `getInstance` instead.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-07-19 07:30:19 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static SimpleAudioEngine* sharedEngine() { return SimpleAudioEngine::getInstance(); }
|
|
|
|
|
|
|
|
/**
|
2015-03-19 10:38:12 +08:00
|
|
|
* Release the shared Engine object.
|
|
|
|
*
|
|
|
|
* @warning It must be called before the application exit, or it will lead to memory leaks.
|
2013-07-19 07:30:19 +08:00
|
|
|
*/
|
|
|
|
static void end();
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2015-03-19 10:38:12 +08:00
|
|
|
* Preload background music.
|
|
|
|
*
|
|
|
|
* @param filePath The path of the background music file.
|
2015-03-19 20:20:16 +08:00
|
|
|
* @js NA
|
2013-09-13 11:41:20 +08:00
|
|
|
* @lua preloadMusic
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2015-03-19 10:38:12 +08:00
|
|
|
virtual void preloadBackgroundMusic(const char* filePath);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 10:38:12 +08:00
|
|
|
* Play background music.
|
|
|
|
*
|
|
|
|
* @param filePath The path of the background music file,or the FileName of T_SoundResInfo.
|
|
|
|
* @param loop Whether the background music loop or not.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js playMusic
|
|
|
|
* @lua playMusic
|
|
|
|
*/
|
2015-03-19 10:38:12 +08:00
|
|
|
virtual void playBackgroundMusic(const char* filePath, bool loop = false);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 10:38:12 +08:00
|
|
|
* Stop playing background music.
|
|
|
|
*
|
|
|
|
* @param releaseData If release the background music data or not.As default value is false.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js stopMusic
|
|
|
|
* @lua stopMusic
|
|
|
|
*/
|
2015-03-19 10:38:12 +08:00
|
|
|
virtual void stopBackgroundMusic(bool releaseData = false);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 10:38:12 +08:00
|
|
|
* Pause playing background music.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js pauseMusic
|
|
|
|
* @lua pauseMusic
|
2013-07-14 23:27:00 +08:00
|
|
|
*/
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual void pauseBackgroundMusic();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 10:38:12 +08:00
|
|
|
* Resume playing background music.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js resumeMusic
|
|
|
|
* @lua resumeMusic
|
2013-07-14 23:27:00 +08:00
|
|
|
*/
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual void resumeBackgroundMusic();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 10:38:12 +08:00
|
|
|
* Rewind playing background music.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js rewindMusic
|
|
|
|
* @lua rewindMusic
|
2013-07-14 23:27:00 +08:00
|
|
|
*/
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual void rewindBackgroundMusic();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-14 23:27:00 +08:00
|
|
|
/**
|
2015-03-19 10:38:12 +08:00
|
|
|
* Indicates whether any background music can be played or not.
|
|
|
|
*
|
|
|
|
* @return <i>true</i> if background music can be played, otherwise <i>false</i>.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js willPlayMusic
|
|
|
|
* @lua willPlayMusic
|
2013-07-14 23:27:00 +08:00
|
|
|
*/
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual bool willPlayBackgroundMusic();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 10:38:12 +08:00
|
|
|
* Indicates whether the background music is playing.
|
|
|
|
*
|
|
|
|
* @return <i>true</i> if the background music is playing, otherwise <i>false</i>.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js isMusicPlaying
|
|
|
|
* @lua isMusicPlaying
|
|
|
|
*/
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual bool isBackgroundMusicPlaying();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-14 23:27:00 +08:00
|
|
|
//
|
2012-04-19 14:35:52 +08:00
|
|
|
// properties
|
2013-07-14 23:27:00 +08:00
|
|
|
//
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2015-03-19 10:38:12 +08:00
|
|
|
* The volume of the background music within the range of 0.0 as the minimum and 1.0 as the maximum.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js getMusicVolume
|
|
|
|
* @lua getMusicVolume
|
|
|
|
*/
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual float getBackgroundMusicVolume();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 10:38:12 +08:00
|
|
|
* Set the volume of background music.
|
|
|
|
*
|
|
|
|
* @param volume must be within the range of 0.0 as the minimum and 1.0 as the maximum.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js setMusicVolume
|
|
|
|
* @lua setMusicVolume
|
|
|
|
*/
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual void setBackgroundMusicVolume(float volume);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 10:38:12 +08:00
|
|
|
* The volume of the effects within the range of 0.0 as the minimum and 1.0 as the maximum.
|
|
|
|
*/
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual float getEffectsVolume();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 10:38:12 +08:00
|
|
|
* Set the volume of sound effects.
|
|
|
|
*
|
|
|
|
* @param volume must be within the range of 0.0 as the minimum and 1.0 as the maximum.
|
|
|
|
*/
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual void setEffectsVolume(float volume);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-14 23:27:00 +08:00
|
|
|
//
|
2012-04-19 14:35:52 +08:00
|
|
|
// for sound effects
|
|
|
|
|
2013-06-03 20:11:00 +08:00
|
|
|
/**
|
2015-03-19 10:38:12 +08:00
|
|
|
* Play sound effect with a file path, pitch, pan and gain.
|
|
|
|
*
|
|
|
|
* @param filePath The path of the effect file.
|
|
|
|
* @param loop Determines whether to loop the effect playing or not. The default value is false.
|
|
|
|
* @param pitch Frequency, normal value is 1.0. Will also change effect play time.
|
|
|
|
* @param pan Stereo effect, in the range of [-1..1] where -1 enables only left channel.
|
|
|
|
* @param gain Volume, in the range of [0..1]. The normal value is 1.
|
|
|
|
* @return The sound id.
|
|
|
|
*
|
|
|
|
* @note Full support is under development, now there are limitations:
|
|
|
|
* - no pitch effect on Samsung Galaxy S2 with OpenSL backend enabled;
|
|
|
|
* - no pitch/pan/gain on win32.
|
|
|
|
*/
|
|
|
|
virtual unsigned int playEffect(const char* filePath, bool loop = false,
|
2013-07-18 06:04:06 +08:00
|
|
|
float pitch = 1.0f, float pan = 0.0f, float gain = 1.0f);
|
2013-06-03 20:11:00 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2015-03-19 10:38:12 +08:00
|
|
|
* Pause playing sound effect.
|
|
|
|
*
|
|
|
|
* @param soundId The return value of function playEffect.
|
|
|
|
*/
|
|
|
|
virtual void pauseEffect(unsigned int soundId);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 10:38:12 +08:00
|
|
|
* Pause all playing sound effect.
|
|
|
|
*/
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual void pauseAllEffects();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 10:38:12 +08:00
|
|
|
* Resume playing sound effect.
|
|
|
|
*
|
|
|
|
* @param soundId The return value of function playEffect.
|
|
|
|
*/
|
|
|
|
virtual void resumeEffect(unsigned int soundId);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 10:38:12 +08:00
|
|
|
* Resume all playing sound effect.
|
|
|
|
*/
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual void resumeAllEffects();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 10:38:12 +08:00
|
|
|
* Stop playing sound effect.
|
|
|
|
*
|
|
|
|
* @param soundId The return value of function playEffect.
|
|
|
|
*/
|
|
|
|
virtual void stopEffect(unsigned int soundId);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 10:38:12 +08:00
|
|
|
* Stop all playing sound effects.
|
|
|
|
*/
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual void stopAllEffects();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 10:38:12 +08:00
|
|
|
* Preload a compressed audio file.
|
|
|
|
*
|
|
|
|
* The compressed audio will be decoded to wave, then written into an internal buffer in SimpleAudioEngine.
|
|
|
|
*
|
|
|
|
* @param filePath The path of the effect file.
|
2015-03-19 18:41:11 +08:00
|
|
|
* @js NA
|
2015-03-19 10:38:12 +08:00
|
|
|
*/
|
|
|
|
virtual void preloadEffect(const char* filePath);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unload the preloaded effect from internal buffer.
|
|
|
|
*
|
|
|
|
* @param filePath The path of the effect file.
|
|
|
|
*/
|
|
|
|
virtual void unloadEffect(const char* filePath);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* Constructor of SimpleAudioEngine.
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2015-03-19 10:38:12 +08:00
|
|
|
SimpleAudioEngine();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-19 10:38:12 +08:00
|
|
|
* Destructor of SimpleAudioEngine.
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2015-03-19 10:38:12 +08:00
|
|
|
virtual ~SimpleAudioEngine();
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end of namespace CocosDenshion
|
|
|
|
|
2015-03-19 10:38:12 +08:00
|
|
|
// end audio group
|
|
|
|
/// @}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
#endif // _SIMPLE_AUDIO_ENGINE_H_
|