OpenALBridge updates
authorkoda
Sun, 11 Oct 2009 16:03:56 +0000
changeset 2415 35d09cbf819a
parent 2414 fd9ca82077d8
child 2416 3278cf087339
OpenALBridge updates - fix problems when building for PPC targets - gives more precise error messages
openalbridge/endianness.c
openalbridge/endianness.h
openalbridge/globals.h
openalbridge/loaders.c
openalbridge/openalbridge.c
openalbridge/wrappers.c
--- a/openalbridge/endianness.c	Sun Oct 11 12:33:12 2009 +0000
+++ b/openalbridge/endianness.c	Sun Oct 11 16:03:56 2009 +0000
@@ -21,12 +21,9 @@
 #ifdef __CPLUSPLUS
 extern "C" {
 #endif 
-    
-    /*from big endian to little endian*/
-    int invert_endianness(uint32_t number){
-        return bswap_32(number);
-    }
-    
+
+
+        
 #ifdef __CPLUSPLUS
 }
 #endif
--- a/openalbridge/endianness.h	Sun Oct 11 12:33:12 2009 +0000
+++ b/openalbridge/endianness.h	Sun Oct 11 16:03:56 2009 +0000
@@ -25,9 +25,39 @@
 #ifdef __CPLUSPLUS
 extern "C" {
 #endif 
+
+
+/* check compiler requirements */
+#if !defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)
+#error Do not know the endianess of this architecture
+#endif
+
+
+/* use byteswap macros from the host system, hopefully optimized ones ;-) 
+ * or define our own version, simple, stupid, straight-forward... */
+#ifdef HAVE_BYTESWAP_H
+#include <byteswap.h>
+#else        
+#define bswap_16(x)	((((x) & 0xFF00) >> 8) | (((x) & 0x00FF) << 8))
+#define bswap_32(x)	((((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8)  | \
+(((x) & 0x0000FF00) << 8)  | (((x) & 0x000000FF) << 24) )
+#endif /* HAVE_BYTESWAP_H */
+
+
+/* swap numbers accordingly to architecture automatically */
+#ifdef __LITTLE_ENDIAN__
+#define ENDIAN_LITTLE_32(x) x
+#define ENDIAN_BIG_32(x)    bswap_32(x)
+#define ENDIAN_LITTLE_16(x) x
+#define ENDIAN_BIG_16(x)    bswap_16(x)
+#elif __BIG_ENDIAN__
+#define ENDIAN_LITTLE_32(x) bswap_32(x)
+#define ENDIAN_BIG_32(x)    x
+#define ENDIAN_LITTLE_16(x) bswap_16(x)
+#define ENDIAN_BIG_16(x)    x    
+#endif
         
-    int invert_endianness(uint32_t number);
-    
+        
 #ifdef __CPLUSPLUS
 }
 #endif
--- a/openalbridge/globals.h	Sun Oct 11 12:33:12 2009 +0000
+++ b/openalbridge/globals.h	Sun Oct 11 16:03:56 2009 +0000
@@ -32,9 +32,10 @@
 
 #include "al.h"
 
+
+/** 1.0 02/03/10 - Defines cross-platform sleep, usleep, etc. [Wu Yongwei] **/
 #ifndef _SLEEP_H
 #define _SLEEP_H
-/** 1.0 02/03/10 - Defines cross-platform sleep, usleep, etc. * By Wu Yongwei **/
 #ifdef _WIN32
 # if defined(_NEED_SLEEP_ONLY) && (defined(_MSC_VER) || defined(__MINGW32__))
 #  include <stdlib.h>
@@ -56,54 +57,54 @@
 #endif
 #endif /* _SLEEP_H */
 
-#ifdef HAVE_BYTESWAP_H
-/* use byteswap macros from the host system, hopefully optimized ones ;-) */
-#include <byteswap.h>
-#else
-/* define our own version, simple, stupid, straight-forward... */
+
+
 
-#define bswap_16(x)	((((x) & 0xFF00) >> 8) | (((x) & 0x00FF) << 8))
 
-#define bswap_32(x)	((((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8)  | \
-                         (((x) & 0x0000FF00) << 8)  | (((x) & 0x000000FF) << 24) )
 
