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() { 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 | /** |
---|
40 | * @todo ability to play back different SoundBuffers on the same SounSource |
---|
41 | */ |
---|
42 | class SoundSource : public BaseObject |
---|
43 | { |
---|
44 | public: |
---|
45 | SoundSource(SoundBuffer* buffer, PNode* sourceNode = NULL); |
---|
46 | ~SoundSource(); |
---|
47 | |
---|
48 | // user interaction |
---|
49 | void play(); |
---|
50 | void stop(); |
---|
51 | void pause(); |
---|
52 | void rewind(); |
---|
53 | |
---|
54 | // development functions |
---|
55 | /** @returns The ID of this Source */ |
---|
56 | inline ALuint getID() const { return this->sourceID; } |
---|
57 | /** @returns the SoundBuffer of this Source */ |
---|
58 | inline SoundBuffer* getBuffer() const { return this->buffer; } |
---|
59 | /** @returns the SourceNode of this Source */ |
---|
60 | inline PNode* getNode() const { return this->sourceNode;} |
---|
61 | |
---|
62 | void setRolloffFactor(ALfloat rolloffFactor); |
---|
63 | |
---|
64 | private: |
---|
65 | ALuint sourceID; //!< The ID of the Source |
---|
66 | SoundBuffer* buffer; //!< The buffer to play in this source. |
---|
67 | PNode* sourceNode; //!< The SourceNode represente the position/velocity... of this source. |
---|
68 | }; |
---|
69 | |
---|
70 | |
---|
71 | |
---|
72 | //! A class that handles audio via the openAudioLibrary |
---|
73 | class SoundEngine : public BaseObject { |
---|
74 | |
---|
75 | public: |
---|
76 | virtual ~SoundEngine(); |
---|
77 | /** @returns a Pointer to the only object of this Class */ |
---|
78 | inline static SoundEngine* getInstance() { if (!singletonRef) singletonRef = new SoundEngine(); return singletonRef; }; |
---|
79 | |
---|
80 | SoundSource* createSource(const char* fileName, PNode* sourceNode = NULL); |
---|
81 | |
---|
82 | void setListener(PNode* listener); |
---|
83 | void setDopplerValues(ALfloat dopplerFactor, ALfloat dopplerVelocity); |
---|
84 | |
---|
85 | |
---|
86 | void addBuffer(SoundBuffer* buffer); |
---|
87 | void removeBuffer(SoundBuffer* buffer); |
---|
88 | void addSource(SoundSource* source); |
---|
89 | void removeSource(SoundSource* source); |
---|
90 | |
---|
91 | void update(); |
---|
92 | |
---|
93 | // administrative |
---|
94 | void flushUnusedBuffers(); |
---|
95 | void flushAllBuffers(); |
---|
96 | void flushAllSources(); |
---|
97 | bool initAudio(); |
---|
98 | |
---|
99 | // error handling: |
---|
100 | static void PrintALErrorString(ALenum err); |
---|
101 | // static void PrintALCErrorString(ALenum err); |
---|
102 | |
---|
103 | |
---|
104 | private: |
---|
105 | SoundEngine(); |
---|
106 | static SoundEngine* singletonRef; //!< Reference to this class |
---|
107 | |
---|
108 | |
---|
109 | PNode* listener; //!< The listener of the Scene |
---|
110 | tList<SoundBuffer>* bufferList; //!< A list of buffers |
---|
111 | tList<SoundSource>* sourceList; //!< A list for all the sources in the scene. |
---|
112 | |
---|
113 | }; |
---|
114 | |
---|
115 | #endif /* _SOUND_ENGINE_H */ |
---|