Cleaned a little Audio system. Added support for stream seeking.

Cleaned a little HaikuTTF.
Updated GLEW.
This commit is contained in:
spartanj
2010-07-26 00:37:38 -03:00
parent 8407055826
commit a7774298b0
29 changed files with 32866 additions and 27659 deletions

View File

@@ -2,38 +2,47 @@
namespace EE { namespace Audio {
cSoundBuffer::cSoundBuffer() : myBuffer (0), myDuration(0.f) {
ALCheck(alGenBuffers(1, &myBuffer));
cSoundBuffer::cSoundBuffer() :
mBuffer(0),
mDuration(0.f)
{
ALCheck( alGenBuffers( 1, &mBuffer ) );
}
cSoundBuffer::cSoundBuffer(const cSoundBuffer& Copy) : cAudioResource(Copy), myBuffer (0), mySamples (Copy.mySamples), myDuration (Copy.myDuration) {
ALCheck(alGenBuffers(1, &myBuffer));
Update(Copy.GetChannelsCount(), Copy.GetSampleRate());
cSoundBuffer::cSoundBuffer(const cSoundBuffer& Copy) :
cAudioResource(Copy),
mBuffer(0),
mSamples(Copy.mSamples),
mDuration(Copy.mDuration)
{
ALCheck( alGenBuffers( 1, &mBuffer ) );
Update( Copy.GetChannelsCount(), Copy.GetSampleRate() );
}
cSoundBuffer::~cSoundBuffer() {
if (myBuffer)
ALCheck(alDeleteBuffers(1, &myBuffer));
if ( mBuffer )
ALCheck( alDeleteBuffers( 1, &mBuffer ) );
}
bool cSoundBuffer::LoadFromFile(const std::string& Filename) {
// Create the sound file
std::auto_ptr<cSoundFile> File(cSoundFile::CreateRead(Filename));
std::auto_ptr<cSoundFile> File( cSoundFile::CreateRead( Filename ) );
// Open the sound file
if ( File.get() ) {
// Get the sound parameters
std::size_t NbSamples = File->GetSamplesCount();
unsigned int ChannelsCount = File->GetChannelsCount();
std::size_t NbSamples = File->GetSamplesCount();
unsigned int ChannelsCount = File->GetChannelsCount();
unsigned int SampleRate = File->GetSampleRate();
// Read the samples from the opened file
mySamples.resize(NbSamples);
if (File->Read(&mySamples[0], NbSamples) == NbSamples) {
mSamples.resize( NbSamples );
if ( File->Read( &mSamples[0], NbSamples ) == NbSamples ) {
cLog::instance()->Write( "Sound file " + Filename + " loaded." );
// Update the internal buffer with the new samples
return Update(ChannelsCount, SampleRate);
return Update( ChannelsCount, SampleRate );
} else {
cLog::instance()->Write( "Failed to read audio data from file \"" + Filename + "\"" );
return false;
@@ -46,31 +55,32 @@ bool cSoundBuffer::LoadFromFile(const std::string& Filename) {
bool cSoundBuffer::LoadFromPack( cPack* Pack, const std::string& FilePackPath ) {
std::vector<Uint8> TmpData;
if ( Pack->IsOpen() && Pack->ExtractFileToMemory( FilePackPath, TmpData ) )
return LoadFromMemory( reinterpret_cast<const char*> (&TmpData[0]), TmpData.size() );
return LoadFromMemory( reinterpret_cast<const char*> ( &TmpData[0] ), TmpData.size() );
return false;
}
bool cSoundBuffer::LoadFromMemory(const char* Data, std::size_t SizeInBytes) {
bool cSoundBuffer::LoadFromMemory( const char* Data, std::size_t SizeInBytes ) {
// Create the sound file
std::auto_ptr<cSoundFile> File(cSoundFile::CreateRead(Data, SizeInBytes));
std::auto_ptr<cSoundFile> File( cSoundFile::CreateRead( Data, SizeInBytes ) );
// Open the sound file
if (File.get()) {
if ( File.get() ) {
// Get the sound parameters
std::size_t NbSamples = File->GetSamplesCount();
unsigned int ChannelsCount = File->GetChannelsCount();
std::size_t NbSamples = File->GetSamplesCount();
unsigned int ChannelsCount = File->GetChannelsCount();
unsigned int SampleRate = File->GetSampleRate();
// Read the samples from the opened file
mySamples.resize(NbSamples);
if (File->Read(&mySamples[0], NbSamples) == NbSamples) {
mSamples.resize( NbSamples );
if ( File->Read( &mSamples[0], NbSamples ) == NbSamples ) {
cLog::instance()->Write( "Sound file loaded from memory." );
// Update the internal buffer with the new samples
return Update(ChannelsCount, SampleRate);
return Update( ChannelsCount, SampleRate );
} else {
cLog::instance()->Write( "Failed to read audio data from file in memory" );
return false;
@@ -81,15 +91,15 @@ bool cSoundBuffer::LoadFromMemory(const char* Data, std::size_t SizeInBytes) {
}
}
bool cSoundBuffer::LoadFromSamples(const Int16* Samples, std::size_t SamplesCount, unsigned int ChannelsCount, unsigned int SampleRate) {
if (Samples && SamplesCount && ChannelsCount && SampleRate) {
bool cSoundBuffer::LoadFromSamples( const Int16 * Samples, std::size_t SamplesCount, unsigned int ChannelsCount, unsigned int SampleRate ) {
if ( Samples && SamplesCount && ChannelsCount && SampleRate ) {
// Copy the new audio samples
mySamples.assign(Samples, Samples + SamplesCount);
mSamples.assign( Samples, Samples + SamplesCount );
cLog::instance()->Write( "Sound file loaded from memory samples." );
// Update the internal buffer with the new samples
return Update(ChannelsCount, SampleRate);
return Update( ChannelsCount, SampleRate );
} else {
// Error...
cLog::instance()->Write( "Failed to load sound buffer from memory Samples" );
@@ -99,10 +109,11 @@ bool cSoundBuffer::LoadFromSamples(const Int16* Samples, std::size_t SamplesCoun
bool cSoundBuffer::SaveToFile(const std::string& Filename) const {
// Create the sound file in write mode
std::auto_ptr<cSoundFile> File(cSoundFile::CreateWrite(Filename, GetChannelsCount(), GetSampleRate()));
if (File.get()) {
std::auto_ptr<cSoundFile> File( cSoundFile::CreateWrite( Filename, GetChannelsCount(), GetSampleRate() ) );
if ( File.get() ) {
// Write the samples to the opened file
File->Write(&mySamples[0], mySamples.size());
File->Write( &mSamples[0], mSamples.size() );
return true;
} else {
// Error...
@@ -112,58 +123,59 @@ bool cSoundBuffer::SaveToFile(const std::string& Filename) const {
}
const Int16* cSoundBuffer::GetSamples() const {
return mySamples.empty() ? NULL : &mySamples[0];
return mSamples.empty() ? NULL : &mSamples[0];
}
std::size_t cSoundBuffer::GetSamplesCount() const {
return mySamples.size();
return mSamples.size();
}
unsigned int cSoundBuffer::GetSampleRate() const {
ALint SampleRate;
ALCheck(alGetBufferi(myBuffer, AL_FREQUENCY, &SampleRate));
ALCheck( alGetBufferi( mBuffer, AL_FREQUENCY, &SampleRate ) );
return SampleRate;
}
unsigned int cSoundBuffer::GetChannelsCount() const {
ALint ChannelsCount;
ALCheck(alGetBufferi(myBuffer, AL_CHANNELS, &ChannelsCount));
ALCheck( alGetBufferi( mBuffer, AL_CHANNELS, &ChannelsCount ) );
return ChannelsCount;
}
eeFloat cSoundBuffer::GetDuration() const {
return myDuration;
return mDuration;
}
cSoundBuffer& cSoundBuffer::operator =(const cSoundBuffer& Other) {
cSoundBuffer Temp(Other);
cSoundBuffer& cSoundBuffer::operator =( const cSoundBuffer& Other ) {
cSoundBuffer Temp( Other );
mSamples.swap( Temp.mSamples );
std::swap( mBuffer, Temp.mBuffer );
std::swap( mDuration, Temp.mDuration );
mySamples.swap(Temp.mySamples);
std::swap(myBuffer, Temp.myBuffer);
std::swap(myDuration, Temp.myDuration);
return *this;
}
bool cSoundBuffer::Update(unsigned int ChannelsCount, unsigned int SampleRate) {
bool cSoundBuffer::Update( unsigned int ChannelsCount, unsigned int SampleRate ) {
// Check parameters
if (!SampleRate || !ChannelsCount || mySamples.empty())
if ( !SampleRate || !ChannelsCount || mSamples.empty() )
return false;
// Find the good format according to the number of channels
ALenum Format = cAudioDevice::instance().GetFormatFromChannelsCount(ChannelsCount);
ALenum Format = cAudioDevice::instance()->GetFormatFromChannelsCount( ChannelsCount );
// Check if the format is valid
if (Format == 0) {
if ( Format == 0 ) {
cLog::instance()->Write( "Unsupported number of channels." );
return false;
}
// Fill the buffer
ALsizei Size = static_cast<ALsizei>(mySamples.size()) * sizeof(Int16);
ALCheck(alBufferData(myBuffer, Format, &mySamples[0], Size, SampleRate));
ALsizei Size = static_cast<ALsizei>( mSamples.size() ) * sizeof(Int16);
ALCheck( alBufferData( mBuffer, Format, &mSamples[0], Size, SampleRate ) );
// Compute the duration
myDuration = static_cast<eeFloat>(mySamples.size()) / SampleRate / ChannelsCount;
mDuration = static_cast<eeFloat>( mSamples.size() ) / SampleRate / ChannelsCount;
return true;
}