5 #include "cAudioManager.h" 7 #include "cAudioSource.h" 8 #include "cAudioStaticSource.h" 9 #include "cAudioPlatform.h" 10 #include "cAudioSleep.h" 14 #include "cPluginManager.h" 16 #include "cMemorySource.h" 17 #include "cRawAudioDecoderFactory.h" 20 #include "cOpenALDeviceContext.h" 24 cAudioManager::~cAudioManager()
38 if(!AudioContext->initialize(deviceName, outputFrequency, eaxEffectSlots))
41 #ifdef CAUDIO_USE_INTERNAL_THREAD 73 AudioContext->shutDown();
74 CAUDIO_DELETE AudioContext;
86 updateSources.clear();
90 size_t count = audioSources.size();
91 for(
size_t i=0; i<count; i++)
97 updateSources.push_back(source);
103 for (
int i=0; i != updateSources.size(); i++)
112 if (!managedAudioSources.empty())
114 size_t count = managedAudioSources.size();
115 for(
size_t i=0; i<count; i++)
123 managedAudioSourcesDelBuffer.push_back(source);
128 count = managedAudioSourcesDelBuffer.size();
129 for(
size_t i=0; i<count; i++)
133 if (it != managedAudioSources.end())
135 managedAudioSources.erase(it);
136 CAUDIO_DELETE source;
139 managedAudioSourcesDelBuffer.clear();
143 void cAudioManager::run()
145 if(!audioSources.empty())
153 return (AudioThread != NULL && AudioThread->isRunning());
156 #if CAUDIO_EFX_ENABLED == 1 157 IAudioEffects* cAudioManager::getEffects()
161 return AudioContext->getEffects();
175 if (!playLooped && !startPaused)
178 if (it != audioSources.end())
180 audioSources.erase(it);
182 managedAudioSources.push_back(pAudioSrc);
188 if (playLooped && !startPaused)
201 if (!playLooped && !startPaused)
204 if (it != audioSources.end())
206 audioSources.erase(it);
208 managedAudioSources.push_back(pAudioSrc);
210 pAudioSrc->
play3d(position);
214 if (playLooped && !startPaused)
216 pAudioSrc->
play3d(position, 1.0,
true);
226 size_t count = audioSources.size();
227 for(
size_t i=0; i<count; i++)
229 audioSources[i]->setVolume(audioSources[i]->getVolume());
240 alSpeedOfSound(speed);
246 return alGetFloat(AL_SPEED_OF_SOUND);
251 alDopplerFactor(factor);
257 return alGetFloat(AL_DOPPLER_FACTOR);
263 size_t count = audioSources.size();
264 for(
size_t i=0; i<count; i++)
266 if(audioSources[i]->isPlaying())
267 audioSources[i]->stop();
271 IAudioSource* cAudioManager::createAudioSource(
IAudioDecoder* decoder,
const cAudioString& audioName,
const cAudioString& dataSource)
278 #if CAUDIO_EFX_ENABLED == 1 279 IAudioSource* audio = CAUDIO_NEW
cAudioSource(decoder, AudioContext, ((cAudioEffects*)getEffects())->getEFXInterface());
287 if(!audioName.empty())
288 audioIndex[audioName] = audio;
290 audioSources.push_back(audio);
291 getLogger()->
logInfo(
"AudioManager",
"Audio Source (%s) created from Data Source %s.", toUTF8(audioName), toUTF8(dataSource));
295 getLogger()->
logError(
"AudioManager",
"Failed to create Audio Source (%s): Error creating audio source.", toUTF8(audioName));
299 getLogger()->
logError(
"AudioManager",
"Failed to create Audio Source (%s): Audio data could not be decoded by (.%s) decoder.",
300 toUTF8(audioName), toUTF8(decoder->
getType()));
308 if(!Initialized)
return NULL;
311 cAudioString audioName = fromUTF8(name);
312 cAudioString path = fromUTF8(filename);
313 cAudioString ext =
getExt(path);
317 getLogger()->
logError(
"AudioManager",
"Failed to create Audio Source (%s): No decoder could be found for (.%s).", toUTF8(audioName), toUTF8(ext));
321 for(
size_t i=0; i<dataSourcePriorityList.size(); ++i)
323 const cAudioString dataSourceName = dataSourcePriorityList[i].second;
328 if(source && source->
isValid())
333 IAudioSource* audio = createAudioSource(decoder, audioName, dataSourceName);
346 if(!Initialized)
return NULL;
349 cAudioString audioName = fromUTF8(name);
350 cAudioString ext = fromUTF8(extension);
354 getLogger()->
logError(
"AudioManager",
"Failed to create Audio Source (%s): Codec (.%s) is not supported.", toUTF8(audioName), toUTF8(ext));
359 if(source && source->
isValid())
364 IAudioSource* audio = createAudioSource(decoder, audioName, _CTEXT(
"cMemorySource"));
373 return createFromRaw(name, pBiffer->getWriteBuffer(), pBiffer->getLength(), frequency, format);
378 if(!Initialized)
return NULL;
381 cAudioString audioName = fromUTF8(name);
385 getLogger()->
logError(
"AudioManager",
"Failed to create Audio Source (%s): Codec (.raw) is not supported.", toUTF8(audioName));
390 if(source && source->
isValid())
395 IAudioSource* audio = createAudioSource(decoder, audioName, _CTEXT(
"cMemorySource"));
404 if(!Initialized)
return NULL;
407 cAudioString path = fromUTF8(filename);
408 cAudioString ext =
getExt(path);
412 getLogger()->
logError(
"AudioManager",
"Failed to create Audio Buffer: No decoder could be found for (.%s).", toUTF8(ext));
416 for(
size_t i=0; i<dataSourcePriorityList.size(); ++i)
418 const cAudioString dataSourceName = dataSourcePriorityList[i].second;
423 if(source && source->
isValid())
442 if(!Initialized)
return NULL;
452 cAudioString ext = fromUTF8(extension);
453 decodermap[ext] = factory;
454 getLogger()->
logInfo(
"AudioManager",
"Audio Decoder for extension .%s registered.", toUTF8(ext));
461 cAudioString ext = fromUTF8(extension);
462 decodermapIterator it = decodermap.find(ext);
463 if(it != decodermap.end())
465 decodermap.erase(it);
466 getLogger()->
logInfo(
"AudioManager",
"Audio Decoder for extension .%s unregistered.", toUTF8(ext));
473 cAudioString ext = fromUTF8(extension);
474 decodermapIterator it = decodermap.find(ext);
475 return (it != decodermap.end());
481 cAudioString ext = fromUTF8(extension);
482 decodermapIterator it = decodermap.find(ext);
483 if(it != decodermap.end())
496 bool compareDataSourcePriorities(std::pair<int, cAudioString> left, std::pair<int, cAudioString> right)
498 return (left.first > right.first);
504 cAudioString safeName = fromUTF8(name);
505 datasourcemap[safeName] = factory;
506 dataSourcePriorityList.push_back(std::pair<int, cAudioString>(priority, safeName));
507 std::sort(dataSourcePriorityList.begin(), dataSourcePriorityList.end(), compareDataSourcePriorities);
509 getLogger()->
logInfo(
"AudioManager",
"Data Source named %s registered (Priority %i).", toUTF8(safeName), priority);
516 cAudioString safeName = fromUTF8(name);
517 datasourcemapIterator it = datasourcemap.find(safeName);
518 if(it != datasourcemap.end())
520 datasourcemap.erase(it);
521 getLogger()->
logInfo(
"AudioManager",
"Data Source named %s unregistered.", toUTF8(safeName));
524 for(
size_t i=0; i<dataSourcePriorityList.size(); ++i)
526 if(dataSourcePriorityList[i].second == safeName)
528 dataSourcePriorityList.erase(dataSourcePriorityList.begin()+i);
533 std::sort(dataSourcePriorityList.begin(), dataSourcePriorityList.end(), compareDataSourcePriorities);
539 cAudioString safeName = fromUTF8(name);
540 datasourcemapIterator it = datasourcemap.find(safeName);
541 return (it != datasourcemap.end());
547 cAudioString safeName = fromUTF8(name);
548 datasourcemapIterator it = datasourcemap.find(safeName);
549 if(it != datasourcemap.end())
559 datasourcemap.clear();
560 dataSourcePriorityList.clear();
567 eventHandlerList.push_back(handler);
575 eventHandlerList.remove(handler);
581 eventHandlerList.clear();
584 void cAudioManager::signalEvent(Events sevent)
589 if(it != eventHandlerList.end())
595 for(; it != eventHandlerList.end(); it++)
604 for(; it != eventHandlerList.end(); it++)
613 for(; it != eventHandlerList.end(); it++)
620 case ON_SOURCECREATE:
622 for(; it != eventHandlerList.end(); it++)
624 (*it)->onSourceCreate();
629 case ON_DECODERREGISTER:
631 for(; it != eventHandlerList.end(); it++)
633 (*it)->onDecoderRegister();
638 case ON_DATASOURCEREGISTER:
640 for(; it != eventHandlerList.end(); it++)
642 (*it)->onDataSourceRegister();
653 cAudioString audioName = fromUTF8(name);
654 audioIndexIterator i = audioIndex.find(audioName);
655 if (i == audioIndex.end())
667 cAudioVector<IAudioSource*>::Type deleteSources;
668 for(audioSourcesIter = audioSources.begin(); audioSourcesIter != audioSources.end(); ++audioSourcesIter)
670 if((*audioSourcesIter))
671 deleteSources.push_back(*audioSourcesIter);
673 for(audioSourcesIter = deleteSources.begin(); audioSourcesIter != deleteSources.end(); ++audioSourcesIter)
674 (*audioSourcesIter)->drop();
676 deleteSources.clear();
677 audioSources.clear();
681 for(managedAudioIter = managedAudioSources.begin(); managedAudioIter != managedAudioSources.end(); managedAudioIter++)
683 if ((*managedAudioIter))
685 CAUDIO_DELETE (*managedAudioIter);
688 managedAudioSources.clear();
691 for(managedAudioSourcesDelIter = managedAudioSourcesDelBuffer.begin(); managedAudioSourcesDelIter != managedAudioSourcesDelBuffer.end(); managedAudioSourcesDelIter++)
693 if ((*managedAudioSourcesDelIter))
695 CAUDIO_DELETE (*managedAudioSourcesDelIter);
698 managedAudioSourcesDelBuffer.clear();
706 audioIndexIterator it = audioIndex.begin();
707 for ( it=audioIndex.begin(); it != audioIndex.end(); it++ )
709 if( it->second == source )
711 audioIndex.erase(it);
715 for(
unsigned int i=0; i<audioSources.size(); ++i)
717 if(source == audioSources[i])
719 audioSources.erase(audioSources.begin()+i);
722 CAUDIO_DELETE source;
virtual void logError(const char *sender, const char *msg,...)=0
Used to log an error message to the logging system.
virtual IAudioSource * createFromAudioBuffer(const char *name, AudioCaptureBuffer *pBiffer, unsigned int frequency, AudioFormats format)
Creates an Audio Source from AudioCaptureBuffer in a memory buffer.
virtual void setMasterVolume(float vol)
Sets master volume. (valid range [0 - 1.0])
virtual void stopAllSounds()
Stops all playing sounds.
virtual float getDopplerFactor() const
Get Doppler Factor.
virtual bool registerDataSource(IDataSourceFactory *factory, const char *name, int priority)
Registers a data source with this manager.
virtual float getMasterVolume() const
Get the master volume.
virtual IAudioSource * createFromMemory(const char *name, const char *data, size_t length, const char *extension)
Creates an Audio Source from a memory buffer using a specific audio codec.
virtual bool isAudioDecoderRegistered(const char *extension)
Returns whether an audio decoder is currently registered for this file type.
Interface for data providers in cAudio.
virtual bool isValid()
Returns whether the source is valid.
virtual bool update()=0
Normally called every frame by the audio manager to update the internal buffers. Note: For internal u...
cAudioString getExt(const cAudioString &filename)
Grabs the current extention of a given string.
virtual void unRegisterAllEventHandlers()
Unregisters all previously registered event handlers from the manager.
virtual IAudioSource * play3D(const char *filename, cVector3 position, bool playLooped=false, bool startPaused=false)
Creates an Audio Source object using the highest priority data source that has the referenced filenam...
virtual cAudioString getType() const =0
Returns the IAudioDecoderType.
virtual float getSpeedOfSound() const
Get Speed of Sound (for doppler computations)
virtual bool isValid()=0
Returns whether the source is valid.
virtual void logInfo(const char *sender, const char *msg,...)=0
Used to log an informational message to the logging system.
virtual void registerEventHandler(IManagerEventHandler *handler)
Registers a new event handler with the manager.
virtual bool isValid()=0
Returns whether the stream is valid for this codec.
Interface for event handlers for playback manager events.
virtual IDataSource * CreateDataSource(const char *filename, bool streamingRequested)=0
Creates a data source instance for use with the engine.
Interface for factories that create Audio Decoders for cAudio.
virtual void unRegisterAllDataSources()
Removes all previously registered data sources.
Interface for a single audio source, which allow you to manipulate sound sources (speakers) in 2D or ...
virtual bool registerAudioDecoder(IAudioDecoderFactory *factory, const char *extension)
Register an Audio Decoder.
virtual bool initialize(const char *deviceName=0x0, int outputFrequency=-1, int eaxEffectSlots=4)
Initializes the manager.
virtual IDataSourceFactory * getDataSourceFactory(const char *name)
Returns a previously registered data source factory.
virtual void grab()
Increments the reference count by one.
virtual void update()
If threading is disabled, you must call this function every frame to update the playback buffers of a...
virtual void releaseAllSources()
Releases ALL Audio Sources (but does not shutdown the manager)
virtual bool drop()
Decrements the reference count by one. If it hits zero, this object is deleted.
virtual void unRegisterAudioDecoder(const char *extension)
Unregister a previously registered Audio Decoder.
virtual IAudioSource * getSoundByName(const char *name)
Returns an Audio Source by its "name" and NULL if the name is not found.
virtual void release(IAudioSource *source)
Releases a single Audio Source, removing it from the manager.
virtual IAudioBuffer * createBuffer(const char *filename)
Creates a Audio Sample using the highest priority data source that has the referenced filename.
virtual bool play3d(const cVector3 &position, const float &soundstr=1.0, const bool &toLoop=false)=0
Plays the source in 3D mode.
virtual bool isStopped() const =0
Returns if the source is stopped.
int getReferenceCount() const
Returns the current reference count of this object.
Class for manipulating vectors in 3D space.
virtual bool isValid() const =0
Returns if the source is ready to be used.
virtual IAudioSource * createFromRaw(const char *name, const char *data, size_t length, unsigned int frequency, AudioFormats format)
Creates an Audio Source from raw audio data in a memory buffer.
virtual void setDopplerFactor(float factor) const
Set Doppler Factor.
virtual void shutDown()
Shuts the manager down, cleaning up audio sources in the process. Does not clean up decoders,...
virtual bool isDataSourceRegistered(const char *name)
Returns whether a data source is currently registered under a certain name.
Interface for creating data sources for use with the engine.
Class used to read from a memory buffer.
virtual IAudioDecoder * CreateAudioDecoder(IDataSource *stream)=0
Returns an audio decoder.
virtual void unRegisterDataSource(const char *name)
Removes a previously registered data source.
virtual bool isUpdateThreadRunning()
Returns if the thread used to update all Audio Managers is running.
virtual void unRegisterEventHandler(IManagerEventHandler *handler)
Unregisters a previously registered event handler from the manager.
interface for a sample (audio buffer): completely loaded into memory, shareable across sources
virtual IAudioSource * createStatic(IAudioBuffer *buffer)
Creates an Audio Source from an Audio Buffer object (see createAudioBuffer())
virtual bool play2d(const bool &toLoop=false)=0
Plays the source in 2D mode.
virtual void unRegisterAllAudioDecoders()
Unregisters all attached Audio Decoders.
virtual void setSpeedOfSound(float speed)
Set Speed of Sound (for doppler computations)
virtual IAudioDecoderFactory * getAudioDecoderFactory(const char *extension)
Returns a registered audio decoder factory.
virtual IAudioSource * play2D(const char *filename, bool playLooped=false, bool startPaused=false)
Creates an Audio Source object using the highest priority data source that has the referenced filenam...
virtual IAudioSource * create(const char *name, const char *filename, bool stream=false)
Creates an Audio Source object using the highest priority data source that has the referenced filenam...
CAUDIO_API ILogger * getLogger()
Gets the interface to the logger.
Interface for all Audio Decoders in cAudio.
CAUDIO_API void cAudioSleep(unsigned int ms)
Causes the current thread to give up control for a certain duration.
AudioFormats
Enumeration of audio formats supported by the engine.
Main namespace for the entire cAudio library.