music_ogg.c
changeset 63 92b13db7d0a5
child 93 a25e6a225de8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/music_ogg.c	Mon Jul 03 00:25:20 2000 +0000
     1.3 @@ -0,0 +1,185 @@
     1.4 +/*
     1.5 +    MIXERLIB:  An audio mixer library based on the SDL library
     1.6 +    Copyright (C) 1997-1999  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    5635-34 Springhouse Dr.
    1.24 +    Pleasanton, CA 94588 (USA)
    1.25 +    slouken@devolution.com
    1.26 +*/
    1.27 +
    1.28 +#ifdef OGG_MUSIC
    1.29 +
    1.30 +/* This file supports Ogg Vorbis music streams */
    1.31 +
    1.32 +#include <stdio.h>
    1.33 +#include <string.h>
    1.34 +
    1.35 +#include "SDL_mixer.h"
    1.36 +#include "music_ogg.h"
    1.37 +
    1.38 +/* This is the format of the audio mixer data */
    1.39 +static SDL_AudioSpec mixer;
    1.40 +
    1.41 +/* Initialize the Ogg Vorbis player, with the given mixer settings
    1.42 +   This function returns 0, or -1 if there was an error.
    1.43 + */
    1.44 +int OGG_init(SDL_AudioSpec *mixerfmt)
    1.45 +{
    1.46 +	mixer = *mixerfmt;
    1.47 +	return(0);
    1.48 +}
    1.49 +
    1.50 +/* Set the volume for an OGG stream */
    1.51 +void OGG_setvolume(OGG_music *music, int volume)
    1.52 +{
    1.53 +	music->volume = volume;
    1.54 +}
    1.55 +
    1.56 +/* Load an OGG stream from the given file */
    1.57 +OGG_music *OGG_new(const char *file)
    1.58 +{
    1.59 +	OGG_music *music;
    1.60 +	FILE *fp;
    1.61 +
    1.62 +	music = (OGG_music *)malloc(sizeof *music);
    1.63 +	if ( music ) {
    1.64 +		/* Initialize the music structure */
    1.65 +		memset(music, 0, (sizeof *music));
    1.66 +		OGG_stop(music);
    1.67 +		OGG_setvolume(music, MIX_MAX_VOLUME);
    1.68 +		music->section = -1;
    1.69 +
    1.70 +		fp = fopen(file, "rb");
    1.71 +		if ( fp == NULL ) {
    1.72 +			SDL_SetError("Couldn't open %s", file);
    1.73 +			free(music);
    1.74 +			return(NULL);
    1.75 +		}
    1.76 +		if ( ov_open(fp, &music->vf, NULL, 0) < 0 ) {
    1.77 +			SDL_SetError("Not an Ogg Vorbis audio stream");
    1.78 +			free(music);
    1.79 +			fclose(fp);
    1.80 +			return(NULL);
    1.81 +		}
    1.82 +	} else {
    1.83 +		SDL_OutOfMemory();
    1.84 +	}
    1.85 +	return(music);
    1.86 +}
    1.87 +
    1.88 +/* Start playback of a given OGG stream */
    1.89 +void OGG_play(OGG_music *music)
    1.90 +{
    1.91 +	music->playing = 1;
    1.92 +}
    1.93 +
    1.94 +/* Return non-zero if a stream is currently playing */
    1.95 +int OGG_playing(OGG_music *music)
    1.96 +{
    1.97 +	return(music->playing);
    1.98 +}
    1.99 +
   1.100 +/* Read some Ogg stream data and convert it for output */
   1.101 +static void OGG_getsome(OGG_music *music)
   1.102 +{
   1.103 +	int section;
   1.104 +	int len;
   1.105 +	char data[4096];
   1.106 +	SDL_AudioCVT *cvt;
   1.107 +
   1.108 +	len = ov_read(&music->vf, data, sizeof(data), 0, 2, 1, &section);
   1.109 +	if ( len <= 0 ) {
   1.110 +		if ( len == 0 ) {
   1.111 +			music->playing = 0;
   1.112 +		}
   1.113 +		return;
   1.114 +	}
   1.115 +	cvt = &music->cvt;
   1.116 +	if ( section != music->section ) {
   1.117 +		vorbis_info *vi;
   1.118 +
   1.119 +		vi = ov_info(&music->vf, -1);
   1.120 +		SDL_BuildAudioCVT(cvt, AUDIO_S16, vi->channels, vi->rate,
   1.121 +		                       mixer.format,mixer.channels,mixer.freq);
   1.122 +		if ( cvt->buf ) {
   1.123 +			free(cvt->buf);
   1.124 +		}
   1.125 +		cvt->buf = (Uint8 *)malloc(sizeof(data)*cvt->len_mult);
   1.126 +		music->section = section;
   1.127 +	}
   1.128 +	if ( cvt->buf ) {
   1.129 +		memcpy(cvt->buf, data, len);
   1.130 +		if ( cvt->needed ) {
   1.131 +			cvt->len = len;
   1.132 +			SDL_ConvertAudio(cvt);
   1.133 +		} else {
   1.134 +			cvt->len_cvt = len;
   1.135 +		}
   1.136 +		music->len_available = music->cvt.len_cvt;
   1.137 +		music->snd_available = music->cvt.buf;
   1.138 +	} else {
   1.139 +		SDL_OutOfMemory();
   1.140 +		music->playing = 0;
   1.141 +	}
   1.142 +}
   1.143 +
   1.144 +/* Play some of a stream previously started with OGG_play() */
   1.145 +void OGG_playAudio(OGG_music *music, Uint8 *snd, int len)
   1.146 +{
   1.147 +	int mixable;
   1.148 +
   1.149 +	while ( (len > 0) && music->playing ) {
   1.150 +		if ( ! music->len_available ) {
   1.151 +			OGG_getsome(music);
   1.152 +		}
   1.153 +		mixable = len;
   1.154 +		if ( mixable > music->len_available ) {
   1.155 +			mixable = music->len_available;
   1.156 +		}
   1.157 +		if ( music->volume == MIX_MAX_VOLUME ) {
   1.158 +			memcpy(snd, music->snd_available, mixable);
   1.159 +		} else {
   1.160 +			SDL_MixAudio(snd, music->snd_available, mixable,
   1.161 +			                              music->volume);
   1.162 +		}
   1.163 +		music->len_available -= mixable;
   1.164 +		music->snd_available += mixable;
   1.165 +		len -= mixable;
   1.166 +		snd += mixable;
   1.167 +	}
   1.168 +}
   1.169 +
   1.170 +/* Stop playback of a stream previously started with OGG_play() */
   1.171 +void OGG_stop(OGG_music *music)
   1.172 +{
   1.173 +	music->playing = 0;
   1.174 +}
   1.175 +
   1.176 +/* Close the given OGG stream */
   1.177 +void OGG_delete(OGG_music *music)
   1.178 +{
   1.179 +	if ( music ) {
   1.180 +		if ( music->cvt.buf ) {
   1.181 +			free(music->cvt.buf);
   1.182 +		}
   1.183 +		ov_clear(&music->vf);
   1.184 +		free(music);
   1.185 +	}
   1.186 +}
   1.187 +
   1.188 +#endif /* OGG_MUSIC */