openalbridge/loaders.c
changeset 2265 eae64600fb69
parent 2260 31756e21c436
child 2266 289dc8e51210
equal deleted inserted replaced
2264:b8fff48235de 2265:eae64600fb69
   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     
   128 
   129     int load_oggvorbis (const char *filename, ALenum *format, char **data, ALsizei *bitsize, ALsizei *freq) {
   129     int load_oggvorbis (const char *filename, ALenum *format, char **data, ALsizei *bitsize, ALsizei *freq) {
   130         /*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 */
   131         FILE		*oggFile;		/*ogg handle*/
   131         FILE		*oggFile;		/*ogg handle*/
   132         OggVorbis_File  oggStream;		/*stream handle*/
   132         OggVorbis_File  oggStream;		/*stream handle*/
   133         vorbis_info	*vorbisInfo;	/*some formatting data*/
   133         vorbis_info	*vorbisInfo;	/*some formatting data*/
   135         int             section, result, size = 0;
   135         int             section, result, size = 0;
   136 #ifdef DEBUG
   136 #ifdef DEBUG
   137         int i;
   137         int i;
   138         vorbis_comment	*vorbisComment;	/*other less useful data*/
   138         vorbis_comment	*vorbisComment;	/*other less useful data*/
   139 #endif
   139 #endif
   140         
   140 
   141         oggFile = Fopen(filename, "rb");
   141         oggFile = Fopen(filename, "rb");
   142         result = ov_open(oggFile, &oggStream, NULL, 0);	/*TODO: check returning value of result*/
   142 	result = ov_open_callbacks(oggFile, &oggStream, NULL, 0, NULL);
   143         
   143 	if (result < 0) {
       
   144 		fprintf (stderr, "ERROR: ov_open_callbacks failed with %X", result) 
       
   145 		fclose(oggFile);
       
   146 		return -1;
       
   147 	}
       
   148 
   144         vorbisInfo = ov_info(&oggStream, -1);
   149         vorbisInfo = ov_info(&oggStream, -1);
   145         pcm_length = ov_pcm_total(&oggStream, -1) << vorbisInfo->channels;	
   150         pcm_length = ov_pcm_total(&oggStream, -1) << vorbisInfo->channels;	
   146         
   151         
   147 #ifdef DEBUG
   152 #ifdef DEBUG
   148         vorbisComment = ov_comment(&oggStream, -1);
   153         vorbisComment = ov_comment(&oggStream, -1);
   169         else {
   174         else {
   170             if (vorbisInfo->channels == 2)
   175             if (vorbisInfo->channels == 2)
   171                 *format = AL_FORMAT_STEREO16;
   176                 *format = AL_FORMAT_STEREO16;
   172             else {
   177             else {
   173                 fprintf(stderr, "ERROR: wrong OGG header - channel value (%d)\n", vorbisInfo->channels);
   178                 fprintf(stderr, "ERROR: wrong OGG header - channel value (%d)\n", vorbisInfo->channels);
   174                 ov_clear (&oggStream);
   179                 ov_clear(&oggStream);
   175                 fclose(oggFile);
   180                 fclose(oggFile);
   176                 return AL_FALSE;
   181                 return AL_FALSE;
   177             }
   182             }
   178         }
   183         }
   179         
   184         
   180         while(size < pcm_length) {
   185         while (size < pcm_length) {
   181             /*ov_read decodes the ogg stream and storse the pcm in data*/
   186             /*ov_read decodes the ogg stream and storse the pcm in data*/
   182             result = ov_read (&oggStream, *data + size, pcm_length - size, 0, 2, 1, &section);
   187             result = ov_read (&oggStream, *data + size, pcm_length - size, 0, 2, 1, &section);
   183             if(result > 0) {
   188             if (result > 0) {
   184                 size += result;
   189                 size += result;
   185             } else {
   190             } else {
   186                 if (result == 0)
   191                 if (result == 0)
   187                     break;
   192                     break;
   188                 else { 
   193                 else { 
   189                     fprintf(stderr, "ERROR: end of file from OGG stream\n");
   194                     fprintf(stderr, "ERROR: end of file from OGG stream\n");
   190                     ov_clear (&oggStream);
   195                     ov_clear(&oggStream);
   191                     fclose(oggFile);
   196                     fclose(oggFile);
   192                     return AL_FALSE;
   197                     return AL_FALSE;
   193                 }
   198                 }
   194             }
   199             }
   195         }
   200         }
   196         
   201         
   197         /*records the last fields*/
   202         /*set the last fields*/
   198         *bitsize = size;
   203         *bitsize = size;
   199         *freq    = vorbisInfo->rate;
   204         *freq    = vorbisInfo->rate;
   200         
   205         
   201         ov_clear (&oggStream);
   206 	/*cleaning time*/
   202         fclose (oggFile);
   207         ov_clear(&oggStream);
       
   208 	fclose(oggFile);
       
   209 
   203         return AL_TRUE;
   210         return AL_TRUE;
   204     }
   211     }
   205     
   212     
   206 #ifdef __CPLUSPLUS
   213 #ifdef __CPLUSPLUS
   207 }
   214 }