* improve code readability (e.g. only keeping the `if (malloc(...) == NULL) handleError;` part, so as not to wrap all the rest of the code in a big (recursive) else block

* remove function `static void sound_SaveTrackData` because the operation it performs doesn't justify a whole function (and its calling overhead)
 * some cleanup in functions sound_ReadTrackFromBuffer and sound_ReadTrackFromFile
  * commented operation of both functions 
  * moved the content of function sound_SaveTrackData (only one statement) to sound_ReadTrackFromBuffer
  * modified sound_ReadTrackFromFile to not allocate double the size of the needed memory for a buffer

git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@1137 4a71c877-e1ca-e34f-864e-861f7616d084
master
Giel van Schijndel 2007-02-18 21:30:51 +00:00
parent 3a1d1a6069
commit b73646d04b
2 changed files with 50 additions and 39 deletions

View File

@ -258,16 +258,6 @@ BOOL sound_QueueSamplePlaying( void )
}
}
//*
// =======================================================================================================================
// =======================================================================================================================
//
static void sound_SaveTrackData( TRACK *psTrack, ALuint buffer )
{
// save buffer name in track
psTrack->iBufferName = buffer;
}
//*
// =======================================================================================================================
// =======================================================================================================================
@ -344,14 +334,17 @@ BOOL sound_ReadTrackFromBuffer( TRACK *psTrack, void *pBuffer, UDWORD udwSize )
static ALuint data_size;
int result, section;
// Set some info for the ovbuf_callbacks functions
ovbuf.buffer = pBuffer;
ovbuf.size = udwSize;
ovbuf.pos = 0;
// Open resource for decoding
if (ov_open_callbacks(&ovbuf, &ogg_stream, NULL, 0, ovbuf_callbacks) < 0) {
return FALSE;
}
// Aquire some info about the sound data
ogg_info = ov_info(&ogg_stream, -1);
if (ogg_info->channels == 1) {
@ -359,27 +352,39 @@ BOOL sound_ReadTrackFromBuffer( TRACK *psTrack, void *pBuffer, UDWORD udwSize )
} else {
format = AL_FORMAT_STEREO16;
}
freq = ogg_info->rate;
freq = ogg_info->rate; // Sample rate in Hz
// Allocate an initial buffer to contain the decoded PCM data
if (data == NULL) {
data_size = 8192;
data = malloc(data_size);
}
// Decode PCM data into the buffer until there is nothing to decode left
result = ov_read(&ogg_stream, (char *)data+size, data_size-size, OGG_ENDIAN, 2, 1, &section);
while( result != 0 ) {
size += result;
// If the PCM buffer has become to small increase it by reallocating double its previous size
if (size == data_size) {
data_size *= 2;
data = realloc(data, data_size);
}
// Decode
result = ov_read(&ogg_stream, (char *)data+size, data_size-size, OGG_ENDIAN, 2, 1, &section);
}
// Close the OggVorbis decoding stream
ov_clear(&ogg_stream);
// Create an OpenAL buffer and fill it with the decoded data
alGenBuffers(1, &buffer);
alBufferData(buffer, format, data, size, freq);
sound_SaveTrackData(psTrack, buffer);
// save buffer name in track
psTrack->iBufferName = buffer;
return TRUE;
}
@ -389,24 +394,32 @@ BOOL sound_ReadTrackFromBuffer( TRACK *psTrack, void *pBuffer, UDWORD udwSize )
//
BOOL sound_ReadTrackFromFile(TRACK *psTrack, char szFileName[])
{
PHYSFS_file * f = PHYSFS_openRead(szFileName);
// Use PhysicsFS to open the file
PHYSFS_file * fileHandle = PHYSFS_openRead(szFileName);
static char* buffer = NULL;
static unsigned int buffer_size = 0;
unsigned int size;
if (f == NULL) return FALSE;
if (fileHandle == NULL) return FALSE;
size = PHYSFS_fileLength(f);
// Aquire size of the file
size = PHYSFS_fileLength(fileHandle);
assert( size > -1 );
// If the size of the file to read is larger than that
// of the current buffer, then increase the buffer's size.
if (size > buffer_size) {
if (buffer != NULL) free(buffer);
buffer_size = size*2;
if (buffer != NULL)
free(buffer);
buffer_size = size;
buffer = (char*)malloc(buffer_size);
}
PHYSFS_read(f, buffer, 1, size);
// Read the specified entirely into memory
PHYSFS_read(fileHandle, buffer, 1, size);
// Now use sound_ReadTrackFromBuffer to decode the file's contents
return sound_ReadTrackFromBuffer(psTrack, buffer, size);
}

View File

@ -199,9 +199,10 @@ TRACK *sound_LoadTrackFromBuffer(char *pBuffer, UDWORD udwSize)
abort();
return NULL;
}
else
{
pTrack->bMemBuffer = TRUE;
// Set filename in struct
pTrack->pName = (char*)MALLOC( strlen(GetLastResourceFilename()) + 1 );
if ( pTrack->pName == NULL )
{
@ -210,18 +211,15 @@ TRACK *sound_LoadTrackFromBuffer(char *pBuffer, UDWORD udwSize)
FREE( pTrack );
return NULL;
}
strcpy( pTrack->pName, GetLastResourceFilename() );
// Set HASH of resource in filename
pTrack->resID = GetLastHashName();
if ( sound_ReadTrackFromBuffer(pTrack, pBuffer, udwSize) == FALSE )
{
return NULL;
}
else
{
return pTrack;
}
}
}
//*