music_ogg.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 19 Oct 2009 01:03:40 +0000
changeset 462 028b317d01d2
parent 386 695494546b3c
child 470 5cebd6c5be2d
permissions -rw-r--r--
Added the AudioUnit framework for the native MIDI code.
     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_InitOgg() < 0 ) {
   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 			Mix_QuitOgg();
   115 			SDL_SetError("Not an Ogg Vorbis audio stream");
   116 			return(NULL);
   117 		}
   118 	} else {
   119 		SDL_OutOfMemory();
   120 	}
   121 	return(music);
   122 }
   123 
   124 /* Start playback of a given OGG stream */
   125 void OGG_play(OGG_music *music)
   126 {
   127 	music->playing = 1;
   128 }
   129 
   130 /* Return non-zero if a stream is currently playing */
   131 int OGG_playing(OGG_music *music)
   132 {
   133 	return(music->playing);
   134 }
   135 
   136 /* Read some Ogg stream data and convert it for output */
   137 static void OGG_getsome(OGG_music *music)
   138 {
   139 	int section;
   140 	int len;
   141 	char data[4096];
   142 	SDL_AudioCVT *cvt;
   143 
   144 #ifdef OGG_USE_TREMOR
   145 	len = vorbis.ov_read(&music->vf, data, sizeof(data), &section);
   146 #else
   147 	len = vorbis.ov_read(&music->vf, data, sizeof(data), 0, 2, 1, &section);
   148 #endif
   149 	if ( len <= 0 ) {
   150 		if ( len == 0 ) {
   151 			music->playing = 0;
   152 		}
   153 		return;
   154 	}
   155 	cvt = &music->cvt;
   156 	if ( section != music->section ) {
   157 		vorbis_info *vi;
   158 
   159 		vi = vorbis.ov_info(&music->vf, -1);
   160 		SDL_BuildAudioCVT(cvt, AUDIO_S16, vi->channels, vi->rate,
   161 		                       mixer.format,mixer.channels,mixer.freq);
   162 		if ( cvt->buf ) {
   163 			free(cvt->buf);
   164 		}
   165 		cvt->buf = (Uint8 *)malloc(sizeof(data)*cvt->len_mult);
   166 		music->section = section;
   167 	}
   168 	if ( cvt->buf ) {
   169 		memcpy(cvt->buf, data, len);
   170 		if ( cvt->needed ) {
   171 			cvt->len = len;
   172 			SDL_ConvertAudio(cvt);
   173 		} else {
   174 			cvt->len_cvt = len;
   175 		}
   176 		music->len_available = music->cvt.len_cvt;
   177 		music->snd_available = music->cvt.buf;
   178 	} else {
   179 		SDL_SetError("Out of memory");
   180 		music->playing = 0;
   181 	}
   182 }
   183 
   184 /* Play some of a stream previously started with OGG_play() */
   185 int OGG_playAudio(OGG_music *music, Uint8 *snd, int len)
   186 {
   187 	int mixable;
   188 
   189 	while ( (len > 0) && music->playing ) {
   190 		if ( ! music->len_available ) {
   191 			OGG_getsome(music);
   192 		}
   193 		mixable = len;
   194 		if ( mixable > music->len_available ) {
   195 			mixable = music->len_available;
   196 		}
   197 		if ( music->volume == MIX_MAX_VOLUME ) {
   198 			memcpy(snd, music->snd_available, mixable);
   199 		} else {
   200 			SDL_MixAudio(snd, music->snd_available, mixable,
   201 			                              music->volume);
   202 		}
   203 		music->len_available -= mixable;
   204 		music->snd_available += mixable;
   205 		len -= mixable;
   206 		snd += mixable;
   207 	}
   208 	
   209 	return len;
   210 }
   211 
   212 /* Stop playback of a stream previously started with OGG_play() */
   213 void OGG_stop(OGG_music *music)
   214 {
   215 	music->playing = 0;
   216 }
   217 
   218 /* Close the given OGG stream */
   219 void OGG_delete(OGG_music *music)
   220 {
   221 	if ( music ) {
   222 		if ( music->cvt.buf ) {
   223 			free(music->cvt.buf);
   224 		}
   225 		vorbis.ov_clear(&music->vf);
   226 		free(music);
   227 		Mix_QuitOgg();
   228 	}
   229 }
   230 
   231 /* Jump (seek) to a given position (time is in seconds) */
   232 void OGG_jump_to_time(OGG_music *music, double time)
   233 {
   234        vorbis.ov_time_seek( &music->vf, time );
   235 }
   236 
   237 #endif /* OGG_MUSIC */