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 | |
---|
13 | #define SOUND_DOPPLER_FACTOR 0.001 //!< A factor for the audible doppler effect |
---|
14 | #define SOUND_DOPPLER_VELOCITY 5000000 //!< A factor for the TravelSpeed of sound |
---|
15 | |
---|
16 | // FORWARD DEFINITION |
---|
17 | class PNode; |
---|
18 | template<class T> class tList; |
---|
19 | |
---|
20 | |
---|
21 | //! A class that represents a datastructure to play Sounds. |
---|
22 | class SoundBuffer : public BaseObject |
---|
23 | { |
---|
24 | public: |
---|
25 | SoundBuffer(const char* fileName); |
---|
26 | ~SoundBuffer(); |
---|
27 | |
---|
28 | /** @returns the ID of the buffer used in this SoundBuffer */ |
---|
29 | inline ALuint getID() const { return this->bufferID; } |
---|
30 | |
---|
31 | private: |
---|
32 | ALuint bufferID; //!< The address of the Buffer. |
---|
33 | |
---|
34 | ALsizei size; //!< The size of the Buffer. |
---|
35 | ALboolean loop; //!< loop information. |
---|
36 | }; |
---|
37 | |
---|
38 | //! A class that represents a SoundSource |
---|
39 | class SoundSource : public BaseObject |
---|
40 | { |
---|
41 | public: |
---|
42 | SoundSource(const PNode* sourceNode = NULL, const SoundBuffer* buffer = NULL); |
---|
43 | ~SoundSource(); |
---|
44 | |
---|
45 | // user interaction |
---|
46 | void play(); |
---|
47 | void play(const SoundBuffer* buffer); |
---|
48 | void stop(); |
---|
49 | void pause(); |
---|
50 | void rewind(); |
---|
51 | |
---|
52 | // development functions |
---|
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;} |
---|
59 | |
---|
60 | void setRolloffFactor(ALfloat rolloffFactor); |
---|
61 | |
---|
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. |
---|
66 | }; |
---|
67 | |
---|
68 | |
---|
69 | |
---|
70 | //! A class that handles audio via the openAudioLibrary |
---|
71 | class SoundEngine : public BaseObject { |
---|
72 | |
---|
73 | public: |
---|
74 | virtual ~SoundEngine(); |
---|
75 | /** @returns a Pointer to the only object of this Class */ |
---|
76 | inline static SoundEngine* getInstance() { if (!singletonRef) singletonRef = new SoundEngine(); return singletonRef; }; |
---|
77 | |
---|
78 | SoundSource* createSource(const char* fileName, PNode* sourceNode = NULL); |
---|
79 | |
---|
80 | void setListener(PNode* listener); |
---|
81 | void setDopplerValues(ALfloat dopplerFactor, ALfloat dopplerVelocity); |
---|
82 | |
---|
83 | |
---|
84 | void addBuffer(SoundBuffer* buffer); |
---|
85 | void removeBuffer(SoundBuffer* buffer); |
---|
86 | void addSource(SoundSource* source); |
---|
87 | void removeSource(SoundSource* source); |
---|
88 | |
---|
89 | void update(); |
---|
90 | |
---|
91 | // administrative |
---|
92 | void flushUnusedBuffers(); |
---|
93 | void flushAllBuffers(); |
---|
94 | void flushAllSources(); |
---|
95 | bool initAudio(); |
---|
96 | |
---|
97 | // error handling: |
---|
98 | static void PrintALErrorString(ALenum err); |
---|
99 | // static void PrintALCErrorString(ALenum err); |
---|
100 | |
---|
101 | |
---|
102 | private: |
---|
103 | SoundEngine(); |
---|
104 | static SoundEngine* singletonRef; //!< Reference to this class |
---|
105 | |
---|
106 | |
---|
107 | PNode* listener; //!< The listener of the Scene |
---|
108 | tList<SoundBuffer>* bufferList; //!< A list of buffers |
---|
109 | tList<SoundSource>* sourceList; //!< A list for all the sources in the scene. |
---|
110 | |
---|
111 | }; |
---|
112 | |
---|
113 | #endif /* _SOUND_ENGINE_H */ |
---|