Added support for adding custom data sources. using registerSource(const char* , IDataSource*) createFromSource(const char*) isSourceRegistered(cont char*) unRegisterSource(cont char*)
This commit is contained in:
parent
b317412796
commit
f0a5c7411d
|
@ -54,6 +54,7 @@ int main(int argc, char* argv[])
|
|||
//Wait for the sound to finish playing
|
||||
while(mysound->isPlaying())
|
||||
cAudio::cAudioSleep(10);
|
||||
|
||||
}
|
||||
|
||||
//Delete all IAudio sounds
|
||||
|
|
|
@ -52,11 +52,13 @@ namespace cAudio
|
|||
|
||||
//!Creates the cAudio object
|
||||
virtual IAudioSource* createFromFile(const char* name, const char* pathToFile, bool stream = false);
|
||||
//!Loads ogg from memory or virtual file system
|
||||
//!Loads data from memory or virtual file system
|
||||
virtual IAudioSource* createFromMemory(const char* name, const char* data, size_t length, const char* extension);
|
||||
//!Loads raw audio from memory.
|
||||
virtual IAudioSource* createFromRaw(const char* name, const char* data, size_t length, unsigned int frequency, AudioFormats format);
|
||||
|
||||
//!Loads audio from alternative data source.
|
||||
virtual IAudioSource* createFromSource(const char* name, const char* source);
|
||||
|
||||
//!Register Audio Codec
|
||||
virtual bool registerAudioDecoder(IAudioDecoderFactory* factory, const char* extension);
|
||||
//!Unregister Audio Codec (allows you to prevent an file type from being playable with new sound sources)
|
||||
|
@ -68,6 +70,16 @@ namespace cAudio
|
|||
//!Returns a registered audio decoder factory
|
||||
virtual IAudioDecoderFactory* getAudioDecoderFactory(const char* extension);
|
||||
|
||||
//!Register a alternative source
|
||||
virtual bool registerSource(const char* identifier,IDataSource* datasource);
|
||||
//! Unregister source (allows you to prevent creation of audio files from specific source)
|
||||
//!Note that all current sound sources will still continue to use any currently used source.
|
||||
//!Will NOT delete any user added factory instance, you must do that yourself
|
||||
virtual void unRegisterSource(const char* source);
|
||||
//!Checks if the source type is registered.
|
||||
virtual bool isSourceRegistered(const char* source);
|
||||
|
||||
|
||||
//! Grabs a list of available devices, as well as the default system one
|
||||
void getAvailableDevices();
|
||||
|
||||
|
@ -95,6 +107,9 @@ namespace cAudio
|
|||
std::vector<IAudioSource*> audioSources;
|
||||
//! Decoder map that holds all decoders by file extension
|
||||
std::map<std::string, IAudioDecoderFactory*> decodermap;
|
||||
//! Archive map that holds all datasource types
|
||||
std::map<std::string, IDataSource*> datasourcemap;
|
||||
|
||||
//! The listener object
|
||||
cListener initlistener;
|
||||
//! Interface for audio effects
|
||||
|
|
|
@ -326,7 +326,7 @@ namespace cAudio
|
|||
std::string audioName = safeCStr(name);
|
||||
IAudioDecoderFactory* factory = getAudioDecoderFactory("raw");
|
||||
if(factory)
|
||||
{
|
||||
{IAudioDecoderFactory* factory = getAudioDecoderFactory("raw");
|
||||
cMemorySource* source = new cMemorySource(data, length, true);
|
||||
if(source)
|
||||
{
|
||||
|
@ -379,6 +379,105 @@ namespace cAudio
|
|||
return NULL;
|
||||
}
|
||||
|
||||
IAudioSource* cAudioManager::createFromSource(const char* name, const char* source)
|
||||
{
|
||||
std::string audioName = safeCStr(name);
|
||||
std::string src = safeCStr(source);
|
||||
|
||||
std::map<std::string, IDataSource*>::iterator it = datasourcemap.find(src);
|
||||
IAudioDecoderFactory* factory = getAudioDecoderFactory("raw");
|
||||
|
||||
if(it != datasourcemap.end())
|
||||
{
|
||||
if(it->second)
|
||||
{
|
||||
if(it->second->isValid())
|
||||
{
|
||||
IAudioDecoder* decoder = factory->CreateAudioDecoder(it->second);
|
||||
|
||||
if(decoder)
|
||||
{
|
||||
if(decoder->isValid())
|
||||
{
|
||||
IAudioSource* audio = new cAudioSource(decoder, Context, initEffects.getEFXInterface());
|
||||
decoder->drop();
|
||||
if(audio)
|
||||
{
|
||||
if(audio->isValid())
|
||||
{
|
||||
if(!audioName.empty())
|
||||
audioIndex[audioName] = audio;
|
||||
|
||||
audioSources.push_back(audio);
|
||||
|
||||
getLogger()->logInfo("AudioManager", "Audio Source (%s) successfully created from raw data. Using ($s) source", audioName.c_str(),src.c_str());
|
||||
|
||||
return audio;
|
||||
}
|
||||
|
||||
audio->drop();
|
||||
getLogger()->logError("AudioManager", "Failed to create Audio Source (%s): Error creating audio source. Using ($) source", audioName.c_str(),src.c_str());
|
||||
return NULL;
|
||||
}
|
||||
|
||||
getLogger()->logError("AudioManager", "Failed to create Audio Source (%s): Could not allocate enough memory.", audioName.c_str());
|
||||
return NULL;
|
||||
}
|
||||
|
||||
decoder->drop();
|
||||
getLogger()->logError("AudioManager", "Failed to create Audio Source (%s): Audio data could not be decoded by (.%s) decoder.", audioName.c_str(), "raw");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
getLogger()->logError("AudioManager", "Failed to create Audio Source (%s): Could not allocate enough memory for decoder.", audioName.c_str());
|
||||
return NULL;
|
||||
}
|
||||
|
||||
getLogger()->logError("AudioManager", "Failed to create Audio Source (%s): Audio data is corrupt.", audioName.c_str());
|
||||
return NULL;
|
||||
}
|
||||
|
||||
getLogger()->logError("AudioManager", "Failed to create Audio Source (%s): Could not allocate enough memory.", audioName.c_str());
|
||||
return NULL;
|
||||
}
|
||||
|
||||
getLogger()->logError("AudioManager", "Failed to create Audio Source (%s): Codec (.%s) is not supported.", audioName.c_str(), "raw");
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
bool cAudioManager::registerSource(const char* identifier,IDataSource* datasource)
|
||||
{
|
||||
cAudioMutexBasicLock lock(Mutex);
|
||||
std::string ident = safeCStr(identifier);
|
||||
datasourcemap[ident] = datasource;
|
||||
getLogger()->logInfo("AudioManager","Audio source %s registered.", ident.c_str());
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
bool cAudioManager::isSourceRegistered(const char* source)
|
||||
{
|
||||
cAudioMutexBasicLock lock(Mutex);
|
||||
std::string src = safeCStr(source);
|
||||
std::map<std::string, IDataSource*>::iterator it = datasourcemap.find(src);
|
||||
return (it != datasourcemap.end());
|
||||
|
||||
}
|
||||
|
||||
void cAudioManager::unRegisterSource(const char* source)
|
||||
{
|
||||
cAudioMutexBasicLock lock(Mutex);
|
||||
std::string src = safeCStr(source);
|
||||
std::map<std::string, IDataSource*>::iterator it = datasourcemap.find(src);
|
||||
if(it != datasourcemap.end())
|
||||
{
|
||||
datasourcemap.erase(it);
|
||||
getLogger()->logInfo("AudioManager", "DataSource .%s unregistered.", src.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
bool cAudioManager::registerAudioDecoder(IAudioDecoderFactory* factory, const char* extension)
|
||||
{
|
||||
cAudioMutexBasicLock lock(Mutex);
|
||||
|
|
|
@ -48,6 +48,8 @@ namespace cAudio
|
|||
virtual IAudioSource* createFromMemory(const char* name, const char* data, size_t length, const char* extension) = 0;
|
||||
//!Loads raw audio from memory.
|
||||
virtual IAudioSource* createFromRaw(const char* name, const char* data, size_t length, unsigned int frequency, AudioFormats format) = 0;
|
||||
//!Loads audio from alternative source.
|
||||
virtual IAudioSource* createFromSource(const char* name, const char* source) = 0;
|
||||
|
||||
//!Register Audio Codec
|
||||
virtual bool registerAudioDecoder(IAudioDecoderFactory* factory, const char* extension) = 0;
|
||||
|
@ -60,6 +62,15 @@ namespace cAudio
|
|||
//!Returns a registered audio decoder factory
|
||||
virtual IAudioDecoderFactory* getAudioDecoderFactory(const char* extension) = 0;
|
||||
|
||||
//!Register a alternative source
|
||||
virtual bool registerSource(const char* identifier, IDataSource* datasource) = 0;
|
||||
//! Unregister source (allows you to prevent creation of audio files from specific source)
|
||||
//!Note that all current sound sources will still continue to use any currently used source.
|
||||
//!Will NOT delete any user added factory instance, you must do that yourself
|
||||
virtual void unRegisterSource(const char* source) = 0;
|
||||
//!Checks if the source type is registered.
|
||||
virtual bool isSourceRegistered(const char* source) = 0;
|
||||
|
||||
//!Returns an interface for the listener
|
||||
virtual IListener* getListener() = 0;
|
||||
virtual IAudioEffects* getEffects() = 0;
|
||||
|
|
Loading…
Reference in New Issue