-#endif /* HAVE_BYTESWAP_H */
 
 #ifdef __CPLUSPLUS
 extern "C" {
 #endif 
-    
-    /*data type for WAV header*/
+        
+/*data type for WAV header*/
 #pragma pack(1)
-    typedef struct _WAV_header_t {
-        uint32_t ChunkID;
-        uint32_t ChunkSize;
-        uint32_t Format;
-        uint32_t Subchunk1ID;
-        uint32_t Subchunk1Size;
-        uint16_t AudioFormat;
-        uint16_t NumChannels;
-        uint32_t SampleRate;
-        uint32_t ByteRate;
-        uint16_t BlockAlign;
-        uint16_t BitsPerSample;
-        uint32_t Subchunk2ID;
-        uint32_t Subchunk2Size;
-    } WAV_header_t;
+        typedef struct _WAV_header_t {
+                uint32_t ChunkID;
+                uint32_t ChunkSize;
+                uint32_t Format;
+                uint32_t Subchunk1ID;
+                uint32_t Subchunk1Size;
+                uint16_t AudioFormat;
+                uint16_t NumChannels;
+                uint32_t SampleRate;
+                uint32_t ByteRate;
+                uint16_t BlockAlign;
+                uint16_t BitsPerSample;
+                uint32_t Subchunk2ID;
+                uint32_t Subchunk2Size;
+        } WAV_header_t;
 #pragma pack()
-    
-    /*data type for passing data between threads*/
+        
+/*data type for passing data between threads*/
 #pragma pack(1)
-    typedef struct _fade_t {
-        uint32_t index;
-        uint16_t quantity;
-    } fade_t;
+        typedef struct _fade_t {
+                uint32_t index;
+                uint16_t quantity;
+        } fade_t;
 #pragma pack()
-    
-    /*other defines*/
-#define FADE_IN	 AL_TRUE
-#define FADE_OUT AL_FALSE
-    
+
+        
+/*file format defines*/
+#define OGG_FILE_FORMAT 0x4F676753
+#define WAV_FILE_FORMAT 0x52494646
+#define WAV_HEADER_SUBCHUNK2ID 0x64617461
+      
+
+/*other defines*/
+#define FADE_IN	 0
+#define FADE_OUT 1
+        
 #ifdef __CPLUSPLUS
 }
 #endif
--- a/openalbridge/loaders.c	Sun Oct 11 12:33:12 2009 +0000
+++ b/openalbridge/loaders.c	Sun Oct 11 16:03:56 2009 +0000
@@ -27,20 +27,21 @@
         FILE *wavfile;
         int32_t t;
         uint32_t n = 0;
+        uint8_t sub0, sub1, sub2, sub3;
         
         wavfile = Fopen(filename, "rb");
         
-        fread(&WAVHeader.ChunkID, sizeof(uint32_t), 1, wavfile);
+        fread(&WAVHeader.ChunkID, sizeof(uint32_t), 1, wavfile);                /*RIFF*/
         fread(&WAVHeader.ChunkSize, sizeof(uint32_t), 1, wavfile);
-        fread(&WAVHeader.Format, sizeof(uint32_t), 1, wavfile);
+        fread(&WAVHeader.Format, sizeof(uint32_t), 1, wavfile);                 /*WAVE*/
         
 #ifdef DEBUG
-        fprintf(stderr, "ChunkID: %X\n", invert_endianness(WAVHeader.ChunkID));
-        fprintf(stderr, "ChunkSize: %d\n", WAVHeader.ChunkSize);
-        fprintf(stderr, "Format: %X\n", invert_endianness(WAVHeader.Format));
+        fprintf(stderr, "ChunkID: %X\n", ENDIAN_BIG_32(WAVHeader.ChunkID));
+        fprintf(stderr, "ChunkSize: %d\n", ENDIAN_LITTLE_32(WAVHeader.ChunkSize));
+        fprintf(stderr, "Format: %X\n", ENDIAN_BIG_32(WAVHeader.Format));
 #endif
         
-        fread(&WAVHeader.Subchunk1ID, sizeof(uint32_t), 1, wavfile);
+        fread(&WAVHeader.Subchunk1ID, sizeof(uint32_t), 1, wavfile);            /*fmt */
         fread(&WAVHeader.Subchunk1Size, sizeof(uint32_t), 1, wavfile);
         fread(&WAVHeader.AudioFormat, sizeof(uint16_t), 1, wavfile);
         fread(&WAVHeader.NumChannels, sizeof(uint16_t), 1, wavfile);
@@ -50,78 +51,93 @@
         fread(&WAVHeader.BitsPerSample, sizeof(uint16_t), 1, wavfile);
         
 #ifdef DEBUG
