Fixed unicode support.

This commit is contained in:
John Norman 2012-07-04 11:08:23 -07:00
parent d9e58fbdf9
commit 6e02974b10
10 changed files with 641 additions and 651 deletions

View File

@ -31,15 +31,6 @@ static cAudioString getExt(const cAudioString& filename)
return filename.substr(filename.find_last_of(_CTEXT(".")) + 1, filename.length()-filename.find_last_of(_CTEXT("."))-1);
}
//! Prevents a bug with NULL passed into cAudioString.
static cAudioString safeCStr(const char* str)
{
if( str != NULL ) return cAudioString(str);
else return cAudioString("");
}
//! Returns a list of files/directories in the supplied directory. Used internally for auto-installation of plugins.
static cAudioVector<cAudioString>::Type getFilesInDirectory(cAudioString path)
{

View File

@ -60,15 +60,7 @@ namespace cAudio
static const TCHAR* toWINSTR(const wchar_t* str)
{
static int id = 0;
static char buffer[8][1024];
id = ++id & 0x7;
int slen = wcslen(str);
int buff_size = WideCharToMultiByte(CP_UTF8, 0, str, (int)(slen < 1023 ? slen : 1023), buffer[id], 1023, 0, false);
buffer[id][buff_size] = 0;
buffer[id][1023] = 0;
return buffer[id];
return str;
}
static const char* toUTF8(const cAudioString& str)
@ -89,8 +81,7 @@ namespace cAudio
int buff_size = MultiByteToWideChar(CP_UTF8, 0, str, (int)strlen(str), 0, 0);
if (buff_size == 0)
return cAudioString();
buffer = new wchar_t[buff_size + 1];
memset((void*)buffer, 0, sizeof(wchar_t) * (buff_size + 1));
MultiByteToWideChar(CP_UTF8, 0, str, (int)strlen(str), buffer, buff_size);
@ -102,14 +93,22 @@ namespace cAudio
#else
static const char* toWINSTR(const char* str)
{
return str;
}
static int id = 0;
static char buffer[8][1024];
id = ++id & 0x7;
int slen = wcslen(str);
int buff_size = WideCharToMultiByte(CP_UTF8, 0, str, (int)(slen < 1023 ? slen : 1023), buffer[id], 1023, 0, false);
buffer[id][buff_size] = 0;
buffer[id][1023] = 0;
return buffer[id];
}
static const char* toUTF8(const cAudioString& str)
{
return str.c_str();
}
static cAudioString fromUTF8(const char* str)
{
return cAudioString(str);

View File

@ -39,10 +39,10 @@ namespace cAudio
if(DeviceName.empty())
CaptureDevice = alcCaptureOpenDevice(NULL, Frequency, convertAudioFormatEnum(Format), InternalBufferSize / SampleSize);
else
CaptureDevice = alcCaptureOpenDevice(DeviceName.c_str(), Frequency, convertAudioFormatEnum(Format), InternalBufferSize / SampleSize);
CaptureDevice = alcCaptureOpenDevice(toUTF8(DeviceName), Frequency, convertAudioFormatEnum(Format), InternalBufferSize / SampleSize);
if(CaptureDevice)
{
DeviceName = alcGetString(CaptureDevice, ALC_CAPTURE_DEVICE_SPECIFIER);
DeviceName = fromUTF8(alcGetString(CaptureDevice, ALC_CAPTURE_DEVICE_SPECIFIER));
Ready = true;
checkError();
getLogger()->logDebug("AudioCapture", "OpenAL Capture Device Opened.");
@ -214,7 +214,7 @@ namespace cAudio
bool cAudioCapture::setDevice(const char* deviceName)
{
cAudioMutexBasicLock lock(Mutex);
DeviceName = safeCStr(deviceName);
DeviceName = fromUTF8(deviceName);
shutdownOpenALDevice();
return initOpenALDevice();
@ -223,7 +223,7 @@ namespace cAudio
bool cAudioCapture::initialize(const char* deviceName, unsigned int frequency, AudioFormats format, unsigned int internalBufferSize)
{
cAudioMutexBasicLock lock(Mutex);
DeviceName = safeCStr(deviceName);
DeviceName = fromUTF8(deviceName);
Frequency = frequency;
InternalBufferSize = internalBufferSize;

View File

@ -258,10 +258,10 @@ namespace cAudio
if(!Initialized) return NULL;
cAudioMutexBasicLock lock(Mutex);
cAudioString audioName = safeCStr(name);
cAudioString path = safeCStr(filename);
cAudioString audioName = fromUTF8(name);
cAudioString path = fromUTF8(filename);
cAudioString ext = getExt(path);
IAudioDecoderFactory* factory = getAudioDecoderFactory(ext.c_str());
IAudioDecoderFactory* factory = getAudioDecoderFactory(toUTF8(ext));
if(!factory) {
getLogger()->logError("AudioManager", "Failed to create Audio Source (%s): No decoder could be found for (.%s).", audioName.c_str(), ext.c_str());
@ -299,9 +299,9 @@ namespace cAudio
if(!Initialized) return NULL;
cAudioMutexBasicLock lock(Mutex);
cAudioString audioName = safeCStr(name);
cAudioString ext = safeCStr(extension);
IAudioDecoderFactory* factory = getAudioDecoderFactory(ext.c_str());
cAudioString audioName = fromUTF8(name);
cAudioString ext = fromUTF8(extension);
IAudioDecoderFactory* factory = getAudioDecoderFactory(toUTF8(ext));
if(!factory) {
getLogger()->logError("AudioManager", "Failed to create Audio Source (%s): Codec (.%s) is not supported.", audioName.c_str(), ext.c_str());
@ -314,7 +314,7 @@ namespace cAudio
IAudioDecoder* decoder = factory->CreateAudioDecoder(source);
source->drop();
IAudioSource* audio = createAudioSource(decoder, audioName, "cMemorySource");
IAudioSource* audio = createAudioSource(decoder, audioName, _CTEXT("cMemorySource"));
if(audio != NULL)
return audio;
@ -334,7 +334,7 @@ namespace cAudio
if(!Initialized) return NULL;
cAudioMutexBasicLock lock(Mutex);
cAudioString audioName = safeCStr(name);
cAudioString audioName = fromUTF8(name);
IAudioDecoderFactory* factory = getAudioDecoderFactory("raw");
if(!factory) {
@ -348,7 +348,7 @@ namespace cAudio
IAudioDecoder* decoder = ((cRawAudioDecoderFactory*)factory)->CreateAudioDecoder(source, frequency, format);
source->drop();
IAudioSource* audio = createAudioSource(decoder, audioName, "cMemorySource");
IAudioSource* audio = createAudioSource(decoder, audioName, _CTEXT("cMemorySource"));
if(audio != NULL)
return audio;
@ -361,7 +361,7 @@ namespace cAudio
bool cAudioManager::registerAudioDecoder(IAudioDecoderFactory* factory, const char* extension)
{
cAudioMutexBasicLock lock(Mutex);
cAudioString ext = safeCStr(extension);
cAudioString ext = fromUTF8(extension);
decodermap[ext] = factory;
getLogger()->logInfo("AudioManager", "Audio Decoder for extension .%s registered.", ext.c_str());
return true;
@ -370,7 +370,7 @@ namespace cAudio
void cAudioManager::unRegisterAudioDecoder(const char* extension)
{
cAudioMutexBasicLock lock(Mutex);
cAudioString ext = safeCStr(extension);
cAudioString ext = fromUTF8(extension);
decodermapIterator it = decodermap.find(ext);
if(it != decodermap.end())
{
@ -382,7 +382,7 @@ namespace cAudio
bool cAudioManager::isAudioDecoderRegistered(const char* extension)
{
cAudioMutexBasicLock lock(Mutex);
cAudioString ext = safeCStr(extension);
cAudioString ext = fromUTF8(extension);
decodermapIterator it = decodermap.find(ext);
return (it != decodermap.end());
}
@ -390,7 +390,7 @@ namespace cAudio
IAudioDecoderFactory* cAudioManager::getAudioDecoderFactory(const char* extension)
{
cAudioMutexBasicLock lock(Mutex);
cAudioString ext = safeCStr(extension);
cAudioString ext = fromUTF8(extension);
decodermapIterator it = decodermap.find(ext);
if(it != decodermap.end())
{
@ -413,7 +413,7 @@ namespace cAudio
bool cAudioManager::registerDataSource(IDataSourceFactory* factory, const char* name, int priority)
{
cAudioMutexBasicLock lock(Mutex);
cAudioString safeName = safeCStr(name);
cAudioString safeName = fromUTF8(name);
datasourcemap[safeName] = factory;
dataSourcePriorityList.push_back(std::pair<int, cAudioString>(priority, safeName));
std::sort(dataSourcePriorityList.begin(), dataSourcePriorityList.end(), compareDataSourcePriorities);
@ -425,7 +425,7 @@ namespace cAudio
void cAudioManager::unRegisterDataSource(const char* name)
{
cAudioMutexBasicLock lock(Mutex);
cAudioString safeName = safeCStr(name);
cAudioString safeName = fromUTF8(name);
datasourcemapIterator it = datasourcemap.find(safeName);
if(it != datasourcemap.end())
{
@ -448,7 +448,7 @@ namespace cAudio
bool cAudioManager::isDataSourceRegistered(const char* name)
{
cAudioMutexBasicLock lock(Mutex);
cAudioString safeName = safeCStr(name);
cAudioString safeName = fromUTF8(name);
datasourcemapIterator it = datasourcemap.find(safeName);
return (it != datasourcemap.end());
}
@ -456,7 +456,7 @@ namespace cAudio
IDataSourceFactory* cAudioManager::getDataSourceFactory(const char* name)
{
cAudioMutexBasicLock lock(Mutex);
cAudioString safeName = safeCStr(name);
cAudioString safeName = fromUTF8(name);
datasourcemapIterator it = datasourcemap.find(safeName);
if(it != datasourcemap.end())
{
@ -562,7 +562,7 @@ namespace cAudio
IAudioSource* cAudioManager::getSoundByName(const char* name)
{
cAudioMutexBasicLock lock(Mutex);
cAudioString audioName = safeCStr(name);
cAudioString audioName = fromUTF8(name);
audioIndexIterator i = audioIndex.find(audioName);
if (i == audioIndex.end())
{

View File

@ -1,82 +1,82 @@
// Copyright (c) 2008-2011 Raynaldo (Wildicv) Rivera, Joshua (Dark_Kilauea) Jones, Murat (wolfmanfx) Sari
// This file is part of the "cAudio Engine"
// For conditions of distribution and use, see copyright notice in cAudio.h
#include "cFileSource.h"
#include "cUtils.h"
#if CAUDIO_COMPILE_WITH_FILE_SOURCE == 1
#include <cstring>
namespace cAudio
{
cFileSource::cFileSource(const char* filename) : pFile(NULL), Valid(false), Filesize(0)
{
cAudioString safeFilename = safeCStr(filename);
if(safeFilename.length() != 0)
{
pFile = fopen(safeFilename.c_str(),"rb");
if(pFile)
Valid = true;
}
if(Valid)
{
fseek(pFile, 0, SEEK_END);
Filesize = ftell(pFile);
fseek(pFile, 0, SEEK_SET);
}
}
cFileSource::~cFileSource()
{
if(pFile)
fclose(pFile);
}
bool cFileSource::isValid()
{
return Valid;
}
int cFileSource::getCurrentPos()
{
return ftell(pFile);
}
int cFileSource::getSize()
{
return Filesize;
}
int cFileSource::read(void* output, int size)
{
return fread(output, sizeof(char), size, pFile);
}
bool cFileSource::seek(int amount, bool relative)
{
if(relative == true)
{
int oldamount = ftell(pFile);
fseek(pFile, amount, SEEK_CUR);
//check against the absolute position
if(oldamount+amount != ftell(pFile))
return false;
}
else
{
fseek(pFile, amount, SEEK_SET);
if(amount != ftell(pFile))
return false;
}
return true;
}
};
#endif
// For conditions of distribution and use, see copyright notice in cAudio.h
#include "cFileSource.h"
#include "cUtils.h"
#if CAUDIO_COMPILE_WITH_FILE_SOURCE == 1
#include <cstring>
namespace cAudio
{
cFileSource::cFileSource(const char* filename) : pFile(NULL), Valid(false), Filesize(0)
{
cAudioString safeFilename = fromUTF8(filename);
if(safeFilename.length() != 0)
{
pFile = fopen(toUTF8(safeFilename),"rb");
if(pFile)
Valid = true;
}
if(Valid)
{
fseek(pFile, 0, SEEK_END);
Filesize = ftell(pFile);
fseek(pFile, 0, SEEK_SET);
}
}
cFileSource::~cFileSource()
{
if(pFile)
fclose(pFile);
}
bool cFileSource::isValid()
{
return Valid;
}
int cFileSource::getCurrentPos()
{
return ftell(pFile);
}
int cFileSource::getSize()
{
return Filesize;
}
int cFileSource::read(void* output, int size)
{
return fread(output, sizeof(char), size, pFile);
}
bool cFileSource::seek(int amount, bool relative)
{
if(relative == true)
{
int oldamount = ftell(pFile);
fseek(pFile, amount, SEEK_CUR);
//check against the absolute position
if(oldamount+amount != ftell(pFile))
return false;
}
else
{
fseek(pFile, amount, SEEK_SET);
if(amount != ftell(pFile))
return false;
}
return true;
}
};
#endif

View File

@ -1,140 +1,140 @@
// Copyright (c) 2008-2011 Raynaldo (Wildicv) Rivera, Joshua (Dark_Kilauea) Jones, Murat (wolfmanfx) Sari
// This file is part of the "cAudio Engine"
// For conditions of distribution and use, see copyright notice in cAudio.h
#include <time.h>
#include <stdio.h>
#include "cLogger.h"
#include "cConsoleLogReceiver.h"
#include "cFileLogReceiver.h"
#include "cUtils.h"
namespace cAudio
{
cLogger::cLogger() : StartTime(0), MinLogLevel(ELL_INFO)
{
StartTime = clock();
}
void cLogger::logCritical( const char* sender, const char *msg, ... )
{
if(ELL_CRITICAL >= MinLogLevel)
{
Mutex.lock();
va_list args;
va_start( args, msg );
broadcastMessage( ELL_CRITICAL, sender, msg, args );
va_end( args );
Mutex.unlock();
}
}
void cLogger::logError( const char* sender, const char *msg, ... )
{
if(ELL_ERROR >= MinLogLevel)
{
Mutex.lock();
va_list args;
va_start( args, msg );
broadcastMessage( ELL_ERROR, sender, msg, args );
va_end( args );
Mutex.unlock();
}
}
void cLogger::logWarning( const char* sender, const char *msg, ... )
{
if(ELL_WARNING >= MinLogLevel)
{
Mutex.lock();
va_list args;
va_start( args, msg );
broadcastMessage( ELL_WARNING, sender, msg, args );
va_end( args );
Mutex.unlock();
}
}
void cLogger::logInfo( const char* sender, const char *msg, ... )
{
if(ELL_INFO >= MinLogLevel)
{
Mutex.lock();
va_list args;
va_start( args, msg );
broadcastMessage( ELL_INFO, sender, msg, args );
va_end( args );
Mutex.unlock();
}
}
void cLogger::logDebug( const char* sender, const char *msg, ... )
{
if(ELL_DEBUG >= MinLogLevel)
{
Mutex.lock();
va_list args;
va_start( args, msg );
broadcastMessage( ELL_DEBUG, sender, msg, args );
va_end( args );
Mutex.unlock();
}
}
void cLogger::setLogLevel( const LogLevel& logLevel )
{
Mutex.lock();
MinLogLevel = logLevel;
Mutex.unlock();
}
void cLogger::broadcastMessage( LogLevel level, const char* sender, const char* msg, va_list args )
{
float messageTime = (clock() - StartTime) / (float)CLOCKS_PER_SEC;
vsnprintf( TempTextBuf, 2048, msg, args );
ReceiversIterator it = Receivers.begin();
for (it = Receivers.begin(); it != Receivers.end(); it++)
{
it->second->OnLogMessage(sender, TempTextBuf, level, messageTime);
}
}
bool cLogger::registerLogReceiver(ILogReceiver* receiver, const char* name)
{
Mutex.lock();
cAudioString logName = safeCStr(name);
Receivers[logName] = receiver;
Mutex.unlock();
return true;
}
void cLogger::unRegisterLogReceiver(const char* name)
{
Mutex.lock();
cAudioString logName = safeCStr(name);
ReceiversIterator it = Receivers.find(logName);
if(it != Receivers.end())
{
Receivers.erase(it);
}
Mutex.unlock();
}
bool cLogger::isLogReceiverRegistered(const char* name)
{
Mutex.lock();
cAudioString logName = safeCStr(name);
ReceiversIterator it = Receivers.find(logName);
bool result = (it != Receivers.end());
Mutex.unlock();
return result;
}
ILogReceiver* cLogger::getLogReceiver(const char* name)
{
Mutex.lock();
cAudioString logName = safeCStr(name);
ReceiversIterator it = Receivers.find(logName);
if(it != Receivers.end())
{
Mutex.unlock();
return it->second;
}
Mutex.unlock();
return NULL;
}
};
// For conditions of distribution and use, see copyright notice in cAudio.h
#include <time.h>
#include <stdio.h>
#include "cLogger.h"
#include "cConsoleLogReceiver.h"
#include "cFileLogReceiver.h"
#include "cUtils.h"
namespace cAudio
{
cLogger::cLogger() : StartTime(0), MinLogLevel(ELL_INFO)
{
StartTime = clock();
}
void cLogger::logCritical( const char* sender, const char *msg, ... )
{
if(ELL_CRITICAL >= MinLogLevel)
{
Mutex.lock();
va_list args;
va_start( args, msg );
broadcastMessage( ELL_CRITICAL, sender, msg, args );
va_end( args );
Mutex.unlock();
}
}
void cLogger::logError( const char* sender, const char *msg, ... )
{
if(ELL_ERROR >= MinLogLevel)
{
Mutex.lock();
va_list args;
va_start( args, msg );
broadcastMessage( ELL_ERROR, sender, msg, args );
va_end( args );
Mutex.unlock();
}
}
void cLogger::logWarning( const char* sender, const char *msg, ... )
{
if(ELL_WARNING >= MinLogLevel)
{
Mutex.lock();
va_list args;
va_start( args, msg );
broadcastMessage( ELL_WARNING, sender, msg, args );
va_end( args );
Mutex.unlock();
}
}
void cLogger::logInfo( const char* sender, const char *msg, ... )
{
if(ELL_INFO >= MinLogLevel)
{
Mutex.lock();
va_list args;
va_start( args, msg );
broadcastMessage( ELL_INFO, sender, msg, args );
va_end( args );
Mutex.unlock();
}
}
void cLogger::logDebug( const char* sender, const char *msg, ... )
{
if(ELL_DEBUG >= MinLogLevel)
{
Mutex.lock();
va_list args;
va_start( args, msg );
broadcastMessage( ELL_DEBUG, sender, msg, args );
va_end( args );
Mutex.unlock();
}
}
void cLogger::setLogLevel( const LogLevel& logLevel )
{
Mutex.lock();
MinLogLevel = logLevel;
Mutex.unlock();
}
void cLogger::broadcastMessage( LogLevel level, const char* sender, const char* msg, va_list args )
{
float messageTime = (clock() - StartTime) / (float)CLOCKS_PER_SEC;
vsnprintf( TempTextBuf, 2048, msg, args );
ReceiversIterator it = Receivers.begin();
for (it = Receivers.begin(); it != Receivers.end(); it++)
{
it->second->OnLogMessage(sender, TempTextBuf, level, messageTime);
}
}
bool cLogger::registerLogReceiver(ILogReceiver* receiver, const char* name)
{
Mutex.lock();
cAudioString logName = fromUTF8(name);
Receivers[logName] = receiver;
Mutex.unlock();
return true;
}
void cLogger::unRegisterLogReceiver(const char* name)
{
Mutex.lock();
cAudioString logName = fromUTF8(name);
ReceiversIterator it = Receivers.find(logName);
if(it != Receivers.end())
{
Receivers.erase(it);
}
Mutex.unlock();
}
bool cLogger::isLogReceiverRegistered(const char* name)
{
Mutex.lock();
cAudioString logName = fromUTF8(name);
ReceiversIterator it = Receivers.find(logName);
bool result = (it != Receivers.end());
Mutex.unlock();
return result;
}
ILogReceiver* cLogger::getLogReceiver(const char* name)
{
Mutex.lock();
cAudioString logName = fromUTF8(name);
ReceiversIterator it = Receivers.find(logName);
if(it != Receivers.end())
{
Mutex.unlock();
return it->second;
}
Mutex.unlock();
return NULL;
}
};

View File

@ -176,9 +176,9 @@ namespace cAudio
return ov_raw_tell(&oggStream);
}
cAudioString cOggDecoder::getType() const
{
return cAudioString("cOggDecoder");
cAudioString cOggDecoder::getType() const
{
return cAudioString(_CTEXT("cOggDecoder"));
}
};

View File

@ -5,7 +5,7 @@
#pragma once
#include "cOpenALAudioDeviceList.h"
#include <al.h>
#include <al.h>
#include <alc.h>
namespace cAudio
@ -15,41 +15,41 @@ namespace cAudio
DeviceType = deviceType;
ALCenum specifier = 0;
ALCenum defaultDevice = 0;
if(DeviceType == DT_RECORDING)
{
specifier = ALC_CAPTURE_DEVICE_SPECIFIER;
defaultDevice = ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER;
}
else
{
if( alcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT") == AL_TRUE )
{
specifier = ALC_ALL_DEVICES_SPECIFIER;
defaultDevice = ALC_DEFAULT_ALL_DEVICES_SPECIFIER;
}
else if( alcIsExtensionPresent(NULL, "ALC_ENUMERATION_EXT") == AL_TRUE )
{
specifier = ALC_DEVICE_SPECIFIER;
defaultDevice = ALC_DEFAULT_DEVICE_SPECIFIER;
}
if(DeviceType == DT_RECORDING)
{
specifier = ALC_CAPTURE_DEVICE_SPECIFIER;
defaultDevice = ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER;
}
else
{
if( alcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT") == AL_TRUE )
{
specifier = ALC_ALL_DEVICES_SPECIFIER;
defaultDevice = ALC_DEFAULT_ALL_DEVICES_SPECIFIER;
}
else if( alcIsExtensionPresent(NULL, "ALC_ENUMERATION_EXT") == AL_TRUE )
{
specifier = ALC_DEVICE_SPECIFIER;
defaultDevice = ALC_DEFAULT_DEVICE_SPECIFIER;
}
}
if (specifier != 0 && defaultDevice != 0)
{
const char* deviceList = alcGetString(NULL, specifier);
if (deviceList)
{
while(*deviceList)
{
cAudioString device(deviceList);
AvailableDevices.push_back(device);
deviceList += strlen(deviceList) + 1;
}
}
// Get the name of the 'default' capture device
DefaultDevice = alcGetString(NULL, defaultDevice);
const char* deviceList = alcGetString(NULL, specifier);
if (deviceList)
{
while(*deviceList)
{
cAudioString device(fromUTF8(deviceList));
AvailableDevices.push_back(device);
deviceList += strlen(deviceList) + 1;
}
}
// Get the name of the 'default' capture device
DefaultDevice = fromUTF8(alcGetString(NULL, defaultDevice));
}
}
@ -71,7 +71,7 @@ namespace cAudio
{
return AvailableDevices[idx];
}
return cAudioString("");
return cAudioString(_CTEXT(""));
}
cAudioString cOpenALAudioDeviceList::getDeviceDescription(unsigned int idx)

View File

@ -1,119 +1,119 @@
// Copyright (c) 2008-2011 Raynaldo (Wildicv) Rivera, Joshua (Dark_Kilauea) Jones, Murat (wolfmanfx) Sari
// This file is part of the "cAudio Engine"
// For conditions of distribution and use, see copyright notice in cAudio.h
#include "cRawDecoder.h"
namespace cAudio{
cRawDecoder::cRawDecoder(IDataSource* stream, unsigned int frequency, AudioFormats format) : IAudioDecoder(stream), Frequency(frequency), Format(format)
{
}
cRawDecoder::~cRawDecoder()
{
}
AudioFormats cRawDecoder::getFormat()
{
return Format;
}
int cRawDecoder::getFrequency()
{
return Frequency;
}
bool cRawDecoder::isSeekingSupported()
{
return true;
}
bool cRawDecoder::isValid()
{
return true;
}
int cRawDecoder::readAudioData(void* output, int amount)
{
return Stream->read(output,amount);
}
bool cRawDecoder::setPosition(int position, bool relative)
{
Stream->seek(position,relative);
return true;
}
bool cRawDecoder::seek(float seconds,bool relative)
{
int SampleSize = 1;
if(Format == EAF_8BIT_MONO)
SampleSize = 1;
else if(Format == EAF_8BIT_STEREO)
SampleSize = 2;
else if(Format == EAF_16BIT_MONO)
SampleSize = 2;
else
SampleSize = 4;
int amountToSeek = seconds * (float)Frequency * (float)SampleSize;
return setPosition(amountToSeek, relative);
}
float cRawDecoder::getTotalTime()
{
int SampleSize = 0;
if(Format == EAF_8BIT_MONO)
SampleSize = 1;
else if(Format == EAF_8BIT_STEREO)
SampleSize = 2;
else if(Format == EAF_16BIT_MONO)
SampleSize = 2;
else
SampleSize = 4;
return (float)Stream->getSize() / ((float)Frequency * (float)SampleSize);
}
int cRawDecoder::getTotalSize()
{
return Stream->getSize();
}
int cRawDecoder::getCompressedSize()
{
return Stream->getSize();
}
float cRawDecoder::getCurrentTime()
{
int SampleSize = 0;
if(Format == EAF_8BIT_MONO)
SampleSize = 1;
else if(Format == EAF_8BIT_STEREO)
SampleSize = 2;
else if(Format == EAF_16BIT_MONO)
SampleSize = 2;
else
SampleSize = 4;
return (float)Stream->getCurrentPos() / ((float)Frequency * (float)SampleSize);
}
int cRawDecoder::getCurrentPosition()
{
return Stream->getCurrentPos();
}
int cRawDecoder::getCurrentCompressedPosition()
{
return Stream->getCurrentPos();
}
cAudioString cRawDecoder::getType() const
{
return cAudioString("cRawDecoder");
}
}
// For conditions of distribution and use, see copyright notice in cAudio.h
#include "cRawDecoder.h"
namespace cAudio{
cRawDecoder::cRawDecoder(IDataSource* stream, unsigned int frequency, AudioFormats format) : IAudioDecoder(stream), Frequency(frequency), Format(format)
{
}
cRawDecoder::~cRawDecoder()
{
}
AudioFormats cRawDecoder::getFormat()
{
return Format;
}
int cRawDecoder::getFrequency()
{
return Frequency;
}
bool cRawDecoder::isSeekingSupported()
{
return true;
}
bool cRawDecoder::isValid()
{
return true;
}
int cRawDecoder::readAudioData(void* output, int amount)
{
return Stream->read(output,amount);
}
bool cRawDecoder::setPosition(int position, bool relative)
{
Stream->seek(position,relative);
return true;
}
bool cRawDecoder::seek(float seconds,bool relative)
{
int SampleSize = 1;
if(Format == EAF_8BIT_MONO)
SampleSize = 1;
else if(Format == EAF_8BIT_STEREO)
SampleSize = 2;
else if(Format == EAF_16BIT_MONO)
SampleSize = 2;
else
SampleSize = 4;
int amountToSeek = seconds * (float)Frequency * (float)SampleSize;
return setPosition(amountToSeek, relative);
}
float cRawDecoder::getTotalTime()
{
int SampleSize = 0;
if(Format == EAF_8BIT_MONO)
SampleSize = 1;
else if(Format == EAF_8BIT_STEREO)
SampleSize = 2;
else if(Format == EAF_16BIT_MONO)
SampleSize = 2;
else
SampleSize = 4;
return (float)Stream->getSize() / ((float)Frequency * (float)SampleSize);
}
int cRawDecoder::getTotalSize()
{
return Stream->getSize();
}
int cRawDecoder::getCompressedSize()
{
return Stream->getSize();
}
float cRawDecoder::getCurrentTime()
{
int SampleSize = 0;
if(Format == EAF_8BIT_MONO)
SampleSize = 1;
else if(Format == EAF_8BIT_STEREO)
SampleSize = 2;
else if(Format == EAF_16BIT_MONO)
SampleSize = 2;
else
SampleSize = 4;
return (float)Stream->getCurrentPos() / ((float)Frequency * (float)SampleSize);
}
int cRawDecoder::getCurrentPosition()
{
return Stream->getCurrentPos();
}
int cRawDecoder::getCurrentCompressedPosition()
{
return Stream->getCurrentPos();
}
cAudioString cRawDecoder::getType() const
{
return cAudioString(_CTEXT("cRawDecoder"));
}
}

View File

@ -1,237 +1,237 @@
// Copyright (c) 2008-2011 Raynaldo (Wildicv) Rivera, Joshua (Dark_Kilauea) Jones, Murat (wolfmanfx) Sari
// This file is part of the "cAudio Engine"
// For conditions of distribution and use, see copyright notice in cAudio.h
#include "cWavDecoder.h"
#include <string.h>
#if CAUDIO_COMPILE_WITH_WAV_DECODER == 1
namespace cAudio
{
cWavDecoder::cWavDecoder(IDataSource* stream) : IAudioDecoder(stream), Valid(false)
{
const char* RIFFTAG = "RIFF";
const char* WAVETAG = "WAVE";
const char* FORMATTAG = "fmt ";
const char* DATATAG = "data";
char ident[4];
int tempint32 = 0;
short tempint16 = 0;
char tempint8 = 0;
unsigned int startOffset = 0;
//Read the first 4 bytes
Stream->seek(0, false);
Stream->read(ident, 4);
//Check to see if it is a valid RIFF file
if(strncmp(ident, RIFFTAG, 4) == 0)
{
Stream->read(&tempint32, 4);
//Check to see if the file is big enough to be valid (not completely accurate)
if(tempint32 >= 44)
{
Stream->read(ident, 4);
//Check that it is a wave file
if(strncmp(ident, WAVETAG, 4) == 0)
{
//Save our position
startOffset = Stream->getCurrentPos();
//Scan for the first fmt chuck (not necessarily right after)
do
{
Stream->read(ident, 4);
}
while((strncmp(ident, FORMATTAG, 4) != 0) && (Stream->getCurrentPos() < Stream->getSize()));
//Did we find it?
if(Stream->getCurrentPos() < (Stream->getSize() - 16))
{
//Yes, read it in
Stream->read(&tempint32, 4);
if(tempint32 >= 16)
{
//Check that it is in PCM format, we don't support compressed wavs
Stream->read(&tempint16, 2);
if(tempint16 == 1)
{
Stream->read(&tempint16, 2);
Channels = tempint16;
//We only support mono or stereo wavs
if(Channels == 1 || Channels == 2)
{
Stream->read(&tempint32, 4);
SampleRate = tempint32;
Stream->read(&tempint32, 4);
ByteRate = tempint32;
Stream->read(&tempint16, 2);
BlockAlign = tempint16;
Stream->read(&tempint16, 2);
BitsPerSample = tempint16;
//We only support 8 bit or 16 bit wavs
if(BitsPerSample == 8 || BitsPerSample == 16)
{
//Reset our pointer to start scanning for the data block
Stream->seek(startOffset, false);
//Scan for the first data chuck (not necessarily right after)
do
{
Stream->read(ident, 4);
}
while((strncmp(ident, DATATAG, 4) != 0) && (Stream->getCurrentPos() < Stream->getSize()));
//Did we find it?
if(Stream->getCurrentPos() < Stream->getSize())
{
//Get size of data block
Stream->read(&tempint32, 4);
DataSize = tempint32;
DataOffset = Stream->getCurrentPos();
Valid = true;
}
}
}
}
}
}
}
}
}
}
cWavDecoder::~cWavDecoder()
{
Channels = 0;
SampleRate = 0;
ByteRate = 0;
BlockAlign = 0;
BitsPerSample = 0;
DataSize = 0;
DataOffset = 0;
Valid = false;
}
AudioFormats cWavDecoder::getFormat()
{
if(Channels == 1 && BitsPerSample == 8)
return EAF_8BIT_MONO;
else if(Channels == 1 && BitsPerSample == 16)
return EAF_16BIT_MONO;
else if(Channels == 2 && BitsPerSample == 8)
return EAF_8BIT_STEREO;
else
return EAF_16BIT_STEREO;
}
int cWavDecoder::getFrequency()
{
return SampleRate;
}
bool cWavDecoder::isSeekingSupported()
{
return true;
}
bool cWavDecoder::isValid()
{
return Valid;
}
int cWavDecoder::readAudioData(void* output, int amount)
{
int currentPos = Stream->getCurrentPos();
int startPos = DataOffset;
int endPos = DataOffset + DataSize;
int amountToRead = amount;
//Bounds checks (and adjustments if possible)
if(currentPos > endPos)
return 0;
if(currentPos < startPos)
{
Stream->seek(startPos, false);
currentPos = Stream->getCurrentPos();
}
if((currentPos + amountToRead) > endPos)
amountToRead = endPos - currentPos;
if(amountToRead < 0)
amountToRead = 0;
return Stream->read(output,amountToRead);
}
bool cWavDecoder::setPosition(int position, bool relative)
{
int currentPos = Stream->getCurrentPos();
int startPos = DataOffset;
int endPos = DataOffset + DataSize;
//Bounds checks (and adjustments if possible)
if(!relative && position < startPos)
position = startPos;
if(!relative && position > endPos)
position = endPos;
if(relative && currentPos + position < startPos)
position = startPos - currentPos;
if(relative && currentPos + position > startPos)
position = endPos - currentPos;
Stream->seek(position,relative);
return true;
}
bool cWavDecoder::seek(float seconds,bool relative)
{
int amountToSeek = seconds * (float)SampleRate * (float)Channels * (float)(BitsPerSample/8);
return setPosition(amountToSeek, relative);
}
float cWavDecoder::getTotalTime()
{
return (float)Stream->getSize() / ((float)SampleRate * (float)Channels * (float)(BitsPerSample/8));
}
int cWavDecoder::getTotalSize()
{
return Stream->getSize();
}
int cWavDecoder::getCompressedSize()
{
return Stream->getSize();
}
float cWavDecoder::getCurrentTime()
{
return (float)Stream->getCurrentPos() / ((float)SampleRate * (float)Channels * (float)(BitsPerSample/8));
}
int cWavDecoder::getCurrentPosition()
{
return Stream->getCurrentPos();
}
int cWavDecoder::getCurrentCompressedPosition()
{
return Stream->getCurrentPos();
}
cAudioString cWavDecoder::getType() const
{
return cAudioString("cWavDecoder");
}
};
#endif
// For conditions of distribution and use, see copyright notice in cAudio.h
#include "cWavDecoder.h"
#include <string.h>
#if CAUDIO_COMPILE_WITH_WAV_DECODER == 1
namespace cAudio
{
cWavDecoder::cWavDecoder(IDataSource* stream) : IAudioDecoder(stream), Valid(false)
{
const char* RIFFTAG = "RIFF";
const char* WAVETAG = "WAVE";
const char* FORMATTAG = "fmt ";
const char* DATATAG = "data";
char ident[4];
int tempint32 = 0;
short tempint16 = 0;
char tempint8 = 0;
unsigned int startOffset = 0;
//Read the first 4 bytes
Stream->seek(0, false);
Stream->read(ident, 4);
//Check to see if it is a valid RIFF file
if(strncmp(ident, RIFFTAG, 4) == 0)
{
Stream->read(&tempint32, 4);
//Check to see if the file is big enough to be valid (not completely accurate)
if(tempint32 >= 44)
{
Stream->read(ident, 4);
//Check that it is a wave file
if(strncmp(ident, WAVETAG, 4) == 0)
{
//Save our position
startOffset = Stream->getCurrentPos();
//Scan for the first fmt chuck (not necessarily right after)
do
{
Stream->read(ident, 4);
}
while((strncmp(ident, FORMATTAG, 4) != 0) && (Stream->getCurrentPos() < Stream->getSize()));
//Did we find it?
if(Stream->getCurrentPos() < (Stream->getSize() - 16))
{
//Yes, read it in
Stream->read(&tempint32, 4);
if(tempint32 >= 16)
{
//Check that it is in PCM format, we don't support compressed wavs
Stream->read(&tempint16, 2);
if(tempint16 == 1)
{
Stream->read(&tempint16, 2);
Channels = tempint16;
//We only support mono or stereo wavs
if(Channels == 1 || Channels == 2)
{
Stream->read(&tempint32, 4);
SampleRate = tempint32;
Stream->read(&tempint32, 4);
ByteRate = tempint32;
Stream->read(&tempint16, 2);
BlockAlign = tempint16;
Stream->read(&tempint16, 2);
BitsPerSample = tempint16;
//We only support 8 bit or 16 bit wavs
if(BitsPerSample == 8 || BitsPerSample == 16)
{
//Reset our pointer to start scanning for the data block
Stream->seek(startOffset, false);
//Scan for the first data chuck (not necessarily right after)
do
{
Stream->read(ident, 4);
}
while((strncmp(ident, DATATAG, 4) != 0) && (Stream->getCurrentPos() < Stream->getSize()));
//Did we find it?
if(Stream->getCurrentPos() < Stream->getSize())
{
//Get size of data block
Stream->read(&tempint32, 4);
DataSize = tempint32;
DataOffset = Stream->getCurrentPos();
Valid = true;
}
}
}
}
}
}
}
}
}
}
cWavDecoder::~cWavDecoder()
{
Channels = 0;
SampleRate = 0;
ByteRate = 0;
BlockAlign = 0;
BitsPerSample = 0;
DataSize = 0;
DataOffset = 0;
Valid = false;
}
AudioFormats cWavDecoder::getFormat()
{
if(Channels == 1 && BitsPerSample == 8)
return EAF_8BIT_MONO;
else if(Channels == 1 && BitsPerSample == 16)
return EAF_16BIT_MONO;
else if(Channels == 2 && BitsPerSample == 8)
return EAF_8BIT_STEREO;
else
return EAF_16BIT_STEREO;
}
int cWavDecoder::getFrequency()
{
return SampleRate;
}
bool cWavDecoder::isSeekingSupported()
{
return true;
}
bool cWavDecoder::isValid()
{
return Valid;
}
int cWavDecoder::readAudioData(void* output, int amount)
{
int currentPos = Stream->getCurrentPos();
int startPos = DataOffset;
int endPos = DataOffset + DataSize;
int amountToRead = amount;
//Bounds checks (and adjustments if possible)
if(currentPos > endPos)
return 0;
if(currentPos < startPos)
{
Stream->seek(startPos, false);
currentPos = Stream->getCurrentPos();
}
if((currentPos + amountToRead) > endPos)
amountToRead = endPos - currentPos;
if(amountToRead < 0)
amountToRead = 0;
return Stream->read(output,amountToRead);
}
bool cWavDecoder::setPosition(int position, bool relative)
{
int currentPos = Stream->getCurrentPos();
int startPos = DataOffset;
int endPos = DataOffset + DataSize;
//Bounds checks (and adjustments if possible)
if(!relative && position < startPos)
position = startPos;
if(!relative && position > endPos)
position = endPos;
if(relative && currentPos + position < startPos)
position = startPos - currentPos;
if(relative && currentPos + position > startPos)
position = endPos - currentPos;
Stream->seek(position,relative);
return true;
}
bool cWavDecoder::seek(float seconds,bool relative)
{
int amountToSeek = seconds * (float)SampleRate * (float)Channels * (float)(BitsPerSample/8);
return setPosition(amountToSeek, relative);
}
float cWavDecoder::getTotalTime()
{
return (float)Stream->getSize() / ((float)SampleRate * (float)Channels * (float)(BitsPerSample/8));
}
int cWavDecoder::getTotalSize()
{
return Stream->getSize();
}
int cWavDecoder::getCompressedSize()
{
return Stream->getSize();
}
float cWavDecoder::getCurrentTime()
{
return (float)Stream->getCurrentPos() / ((float)SampleRate * (float)Channels * (float)(BitsPerSample/8));
}
int cWavDecoder::getCurrentPosition()
{
return Stream->getCurrentPos();
}
int cWavDecoder::getCurrentCompressedPosition()
{
return Stream->getCurrentPos();
}
cAudioString cWavDecoder::getType() const
{
return cAudioString(_CTEXT("cWavDecoder"));
}
};
#endif