music_ogg.c
changeset 312 8a536296ef3d
parent 281 33730d0864d8
child 353 21fe62f9c4c6
equal deleted inserted replaced
311:9c32dbc9e383 312:8a536296ef3d
    29 #include <stdio.h>
    29 #include <stdio.h>
    30 #include <stdlib.h>
    30 #include <stdlib.h>
    31 #include <string.h>
    31 #include <string.h>
    32 
    32 
    33 #include "SDL_mixer.h"
    33 #include "SDL_mixer.h"
       
    34 #include "dynamic_ogg.h"
    34 #include "music_ogg.h"
    35 #include "music_ogg.h"
    35 
    36 
    36 /* This is the format of the audio mixer data */
    37 /* This is the format of the audio mixer data */
    37 static SDL_AudioSpec mixer;
    38 static SDL_AudioSpec mixer;
    38 
    39 
    63 		memset(music, 0, (sizeof *music));
    64 		memset(music, 0, (sizeof *music));
    64 		OGG_stop(music);
    65 		OGG_stop(music);
    65 		OGG_setvolume(music, MIX_MAX_VOLUME);
    66 		OGG_setvolume(music, MIX_MAX_VOLUME);
    66 		music->section = -1;
    67 		music->section = -1;
    67 
    68 
       
    69 		if ( Mix_InitOgg() < 0 ) {
       
    70 			return(NULL);
       
    71 		}
    68 		fp = fopen(file, "rb");
    72 		fp = fopen(file, "rb");
    69 		if ( fp == NULL ) {
    73 		if ( fp == NULL ) {
       
    74 			free(music);
       
    75 			Mix_QuitOgg();
    70 			SDL_SetError("Couldn't open %s", file);
    76 			SDL_SetError("Couldn't open %s", file);
       
    77 			return(NULL);
       
    78 		}
       
    79 		if ( vorbis.ov_open(fp, &music->vf, NULL, 0) < 0 ) {
       
    80 			fclose(fp);
    71 			free(music);
    81 			free(music);
    72 			return(NULL);
    82 			Mix_QuitOgg();
    73 		}
       
    74 		if ( ov_open(fp, &music->vf, NULL, 0) < 0 ) {
       
    75 			SDL_SetError("Not an Ogg Vorbis audio stream");
    83 			SDL_SetError("Not an Ogg Vorbis audio stream");
    76 			free(music);
       
    77 			fclose(fp);
       
    78 			return(NULL);
    84 			return(NULL);
    79 		}
    85 		}
    80 	} else {
    86 	} else {
    81 		SDL_SetError("Out of memory");
    87 		SDL_OutOfMemory();
    82 	}
    88 	}
    83 	return(music);
    89 	return(music);
    84 }
    90 }
    85 
    91 
    86 
    92 
   121 		memset(music, 0, (sizeof *music));
   127 		memset(music, 0, (sizeof *music));
   122 		OGG_stop(music);
   128 		OGG_stop(music);
   123 		OGG_setvolume(music, MIX_MAX_VOLUME);
   129 		OGG_setvolume(music, MIX_MAX_VOLUME);
   124 		music->section = -1;
   130 		music->section = -1;
   125 
   131 
   126 		if ( ov_open_callbacks(rw, &music->vf, NULL, 0, callbacks) < 0 ) {
   132 		if ( Mix_InitOgg() < 0 ) {
   127 			SDL_SetError("Not an Ogg Vorbis audio stream");
   133 			return(NULL);
       
   134 		}
       
   135 		if ( vorbis.ov_open_callbacks(rw, &music->vf, NULL, 0, callbacks) < 0 ) {
   128 			free(music);
   136 			free(music);
   129 			SDL_RWclose(rw);
   137 			SDL_RWclose(rw);
       
   138 			Mix_QuitOgg();
       
   139 			SDL_SetError("Not an Ogg Vorbis audio stream");
   130 			return(NULL);
   140 			return(NULL);
   131 		}
   141 		}
   132 	} else {
   142 	} else {
   133 		SDL_SetError("Out of memory");
   143 		SDL_OutOfMemory();
   134 	}
   144 	}
   135 	return(music);
   145 	return(music);
   136 }
   146 }
   137 
   147 
   138 /* Start playback of a given OGG stream */
   148 /* Start playback of a given OGG stream */
   153 	int section;
   163 	int section;
   154 	int len;
   164 	int len;
   155 	char data[4096];
   165 	char data[4096];
   156 	SDL_AudioCVT *cvt;
   166 	SDL_AudioCVT *cvt;
   157 
   167 
   158 	len = ov_read(&music->vf, data, sizeof(data), 0, 2, 1, &section);
   168 	len = vorbis.ov_read(&music->vf, data, sizeof(data), 0, 2, 1, &section);
   159 	if ( len <= 0 ) {
   169 	if ( len <= 0 ) {
   160 		if ( len == 0 ) {
   170 		if ( len == 0 ) {
   161 			music->playing = 0;
   171 			music->playing = 0;
   162 		}
   172 		}
   163 		return;
   173 		return;
   164 	}
   174 	}
   165 	cvt = &music->cvt;
   175 	cvt = &music->cvt;
   166 	if ( section != music->section ) {
   176 	if ( section != music->section ) {
   167 		vorbis_info *vi;
   177 		vorbis_info *vi;
   168 
   178 
   169 		vi = ov_info(&music->vf, -1);
   179 		vi = vorbis.ov_info(&music->vf, -1);
   170 		SDL_BuildAudioCVT(cvt, AUDIO_S16, vi->channels, vi->rate,
   180 		SDL_BuildAudioCVT(cvt, AUDIO_S16, vi->channels, vi->rate,
   171 		                       mixer.format,mixer.channels,mixer.freq);
   181 		                       mixer.format,mixer.channels,mixer.freq);
   172 		if ( cvt->buf ) {
   182 		if ( cvt->buf ) {
   173 			free(cvt->buf);
   183 			free(cvt->buf);
   174 		}
   184 		}
   230 {
   240 {
   231 	if ( music ) {
   241 	if ( music ) {
   232 		if ( music->cvt.buf ) {
   242 		if ( music->cvt.buf ) {
   233 			free(music->cvt.buf);
   243 			free(music->cvt.buf);
   234 		}
   244 		}
   235 		ov_clear(&music->vf);
   245 		vorbis.ov_clear(&music->vf);
   236 		free(music);
   246 		free(music);
       
   247 		Mix_QuitOgg();
   237 	}
   248 	}
   238 }
   249 }
   239 
   250 
   240 /* Jump (seek) to a given position (time is in seconds) */
   251 /* Jump (seek) to a given position (time is in seconds) */
   241 void OGG_jump_to_time(OGG_music *music, double time)
   252 void OGG_jump_to_time(OGG_music *music, double time)
   242 {
   253 {
   243        ov_time_seek( &music->vf, time );
   254        vorbis.ov_time_seek( &music->vf, time );
   244 }
   255 }
   245 
   256 
   246 #endif /* OGG_MUSIC */
   257 #endif /* OGG_MUSIC */