-        fprintf(stderr, "Subchunk1ID: %X\n", invert_endianness(WAVHeader.Subchunk1ID));
-        fprintf(stderr, "Subchunk1Size: %d\n", WAVHeader.Subchunk1Size);
-        fprintf(stderr, "AudioFormat: %d\n", WAVHeader.AudioFormat);
-        fprintf(stderr, "NumChannels: %d\n", WAVHeader.NumChannels);
-        fprintf(stderr, "SampleRate: %d\n", WAVHeader.SampleRate);
-        fprintf(stderr, "ByteRate: %d\n", WAVHeader.ByteRate);
-        fprintf(stderr, "BlockAlign: %d\n", WAVHeader.BlockAlign);
-        fprintf(stderr, "BitsPerSample: %d\n", WAVHeader.BitsPerSample);
+        fprintf(stderr, "Subchunk1ID: %X\n", ENDIAN_BIG_32(WAVHeader.Subchunk1ID));
+        fprintf(stderr, "Subchunk1Size: %d\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk1Size));
+        fprintf(stderr, "AudioFormat: %d\n", ENDIAN_LITTLE_16(WAVHeader.AudioFormat));
+        fprintf(stderr, "NumChannels: %d\n", ENDIAN_LITTLE_16(WAVHeader.NumChannels));
+        fprintf(stderr, "SampleRate: %d\n", ENDIAN_LITTLE_32(WAVHeader.SampleRate));
+        fprintf(stderr, "ByteRate: %d\n", ENDIAN_LITTLE_32(WAVHeader.ByteRate));
+        fprintf(stderr, "BlockAlign: %d\n", ENDIAN_LITTLE_16(WAVHeader.BlockAlign));
+        fprintf(stderr, "BitsPerSample: %d\n", ENDIAN_LITTLE_16(WAVHeader.BitsPerSample));
 #endif
-        
-        do { /*remove useless header chunks (plenty room for improvements)*/
-            t = fread(&WAVHeader.Subchunk2ID, sizeof(uint32_t), 1, wavfile);
-            if (invert_endianness(WAVHeader.Subchunk2ID) == 0x64617461)
-                break;
-            if (t <= 0) { /*eof*/
-                fprintf(stderr, "ERROR: wrong WAV header\n");
-                return AL_FALSE;
-            }
+
+        /*remove useless header chunks by looking for the WAV_HEADER_SUBCHUNK2ID integer */
+        do {
+                t = fread(&sub0, sizeof(uint8_t), 1, wavfile);
+                if(sub0 == 0x64) {
+                        t = fread(&sub1, sizeof(uint8_t), 1, wavfile);
+                        if(sub1 == 0x61) {
+                                t = fread(&sub2, sizeof(uint8_t), 1, wavfile);
+                                if(sub2 == 0x74) {
+                                        t = fread(&sub3, sizeof(uint8_t), 1, wavfile);
+                                        if(sub3 == 0x61) {
+                                                WAVHeader.Subchunk2ID = WAV_HEADER_SUBCHUNK2ID;
+                                                break;                                                
+                                        } 
+                                }       
+                        }
+                }
+                
+                if (t <= 0) { 
+                        /*eof*/
+                        fprintf(stderr, "ERROR 'load_wavpcm()': wrong WAV header\n");
+                        return AL_FALSE;
+                }
         } while (1);
+            
         fread(&WAVHeader.Subchunk2Size, sizeof(uint32_t), 1, wavfile);
         
 #ifdef DEBUG
-        fprintf(stderr, "Subchunk2ID: %X\n", invert_endianness(WAVHeader.Subchunk2ID));
-        fprintf(stderr, "Subchunk2Size: %d\n", WAVHeader.Subchunk2Size);
+        fprintf(stderr, "Subchunk2ID: %X\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk2ID));
+        fprintf(stderr, "Subchunk2Size: %d\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size));
 #endif
         
-        *data = (char*) Malloc (sizeof(char) * WAVHeader.Subchunk2Size);
+        *data = (char*) Malloc (sizeof(char) * ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size));
         
-        /*this could be improved*/
+        /*read the actual sound data*/
         do {
-            n += fread(&((*data)[n]), sizeof(uint8_t), 1, wavfile);
-        } while (n < WAVHeader.Subchunk2Size);
+                n += fread(&((*data)[n]), sizeof(uint8_t), 4, wavfile);
+        } while (n < ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size));
         
         fclose(wavfile);	
-        
+            
 #ifdef DEBUG
-        fprintf(stderr, "Last two bytes of data: %X%X\n", (*data)[n-2], (*data)[n-1]);
+        fprintf(stderr, "WAV data loaded\n");
 #endif
-        
-        /*remaining parameters*/
+            
+        /*set parameters for OpenAL*/
         /*Valid formats are AL_FORMAT_MONO8, AL_FORMAT_MONO16, AL_FORMAT_STEREO8, and AL_FORMAT_STEREO16*/
-        if (WAVHeader.NumChannels == 1) {
-            if (WAVHeader.BitsPerSample == 8)
+        if (ENDIAN_LITTLE_16(WAVHeader.NumChannels) == 1) {
+            if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 8)
                 *format = AL_FORMAT_MONO8;
             else {
-                if (WAVHeader.BitsPerSample == 16)
+                if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 16)
                     *format = AL_FORMAT_MONO16;
                 else {
-                    fprintf(stderr, "ERROR: wrong WAV header - bitsample value\n");
+                    fprintf(stderr, "ERROR 'load_wavpcm()': wrong WAV header - bitsample value\n");
                     return AL_FALSE;
                 }
             } 
         } else {
-            if (WAVHeader.NumChannels == 2) {
-                if (WAVHeader.BitsPerSample == 8)
+            if (ENDIAN_LITTLE_16(WAVHeader.NumChannels) == 2) {
+                if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 8)
                     *format = AL_FORMAT_STEREO8;
                 else {
-                    if (WAVHeader.BitsPerSample == 16)
+                    if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 16)
                         *format = AL_FORMAT_STEREO16;
                     else {
-                        fprintf(stderr, "ERROR: wrong WAV header - bitsample value\n");
+                        fprintf(stderr, "ERROR 'load_wavpcm()': wrong WAV header - bitsample value\n");
                         return AL_FALSE;
                     }				
                 }
             } else {
-                fprintf(stderr, "ERROR: wrong WAV header - format value\n");
+                fprintf(stderr, "ERROR 'load_wavpcm()': wrong WAV header - format value\n");
                 return AL_FALSE;
             }
         }
         
