2 #include "cAudioSource.h" 28 unsigned errorcount = 0;
29 while (totalread < totalSize)
31 const int actualread = decoder->
readAudioData(&data[totalread], totalSize - totalread);
33 totalread += actualread;
34 }
else if (actualread < 0) {
36 getLogger()->
logDebug(
"Audio Buffer",
"Decoder returned an error: %i (%i of 3)", actualread, errorcount);
44 if (totalread != totalSize) {
48 alGenBuffers(1, &Buffer);
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);
63 alDeleteBuffers(1, &Buffer);
67 bool isValid()
const {
return Valid; }
68 unsigned getBuffer()
const {
return Buffer; }
70 int getChannels()
const 73 alGetBufferi(Buffer, AL_CHANNELS, &channels);
77 int getTotalAudioSize()
const 80 alGetBufferi(Buffer, AL_SIZE, &size);
84 float getTotalAudioTime()
const {
return TotalTime; }
117 alSourcei(Source, AL_BUFFER, Buffer->getBuffer());
130 alSourcePlay(Source);
133 signalEvent(ON_PLAY);
134 oldState = AL_PLAYING;
140 alSourcei(Source, AL_SOURCE_RELATIVE,
true);
141 alSourcei(Source, AL_LOOPING, toLoop);
148 alSourcei(Source, AL_SOURCE_RELATIVE,
false);
149 alSourcei(Source, AL_LOOPING, toLoop);
151 setPosition(position);
152 setStrength(soundstr);
158 alSourcePause(Source);
161 signalEvent(ON_PAUSE);
162 oldState = AL_PAUSED;
167 alSourceStop(Source);
168 alSourceRewind(Source);
171 signalEvent(ON_STOP);
172 oldState = AL_STOPPED;
177 alSourcei(Source, AL_LOOPING, toLoop);
181 bool seek(
const float& seconds,
bool relative)
183 float start = relative ? getCurrentAudioTime() : 0.f;
184 alSourcef(Source, AL_SEC_OFFSET, start + seconds);
197 alGetSourcef(Source, AL_SEC_OFFSET, &time);
204 alGetSourcei(Source, AL_BYTE_OFFSET, &offset);
212 if(isValid() || isPlaying()) {
213 signalEvent(ON_UPDATE);
217 alGetSourcei(Source, AL_SOURCE_STATE, &state);
219 if(state == AL_STOPPED && oldState != state)
222 signalEvent(ON_STOP);
225 return state != AL_STOPPED;
230 return Buffer ? Buffer->isValid() :
false;
235 ALint looping =
false;
236 alGetSourcei(Source, AL_LOOPING, &looping);
237 return looping == AL_TRUE;
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() {}
virtual void logError(const char *sender, const char *msg,...)=0
Used to log an error message to the logging system.
ALenum convertAudioFormatEnum(AudioFormats format)
Converts our audio format enum to OpenAL's.
bool setBuffer(IAudioBuffer *buffer)
Change the audio buffer associated with the source.
bool play3d(const cVector3 &position, const float &soundstr, const bool &toLoop)
Plays the source in 3D mode.
bool play2d(const bool &toLoop)
Plays the source in 2D mode.
virtual bool setPosition(int position, bool relative)=0
Sets the position in the stream to read from.
bool seek(const float &seconds, bool relative)
Seeks through the audio stream to a specific spot.
float getTotalAudioTime()
Returns the total amount of time in the audio stream. See IAudioDecoder for details.
int getCurrentAudioPosition()
Returns the current position in the decoded audio stream in bytes. See IAudioDecoder for details...
IAudioBuffer * getBuffer()
Get the audio buffer associated with the source.
virtual void logDebug(const char *sender, const char *msg,...)=0
Used to log a debug message to the logging system.
virtual int getTotalSize()=0
If available, returns the total decoded size of the audio stream. Returns a negative number if this c...
int getCurrentCompressedAudioPosition()
Returns the current position in the original audio stream in bytes. See IAudioDecoder for details...
void loop(const bool &toLoop)
Controls whether the source should loop or not.
virtual void grab()
Increments the reference count by one.
virtual bool drop()
Decrements the reference count by one. If it hits zero, this object is deleted.
bool isLooping() const
Returns if the source is looping.
virtual float getTotalTime()=0
If seeking is supported, will return the length of the audio steam in seconds. Returns a negative num...
void stop()
Stops playback of the sound source.
bool play()
Plays the source with the last set parameters.
Class for manipulating vectors in 3D space.
int getTotalAudioSize()
Returns the total decoded size of the audio stream. See IAudioDecoder for details.
int getCompressedAudioSize()
Returns the original size of the audio stream. See IAudioDecoder for details.
virtual int getFrequency()=0
Returns the frequency (sample rate) of the audio data.
void pause()
Pauses playback of the sound source.
virtual AudioFormats getFormat()=0
Returns the format of the audio data.
interface for a sample (audio buffer): completely loaded into memory, shareable across sources ...
bool isValid() const
Returns if the source is ready to be used.
virtual int readAudioData(void *output, int amount)=0
Reads a section of data out of the audio stream.
CAUDIO_API ILogger * getLogger()
Gets the interface to the logger.
float getCurrentAudioTime()
Returns the current position in the audio stream in seconds. See IAudioDecoder for details...
Interface for all Audio Decoders in cAudio.
Main namespace for the entire cAudio library.
bool update()
Normally called every frame by the audio manager to update the internal buffers. Note: For internal u...