* Unify AudioSource creation (Code were unreadable)
This commit is contained in:
parent
c0d73de948
commit
83a61f0494
|
@ -59,6 +59,7 @@ class cMP3Decoder : public IAudioDecoder
|
|||
virtual float getCurrentTime();
|
||||
virtual int getCurrentPosition();
|
||||
virtual int getCurrentCompressedPosition();
|
||||
cAudioString getType() const;
|
||||
|
||||
protected:
|
||||
MPAuDecContext* Context;
|
||||
|
|
|
@ -305,3 +305,8 @@ int cMP3Decoder::getCurrentCompressedPosition()
|
|||
return Stream->getCurrentPos();
|
||||
}
|
||||
|
||||
cAudioString cMP3Decoder::getType() const
|
||||
{
|
||||
return cAudioString("cMP3Decoder");
|
||||
}
|
||||
|
||||
|
|
|
@ -84,6 +84,8 @@ namespace cAudio
|
|||
protected:
|
||||
virtual void run();
|
||||
|
||||
IAudioSource* createAudioSource(IAudioDecoder* decoder, const cAudioString& audioName, const cAudioString& dataSource);
|
||||
|
||||
private:
|
||||
//! Mutex for thread syncronization
|
||||
cAudioMutex Mutex;
|
||||
|
|
|
@ -37,6 +37,7 @@ namespace cAudio
|
|||
virtual float getCurrentTime();
|
||||
virtual int getCurrentPosition();
|
||||
virtual int getCurrentCompressedPosition();
|
||||
virtual cAudioString getType() const;
|
||||
|
||||
protected:
|
||||
//! Callbacks used for read memory
|
||||
|
|
|
@ -29,6 +29,7 @@ namespace cAudio
|
|||
virtual float getCurrentTime();
|
||||
virtual int getCurrentPosition();
|
||||
virtual int getCurrentCompressedPosition();
|
||||
virtual cAudioString getType() const;
|
||||
|
||||
private:
|
||||
unsigned int Frequency;
|
||||
|
|
|
@ -32,6 +32,7 @@ namespace cAudio
|
|||
virtual float getCurrentTime();
|
||||
virtual int getCurrentPosition();
|
||||
virtual int getCurrentCompressedPosition();
|
||||
virtual cAudioString getType() const;
|
||||
|
||||
private:
|
||||
short Channels;
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include "IRefCounted.h"
|
||||
#include "IDataSource.h"
|
||||
#include "EAudioFormats.h"
|
||||
#include "cAudioString.h"
|
||||
|
||||
namespace cAudio
|
||||
{
|
||||
|
@ -69,6 +70,10 @@ namespace cAudio
|
|||
|
||||
//! Returns the position in the compressed (original) audio stream before decoding.
|
||||
virtual int getCurrentCompressedPosition() = 0;
|
||||
|
||||
//! Returns the IAudioDecoderType.
|
||||
virtual cAudioString getType() const = 0;
|
||||
|
||||
protected:
|
||||
//! Pointer to the data source to take audio data from.
|
||||
IDataSource* Stream;
|
||||
|
|
|
@ -135,64 +135,74 @@ namespace cAudio
|
|||
return true;
|
||||
}
|
||||
|
||||
|
||||
IAudioSource* cAudioManager::createAudioSource(IAudioDecoder* decoder, const cAudioString& audioName, const cAudioString& dataSource)
|
||||
{
|
||||
if(decoder && decoder->isValid())
|
||||
{
|
||||
#if CAUDIO_EFX_ENABLED == 1
|
||||
IAudioSource* audio = CAUDIO_NEW cAudioSource(decoder, Context, initEffects.getEFXInterface());
|
||||
#else
|
||||
IAudioSource* audio = CAUDIO_NEW cAudioSource(decoder, Context);
|
||||
#endif
|
||||
decoder->drop();
|
||||
|
||||
if(audio && audio->isValid())
|
||||
{
|
||||
if(!audioName.empty())
|
||||
audioIndex[audioName] = audio;
|
||||
|
||||
audioSources.push_back(audio);
|
||||
getLogger()->logInfo("AudioManager", "Audio Source (%s) created from Data Source %s.", audioName.c_str(), dataSource.c_str());
|
||||
return audio;
|
||||
}
|
||||
|
||||
getLogger()->logError("AudioManager", "Failed to create Audio Source (%s): Error creating audio source.", audioName.c_str());
|
||||
audio->drop();
|
||||
return NULL;
|
||||
}
|
||||
getLogger()->logError("AudioManager", "Failed to create Audio Source (%s): Audio data could not be decoded by (.%s) decoder.", audioName.c_str(), decoder->getType());
|
||||
decoder->drop();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
IAudioSource* cAudioManager::create(const char* name, const char* filename, bool stream)
|
||||
{
|
||||
cAudioMutexBasicLock lock(Mutex);
|
||||
|
||||
cAudioString audioName = safeCStr(name);
|
||||
cAudioString path = safeCStr(filename);
|
||||
|
||||
cAudioString ext = getExt(path);
|
||||
IAudioDecoderFactory* factory = getAudioDecoderFactory(ext.c_str());
|
||||
|
||||
if(factory)
|
||||
{
|
||||
for(size_t i=0; i<dataSourcePriorityList.size(); ++i)
|
||||
{
|
||||
IDataSourceFactory* dataFactory = datasourcemap[dataSourcePriorityList[i].second];
|
||||
if(dataFactory)
|
||||
{
|
||||
IDataSource* source = dataFactory->CreateDataSource(filename, stream);
|
||||
if(source && source->isValid())
|
||||
{
|
||||
IAudioDecoder* decoder = factory->CreateAudioDecoder(source);
|
||||
source->drop();
|
||||
if(decoder && decoder->isValid())
|
||||
{
|
||||
#if CAUDIO_EFX_ENABLED == 1
|
||||
IAudioSource* audio = CAUDIO_NEW cAudioSource(decoder, Context, initEffects.getEFXInterface());
|
||||
#else
|
||||
IAudioSource* audio = CAUDIO_NEW cAudioSource(decoder, Context);
|
||||
#endif
|
||||
decoder->drop();
|
||||
|
||||
if(audio && audio->isValid())
|
||||
{
|
||||
if(!audioName.empty())
|
||||
audioIndex[audioName] = audio;
|
||||
|
||||
audioSources.push_back(audio);
|
||||
|
||||
getLogger()->logInfo("AudioManager", "Audio Source (%s) created from file %s from Data Source %s.", audioName.c_str(), path.c_str(), dataSourcePriorityList[i].second.c_str());
|
||||
|
||||
return audio;
|
||||
}
|
||||
getLogger()->logError("AudioManager", "Failed to create Audio Source (%s): Error creating audio source.", audioName.c_str());
|
||||
audio->drop();
|
||||
return NULL;
|
||||
}
|
||||
getLogger()->logError("AudioManager", "Failed to create Audio Source (%s): Audio data could not be decoded by (.%s) decoder.", audioName.c_str(), ext.c_str());
|
||||
decoder->drop();
|
||||
return NULL;
|
||||
}
|
||||
if(source)
|
||||
source->drop();
|
||||
}
|
||||
}
|
||||
getLogger()->logError("AudioManager", "Failed to create Audio Source (%s): File could not be found (.%s).", audioName.c_str(), path.c_str());
|
||||
if(!factory) {
|
||||
getLogger()->logError("AudioManager", "Failed to create Audio Source (%s): No decoder could be found for (.%s).", audioName.c_str(), ext.c_str());
|
||||
return NULL;
|
||||
}
|
||||
getLogger()->logError("AudioManager", "Failed to create Audio Source (%s): No decoder could be found for (.%s).", audioName.c_str(), ext.c_str());
|
||||
|
||||
for(size_t i=0; i<dataSourcePriorityList.size(); ++i)
|
||||
{
|
||||
const cAudioString dataSourceName = dataSourcePriorityList[i].second;
|
||||
IDataSourceFactory* dataFactory = datasourcemap[dataSourceName];
|
||||
if(dataFactory)
|
||||
{
|
||||
IDataSource* source = dataFactory->CreateDataSource(filename, stream);
|
||||
if(source && source->isValid())
|
||||
{
|
||||
IAudioDecoder* decoder = factory->CreateAudioDecoder(source);
|
||||
source->drop();
|
||||
|
||||
IAudioSource* audio = createAudioSource(decoder, audioName, dataSourceName);
|
||||
if(audio != NULL)
|
||||
return audio;
|
||||
|
||||
if(source)
|
||||
source->drop();
|
||||
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -203,125 +213,52 @@ namespace cAudio
|
|||
cAudioString audioName = safeCStr(name);
|
||||
cAudioString ext = safeCStr(extension);
|
||||
IAudioDecoderFactory* factory = getAudioDecoderFactory(ext.c_str());
|
||||
if(factory)
|
||||
{
|
||||
cMemorySource* source = CAUDIO_NEW cMemorySource(data, length, true);
|
||||
if(source)
|
||||
{
|
||||
if(source->isValid())
|
||||
{
|
||||
IAudioDecoder* decoder = factory->CreateAudioDecoder(source);
|
||||
source->drop();
|
||||
if(decoder)
|
||||
{
|
||||
if(decoder->isValid())
|
||||
{
|
||||
#if CAUDIO_EFX_ENABLED == 1
|
||||
IAudioSource* audio = CAUDIO_NEW cAudioSource(decoder, Context, initEffects.getEFXInterface());
|
||||
#else
|
||||
IAudioSource* audio = CAUDIO_NEW cAudioSource(decoder, Context);
|
||||
#endif
|
||||
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 memory.", audioName.c_str());
|
||||
|
||||
return audio;
|
||||
}
|
||||
audio->drop();
|
||||
getLogger()->logError("AudioManager", "Failed to create Audio Source (%s): Error creating audio source.", audioName.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(), ext.c_str());
|
||||
return NULL;
|
||||
}
|
||||
getLogger()->logError("AudioManager", "Failed to create Audio Source (%s): Could not allocate enough memory for decoder.", audioName.c_str());
|
||||
return NULL;
|
||||
}
|
||||
source->drop();
|
||||
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());
|
||||
if(!factory) {
|
||||
getLogger()->logError("AudioManager", "Failed to create Audio Source (%s): Codec (.%s) is not supported.", audioName.c_str(), ext.c_str());
|
||||
return NULL;
|
||||
}
|
||||
getLogger()->logError("AudioManager", "Failed to create Audio Source (%s): Codec (.%s) is not supported.", audioName.c_str(), ext.c_str());
|
||||
|
||||
cMemorySource* source = CAUDIO_NEW cMemorySource(data, length, true);
|
||||
if(source && source->isValid())
|
||||
{
|
||||
IAudioDecoder* decoder = factory->CreateAudioDecoder(source);
|
||||
source->drop();
|
||||
|
||||
IAudioSource* audio = createAudioSource(decoder, audioName, "cMemorySource");
|
||||
if(audio != NULL)
|
||||
return audio;
|
||||
|
||||
if(source)
|
||||
source->drop();
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
IAudioSource* cAudioManager::createFromRaw(const char* name, const char* data, size_t length, unsigned int frequency, AudioFormats format)
|
||||
{
|
||||
cAudioMutexBasicLock lock(Mutex);
|
||||
|
||||
cAudioString audioName = safeCStr(name);
|
||||
IAudioDecoderFactory* factory = getAudioDecoderFactory("raw");
|
||||
if(factory)
|
||||
{
|
||||
cMemorySource* source = CAUDIO_NEW cMemorySource(data, length, true);
|
||||
if(source)
|
||||
{
|
||||
if(source->isValid())
|
||||
{
|
||||
IAudioDecoder* decoder = ((cRawAudioDecoderFactory*)factory)->CreateAudioDecoder(source, frequency, format);
|
||||
source->drop();
|
||||
if(decoder)
|
||||
{
|
||||
if(decoder->isValid())
|
||||
{
|
||||
#if CAUDIO_EFX_ENABLED == 1
|
||||
IAudioSource* audio = CAUDIO_NEW cAudioSource(decoder, Context, initEffects.getEFXInterface());
|
||||
#else
|
||||
IAudioSource* audio = CAUDIO_NEW cAudioSource(decoder, Context);
|
||||
#endif
|
||||
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.", audioName.c_str());
|
||||
|
||||
return audio;
|
||||
}
|
||||
audio->drop();
|
||||
getLogger()->logError("AudioManager", "Failed to create Audio Source (%s): Error creating audio source.", audioName.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;
|
||||
}
|
||||
source->drop();
|
||||
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());
|
||||
if(!factory) {
|
||||
getLogger()->logError("AudioManager", "Failed to create Audio Source (%s): Codec (.raw) is not supported.", audioName.c_str());
|
||||
return NULL;
|
||||
}
|
||||
getLogger()->logError("AudioManager", "Failed to create Audio Source (%s): Codec (.%s) is not supported.", audioName.c_str(), "raw");
|
||||
|
||||
cMemorySource* source = CAUDIO_NEW cMemorySource(data, length, true);
|
||||
if(source && source->isValid())
|
||||
{
|
||||
IAudioDecoder* decoder = ((cRawAudioDecoderFactory*)factory)->CreateAudioDecoder(source, frequency, format);
|
||||
source->drop();
|
||||
|
||||
IAudioSource* audio = createAudioSource(decoder, audioName, "cMemorySource");
|
||||
if(audio != NULL)
|
||||
return audio;
|
||||
|
||||
if(source)
|
||||
source->drop();
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
|
@ -175,6 +175,11 @@ namespace cAudio
|
|||
{
|
||||
return ov_raw_tell(&oggStream);
|
||||
}
|
||||
|
||||
cAudioString cOggDecoder::getType() const
|
||||
{
|
||||
return cAudioString("cOggDecoder");
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -111,4 +111,9 @@ namespace cAudio{
|
|||
{
|
||||
return Stream->getCurrentPos();
|
||||
}
|
||||
|
||||
cAudioString cRawDecoder::getType() const
|
||||
{
|
||||
return cAudioString("cRawDecoder");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -225,6 +225,11 @@ namespace cAudio
|
|||
{
|
||||
return Stream->getCurrentPos();
|
||||
}
|
||||
|
||||
cAudioString cWavDecoder::getType() const
|
||||
{
|
||||
return cAudioString("cWavDecoder");
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1348,6 +1348,11 @@ int SwigDirector_IAudioDecoder::getCurrentCompressedPosition() {
|
|||
return c_result;
|
||||
}
|
||||
|
||||
cAudioString SwigDirector_IAudioDecoder::getType() const
|
||||
{
|
||||
return cAudioString("");
|
||||
}
|
||||
|
||||
void SwigDirector_IAudioDecoder::swig_connect_director(SWIG_Callback0_t callbackgetFormat, SWIG_Callback1_t callbackgetFrequency, SWIG_Callback2_t callbackisSeekingSupported, SWIG_Callback3_t callbackisValid, SWIG_Callback4_t callbackreadAudioData, SWIG_Callback5_t callbacksetPosition, SWIG_Callback6_t callbackseek, SWIG_Callback7_t callbackgetTotalTime, SWIG_Callback8_t callbackgetTotalSize, SWIG_Callback9_t callbackgetCompressedSize, SWIG_Callback10_t callbackgetCurrentTime, SWIG_Callback11_t callbackgetCurrentPosition, SWIG_Callback12_t callbackgetCurrentCompressedPosition) {
|
||||
swig_callbackgetFormat = callbackgetFormat;
|
||||
swig_callbackgetFrequency = callbackgetFrequency;
|
||||
|
|
|
@ -169,6 +169,7 @@ public:
|
|||
virtual float getCurrentTime();
|
||||
virtual int getCurrentPosition();
|
||||
virtual int getCurrentCompressedPosition();
|
||||
virtual cAudioString getType() const;
|
||||
|
||||
typedef int (SWIGSTDCALL* SWIG_Callback0_t)();
|
||||
typedef int (SWIGSTDCALL* SWIG_Callback1_t)();
|
||||
|
|
Loading…
Reference in New Issue