-        *bitsize = WAVHeader.Subchunk2Size;
-        *freq = WAVHeader.SampleRate;
+        *bitsize = ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size);
+        *freq    = ENDIAN_LITTLE_32(WAVHeader.SampleRate);
         return AL_TRUE;
     }
     
@@ -139,7 +155,7 @@
 
 	result = ov_fopen((char*) filename, &oggStream);
 	if (result < 0) {
-		fprintf (stderr, "ERROR: ov_open_callbacks failed with %X", result);
+		fprintf (stderr, "ERROR 'load_oggvorbis()': ov_fopen failed with %X", result);
                 ov_clear(&oggStream);
 		return -1;
 	}
@@ -160,7 +176,7 @@
         fprintf(stderr, "PCM data size: %lld\n", pcm_length);
         fprintf(stderr, "# comment: %d\n", vorbisComment->comments);
         for (i = 0; i < vorbisComment->comments; i++)
-            fprintf(stderr, "\tComment %d: %s\n", i, vorbisComment->user_comments[i]);
+                fprintf(stderr, "\tComment %d: %s\n", i, vorbisComment->user_comments[i]);
 #endif
         
         /*allocates enough room for the decoded data*/
@@ -173,7 +189,7 @@
             if (vorbisInfo->channels == 2)
                 *format = AL_FORMAT_STEREO16;
             else {
-                fprintf(stderr, "ERROR: wrong OGG header - channel value (%d)\n", vorbisInfo->channels);
+                fprintf(stderr, "ERROR 'load_oggvorbis()': wrong OGG header - channel value (%d)\n", vorbisInfo->channels);
                 ov_clear(&oggStream);
                 return AL_FALSE;
             }
@@ -188,7 +204,7 @@
                 if (result == 0)
                     break;
                 else { 
-                    fprintf(stderr, "ERROR: end of file from OGG stream\n");
+                    fprintf(stderr, "ERROR 'load_oggvorbis()': end of file from OGG stream\n");
                     ov_clear(&oggStream);
                     return AL_FALSE;
                 }
--- a/openalbridge/openalbridge.c	Sun Oct 11 12:33:12 2009 +0000
+++ b/openalbridge/openalbridge.c	Sun Oct 11 16:03:56 2009 +0000
@@ -82,21 +82,21 @@
         ALfloat ListenerOri[] = { 0.0, 0.0, -1.0,  0.0, 1.0, 0.0 };
         
         if(openalReady == AL_TRUE) {
-            fprintf(stderr, "ERROR: OpenAL already initialized\n");
+            fprintf(stderr, "ERROR 'openal_init()': OpenAL already initialized\n");
             return AL_FALSE;
         }
 
 		if(usehardware)
 		{
 			if ((device = alcOpenDevice(NULL)) == NULL) {
-				fprintf(stderr, "ERROR: Failed to open sound device\n");
+				fprintf(stderr, "ERROR 'openal_init()': Failed to open sound device\n");
 				return AL_FALSE;
 			}
 		}
 		else
 		{
 			if ((device = alcOpenDevice("Generic Software")) == NULL) {
-				fprintf(stderr, "ERROR: Failed to open sound device\n");
+				fprintf(stderr, "ERROR 'openal_init()': Failed to open sound device\n");
 				return AL_FALSE;
 			}
 		}
