openalbridge/loaders.c
changeset 2259 ca42efdce3ce
parent 2257 7eb31efcfb9b
child 2260 31756e21c436
--- a/openalbridge/loaders.c	Sun Jul 12 13:57:20 2009 +0000
+++ b/openalbridge/loaders.c	Sun Jul 12 15:42:54 2009 +0000
@@ -21,185 +21,185 @@
 #ifdef __CPLUSPLUS
 extern "C" {
 #endif 
-	
-	int load_wavpcm (const char *filename, ALenum *format, char ** data, ALsizei *bitsize, ALsizei *freq) {
-		WAV_header_t WAVHeader;
-		FILE *wavfile;
-		int32_t t;
-		uint32_t n = 0;
-		
-		wavfile = Fopen(filename, "rb");
-		
-		fread(&WAVHeader.ChunkID, sizeof(uint32_t), 1, wavfile);
-		fread(&WAVHeader.ChunkSize, sizeof(uint32_t), 1, wavfile);
-		fread(&WAVHeader.Format, sizeof(uint32_t), 1, wavfile);
-		
+    
+    int load_wavpcm (const char *filename, ALenum *format, char ** data, ALsizei *bitsize, ALsizei *freq) {
+        WAV_header_t WAVHeader;
+        FILE *wavfile;
+        int32_t t;
+        uint32_t n = 0;
+        
+        wavfile = Fopen(filename, "rb");
+        
+        fread(&WAVHeader.ChunkID, sizeof(uint32_t), 1, wavfile);
+        fread(&WAVHeader.ChunkSize, sizeof(uint32_t), 1, wavfile);
+        fread(&WAVHeader.Format, sizeof(uint32_t), 1, wavfile);
+        
 #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", invert_endianness(WAVHeader.ChunkID));
+        fprintf(stderr, "ChunkSize: %d\n", WAVHeader.ChunkSize);
+        fprintf(stderr, "Format: %X\n", invert_endianness(WAVHeader.Format));
 #endif
-		
-		fread(&WAVHeader.Subchunk1ID, sizeof(uint32_t), 1, wavfile);
-		fread(&WAVHeader.Subchunk1Size, sizeof(uint32_t), 1, wavfile);
-		fread(&WAVHeader.AudioFormat, sizeof(uint16_t), 1, wavfile);
-		fread(&WAVHeader.NumChannels, sizeof(uint16_t), 1, wavfile);
-		fread(&WAVHeader.SampleRate, sizeof(uint32_t), 1, wavfile);
-		fread(&WAVHeader.ByteRate, sizeof(uint32_t), 1, wavfile);
-		fread(&WAVHeader.BlockAlign, sizeof(uint16_t), 1, wavfile);
-		fread(&WAVHeader.BitsPerSample, sizeof(uint16_t), 1, wavfile);
-		
+        
+        fread(&WAVHeader.Subchunk1ID, sizeof(uint32_t), 1, wavfile);
+        fread(&WAVHeader.Subchunk1Size, sizeof(uint32_t), 1, wavfile);
+        fread(&WAVHeader.AudioFormat, sizeof(uint16_t), 1, wavfile);
+        fread(&WAVHeader.NumChannels, sizeof(uint16_t), 1, wavfile);
+        fread(&WAVHeader.SampleRate, sizeof(uint32_t), 1, wavfile);
+        fread(&WAVHeader.ByteRate, sizeof(uint32_t), 1, wavfile);
+        fread(&WAVHeader.BlockAlign, sizeof(uint16_t), 1, wavfile);
+        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", 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);
 #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;
-			}
-		} while (1);
-		fread(&WAVHeader.Subchunk2Size, sizeof(uint32_t), 1, wavfile);
-		
+        
+        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;
+            }
+        } 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", invert_endianness(WAVHeader.Subchunk2ID));
+        fprintf(stderr, "Subchunk2Size: %d\n", WAVHeader.Subchunk2Size);
 #endif
-		
-		*data = (char*) Malloc (sizeof(char) * WAVHeader.Subchunk2Size);
-		
-		/*this could be improved*/
-		do {
-			n += fread(&((*data)[n]), sizeof(uint8_t), 1, wavfile);
-		} while (n < WAVHeader.Subchunk2Size);
-		
-		fclose(wavfile);	
-		
+        
+        *data = (char*) Malloc (sizeof(char) * WAVHeader.Subchunk2Size);
+        
+        /*this could be improved*/
+        do {
+            n += fread(&((*data)[n]), sizeof(uint8_t), 1, wavfile);
+        } while (n < WAVHeader.Subchunk2Size);
+        
+        fclose(wavfile);	
+        
 #ifdef DEBUG
