[4597] | 1 | /*! |
---|
[4959] | 2 | * @file sound_engine.h |
---|
| 3 | * Definition of the SoundEngine singleton Class |
---|
| 4 | */ |
---|
[4504] | 5 | |
---|
| 6 | #ifndef _SOUND_ENGINE_H |
---|
| 7 | #define _SOUND_ENGINE_H |
---|
| 8 | |
---|
| 9 | #include "base_object.h" |
---|
| 10 | #include "alincl.h" |
---|
| 11 | |
---|
[4506] | 12 | #define SOUND_DOPPLER_FACTOR 0.001 //!< A factor for the audible doppler effect |
---|
| 13 | #define SOUND_DOPPLER_VELOCITY 5000000 //!< A factor for the TravelSpeed of sound |
---|
[4504] | 14 | |
---|
| 15 | // FORWARD DEFINITION |
---|
| 16 | class PNode; |
---|
| 17 | template<class T> class tList; |
---|
[4985] | 18 | class IniParser; |
---|
[4504] | 19 | |
---|
| 20 | |
---|
| 21 | //! A class that represents a datastructure to play Sounds. |
---|
[4597] | 22 | class SoundBuffer : public BaseObject |
---|
[4504] | 23 | { |
---|
[4959] | 24 | public: |
---|
| 25 | SoundBuffer(const char* fileName); |
---|
| 26 | ~SoundBuffer(); |
---|
[4504] | 27 | |
---|
[4959] | 28 | /** @returns the ID of the buffer used in this SoundBuffer */ |
---|
| 29 | inline ALuint getID() const { return this->bufferID; } |
---|
[4504] | 30 | |
---|
[4959] | 31 | private: |
---|
| 32 | ALuint bufferID; //!< The address of the Buffer. |
---|
[4504] | 33 | |
---|
[4959] | 34 | ALsizei size; //!< The size of the Buffer. |
---|
| 35 | ALboolean loop; //!< loop information. |
---|
[4504] | 36 | }; |
---|
| 37 | |
---|
| 38 | //! A class that represents a SoundSource |
---|
[4761] | 39 | class SoundSource : public BaseObject |
---|
[4504] | 40 | { |
---|
[4959] | 41 | public: |
---|
| 42 | SoundSource(const PNode* sourceNode = NULL, const SoundBuffer* buffer = NULL); |
---|
| 43 | ~SoundSource(); |
---|
[4597] | 44 | |
---|
[4504] | 45 | // user interaction |
---|
[4959] | 46 | void play(); |
---|
| 47 | void play(const SoundBuffer* buffer); |
---|
| 48 | void stop(); |
---|
| 49 | void pause(); |
---|
| 50 | void rewind(); |
---|
[4597] | 51 | |
---|
[4504] | 52 | // development functions |
---|
[4959] | 53 | /** @returns The ID of this Source */ |
---|
| 54 | inline ALuint getID() const { return this->sourceID; } |
---|
| 55 | /** @returns the SoundBuffer of this Source */ |
---|
| 56 | inline const SoundBuffer* getBuffer() const { return this->buffer; } |
---|
| 57 | /** @returns the SourceNode of this Source */ |
---|
| 58 | inline const PNode* getNode() const { return this->sourceNode;} |
---|
[4504] | 59 | |
---|
[4959] | 60 | void setRolloffFactor(ALfloat rolloffFactor); |
---|
[4504] | 61 | |
---|
[4959] | 62 | private: |
---|
| 63 | ALuint sourceID; //!< The ID of the Source |
---|
| 64 | const SoundBuffer* buffer; //!< The buffer to play in this source. |
---|
| 65 | const PNode* sourceNode; //!< The SourceNode represente the position/velocity... of this source. |
---|
[4504] | 66 | }; |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | //! A class that handles audio via the openAudioLibrary |
---|
| 71 | class SoundEngine : public BaseObject { |
---|
| 72 | |
---|
[4959] | 73 | public: |
---|
| 74 | virtual ~SoundEngine(); |
---|
| 75 | /** @returns a Pointer to the only object of this Class */ |
---|
[5216] | 76 | inline static SoundEngine* getInstance() { if (!SoundEngine::singletonRef) SoundEngine::singletonRef = new SoundEngine(); return SoundEngine::singletonRef; }; |
---|
[4504] | 77 | |
---|
[4985] | 78 | void loadSettings(IniParser* iniParser); |
---|
| 79 | |
---|
[4959] | 80 | SoundSource* createSource(const char* fileName, PNode* sourceNode = NULL); |
---|
[4504] | 81 | |
---|
[4961] | 82 | /** @param listener the listener in the scene */ |
---|
| 83 | void setListener(PNode* listener) { this->listener = listener; }; |
---|
[4959] | 84 | void setDopplerValues(ALfloat dopplerFactor, ALfloat dopplerVelocity); |
---|
[4504] | 85 | |
---|
| 86 | |
---|
[4985] | 87 | /** @returns the Music Volume in % */ |
---|
| 88 | inline float getMusicVolume() { return this->musicVolume; }; |
---|
| 89 | /** @returns the Effects Volume in % */ |
---|
| 90 | inline float getEffectsVolume() { return this->effectsVolume; }; |
---|
| 91 | |
---|
[4961] | 92 | void update(); |
---|
| 93 | |
---|
| 94 | // administrative |
---|
[4959] | 95 | void addBuffer(SoundBuffer* buffer); |
---|
| 96 | void removeBuffer(SoundBuffer* buffer); |
---|
| 97 | void addSource(SoundSource* source); |
---|
[4504] | 98 | |
---|
[4959] | 99 | void flushUnusedBuffers(); |
---|
| 100 | void flushAllBuffers(); |
---|
| 101 | void flushAllSources(); |
---|
| 102 | bool initAudio(); |
---|
[4504] | 103 | |
---|
| 104 | // error handling: |
---|
[4959] | 105 | static void PrintALErrorString(ALenum err); |
---|
[4504] | 106 | // static void PrintALCErrorString(ALenum err); |
---|
| 107 | |
---|
| 108 | |
---|
[4959] | 109 | private: |
---|
| 110 | SoundEngine(); |
---|
| 111 | void listDevices(); |
---|
[4504] | 112 | |
---|
[4959] | 113 | private: |
---|
| 114 | static SoundEngine* singletonRef; //!< Reference to this class |
---|
[4504] | 115 | |
---|
[4985] | 116 | float musicVolume; //!< the maximum volume of the music in % (0f,1f] |
---|
| 117 | float effectsVolume; //!< the maximum volume of sound-effects in % (0f,1f] |
---|
[4959] | 118 | PNode* listener; //!< The listener of the Scene |
---|
[4960] | 119 | tList<BaseObject>* bufferList; //!< A list of buffers |
---|
| 120 | tList<BaseObject>* sourceList; //!< A list for all the sources in the scene. |
---|
[4959] | 121 | |
---|
[4504] | 122 | }; |
---|
| 123 | |
---|
| 124 | #endif /* _SOUND_ENGINE_H */ |
---|