@@ -106,7 +106,7 @@
         alcMakeContextCurrent(context);
         alcProcessContext(context);
         
-        if (AlGetError("ERROR %d: Creating a new contex\n") != AL_TRUE)
+        if (AlGetError("ERROR %d in 'openal_init()': Creating a new contex\n") != AL_TRUE)
             return AL_FALSE;
         
         /*allocate memory space for buffers and sources*/
@@ -115,6 +115,7 @@
 	else
 	    globalsize = memorysize;
         increment  = globalsize;
+            
         Buffers = (ALuint*) Malloc(sizeof(ALuint)*globalsize);
         Sources = (ALuint*) Malloc(sizeof(ALuint)*globalsize);
         
@@ -140,7 +141,7 @@
         globalsize += increment;
 
 #ifdef DEBUG
-        fprintf(stderr, "OpenAL: Realloc in process from %d to %d\n", oldsize, globalsize);
+        fprintf(stderr, "OpenALBridge: Realloc in process from %d to %d\n", oldsize, globalsize);
 #endif
         
         Buffers = (ALuint*) Realloc(Buffers, sizeof(ALuint)*globalsize);
@@ -162,7 +163,7 @@
         FILE *fp;
         
         if(openalReady == AL_FALSE) {
-            fprintf(stderr, "ERROR: OpenAL not initialized\n");
+            fprintf(stderr, "ERROR 'openal_loadfile()': OpenAL not initialized\n");
             return AL_FALSE;
         }
         
@@ -178,39 +179,42 @@
         fclose (fp);
         
         if (error < 0) {
-            fprintf(stderr, "ERROR: file %s is too short \n", filename);
+            fprintf(stderr, "ERROR 'openal_loadfile()': file %s is too short \n", filename);
             return -2;
         }
         
         /*prepare the buffer to receive data*/
         alGenBuffers(1, &Buffers[globalindex]);
         
-        if (AlGetError("ERROR %d: Allocating memory for buffers\n") != AL_TRUE)
+        if (AlGetError("ERROR %d in 'openal_loadfile()': Allocating memory for buffers\n") != AL_TRUE)
             return -3;
         
         /*prepare the source to emit sound*/
         alGenSources(1, &Sources[globalindex]);
         
-        if (AlGetError("ERROR %d: Allocating memory for sources\n") != AL_TRUE)
+        if (AlGetError("ERROR %d in 'openal_loadfile()': Allocating memory for sources\n") != AL_TRUE)
             return -4;
         
         
-        if (fileformat == 0x5367674F) /*check if ogg*/
-            error = load_oggvorbis (filename, &format, &data, &bitsize, &freq);
-        else {
-            if (fileformat == 0x46464952) /*check if wav*/
-                error = load_wavpcm (filename, &format, &data, &bitsize, &freq);
-            else {
-                fprintf(stderr, "ERROR: File format (%08X) not supported!\n", invert_endianness(fileformat));
-                return -5;
-            }
+        switch (ENDIAN_BIG_32(fileformat)) {
+                case OGG_FILE_FORMAT:
+                        error = load_oggvorbis (filename, &format, &data, &bitsize, &freq);
+                        break;
+                case WAV_FILE_FORMAT:
+                        error = load_wavpcm (filename, &format, &data, &bitsize, &freq);
+                        break;
+                default:
+                        fprintf(stderr, "ERROR 'openal_loadfile()': File format (%08X) not supported!\n", ENDIAN_BIG_32(fileformat));
+                        return -5;
+                        break;
         }
         
+
         /*copy pcm data in one buffer*/
         alBufferData(Buffers[globalindex], format, data, bitsize, freq);
         free(data);		/*deallocate data to save memory*/
         
-        if (AlGetError("ERROR %d: Writing data to buffer\n") != AL_TRUE)
+        if (AlGetError("ERROR %d in 'openal_loadfile()': Writing data to buffer\n") != AL_TRUE)
             return -6;
         
         /*set source properties that it will use when it's in playback*/
@@ -221,7 +225,7 @@
         alSourcefv(Sources[globalindex], AL_VELOCITY, SourceVel             );
         alSourcei (Sources[globalindex], AL_LOOPING,  0                     );
         
-        if (AlGetError("ERROR %d: Setting source properties\n") != AL_TRUE)
+        if (AlGetError("ERROR %d in 'openal_loadfile()': Setting source properties\n") != AL_TRUE)
             return -7;
         
         alGetError();  /* clear any AL errors beforehand */
@@ -236,18 +240,18 @@
         ALint loop;
         
         if(openalReady == AL_FALSE) {
-            fprintf(stderr, "ERROR: OpenAL not initialized\n");
+            fprintf(stderr, "ERROR 'openal_toggleloop()': OpenAL not initialized\n");
             return AL_FALSE;
         }
         
         if (index >= globalsize) {
-            fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex);
+            fprintf(stderr, "ERROR 'openal_toggleloop()': index out of bounds (got %d, max %d)\n", index, globalindex);
             return AL_FALSE;
         }
         
         alGetSourcei (Sources[index], AL_LOOPING, &loop);
         alSourcei (Sources[index], AL_LOOPING, !((uint8_t) loop) & 0x00000001);
