music_ogg.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 20 Dec 2001 00:01:30 +0000
changeset 155 53f259dae79d
parent 152 c97650d4f432
child 241 503416fca921
permissions -rw-r--r--
*** empty log message ***
     1 /*
     2     SDL_mixer:  An audio mixer library based on the SDL library
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001  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 "music_ogg.h"
    35 
    36 /* This is the format of the audio mixer data */
    37 static SDL_AudioSpec mixer;
    38 
    39 /* Initialize the Ogg Vorbis player, with the given mixer settings
    40    This function returns 0, or -1 if there was an error.
    41  */
    42 int OGG_init(SDL_AudioSpec *mixerfmt)
    43 {
    44 	mixer = *mixerfmt;
    45 	return(0);
    46 }
    47 
    48 /* Set the volume for an OGG stream */
    49 void OGG_setvolume(OGG_music *music, int volume)
    50 {
    51 	music->volume = volume;
    52 }
    53 
    54 /* Load an OGG stream from the given file */
    55 OGG_music *OGG_new(const char *file)
    56 {
    57 	OGG_music *music;
    58 	FILE *fp;
    59 
    60 	music = (OGG_music *)malloc(sizeof *music);
    61 	if ( music ) {
    62 		/* Initialize the music structure */
    63 		memset(music, 0, (sizeof *music));
    64 		OGG_stop(music);
    65 		OGG_setvolume(music, MIX_MAX_VOLUME);
    66 		music->section = -1;
    67 
    68 		fp = fopen(file, "rb");
    69 		if ( fp == NULL ) {
    70 			SDL_SetError("Couldn't open %s", file);
    71 			free(music);
    72 			return(NULL);
    73 		}
    74 		if ( ov_open(fp, &music->vf, NULL, 0) < 0 ) {
    75 			SDL_SetError("Not an Ogg Vorbis audio stream");
    76 			free(music);
    77 			fclose(fp);
    78 			return(NULL);
    79 		}
    80 	} else {
    81 		SDL_SetError("Out of memory");
    82 	}
    83 	return(music);
    84 }
    85 
    86 /* Start playback of a given OGG stream */
    87 void OGG_play(OGG_music *music)
    88 {
    89 	music->playing = 1;
    90 }
    91 
    92 /* Return non-zero if a stream is currently playing */
    93 int OGG_playing(OGG_music *music)
    94 {
    95 	return(music->playing);
    96 }
    97 
    98 /* Read some Ogg stream data and convert it for output */
    99 static void OGG_getsome(OGG_music *music)
   100 {
   101 	int section;
   102 	int len;
   103 	char data[4096];
   104 	SDL_AudioCVT *cvt;
   105 
   106 	len = ov_read(&music->vf, data, sizeof(data), 0, 2, 1, &section);
   107 	if ( len <= 0 ) {
   108 		if ( len == 0 ) {
   109 			music->playing = 0;
   110 		}
   111 		return;
   112 	}
   113 	cvt = &music->cvt;
   114 	if ( section != music->section ) {
   115 		vorbis_info *vi;
   116 
   117 		vi = ov_info(&music->vf, -1);
   118 		SDL_BuildAudioCVT(cvt, AUDIO_S16, vi->channels, vi->rate,
   119 		                       mixer.format,mixer.channels,mixer.freq);
   120 		if ( cvt->buf ) {
   121 			free(cvt->buf);
   122 		}
   123 		cvt->buf = (Uint8 *)malloc(sizeof(data)*cvt->len_mult);
   124 		music->section = section;
   125 	}
   126 	if ( cvt->buf ) {
   127 		memcpy(cvt->buf, data, len);
   128 		if ( cvt->needed ) {
   129 			cvt->len = len;
   130 			SDL_ConvertAudio(cvt);
   131 		} else {
   132 			cvt->len_cvt = len;
   133 		}
   134 		music->len_available = music->cvt.len_cvt;
   135 		music->snd_available = music->cvt.buf;
   136 	} else {
   137 		SDL_SetError("Out of memory");
   138 		music->playing = 0;
   139 	}
   140 }
   141 
   142 /* Play some of a stream previously started with OGG_play() */
   143 void OGG_playAudio(OGG_music *music, Uint8 *snd, int len)
   144 {
   145 	int mixable;
   146 
   147 	while ( (len > 0) && music->playing ) {
   148 		if ( ! music->len_available ) {
   149 			OGG_getsome(music);
   150 		}
   151 		mixable = len;
   152 		if ( mixable > music->len_available ) {
   153 			mixable = music->len_available;
   154 		}
   155 		if ( music->volume == MIX_MAX_VOLUME ) {
   156 			memcpy(snd, music->snd_available, mixable);
   157 		} else {
   158 			SDL_MixAudio(snd, music->snd_available, mixable,
   159 			                              music->volume);
   160 		}
   161 		music->len_available -= mixable;
   162 		music->snd_available += mixable;
   163 		len -= mixable;
   164 		snd += mixable;
   165 	}
   166 }
   167 
   168 /* Stop playback of a stream previously started with OGG_play() */
   169 void OGG_stop(OGG_music *music)
   170 {
   171 	music->playing = 0;
   172 }
   173 
   174 /* Close the given OGG stream */
   175 void OGG_delete(OGG_music *music)
   176 {
   177 	if ( music ) {
   178 		if ( music->cvt.buf ) {
   179 			free(music->cvt.buf);
   180 		}
   181 		ov_clear(&music->vf);
   182 		free(music);
   183 	}
   184 }
   185 
   186 /* Jump (seek) to a given position (time is in seconds) */
   187 void OGG_jump_to_time(OGG_music *music, double time)
   188 {
   189        ov_time_seek( &music->vf, time );
   190 }
   191 
   192 #endif /* OGG_MUSIC */