| 1 | /*! |
|---|
| 2 | * @file sound_engine.h |
|---|
| 3 | * Definition of the SoundEngine singleton Class |
|---|
| 4 | */ |
|---|
| 5 | |
|---|
| 6 | #ifndef _SOUND_ENGINE_H |
|---|
| 7 | #define _SOUND_ENGINE_H |
|---|
| 8 | |
|---|
| 9 | #include "base_object.h" |
|---|
| 10 | #include "alincl.h" |
|---|
| 11 | |
|---|
| 12 | #include "sound_buffer.h" |
|---|
| 13 | #include "sound_source.h" |
|---|
| 14 | |
|---|
| 15 | #include <list> |
|---|
| 16 | #include <stack> |
|---|
| 17 | #include "threading.h" |
|---|
| 18 | |
|---|
| 19 | #define SOUND_DOPPLER_FACTOR 0.001 //!< A factor for the audible doppler effect |
|---|
| 20 | #define SOUND_DOPPLER_VELOCITY 500 //!< A factor for the TravelSpeed of sound |
|---|
| 21 | |
|---|
| 22 | // FORWARD DECLARATION |
|---|
| 23 | class PNode; |
|---|
| 24 | class IniParser; |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | //! A class that handles audio via the openAudioLibrary |
|---|
| 28 | class SoundEngine : public BaseObject { |
|---|
| 29 | public: |
|---|
| 30 | virtual ~SoundEngine(); |
|---|
| 31 | /** @returns a Pointer to the only object of this Class */ |
|---|
| 32 | inline static SoundEngine* getInstance() { if (!SoundEngine::singletonRef) SoundEngine::singletonRef = new SoundEngine(); return SoundEngine::singletonRef; }; |
|---|
| 33 | |
|---|
| 34 | void loadSettings(); |
|---|
| 35 | |
|---|
| 36 | SoundSource* createSource(const std::string& fileName, PNode* sourceNode = NULL); |
|---|
| 37 | |
|---|
| 38 | /** @param listener the listener in the scene */ |
|---|
| 39 | void setListener(PNode* listener) { this->listener = listener; }; |
|---|
| 40 | void setDopplerValues(ALfloat dopplerFactor, ALfloat dopplerVelocity); |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | /** @returns the Music Volume in % */ |
|---|
| 44 | inline float getMusicVolume() { return this->musicVolume; }; |
|---|
| 45 | /** @returns the Effects Volume in % */ |
|---|
| 46 | inline float getEffectsVolume() { return this->effectsVolume; }; |
|---|
| 47 | |
|---|
| 48 | void update(); |
|---|
| 49 | |
|---|
| 50 | // administrative |
|---|
| 51 | void popALSource(ALuint& source); |
|---|
| 52 | void pushALSource(ALuint& source); |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | bool initAudio(); |
|---|
| 56 | bool allocateSources(unsigned int count); |
|---|
| 57 | |
|---|
| 58 | // error handling: |
|---|
| 59 | static bool checkError(const std::string& error, unsigned int line); |
|---|
| 60 | bool checkALCError(const std::string& error, unsigned int line); |
|---|
| 61 | static const char* getALErrorString(ALenum err); |
|---|
| 62 | static const char* getALCErrorString(ALenum err); |
|---|
| 63 | |
|---|
| 64 | private: |
|---|
| 65 | SoundEngine(); |
|---|
| 66 | |
|---|
| 67 | void listDevices(); |
|---|
| 68 | |
|---|
| 69 | private: |
|---|
| 70 | static SoundEngine* singletonRef; //!< Reference to this class |
|---|
| 71 | |
|---|
| 72 | ALCdevice* device; //!< the used audio-device. |
|---|
| 73 | ALCcontext* context; //!< the context, currently in use. |
|---|
| 74 | |
|---|
| 75 | float musicVolume; //!< the maximum volume of the music in % (0f,1f] |
|---|
| 76 | float effectsVolume; //!< the maximum volume of sound-effects in % (0f,1f] |
|---|
| 77 | PNode* listener; //!< The listener of the Scene |
|---|
| 78 | |
|---|
| 79 | const std::list<BaseObject*>* bufferList; //!< A list of buffers |
|---|
| 80 | const std::list<BaseObject*>* sourceList; //!< A list for all the sources in the scene. |
|---|
| 81 | |
|---|
| 82 | unsigned int maxSourceCount; //!< How many Sources is the Maximum |
|---|
| 83 | std::stack<ALuint> ALSources; //!< A list of real openAL-Sources, the engine allocates, and stores for reuse. |
|---|
| 84 | |
|---|
| 85 | SDL_mutex* sourceMutex; //!< A mutex so we can not harm the stack |
|---|
| 86 | }; |
|---|
| 87 | |
|---|
| 88 | #endif /* _SOUND_ENGINE_H */ |
|---|