openalbridge/loaders.c
changeset 2257 7eb31efcfb9b
parent 2220 110266ba2ef7
child 2259 ca42efdce3ce
equal deleted inserted replaced
2256:30797f3a4321 2257:7eb31efcfb9b
     1 /*
     1 /*
     2  * OpenAL Bridge - a simple portable library for OpenAL interface
     2  * OpenAL Bridge - a simple portable library for OpenAL interface
     3  * Copyright (c) 2009 Vittorio Giovara <vittorio.giovara@gmail.com>
     3  * Copyright (c) 2009 Vittorio Giovara <vittorio.giovara@gmail.com>
     4  *
     4  *
     5  * This program is free software; you can redistribute it and/or modify
     5  * This program is free software; you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     6  * it under the terms of the GNU Lesser General Public License as published by
     7  * the Free Software Foundation; version 2 of the License
     7  * the Free Software Foundation; version 2 of the License
     8  *
     8  *
     9  * This program is distributed in the hope that it will be useful,
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    12  * GNU Lesser General Public License for more details.
    13  *
    13  *
    14  * You should have received a copy of the GNU General Public License
    14  * You should have received a copy of the GNU Lesser General Public License
    15  * along with this program; if not, write to the Free Software
    15  * along with this program; if not, write to the Free Software
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    17  */
    17  */
    18 
    18 
    19 #include "loaders.h"
    19 #include "loaders.h"
    20 
    20 
    21 #ifdef __CPLUSPLUS
    21 #ifdef __CPLUSPLUS
    22 extern "C" {
    22 extern "C" {
    23 #endif 
    23 #endif 
    24 	
    24 	
    25 	int load_WavPcm (const char *filename, ALenum *format, char ** data, ALsizei *bitsize, ALsizei *freq) {
    25 	int load_wavpcm (const char *filename, ALenum *format, char ** data, ALsizei *bitsize, ALsizei *freq) {
    26 		WAV_header_t WAVHeader;
    26 		WAV_header_t WAVHeader;
    27 		FILE *wavfile;
    27 		FILE *wavfile;
    28 		int32_t t;
    28 		int32_t t;
    29 		uint32_t n = 0;
    29 		uint32_t n = 0;
    30 		
    30 		
   123 		*bitsize = WAVHeader.Subchunk2Size;
   123 		*bitsize = WAVHeader.Subchunk2Size;
   124 		*freq = WAVHeader.SampleRate;
   124 		*freq = WAVHeader.SampleRate;
   125 		return AL_TRUE;
   125 		return AL_TRUE;
   126 	}
   126 	}
   127 	
   127 	
   128 	int load_OggVorbis (const char *filename, ALenum *format, char **data, ALsizei *bitsize, ALsizei *freq) {
   128     
       
   129 	int load_oggvorbis (const char *filename, ALenum *format, char **data, ALsizei *bitsize, ALsizei *freq) {
   129 		/*implementation inspired from http://www.devmaster.net/forums/showthread.php?t=1153 */
   130 		/*implementation inspired from http://www.devmaster.net/forums/showthread.php?t=1153 */
   130 		FILE			*oggFile;		/*ogg handle*/
   131 		FILE			*oggFile;		/*ogg handle*/
   131 		OggVorbis_File  oggStream;		/*stream handle*/
   132 		OggVorbis_File  oggStream;		/*stream handle*/
   132 		vorbis_info		*vorbisInfo;	/*some formatting data*/
   133 		vorbis_info		*vorbisInfo;	/*some formatting data*/
   133 		int64_t			pcm_length;		/*length of the decoded data*/
   134 		int64_t			pcm_length;		/*length of the decoded data*/
   137 		int i;
   138 		int i;
   138 		vorbis_comment	*vorbisComment;	/*other less useful data*/
   139 		vorbis_comment	*vorbisComment;	/*other less useful data*/
   139 #endif
   140 #endif
   140 		
   141 		
   141 		oggFile = Fopen(filename, "rb");
   142 		oggFile = Fopen(filename, "rb");
   142 		result = ov_open(oggFile, &oggStream, NULL, 0);
   143 		result = ov_open(oggFile, &oggStream, NULL, 0);	/*TODO: check returning value of result*/
   143 		/*TODO: check returning value of result*/
   144                 fclose(oggFile);
   144 		
   145             
   145 		vorbisInfo = ov_info(&oggStream, -1);
   146 		vorbisInfo = ov_info(&oggStream, -1);
   146 		pcm_length = ov_pcm_total(&oggStream,-1) << vorbisInfo->channels;	
   147 		pcm_length = ov_pcm_total(&oggStream, -1) << vorbisInfo->channels;	
   147 		
   148 		
   148 #ifdef DEBUG
   149 #ifdef DEBUG
   149 		vorbisComment = ov_comment(&oggStream, -1);
   150 		vorbisComment = ov_comment(&oggStream, -1);
   150 		fprintf(stderr, "Version: %d\n", vorbisInfo->version);
   151 		fprintf(stderr, "Version: %d\n", vorbisInfo->version);
   151 		fprintf(stderr, "Channels: %d\n", vorbisInfo->channels);
   152 		fprintf(stderr, "Channels: %d\n", vorbisInfo->channels);
   174 				fprintf(stderr, "ERROR: wrong OGG header - channel value (%d)\n", vorbisInfo->channels);
   175 				fprintf(stderr, "ERROR: wrong OGG header - channel value (%d)\n", vorbisInfo->channels);
   175 				return AL_FALSE;
   176 				return AL_FALSE;
   176 			}
   177 			}
   177 		}
   178 		}
   178 		
   179 		
   179 		while(size < pcm_length)	{
   180 		while(size < pcm_length) {
   180 			/*ov_read decodes the ogg stream and storse the pcm in data*/
   181 			/*ov_read decodes the ogg stream and storse the pcm in data*/
   181 			result = ov_read (&oggStream, *data + size, pcm_length - size, 0, 2, 1, &section);
   182 			result = ov_read (&oggStream, *data + size, pcm_length - size, 0, 2, 1, &section);
   182 			if(result > 0) {
   183 			if(result > 0) {
   183 				size += result;
   184 				size += result;
   184 			} else {
   185 			} else {
   191 			}
   192 			}
   192 		}
   193 		}
   193 		
   194 		
   194 		/*records the last fields*/
   195 		/*records the last fields*/
   195 		*bitsize = size;
   196 		*bitsize = size;
   196 		*freq = vorbisInfo->rate;
   197 		*freq    = vorbisInfo->rate;
       
   198             
       
   199                 ov_clear (&oggStream);
   197 		return AL_TRUE;
   200 		return AL_TRUE;
   198 	}
   201 	}
   199 	
   202 	
   200 #ifdef __CPLUSPLUS
   203 #ifdef __CPLUSPLUS
   201 }
   204 }