music_ogg.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Mar 2011 23:11:29 -0700
changeset 505 5b24d089d9f5
parent 470 5cebd6c5be2d
child 518 8bc9b5fd2aae
permissions -rw-r--r--
Fixed ov_time_seek signature for Tremor
     1 /*
     2     SDL_mixer:  An audio mixer library based on the SDL library
     3     Copyright (C) 1997-2009 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /* $Id$ */
    24 
    25 #ifdef OGG_MUSIC
    26 
    27 /* This file supports Ogg Vorbis music streams */
    28 
    29 #include <stdio.h>
    30 #include <stdlib.h>
    31 #include <string.h>
    32 
    33 #include "SDL_mixer.h"
    34 #include "dynamic_ogg.h"
    35 #include "music_ogg.h"
    36 
    37 /* This is the format of the audio mixer data */
    38 static SDL_AudioSpec mixer;
    39 
    40 /* Initialize the Ogg Vorbis player, with the given mixer settings
    41    This function returns 0, or -1 if there was an error.
    42  */
    43 int OGG_init(SDL_AudioSpec *mixerfmt)
    44 {
    45 	mixer = *mixerfmt;
    46 	return(0);
    47 }
    48 
    49 /* Set the volume for an OGG stream */
    50 void OGG_setvolume(OGG_music *music, int volume)
    51 {
    52 	music->volume = volume;
    53 }
    54 
    55 /* Load an OGG stream from the given file */
    56 OGG_music *OGG_new(const char *file)
    57 {
    58 	SDL_RWops *rw;
    59 
    60 	rw = SDL_RWFromFile(file, "rb");
    61 	if ( rw == NULL ) {
    62 		SDL_SetError("Couldn't open %s", file);
    63 		return NULL;
    64 	}
    65 	return OGG_new_RW(rw);
    66 }
    67 
    68 
    69 static size_t sdl_read_func(void *ptr, size_t size, size_t nmemb, void *datasource)
    70 {
    71     return SDL_RWread((SDL_RWops*)datasource, ptr, size, nmemb);
    72 }
    73 
    74 static int sdl_seek_func(void *datasource, ogg_int64_t offset, int whence)
    75 {
    76     return SDL_RWseek((SDL_RWops*)datasource, (int)offset, whence);
    77 }
    78 
    79 static int sdl_close_func(void *datasource)
    80 {
    81     return SDL_RWclose((SDL_RWops*)datasource);
    82 }
    83 
    84 static long sdl_tell_func(void *datasource)
    85 {
    86     return SDL_RWtell((SDL_RWops*)datasource);
    87 }
    88 
    89 /* Load an OGG stream from an SDL_RWops object */
    90 OGG_music *OGG_new_RW(SDL_RWops *rw)
    91 {
    92 	OGG_music *music;
    93 	ov_callbacks callbacks;
    94 
    95 	callbacks.read_func = sdl_read_func;
    96 	callbacks.seek_func = sdl_seek_func;
    97 	callbacks.close_func = sdl_close_func;
    98 	callbacks.tell_func = sdl_tell_func;
    99 
   100 	music = (OGG_music *)malloc(sizeof *music);
   101 	if ( music ) {
   102 		/* Initialize the music structure */
   103 		memset(music, 0, (sizeof *music));
   104 		OGG_stop(music);
   105 		OGG_setvolume(music, MIX_MAX_VOLUME);
   106 		music->section = -1;
   107 
   108 		if ( !Mix_Init(MIX_INIT_OGG) ) {
   109 			return(NULL);
   110 		}
   111 		if ( vorbis.ov_open_callbacks(rw, &music->vf, NULL, 0, callbacks) < 0 ) {
   112 			free(music);
   113 			SDL_RWclose(rw);
   114 			SDL_SetError("Not an Ogg Vorbis audio stream");
   115 			return(NULL);
   116 		}
   117 	} else {
   118 		SDL_OutOfMemory();
   119 	}
   120 	return(music);
   121 }
   122 
   123 /* Start playback of a given OGG stream */
   124 void OGG_play(OGG_music *music)
   125 {
   126 	music->playing = 1;
   127 }
   128 
   129 /* Return non-zero if a stream is currently playing */
   130 int OGG_playing(OGG_music *music)
   131 {
   132 	return(music->playing);
   133 }
   134 
   135 /* Read some Ogg stream data and convert it for output */
   136 static void OGG_getsome(OGG_music *music)
   137 {
   138 	int section;
   139 	int len;
   140 	char data[4096];
   141 	SDL_AudioCVT *cvt;
   142 
   143 #ifdef OGG_USE_TREMOR
   144 	len = vorbis.ov_read(&music->vf, data, sizeof(data), &section);
   145 #else
   146 	len = vorbis.ov_read(&music->vf, data, sizeof(data), 0, 2, 1, &section);
   147 #endif
   148 	if ( len <= 0 ) {
   149 		if ( len == 0 ) {
   150 			music->playing = 0;
   151 		}
   152 		return;
   153 	}
   154 	cvt = &music->cvt;
   155 	if ( section != music->section ) {
   156 		vorbis_info *vi;
   157 
   158 		vi = vorbis.ov_info(&music->vf, -1);
   159 		SDL_BuildAudioCVT(cvt, AUDIO_S16, vi->channels, vi->rate,
   160 		                       mixer.format,mixer.channels,mixer.freq);
   161 		if ( cvt->buf ) {
   162 			free(cvt->buf);
   163 		}
   164 		cvt->buf = (Uint8 *)malloc(sizeof(data)*cvt->len_mult);
   165 		music->section = section;
   166 	}
   167 	if ( cvt->buf ) {
   168 		memcpy(cvt->buf, data, len);
   169 		if ( cvt->needed ) {
   170 			cvt->len = len;
   171 			SDL_ConvertAudio(cvt);
   172 		} else {
   173 			cvt->len_cvt = len;
   174 		}
   175 		music->len_available = music->cvt.len_cvt;
   176 		music->snd_available = music->cvt.buf;
   177 	} else {
   178 		SDL_SetError("Out of memory");
   179 		music->playing = 0;
   180 	}
   181 }
   182 
   183 /* Play some of a stream previously started with OGG_play() */
   184 int OGG_playAudio(OGG_music *music, Uint8 *snd, int len)
   185 {
   186 	int mixable;
   187 
   188 	while ( (len > 0) && music->playing ) {
   189 		if ( ! music->len_available ) {
   190 			OGG_getsome(music);
   191 		}
   192 		mixable = len;
   193 		if ( mixable > music->len_available ) {
   194 			mixable = music->len_available;
   195 		}
   196 		if ( music->volume == MIX_MAX_VOLUME ) {
   197 			memcpy(snd, music->snd_available, mixable);
   198 		} else {
   199 			SDL_MixAudio(snd, music->snd_available, mixable,
   200 			                              music->volume);
   201 		}
   202 		music->len_available -= mixable;
   203 		music->snd_available += mixable;
   204 		len -= mixable;
   205 		snd += mixable;
   206 	}
   207 	
   208 	return len;
   209 }
   210 
   211 /* Stop playback of a stream previously started with OGG_play() */
   212 void OGG_stop(OGG_music *music)
   213 {
   214 	music->playing = 0;
   215 }
   216 
   217 /* Close the given OGG stream */
   218 void OGG_delete(OGG_music *music)
   219 {
   220 	if ( music ) {
   221 		if ( music->cvt.buf ) {
   222 			free(music->cvt.buf);
   223 		}
   224 		vorbis.ov_clear(&music->vf);
   225 		free(music);
   226 	}
   227 }
   228 
   229 /* Jump (seek) to a given position (time is in seconds) */
   230 void OGG_jump_to_time(OGG_music *music, double time)
   231 {
   232 #ifdef OGG_USE_TREMOR
   233        vorbis.ov_time_seek( &music->vf, (ogg_int64_t)time );
   234 #else
   235        vorbis.ov_time_seek( &music->vf, time );
   236 #endif
   237 }
   238 
   239 #endif /* OGG_MUSIC */