openalbridge/loaders.c
changeset 2266 289dc8e51210
parent 2265 eae64600fb69
child 2415 35d09cbf819a
equal deleted inserted replaced
2265:eae64600fb69 2266:289dc8e51210
   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         OggVorbis_File  oggStream;	/*stream handle*/
   132         OggVorbis_File  oggStream;		/*stream handle*/
       
   133         vorbis_info	*vorbisInfo;	/*some formatting data*/
   132         vorbis_info	*vorbisInfo;	/*some formatting data*/
   134         int64_t		pcm_length;		/*length of the decoded data*/
   133         int64_t		pcm_length;	/*length of the decoded data*/
   135         int             section, result, size = 0;
   134         int             section, result, size = 0;
   136 #ifdef DEBUG
   135 #ifdef DEBUG
   137         int i;
   136         int i;
   138         vorbis_comment	*vorbisComment;	/*other less useful data*/
   137         vorbis_comment	*vorbisComment;	/*other less useful data*/
   139 #endif
   138 #endif
   140 
   139 
   141         oggFile = Fopen(filename, "rb");
   140 	result = ov_fopen((char*) filename, &oggStream);
   142 	result = ov_open_callbacks(oggFile, &oggStream, NULL, 0, NULL);
       
   143 	if (result < 0) {
   141 	if (result < 0) {
   144 		fprintf (stderr, "ERROR: ov_open_callbacks failed with %X", result) 
   142 		fprintf (stderr, "ERROR: ov_open_callbacks failed with %X", result);
   145 		fclose(oggFile);
   143                 ov_clear(&oggStream);
   146 		return -1;
   144 		return -1;
   147 	}
   145 	}
   148 
   146 
   149         vorbisInfo = ov_info(&oggStream, -1);
   147         vorbisInfo = ov_info(&oggStream, -1);
   150         pcm_length = ov_pcm_total(&oggStream, -1) << vorbisInfo->channels;	
   148         pcm_length = ov_pcm_total(&oggStream, -1) << vorbisInfo->channels;	
   175             if (vorbisInfo->channels == 2)
   173             if (vorbisInfo->channels == 2)
   176                 *format = AL_FORMAT_STEREO16;
   174                 *format = AL_FORMAT_STEREO16;
   177             else {
   175             else {
   178                 fprintf(stderr, "ERROR: wrong OGG header - channel value (%d)\n", vorbisInfo->channels);
   176                 fprintf(stderr, "ERROR: wrong OGG header - channel value (%d)\n", vorbisInfo->channels);
   179                 ov_clear(&oggStream);
   177                 ov_clear(&oggStream);
   180                 fclose(oggFile);
       
   181                 return AL_FALSE;
   178                 return AL_FALSE;
   182             }
   179             }
   183         }
   180         }
   184         
   181         
   185         while (size < pcm_length) {
   182         while (size < pcm_length) {
   191                 if (result == 0)
   188                 if (result == 0)
   192                     break;
   189                     break;
   193                 else { 
   190                 else { 
   194                     fprintf(stderr, "ERROR: end of file from OGG stream\n");
   191                     fprintf(stderr, "ERROR: end of file from OGG stream\n");
   195                     ov_clear(&oggStream);
   192                     ov_clear(&oggStream);
   196                     fclose(oggFile);
       
   197                     return AL_FALSE;
   193                     return AL_FALSE;
   198                 }
   194                 }
   199             }
   195             }
   200         }
   196         }
   201         
   197         
   203         *bitsize = size;
   199         *bitsize = size;
   204         *freq    = vorbisInfo->rate;
   200         *freq    = vorbisInfo->rate;
   205         
   201         
   206 	/*cleaning time*/
   202 	/*cleaning time*/
   207         ov_clear(&oggStream);
   203         ov_clear(&oggStream);
   208 	fclose(oggFile);
       
   209 
   204 
   210         return AL_TRUE;
   205         return AL_TRUE;
   211     }
   206     }
   212     
   207     
   213 #ifdef __CPLUSPLUS
   208 #ifdef __CPLUSPLUS