-        if (AlGetError("ERROR %d: Getting or setting loop property\n") != AL_TRUE)
+        if (AlGetError("ERROR %d in 'openal_toggleloop()': Getting or setting loop property\n") != AL_TRUE)
             return AL_FALSE;
         
         alGetError();  /* clear any AL errors beforehand */
@@ -258,20 +262,20 @@
     
     ALboolean openal_setvolume (uint32_t index, uint8_t percentage) {
         if(openalReady == AL_FALSE) {
-            fprintf(stderr, "ERROR: OpenAL not initialized\n");
+            fprintf(stderr, "ERROR 'openal_setvolume()': OpenAL not initialized\n");
             return AL_FALSE;
         }
         
         /*Set volume for sound number index*/
         if (index >= globalindex) {
-            fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex);
+            fprintf(stderr, "ERROR 'openal_setvolume()': index out of bounds (got %d, max %d)\n", index, globalindex);
             return AL_FALSE;
         }
         
         if (percentage > 100)
             percentage = 100;
         alSourcef (Sources[index], AL_GAIN, (float) percentage/100.0f);
-        if (AlGetError2("ERROR %d: setting volume for sound %d\n", index) != AL_TRUE)
+        if (AlGetError2("ERROR %d in 'openal_setvolume()': setting volume for sound %d\n", index) != AL_TRUE)
             return AL_FALSE;
         
         alGetError();  /* clear any AL errors beforehand */
@@ -282,7 +286,7 @@
     
     ALboolean openal_setglobalvolume (uint8_t percentage) {
         if(openalReady == AL_FALSE) {
-            fprintf(stderr, "ERROR: OpenAL not initialized\n");
+            fprintf(stderr, "ERROR 'openal_setglobalvolume()': OpenAL not initialized\n");
             return AL_FALSE;
         }
         
@@ -290,7 +294,7 @@
         if (percentage > 100)
             percentage = 100;
         alListenerf (AL_GAIN, (float) percentage/100.0f);
-        if (AlGetError("ERROR %d: Setting global volume\n") != AL_TRUE)
+        if (AlGetError("ERROR %d in 'openal_setglobalvolume()': Setting global volume\n") != AL_TRUE)
             return AL_FALSE;
         
         alGetError();  /* clear any AL errors beforehand */
@@ -304,7 +308,7 @@
         ALfloat mute;
         
         if(openalReady == AL_FALSE) {
-            fprintf(stderr, "ERROR: OpenAL not initialized\n");
+            fprintf(stderr, "ERROR 'openal_togglemute()': OpenAL not initialized\n");
             return AL_FALSE;
         }
         
@@ -314,7 +318,7 @@
         else
             mute = 1.0;
         alListenerf (AL_GAIN, mute);
-        if (AlGetError("ERROR %d: Setting mute property\n") != AL_TRUE)
+        if (AlGetError("ERROR %d in 'openal_togglemute()': Setting mute property\n") != AL_TRUE)
             return AL_FALSE;
         
         alGetError();  /* clear any AL errors beforehand */
@@ -334,7 +338,7 @@
         fade_t *fade;
         
         if(openalReady == AL_FALSE) {
-            fprintf(stderr, "ERROR: OpenAL not initialized\n");
+            fprintf(stderr, "ERROR 'openal_fade()': OpenAL not initialized\n");
             return AL_FALSE;
         }
         
@@ -343,29 +347,31 @@
         fade->quantity = quantity;
         
         if (index >= globalindex) {
-            fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex);
+            fprintf(stderr, "ERROR 'openal_fade()': index out of bounds (got %d, max %d)\n", index, globalindex);
             return AL_FALSE;
         }