-		fprintf(stderr, "Last two bytes of data: %X%X\n", (*data)[n-2], (*data)[n-1]);
+        fprintf(stderr, "Last two bytes of data: %X%X\n", (*data)[n-2], (*data)[n-1]);
 #endif
-		
-		/*remaining parameters*/
-		/*Valid formats are AL_FORMAT_MONO8, AL_FORMAT_MONO16, AL_FORMAT_STEREO8, and AL_FORMAT_STEREO16*/
-		if (WAVHeader.NumChannels == 1) {
-			if (WAVHeader.BitsPerSample == 8)
-				*format = AL_FORMAT_MONO8;
-			else {
-				if (WAVHeader.BitsPerSample == 16)
-					*format = AL_FORMAT_MONO16;
-				else {
-					fprintf(stderr, "ERROR: wrong WAV header - bitsample value\n");
-					return AL_FALSE;
-				}
-			} 
-		} else {
-			if (WAVHeader.NumChannels == 2) {
-				if (WAVHeader.BitsPerSample == 8)
-					*format = AL_FORMAT_STEREO8;
-				else {
-					if (WAVHeader.BitsPerSample == 16)
-						*format = AL_FORMAT_STEREO16;
-					else {
-						fprintf(stderr, "ERROR: wrong WAV header - bitsample value\n");
-						return AL_FALSE;
-					}				
-				}
-			} else {
-				fprintf(stderr, "ERROR: wrong WAV header - format value\n");
-				return AL_FALSE;
-			}
-		}
-		
-		*bitsize = WAVHeader.Subchunk2Size;
-		*freq = WAVHeader.SampleRate;
-		return AL_TRUE;
-	}
-	
+        
+        /*remaining parameters*/
+        /*Valid formats are AL_FORMAT_MONO8, AL_FORMAT_MONO16, AL_FORMAT_STEREO8, and AL_FORMAT_STEREO16*/
+        if (WAVHeader.NumChannels == 1) {
+            if (WAVHeader.BitsPerSample == 8)
+                *format = AL_FORMAT_MONO8;
+            else {
+                if (WAVHeader.BitsPerSample == 16)
+                    *format = AL_FORMAT_MONO16;
+                else {
+                    fprintf(stderr, "ERROR: wrong WAV header - bitsample value\n");
+                    return AL_FALSE;
+                }
+            } 
+        } else {
+            if (WAVHeader.NumChannels == 2) {
+                if (WAVHeader.BitsPerSample == 8)
+                    *format = AL_FORMAT_STEREO8;
+                else {
+                    if (WAVHeader.BitsPerSample == 16)
+                        *format = AL_FORMAT_STEREO16;
+                    else {
+                        fprintf(stderr, "ERROR: wrong WAV header - bitsample value\n");
+                        return AL_FALSE;
+                    }				
+                }
+            } else {
+                fprintf(stderr, "ERROR: wrong WAV header - format value\n");
+                return AL_FALSE;
+            }
+        }
+        
+        *bitsize = WAVHeader.Subchunk2Size;
+        *freq = WAVHeader.SampleRate;
+        return AL_TRUE;
+    }
     
