2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2010 Steve Oldmeadow
|
|
|
|
|
|
|
|
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_
|
|
|
|
|
|
|
|
#include <stddef.h>
|
2012-07-31 09:38:36 +08:00
|
|
|
#include "Export.h"
|
2012-08-29 14:52:30 +08:00
|
|
|
#include <typeinfo>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.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
|
|
|
|
|
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
|
|
|
/**
|
2013-07-14 23:27:00 +08:00
|
|
|
@class SimpleAudioEngine
|
|
|
|
@brief Offers a VERY simple interface to play background music & sound effects.
|
2013-07-19 07:00:24 +08:00
|
|
|
@note Make sure to call SimpleAudioEngine::end() when the sound engine is not needed anymore
|
|
|
|
to release allocated resources.
|
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
|
|
|
/**
|
|
|
|
@brief Get the shared Engine object,it will new one when first time be called
|
|
|
|
*/
|
|
|
|
static SimpleAudioEngine* getInstance();
|
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(); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Release the shared Engine object
|
2013-11-12 12:21:01 +08:00
|
|
|
@warning It must be called before the application exit, or a memory leak will be casued.
|
2013-07-19 07:30:19 +08:00
|
|
|
*/
|
|
|
|
static void end();
|
|
|
|
|
2013-07-18 06:04:06 +08:00
|
|
|
protected:
|
2012-04-19 14:35:52 +08:00
|
|
|
SimpleAudioEngine();
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual ~SimpleAudioEngine();
|
|
|
|
|
|
|
|
public:
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Preload background music
|
2013-07-19 07:00:24 +08:00
|
|
|
@param pszFilePath The path of the background music file.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js preloadMusic
|
|
|
|
* @lua preloadMusic
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual void preloadBackgroundMusic(const char* pszFilePath);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2013-09-13 11:41:20 +08:00
|
|
|
@brief Play background music
|
|
|
|
@param pszFilePath The path of the background music file,or the FileName of T_SoundResInfo
|
|
|
|
@param bLoop Whether the background music loop or not
|
|
|
|
* @js playMusic
|
|
|
|
* @lua playMusic
|
|
|
|
*/
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual void playBackgroundMusic(const char* pszFilePath, bool bLoop = false);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2013-09-13 11:41:20 +08:00
|
|
|
@brief Stop playing background music
|
|
|
|
@param bReleaseData If release the background music data or not.As default value is false
|
|
|
|
* @js stopMusic
|
|
|
|
* @lua stopMusic
|
|
|
|
*/
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual void stopBackgroundMusic(bool bReleaseData = false);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2013-09-13 11:41:20 +08:00
|
|
|
@brief Pause playing background music
|
|
|
|
* @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
|
|
|
|
|
|
|
/**
|
2013-09-13 11:41:20 +08:00
|
|
|
@brief Resume playing background music
|
|
|
|
* @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
|
|
|
|
|
|
|
/**
|
2013-09-13 11:41:20 +08:00
|
|
|
@brief Rewind playing background music
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
@brief 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
|
|
|
|
|
|
|
/**
|
2013-09-13 11:41:20 +08:00
|
|
|
@brief Indicates whether the background music is playing
|
|
|
|
@return <i>true</i> if the background music is playing, otherwise <i>false</i>
|
|
|
|
* @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
|
|
|
/**
|
2013-09-13 11:41:20 +08:00
|
|
|
@brief The volume of the background music within the range of 0.0 as the minimum and 1.0 as the maximum.
|
|
|
|
* @js getMusicVolume
|
|
|
|
* @lua getMusicVolume
|
|
|
|
*/
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual float getBackgroundMusicVolume();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2013-09-13 11:41:20 +08:00
|
|
|
@brief 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.
|
|
|
|
* @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
|
|
|
|
|
|
|
/**
|
2013-07-14 23:27:00 +08:00
|
|
|
@brief The volume of the effects within the range of 0.0 as the minimum and 1.0 as the maximum.
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual float getEffectsVolume();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2013-07-14 23:27:00 +08:00
|
|
|
@brief Set the volume of sound effects
|
2013-07-19 07:00:24 +08:00
|
|
|
@param volume must be within the range of 0.0 as the minimum and 1.0 as the maximum.
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
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
|
|
|
/**
|
2013-07-14 23:27:00 +08:00
|
|
|
@brief Play sound effect with a file path, pitch, pan and gain
|
2013-07-19 07:00:24 +08:00
|
|
|
@param pszFilePath The path of the effect file.
|
2013-07-14 23:27:00 +08:00
|
|
|
@param bLoop Determines whether to loop the effect playing or not. The default value is false.
|
2013-06-22 04:01:00 +08:00
|
|
|
@param pitch Frequency, normal value is 1.0. Will also change effect play time.
|
2013-07-14 23:27:00 +08:00
|
|
|
@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 OpenAL source id
|
2013-06-22 04:01:00 +08:00
|
|
|
|
|
|
|
@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 emscrippten, win32, marmalade.
|
2013-06-03 20:11:00 +08:00
|
|
|
*/
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual unsigned int playEffect(const char* pszFilePath, bool bLoop = false,
|
|
|
|
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
|
|
|
/**
|
|
|
|
@brief Pause playing sound effect
|
|
|
|
@param nSoundId The return value of function playEffect
|
|
|
|
*/
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual void pauseEffect(unsigned int nSoundId);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Pause all playing sound effect
|
|
|
|
*/
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual void pauseAllEffects();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Resume playing sound effect
|
|
|
|
@param nSoundId The return value of function playEffect
|
|
|
|
*/
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual void resumeEffect(unsigned int nSoundId);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Resume all playing sound effect
|
|
|
|
*/
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual void resumeAllEffects();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Stop playing sound effect
|
|
|
|
@param nSoundId The return value of function playEffect
|
|
|
|
*/
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual void stopEffect(unsigned int nSoundId);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Stop all playing sound effects
|
|
|
|
*/
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual void stopAllEffects();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
@brief preload a compressed audio file
|
2013-07-14 23:27:00 +08:00
|
|
|
@details the compressed audio will be decoded to wave, then written into an internal buffer in SimpleAudioEngine
|
2013-07-19 07:00:24 +08:00
|
|
|
@param pszFilePath The path of the effect file
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual void preloadEffect(const char* pszFilePath);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
@brief unload the preloaded effect from internal buffer
|
2013-07-19 07:00:24 +08:00
|
|
|
@param pszFilePath The path of the effect file
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-07-18 06:04:06 +08:00
|
|
|
virtual void unloadEffect(const char* pszFilePath);
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end of namespace CocosDenshion
|
|
|
|
|
|
|
|
#endif // _SIMPLE_AUDIO_ENGINE_H_
|