[48] | 1 | /** |
---|
| 2 | * SoundManager.h |
---|
| 3 | * |
---|
| 4 | * Original Code : Van Stokes, Jr. (http://www.EvilMasterMindsInc.com) - Aug 05 |
---|
| 5 | * Modified Code : Steven Gay - mec3@bluewin.ch - Septembre 2005 - Jan 06 |
---|
| 6 | * |
---|
| 7 | * |
---|
| 8 | * Partial Documentation |
---|
| 9 | * ===================== |
---|
| 10 | * |
---|
| 11 | * Very simple SoundManager using OpenAl. |
---|
| 12 | * |
---|
| 13 | * For a more complete one, you should see : |
---|
| 14 | * OpenAL++ http://alpp.sourceforge.net/ |
---|
| 15 | * |
---|
| 16 | **************************************************************************** |
---|
| 17 | * COMPILE |
---|
| 18 | * |
---|
| 19 | * - Don't forget to link to : ALut.lib and OpenAL32.lib. |
---|
| 20 | * The order is important. |
---|
| 21 | * |
---|
| 22 | * - With CodeBlocks there is one warning I didn't resolve : |
---|
| 23 | * "Warning: .drectve `/DEFAULTLIB:"uuid.lib" /DEFAULTLIB:"uuid.lib" ' unrecognized" |
---|
| 24 | * |
---|
| 25 | **************************************************************************** |
---|
| 26 | * USAGE |
---|
| 27 | * |
---|
| 28 | * 1. Create the object from the class with createManager() |
---|
| 29 | * |
---|
| 30 | * 2. Call the init() function |
---|
| 31 | * |
---|
| 32 | * 3. Call the loadDefaultSounds() function to PRE-LOAD audio into the buffers. |
---|
| 33 | * This is optional. Review the function to make changes that you need. |
---|
| 34 | * |
---|
| 35 | * 4. Set the Listener Location by calling setListenerPosition() function |
---|
| 36 | * continually call this as your Listener (camera) position changes! |
---|
| 37 | * |
---|
| 38 | * 5. For each object that emits sound, call the loadSound() function. |
---|
| 39 | * CAREFUL : The filename must be unique. |
---|
| 40 | * |
---|
| 41 | * 6. Optional : For each object you can set the all the parameters of the |
---|
| 42 | * sound with setSound() or only the position, velocity and direction with |
---|
| 43 | * setSoundPosition(). |
---|
| 44 | * |
---|
| 45 | * 7. Call the playAudioSource() to play the sound at some event. |
---|
| 46 | * This function will play the sound and then stop. It will NOT repeat playing. |
---|
| 47 | * Use stopAudioSource() to stop a sound from playing if its still playing |
---|
| 48 | * |
---|
| 49 | * 8. Call pauseAudio() or pauseAllAudio() to pause one or all sound(s). |
---|
| 50 | * Call resumeAudio() or resumeAllAudio() to resume one or all paused sound(s). |
---|
| 51 | * |
---|
| 52 | * 9. When your object is done emitting sounds (when out of range for example) |
---|
| 53 | * call releaseAudioSource(). |
---|
| 54 | * It is important to release your source when you are no longer going to |
---|
| 55 | * need it because you are limited in the number of sources you can have. |
---|
| 56 | * |
---|
| 57 | * 10. If your objects moves (other than the listener/camera) then |
---|
| 58 | * continually update the objects position by calling setSourcePosition(). |
---|
| 59 | * |
---|
| 60 | ****************************************************************************** |
---|
| 61 | * |
---|
| 62 | * Additional informations : |
---|
| 63 | * |
---|
| 64 | * Ogg Vorbis - http://www.xiph.org/downloads/ |
---|
| 65 | * - http://www.illiminable.com/ogg/ |
---|
| 66 | * Flac - http://flac.sourceforge.net/features.html |
---|
| 67 | * Theora - http://www.theora.org/ |
---|
| 68 | * |
---|
| 69 | * |
---|
| 70 | ****************************************************************************** |
---|
| 71 | * |
---|
| 72 | * TODO |
---|
| 73 | * |
---|
| 74 | * loadOGG() |
---|
| 75 | * alSourcePause() |
---|
| 76 | * |
---|
| 77 | * Use the EAX functions ! |
---|
| 78 | * |
---|
| 79 | ******************************************************************************/ |
---|
| 80 | |
---|
| 81 | #ifndef __SOUNDMANAGER_H__ |
---|
| 82 | #define __SOUNDMANAGER_H__ |
---|
| 83 | |
---|
| 84 | #include <stdio.h> |
---|
| 85 | #include <string> |
---|
| 86 | |
---|
| 87 | // Comment this line if you don't have the EAX 2.0 SDK installed |
---|
| 88 | //#define _USEEAX |
---|
| 89 | |
---|
| 90 | #ifdef _USEEAX |
---|
| 91 | #include "eax.h" |
---|
| 92 | #endif |
---|
| 93 | |
---|
| 94 | // OpenAl version 1.0 |
---|
| 95 | // #include <al/alctypes.h> |
---|
| 96 | // #include <al/altypes.h> |
---|
| 97 | // #include <al/alc.h> |
---|
| 98 | // #include <al/al.h> |
---|
| 99 | // #include <alut/alut.h> |
---|
| 100 | // OpenAl version 1.1 |
---|
| 101 | #include <al.h> |
---|
| 102 | #include <alc.h> |
---|
| 103 | #include <alut.h> |
---|
| 104 | |
---|
| 105 | // Modify this as you need. |
---|
| 106 | #ifdef OGRE |
---|
| 107 | #include "OgreVector3.h" |
---|
| 108 | #include "OgreQuaternion.h" |
---|
| 109 | using namespace Ogre; |
---|
| 110 | #else |
---|
| 111 | #include "Vector3.h" |
---|
| 112 | using namespace gameengine::utilities; |
---|
| 113 | #endif |
---|
| 114 | |
---|
| 115 | // Be very careful with these two parameters |
---|
| 116 | // It is very dependant on the audio hardware your |
---|
| 117 | // user is using. It you get too large, it may work |
---|
| 118 | // on one persons system but not on another. |
---|
| 119 | // TODO Write a fct testing the hardware ! |
---|
| 120 | #define MAX_AUDIO_BUFFERS 64 |
---|
| 121 | #define MAX_AUDIO_SOURCES 16 |
---|
| 122 | |
---|
| 123 | // Used to store sound filenames |
---|
| 124 | #define MAX_FILENAME_LENGTH 40 |
---|
| 125 | |
---|
| 126 | class SoundManager |
---|
| 127 | { |
---|
| 128 | private: |
---|
| 129 | // EAX related |
---|
| 130 | bool isEAXPresent; |
---|
| 131 | #ifdef _USEEAX |
---|
| 132 | // EAX 2.0 GUIDs |
---|
| 133 | const GUID DSPROPSETID_EAX20_ListenerProperties |
---|
| 134 | = { 0x306a6a8, 0xb224, 0x11d2, { 0x99, 0xe5, 0x0, 0x0, 0xe8, 0xd8, 0xc7, 0x22 } }; |
---|
| 135 | |
---|
| 136 | const GUID DSPROPSETID_EAX20_BufferProperties |
---|
| 137 | = { 0x306a6a7, 0xb224, 0x11d2, {0x99, 0xe5, 0x0, 0x0, 0xe8, 0xd8, 0xc7, 0x22 } }; |
---|
| 138 | |
---|
| 139 | EAXSet eaxSet; // EAXSet function, retrieved if EAX Extension is supported |
---|
| 140 | EAXGet eaxGet; // EAXGet function, retrieved if EAX Extension is supported |
---|
| 141 | #endif // _USEEAX |
---|
| 142 | |
---|
| 143 | bool isInitialised; |
---|
| 144 | ALCdevice* mSoundDevice; |
---|
| 145 | ALCcontext* mSoundContext; |
---|
| 146 | |
---|
| 147 | std::string mAudioPath; |
---|
| 148 | |
---|
| 149 | bool isSoundOn; |
---|
| 150 | |
---|
| 151 | ALfloat position[3]; |
---|
| 152 | ALfloat velocity[3]; |
---|
| 153 | ALfloat orientation[6]; |
---|
| 154 | |
---|
| 155 | // Needed because of hardware limitation |
---|
| 156 | // Audio sources |
---|
| 157 | unsigned int mAudioSourcesInUseCount; |
---|
| 158 | unsigned int mAudioSources[ MAX_AUDIO_SOURCES ]; |
---|
| 159 | bool mAudioSourceInUse[ MAX_AUDIO_SOURCES ]; |
---|
| 160 | |
---|
| 161 | // Audio buffers |
---|
| 162 | unsigned int mAudioBuffersInUseCount; |
---|
| 163 | unsigned int mAudioBuffers[ MAX_AUDIO_BUFFERS ]; |
---|
| 164 | bool mAudioBufferInUse[ MAX_AUDIO_BUFFERS ]; |
---|
| 165 | char mAudioBufferFileName[ MAX_AUDIO_BUFFERS ][ MAX_FILENAME_LENGTH ]; |
---|
| 166 | |
---|
| 167 | // Function to check if the soundFile is already loaded into a buffer |
---|
| 168 | int locateAudioBuffer( std::string filename ); |
---|
| 169 | int loadAudioInToSystem( std::string filename ); |
---|
| 170 | bool loadWAV( std::string filename, ALuint pDestAudioBuffer ); |
---|
| 171 | bool loadOGG( std::string filename, ALuint pDestAudioBuffer ); |
---|
| 172 | // TODO bool loadAU( std::string filename, ALuint pDestAudioBuffer ); |
---|
| 173 | |
---|
| 174 | // Ogg Vorbis extension |
---|
| 175 | bool bOggExtensionPresent; |
---|
| 176 | |
---|
| 177 | public: |
---|
| 178 | static SoundManager* mSoundManager; |
---|
| 179 | |
---|
| 180 | SoundManager( void ); |
---|
| 181 | virtual ~SoundManager( void ); |
---|
| 182 | void SoundManager::selfDestruct( void ); |
---|
| 183 | static SoundManager* createManager( void ); |
---|
| 184 | static SoundManager* getSingletonPtr( void ) { return mSoundManager; }; |
---|
| 185 | |
---|
| 186 | bool init( void ); |
---|
| 187 | bool getIsSoundOn( void ) { return isSoundOn; }; |
---|
| 188 | void setAudioPath( char* path ) { mAudioPath = std::string( path ); }; |
---|
| 189 | |
---|
| 190 | bool checkALError( void ); |
---|
| 191 | bool checkALError( std::string pMsg ); |
---|
| 192 | |
---|
| 193 | /** See http://www.openal.org/windows_enumeration.html for installing other |
---|
| 194 | * devices. You should at least have "Generic Hardware". |
---|
| 195 | */ |
---|
| 196 | std::string listAvailableDevices( void ); |
---|
| 197 | |
---|
| 198 | // careful : testSound should not be used it doesn't use the resource limitation |
---|
| 199 | void testSound( const char* wavFile ); |
---|
| 200 | |
---|
| 201 | // Aquire an Audio Source |
---|
| 202 | // filename = pass in the sound file to play for this source (ex. "myfile.wav") |
---|
| 203 | // audioId = returns the AudioSource identifier you will need for the PlayAudioSource(); |
---|
| 204 | bool loadAudio( std::string filename, unsigned int *audioId, bool loop ); |
---|
| 205 | bool releaseAudio( unsigned int audioID ); |
---|
| 206 | |
---|
| 207 | // deprecated |
---|
| 208 | bool aquireAudioSource( char* file, unsigned int *audioId ) |
---|
| 209 | { return loadAudio( std::string(file), audioId, false ); }; |
---|
| 210 | |
---|
| 211 | // Returns true if the audio is started from the beginning |
---|
| 212 | // false if error or if already playing |
---|
| 213 | bool playAudio( unsigned int audioId, bool forceRestart ); |
---|
| 214 | bool stopAudio( unsigned int audioID ); |
---|
| 215 | bool stopAllAudio( void ); |
---|
| 216 | |
---|
| 217 | bool pauseAudio( unsigned int audioID ); |
---|
| 218 | bool pauseAllAudio( void ); |
---|
| 219 | bool resumeAudio( unsigned int audioID ); |
---|
| 220 | bool resumeAllAudio( void ); |
---|
| 221 | |
---|
| 222 | bool setSoundPosition( unsigned int audioID, Vector3 position ); |
---|
| 223 | |
---|
| 224 | bool setSoundPosition( unsigned int audioID, Vector3 position, |
---|
| 225 | Vector3 velocity, Vector3 direction ); |
---|
| 226 | |
---|
| 227 | bool setSound( unsigned int audioID, Vector3 position, |
---|
| 228 | Vector3 velocity, Vector3 direction, float maxDistance, |
---|
| 229 | bool playNow, bool forceRestart, float minGain ); |
---|
| 230 | |
---|
| 231 | bool setListenerPosition( Vector3 position, Vector3 velocity, |
---|
| 232 | Quaternion orientation ); |
---|
| 233 | |
---|
| 234 | bool isOggExtensionPresent( void ); |
---|
| 235 | |
---|
| 236 | |
---|
| 237 | /** |
---|
| 238 | * Preload audio files into the system. |
---|
| 239 | * Not obligatory, the files can also be loaded on the fly. |
---|
| 240 | */ |
---|
| 241 | bool loadDefaultSounds( std::string filename ); |
---|
| 242 | // Function to trim the trailing crap from a string. |
---|
| 243 | void trimTrailingSpace( char *s ); |
---|
| 244 | |
---|
| 245 | }; |
---|
| 246 | |
---|
| 247 | #endif /*__SOUNDMANAGER_H__*/ |
---|