-	int load_oggvorbis (const char *filename, ALenum *format, char **data, ALsizei *bitsize, ALsizei *freq) {
-		/*implementation inspired from http://www.devmaster.net/forums/showthread.php?t=1153 */
-		FILE			*oggFile;		/*ogg handle*/
-		OggVorbis_File  oggStream;		/*stream handle*/
-		vorbis_info		*vorbisInfo;	/*some formatting data*/
-		int64_t			pcm_length;		/*length of the decoded data*/
-		int size = 0;
-		int section, result;
+    
+    int load_oggvorbis (const char *filename, ALenum *format, char **data, ALsizei *bitsize, ALsizei *freq) {
+        /*implementation inspired from http://www.devmaster.net/forums/showthread.php?t=1153 */
+        FILE		*oggFile;		/*ogg handle*/
+        OggVorbis_File  oggStream;		/*stream handle*/
+        vorbis_info	*vorbisInfo;	/*some formatting data*/
+        int64_t		pcm_length;		/*length of the decoded data*/
+        int size = 0;
+        int section, result;
 #ifdef DEBUG
-		int i;
-		vorbis_comment	*vorbisComment;	/*other less useful data*/
+        int i;
+        vorbis_comment	*vorbisComment;	/*other less useful data*/
 #endif
-		
-		oggFile = Fopen(filename, "rb");
-		result = ov_open(oggFile, &oggStream, NULL, 0);	/*TODO: check returning value of result*/
-                fclose(oggFile);
-            
-		vorbisInfo = ov_info(&oggStream, -1);
-		pcm_length = ov_pcm_total(&oggStream, -1) << vorbisInfo->channels;	
-		
+        
+        oggFile = Fopen(filename, "rb");
+        result = ov_open(oggFile, &oggStream, NULL, 0);	/*TODO: check returning value of result*/
+        fclose(oggFile);
+        
+        vorbisInfo = ov_info(&oggStream, -1);
+        pcm_length = ov_pcm_total(&oggStream, -1) << vorbisInfo->channels;	
+        
 #ifdef DEBUG
-		vorbisComment = ov_comment(&oggStream, -1);
-		fprintf(stderr, "Version: %d\n", vorbisInfo->version);
-		fprintf(stderr, "Channels: %d\n", vorbisInfo->channels);
-		fprintf(stderr, "Rate (Hz): %ld\n", vorbisInfo->rate);
-		fprintf(stderr, "Bitrate Upper: %ld\n", vorbisInfo->bitrate_upper);
-		fprintf(stderr, "Bitrate Nominal: %ld\n", vorbisInfo->bitrate_nominal);
-		fprintf(stderr, "Bitrate Lower: %ld\n", vorbisInfo->bitrate_lower);
-		fprintf(stderr, "Bitrate Windows: %ld\n", vorbisInfo->bitrate_window);
-		fprintf(stderr, "Vendor: %s\n", vorbisComment->vendor);
-		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]);
+        vorbisComment = ov_comment(&oggStream, -1);
+        fprintf(stderr, "Version: %d\n", vorbisInfo->version);
+        fprintf(stderr, "Channels: %d\n", vorbisInfo->channels);
+        fprintf(stderr, "Rate (Hz): %ld\n", vorbisInfo->rate);
+        fprintf(stderr, "Bitrate Upper: %ld\n", vorbisInfo->bitrate_upper);
+        fprintf(stderr, "Bitrate Nominal: %ld\n", vorbisInfo->bitrate_nominal);
+        fprintf(stderr, "Bitrate Lower: %ld\n", vorbisInfo->bitrate_lower);
+        fprintf(stderr, "Bitrate Windows: %ld\n", vorbisInfo->bitrate_window);
+        fprintf(stderr, "Vendor: %s\n", vorbisComment->vendor);
+        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]);
 #endif
-		
-		/*allocates enough room for the decoded data*/
-		*data = (char*) Malloc (sizeof(char) * pcm_length);
-		
-		/*there *should* not be ogg at 8 bits*/
-		if (vorbisInfo->channels == 1)
-			*format = AL_FORMAT_MONO16;
-		else {
-			if (vorbisInfo->channels == 2)
-				*format = AL_FORMAT_STEREO16;
-			else {
-				fprintf(stderr, "ERROR: wrong OGG header - channel value (%d)\n", vorbisInfo->channels);
-				return AL_FALSE;
-			}
-		}
-		
-		while(size < pcm_length) {
-			/*ov_read decodes the ogg stream and storse the pcm in data*/
-			result = ov_read (&oggStream, *data + size, pcm_length - size, 0, 2, 1, &section);
-			if(result > 0) {
-				size += result;
-			} else {
-				if (result == 0)
-					break;
-				else { 
-					fprintf(stderr, "ERROR: end of file from OGG stream\n");
-					return AL_FALSE;
-				}
-			}
-		}
-		
-		/*records the last fields*/
-		*bitsize = size;
-		*freq    = vorbisInfo->rate;
-            
-                ov_clear (&oggStream);
-		return AL_TRUE;
-	}
-	
+        
+        /*allocates enough room for the decoded data*/
+        *data = (char*) Malloc (sizeof(char) * pcm_length);
+        
+        /*there *should* not be ogg at 8 bits*/
+        if (vorbisInfo->channels == 1)
+            *format = AL_FORMAT_MONO16;
+        else {
+            if (vorbisInfo->channels == 2)
+                *format = AL_FORMAT_STEREO16;
+            else {
+                fprintf(stderr, "ERROR: wrong OGG header - channel value (%d)\n", vorbisInfo->channels);
+                return AL_FALSE;
+            }
+        }
+        
+        while(size < pcm_length) {
+            /*ov_read decodes the ogg stream and storse the pcm in data*/
+            result = ov_read (&oggStream, *data + size, pcm_length - size, 0, 2, 1, &section);
+            if(result > 0) {
+                size += result;
+            } else {
+                if (result == 0)
+                    break;
+                else { 
+                    fprintf(stderr, "ERROR: end of file from OGG stream\n");
+                    return AL_FALSE;
+                }
+            }
+        }
+        
+        /*records the last fields*/
+        *bitsize = size;
+        *freq    = vorbisInfo->rate;
+        
+        ov_clear (&oggStream);
+        return AL_TRUE;
+    }
+    
 #ifdef __CPLUSPLUS
 }
 #endif