-        
-        if (direction == FADE_IN)
+            
+            switch (direction) {
+                    case FADE_IN:
 #ifndef _WIN32
-            pthread_create(&thread, NULL, helper_fadein, (void*) fade);
+                            pthread_create(&thread, NULL, helper_fadein, (void*) fade);
 #else
-        Thread = _beginthread(&helper_fadein, 0, (void*) fade);
+                            Thread = _beginthread(&helper_fadein, 0, (void*) fade);
 #endif
-        else {
-            if (direction == FADE_OUT)
+                            break;
+                    case FADE_OUT:
 #ifndef _WIN32
-                pthread_create(&thread, NULL, helper_fadeout, (void*) fade);
+                            pthread_create(&thread, NULL, helper_fadeout, (void*) fade);
 #else
-            Thread = _beginthread(&helper_fadeout, 0, (void*) fade);
+                            Thread = _beginthread(&helper_fadeout, 0, (void*) fade);
 #endif	
-            else {
-                fprintf(stderr, "ERROR: unknown direction for fade (%d)\n", direction);
-                free(fade);
-                return AL_FALSE;
+                            break;
+                    default:
+                            fprintf(stderr, "ERROR 'openal_fade()': unknown direction for fade (%d)\n", direction);
+                            free(fade);
+                            return AL_FALSE;
+                            break;
             }
-        }
         
 #ifndef _WIN32
         pthread_detach(thread);
@@ -391,16 +397,17 @@
     
     ALboolean openal_setposition (uint32_t index, float x, float y, float z) {
         if(openalReady == AL_FALSE) {
-            fprintf(stderr, "ERROR: OpenAL not initialized\n");
+            fprintf(stderr, "ERROR 'openal_setposition()': OpenAL not initialized\n");
             return AL_FALSE;
         }
+        
         if (index >= globalindex) {
-            fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex);
+            fprintf(stderr, "ERROR 'openal_setposition()': index out of bounds (got %d, max %d)\n", index, globalindex);
             return AL_FALSE;
         }
         
         alSource3f(Sources[index], AL_POSITION, x, y, z);
-        if (AlGetError2("ERROR %d: setting position for sound %d\n", index) != AL_TRUE)
+        if (AlGetError2("ERROR %d in 'openal_setposition()': setting position for sound %d\n", index) != AL_TRUE)
             return AL_FALSE;
         
         return AL_TRUE;
@@ -409,17 +416,17 @@
     
     ALboolean openal_playsound (uint32_t index){
         if(openalReady == AL_FALSE) {
-            fprintf(stderr, "ERROR: OpenAL not initialized\n");
+            fprintf(stderr, "ERROR 'openal_playsound()': OpenAL not initialized\n");
             return AL_FALSE;
         }
         
         /*Play sound number index*/
         if (index >= globalindex) {
-            fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex);
+            fprintf(stderr, "ERROR 'openal_playsound()': index out of bounds (got %d, max %d)\n", index, globalindex);
             return AL_FALSE;
         }
         alSourcePlay(Sources[index]);
-        if (AlGetError2("ERROR %d: Playing sound %d\n", index) != AL_TRUE)
+        if (AlGetError2("ERROR %d in 'openal_playsound()': Playing sound %d\n", index) != AL_TRUE)
             return AL_FALSE;
         
         alGetError();  /* clear any AL errors beforehand */
@@ -430,17 +437,17 @@
     
     ALboolean openal_pausesound(uint32_t index){
         if(openalReady == AL_FALSE) {
-            fprintf(stderr, "ERROR: OpenAL not initialized\n");
+            fprintf(stderr, "ERROR 'openal_pausesound()': OpenAL not initialized\n");
             return AL_FALSE;
         }
         
         /*Pause sound number index*/
         if (index >= globalindex) {
-            fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex);
+            fprintf(stderr, "ERROR 'openal_pausesound()': index out of bounds (got %d, max %d)\n", index, globalindex);
             return AL_FALSE;
         }
         alSourcePause(Sources[index]);
-        if (AlGetError2("ERROR %d: Pausing sound %d\n", index) != AL_TRUE)
+        if (AlGetError2("ERROR %d in 'openal_pausesound()': Pausing sound %d\n", index) != AL_TRUE)
             return AL_FALSE;
         
         return AL_TRUE;
@@ -449,17 +456,17 @@
     
     ALboolean openal_stopsound(uint32_t index){
         if(openalReady == AL_FALSE) {
-            fprintf(stderr, "ERROR: OpenAL not initialized\n");
+            fprintf(stderr, "ERROR 'openal_stopsound()': OpenAL not initialized\n");
             return AL_FALSE;
         }
         
         /*Stop sound number index*/
         if (index >= globalindex) {
-            fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)\n", index, globalindex);
+            fprintf(stderr, "ERROR 'openal_stopsound()': index out of bounds (got %d, max %d)\n", index, globalindex);
             return AL_FALSE;
         }
         alSourceStop(Sources[index]);
-        if (AlGetError2("ERROR %d: Stopping sound %d\n", index) != AL_TRUE)
+        if (AlGetError2("ERROR %d in 'openal_stopsound()': Stopping sound %d\n", index) != AL_TRUE)
             return AL_FALSE;
         
         alGetError();  /* clear any AL errors beforehand */
--- a/openalbridge/wrappers.c	Sun Oct 11 12:33:12 2009 +0000
+++ b/openalbridge/wrappers.c	Sun Oct 11 16:03:56 2009 +0000
@@ -26,8 +26,9 @@
     
     void *Malloc (size_t nbytes) {
         void *aptr;
+            
         if ((aptr = malloc(nbytes)) == NULL) {
-            fprintf(stderr, "ERROR: not enough memory! malloc() failed\n");
+            fprintf(stderr, "ERROR 'malloc()': not enough memory\n");
             exit(-1);
         }
         return aptr;
@@ -38,7 +39,7 @@
         aptr = realloc(aptr, nbytes);
         
         if (aptr == NULL) {
-            fprintf(stderr, "ERROR: not enough memory! realloc() failed\n");
+            fprintf(stderr, "ERROR 'realloc()': not enough memory\n");
             free(aptr);
             exit(-1);
         }
@@ -49,11 +50,11 @@
     FILE *Fopen (const char *fname, char *mode)	{
         FILE *fp;
         if ((fp=fopen(fname,mode)) == NULL)
-            fprintf (stderr, "ERROR: can't open file %s in mode '%s'\n", fname, mode);
+            fprintf (stderr, "ERROR 'fopen()': can't open file %s in mode '%s'\n", fname, mode);
         return fp;
     }
     
-    
+    /*TODO make a proper error reporting routine*/
     ALint AlGetError (const char *str) {
         ALenum error;
         
@@ -76,12 +77,7 @@
             return AL_TRUE;
     }
     
-#ifndef _WIN32
-    void *helper_fadein(void *tmp) 
-#else
-    void *helper_fadein(void *tmp) 
-#endif
-    {
+    void *helper_fadein(void *tmp) {
         ALfloat gain;
         ALfloat target_gain;
         fade_t *fade;
@@ -94,7 +90,7 @@
         free (fade);
         
 #ifdef DEBUG
-        fprintf(stderr, "Fade-out: index %d quantity %d\n", index, quantity);
+        fprintf(stderr, "Fade-in: index %d quantity %d\n", index, quantity);
 #endif
         
         /*save the volume desired after the fade*/
@@ -106,13 +102,13 @@
         
         for (gain = 0.0f ; gain <= target_gain; gain += (float) quantity/10000) {
 #ifdef DEBUG
-            fprintf(stderr, "Fade-in: Set gain to: %f\n", gain);
+            fprintf(stderr, "Fade-in: set gain to: %f\n", gain);
 #endif
             alSourcef(Sources[index], AL_GAIN, gain);
             usleep(10000);
         }
         
-        AlGetError("ERROR %d: Setting fade in volume\n");
+        AlGetError("ERROR %d in 'helper_fadein()': Setting fade in volume\n");
         
 #ifndef _WIN32
         pthread_exit(NULL);
@@ -122,13 +118,7 @@
         return 0;
     }
     
-    
-#ifndef _WIN32
-    void *helper_fadeout(void *tmp) 
-#else
-    void *helper_fadeout(void *tmp) 	
-#endif
-    {
+    void *helper_fadeout(void *tmp) {
         ALfloat gain;
         ALfloat old_gain;
         fade_t *fade;
@@ -154,7 +144,7 @@
             usleep(10000);
         }
         
-        AlGetError("ERROR %d: Setting fade out volume\n");
+        AlGetError("ERROR %d in 'helper_fadeout()': Setting fade out volume\n");
         
         /*stop that sound and reset its volume*/
         alSourceStop (Sources[index]);