cAudio 2.3.0
3d Audio Engine
Loading...
Searching...
No Matches
cAudioStaticSource.h
1
2#include "cAudioSource.h"
3
4namespace cAudio
5{
7 {
8 ALuint Buffer;
9 bool Valid;
10 float TotalTime;
11
12 public:
13
15 {
16 Buffer = 0;
17 Valid = false;
18 TotalTime = decoder->getTotalTime();
19
20 decoder->setPosition(0, false);
21
22 const int totalSize = decoder->getTotalSize()&~1;
23 if (totalSize > 0)
24 {
25 cAudioVector<char>::Type data(totalSize, 0);
26
27 int totalread = 0;
28 unsigned errorcount = 0;
29 while (totalread < totalSize)
30 {
31 const int actualread = decoder->readAudioData(&data[totalread], totalSize - totalread);
32 if (actualread > 0) {
33 totalread += actualread;
34 } else if (actualread < 0) {
35 ++errorcount;
36 getLogger()->logDebug("Audio Buffer", "Decoder returned an error: %i (%i of 3)", actualread, errorcount);
37 if(errorcount >= 3) {
38 break;
39 }
40 } else {
41 break;
42 }
43 }
44 if (totalread != totalSize) {
45 getLogger()->logError("Audio Buffer", "Stopped short");
46 }
47
48 alGenBuffers(1, &Buffer);
49 checkALError();
50 const ALsizei freq = decoder->getFrequency();
51 const ALenum format = convertAudioFormatEnum(decoder->getFormat());
52 getLogger()->logDebug("Audio Buffer", "Buffered %d bytes of data into buffer %d at %d hz.", totalread, (int)Buffer, (int)freq);
53 alBufferData(Buffer, format, &data[0], data.size(), freq);
54 checkALError();
55 Valid = true;
56 }
57 }
58
60 {
61 if (Valid)
62 {
63 alDeleteBuffers(1, &Buffer);
64 }
65 }
66
67 bool isValid() const { return Valid; }
68 unsigned getBuffer() const { return Buffer; }
69
70 int getChannels() const
71 {
72 ALint channels = 0;
73 alGetBufferi(Buffer, AL_CHANNELS, &channels);
74 return channels;
75 }
76
77 int getTotalAudioSize() const
78 {
79 ALint size = -1;
80 alGetBufferi(Buffer, AL_SIZE, &size);
81 return size;
82 }
83
84 float getTotalAudioTime() const { return TotalTime; }
85 };
86
87
89 {
90 IAudioBuffer* Buffer;
91
92 public:
94 : cAudioSourceBase(context), Buffer(NULL)
95 {
96 setBuffer(buffer);
97 }
98
100 {
101 if (Buffer)
102 Buffer->drop();
103 }
104
106 {
107 if (isPlaying())
108 stop();
109
110 if (Buffer)
111 Buffer->drop();
112
113 Buffer = buffer;
114
115 if (Buffer) {
116 Buffer->grab();
117 alSourcei(Source, AL_BUFFER, Buffer->getBuffer());
118 checkALError();
119 }
120 return true;
121 }
122
124 {
125 return Buffer;
126 }
127
128 bool play()
129 {
130 alSourcePlay(Source);
131 checkALError();
132 getLogger()->logDebug("Audio Static Source", "Source playing.");
133 signalEvent(ON_PLAY);
134 oldState = AL_PLAYING;
135 return true;
136 }
137
138 bool play2d(const bool& toLoop)
139 {
140 alSourcei(Source, AL_SOURCE_RELATIVE, true);
141 alSourcei(Source, AL_LOOPING, toLoop);
142 checkALError();
143 return play();
144 }
145
146 bool play3d(const cVector3& position, const float& soundstr, const bool& toLoop)
147 {
148 alSourcei(Source, AL_SOURCE_RELATIVE, false);
149 alSourcei(Source, AL_LOOPING, toLoop);
150 checkALError();
151 setPosition(position);
152 setStrength(soundstr);
153 return play();
154 }
155
156 void pause()
157 {
158 alSourcePause(Source);
159 checkALError();
160 getLogger()->logDebug("Audio Static Source", "Source paused.");
161 signalEvent(ON_PAUSE);
162 oldState = AL_PAUSED;
163 }
164
165 void stop()
166 {
167 alSourceStop(Source);
168 alSourceRewind(Source);
169 checkALError();
170 getLogger()->logDebug("Audio Static Source", "Source stopped.");
171 signalEvent(ON_STOP);
172 oldState = AL_STOPPED;
173 }
174
175 void loop(const bool& toLoop)
176 {
177 alSourcei(Source, AL_LOOPING, toLoop);
178 checkALError();
179 }
180
181 bool seek(const float& seconds, bool relative)
182 {
183 float start = relative ? getCurrentAudioTime() : 0.f;
184 alSourcef(Source, AL_SEC_OFFSET, start + seconds);
185 checkALError();
186 return true;
187 }
188
189 float getTotalAudioTime() { return Buffer ? Buffer->getTotalAudioTime() : 0.f; }
190 int getTotalAudioSize() { return Buffer ? Buffer->getTotalAudioSize() : 0; }
191
192 int getCompressedAudioSize() { return -1; }
193
195 {
196 float time = -1;
197 alGetSourcef(Source, AL_SEC_OFFSET, &time);
198 return time;
199 }
200
202 {
203 int offset = -1;
204 alGetSourcei(Source, AL_BYTE_OFFSET, &offset);
205 return offset;
206 }
207
209
210 bool update()
211 {
212 if(isValid() || isPlaying()) {
213 signalEvent(ON_UPDATE);
214 }
215
216 ALenum state;
217 alGetSourcei(Source, AL_SOURCE_STATE, &state);
218 checkALError();
219 if(state == AL_STOPPED && oldState != state)
220 {
221 getLogger()->logDebug("Audio Static Source", "Source stopped.");
222 signalEvent(ON_STOP);
223 oldState = state;
224 }
225 return state != AL_STOPPED;
226 }
227
228 bool isValid() const
229 {
230 return Buffer ? Buffer->isValid() : false;
231 }
232
233 bool isLooping() const
234 {
235 ALint looping = false;
236 alGetSourcei(Source, AL_LOOPING, &looping);
237 return looping == AL_TRUE;
238 }
239
240#if CAUDIO_EFX_ENABLED == 1
241 virtual unsigned int getNumEffectSlotsAvailable() const { return 0; }
242 virtual bool attachEffect(unsigned int slot, IEffect* effect) { return false; }
243 virtual void removeEffect(unsigned int slot) {}
244 virtual bool attachFilter(IFilter* filter) { return false; }
245 virtual void removeFilter() {}
246#endif
247 };
248}
interface for a sample (audio buffer): completely loaded into memory, shareable across sources
Definition: IAudioSource.h:19
Interface for all Audio Decoders in cAudio.
Definition: IAudioDecoder.h:16
virtual float getTotalTime()=0
If seeking is supported, will return the length of the audio steam in seconds. Returns a negative num...
virtual int getFrequency()=0
Returns the frequency (sample rate) of the audio data.
virtual AudioFormats getFormat()=0
Returns the format of the audio data.
virtual bool setPosition(int position, bool relative)=0
Sets the position in the stream to read from.
virtual int readAudioData(void *output, int amount)=0
Reads a section of data out of the audio stream.
virtual int getTotalSize()=0
If available, returns the total decoded size of the audio stream. Returns a negative number if this c...
virtual void logDebug(const char *sender, const char *msg,...)=0
Used to log a debug message to the logging system.
virtual void logError(const char *sender, const char *msg,...)=0
Used to log an error message to the logging system.
virtual void grab()
Increments the reference count by one.
Definition: IRefCounted.h:19
virtual bool drop()
Decrements the reference count by one. If it hits zero, this object is deleted.
Definition: IRefCounted.h:25
virtual bool isPlaying() const
Returns if the source is playing.
void signalEvent(Events sevent)
Signals a event to all event handlers.
virtual void setStrength(const float &soundstrength)
Sets how well the source carries over distance.
virtual void setPosition(const cVector3 &position)
Sets the position of the source in 3D space.
ALuint Source
OpenAL source.
Definition: cAudioSource.h:108
float getCurrentAudioTime()
Returns the current position in the audio stream in seconds. See IAudioDecoder for details.
int getCurrentAudioPosition()
Returns the current position in the decoded audio stream in bytes. See IAudioDecoder for details.
bool play()
Plays the source with the last set parameters.
bool seek(const float &seconds, bool relative)
Seeks through the audio stream to a specific spot.
bool play2d(const bool &toLoop)
Plays the source in 2D mode.
bool play3d(const cVector3 &position, const float &soundstr, const bool &toLoop)
Plays the source in 3D mode.
IAudioBuffer * getBuffer()
Get the audio buffer associated with the source.
int getTotalAudioSize()
Returns the total decoded size of the audio stream. See IAudioDecoder for details.
bool isLooping() const
Returns if the source is looping.
void stop()
Stops playback of the sound source.
bool setBuffer(IAudioBuffer *buffer)
Change the audio buffer associated with the source.
bool update()
Normally called every frame by the audio manager to update the internal buffers. Note: For internal u...
int getCurrentCompressedAudioPosition()
Returns the current position in the original audio stream in bytes. See IAudioDecoder for details.
int getCompressedAudioSize()
Returns the original size of the audio stream. See IAudioDecoder for details.
float getTotalAudioTime()
Returns the total amount of time in the audio stream. See IAudioDecoder for details.
void loop(const bool &toLoop)
Controls whether the source should loop or not.
bool isValid() const
Returns if the source is ready to be used.
void pause()
Pauses playback of the sound source.
Class for manipulating vectors in 3D space.
Definition: cVector3.h:23
Main namespace for the entire cAudio library.
Definition: cAudioCapture.h:16
ALenum convertAudioFormatEnum(AudioFormats format)
Converts our audio format enum to OpenAL's.
Definition: cOpenALUtil.h:48
CAUDIO_API ILogger * getLogger()
Gets the interface to the logger.
Definition: cAudio.cpp:45