music_flac.c
changeset 382 50501e45c57b
child 386 695494546b3c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/music_flac.c	Wed Feb 27 07:31:03 2008 +0000
     1.3 @@ -0,0 +1,591 @@
     1.4 +/*
     1.5 +    SDL_mixer:  An audio mixer library based on the SDL library
     1.6 +    Copyright (C) 1997-2004 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 +    slouken@libsdl.org
    1.24 +
    1.25 +
    1.26 +    This file is used to support SDL_LoadMUS playback of FLAC files.
    1.27 +   											~ Austen Dicken (admin@cvpcs.org)
    1.28 +*/
    1.29 +
    1.30 +#ifdef FLAC_MUSIC
    1.31 +
    1.32 +#include <stdio.h>
    1.33 +#include <stdlib.h>
    1.34 +#include <string.h>
    1.35 +
    1.36 +#include "SDL_mixer.h"
    1.37 +#include "dynamic_flac.h"
    1.38 +#include "music_flac.h"
    1.39 +
    1.40 +/* This is the format of the audio mixer data */
    1.41 +static SDL_AudioSpec mixer;
    1.42 +
    1.43 +/* Initialize the FLAC player, with the given mixer settings
    1.44 +   This function returns 0, or -1 if there was an error.
    1.45 + */
    1.46 +int FLAC_init(SDL_AudioSpec *mixerfmt) {
    1.47 +	mixer = *mixerfmt;
    1.48 +	return(0);
    1.49 +}
    1.50 +
    1.51 +/* Set the volume for an FLAC stream */
    1.52 +void FLAC_setvolume(FLAC_music *music, int volume) {
    1.53 +	music->volume = volume;
    1.54 +}
    1.55 +
    1.56 +/* Load an FLAC stream from the given file */
    1.57 +FLAC_music *FLAC_new(const char *file) {
    1.58 +	SDL_RWops *rw;
    1.59 +
    1.60 +	rw = SDL_RWFromFile (file, "rb");
    1.61 +	if (rw == NULL) {
    1.62 +		SDL_SetError ("Couldn't open %s", file);
    1.63 +		return NULL;
    1.64 +	}
    1.65 +	return FLAC_new_RW (rw);
    1.66 +}
    1.67 +
    1.68 +static FLAC__StreamDecoderReadStatus flac_read_music_cb(
    1.69 +									const FLAC__StreamDecoder *decoder,
    1.70 +									FLAC__byte buffer[],
    1.71 +									size_t *bytes,
    1.72 +									void *client_data) {
    1.73 +	FLAC_music *data = (FLAC_music*)client_data;
    1.74 +
    1.75 +	// make sure there is something to be reading
    1.76 +	if (*bytes > 0) {
    1.77 +		*bytes = SDL_RWread (data->rwops, buffer, sizeof (FLAC__byte), *bytes);
    1.78 +
    1.79 +		if (*bytes < 0) { // error in read
    1.80 +			return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
    1.81 +		}
    1.82 +		else if (*bytes == 0 ) { // no data was read (EOF)
    1.83 +			return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
    1.84 +		}
    1.85 +		else { // data was read, continue
    1.86 +			return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
    1.87 +		}
    1.88 +	}
    1.89 +	else {
    1.90 +		return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
    1.91 +	}
    1.92 +}
    1.93 +
    1.94 +static FLAC__StreamDecoderSeekStatus flac_seek_music_cb(
    1.95 +									const FLAC__StreamDecoder *decoder,
    1.96 +									FLAC__uint64 absolute_byte_offset,
    1.97 +									void *client_data) {
    1.98 +	FLAC_music *data = (FLAC_music*)client_data;
    1.99 +
   1.100 +	if (SDL_RWseek (data->rwops, absolute_byte_offset, SEEK_SET) < 0) {
   1.101 +		return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
   1.102 +	}
   1.103 +	else {
   1.104 +		return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
   1.105 +	}
   1.106 +}
   1.107 +
   1.108 +static FLAC__StreamDecoderTellStatus flac_tell_music_cb(
   1.109 +									const FLAC__StreamDecoder *decoder,
   1.110 +									FLAC__uint64 *absolute_byte_offset,
   1.111 +									void *client_data ) {
   1.112 +	FLAC_music *data = (FLAC_music*)client_data;
   1.113 +
   1.114 +	int pos = SDL_RWtell (data->rwops);
   1.115 +
   1.116 +	if (pos < 0) {
   1.117 +		return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
   1.118 +	}
   1.119 +	else {
   1.120 +		*absolute_byte_offset = (FLAC__uint64)pos;
   1.121 +		return FLAC__STREAM_DECODER_TELL_STATUS_OK;
   1.122 +	}
   1.123 +}
   1.124 +
   1.125 +static FLAC__StreamDecoderLengthStatus flac_length_music_cb (
   1.126 +									const FLAC__StreamDecoder *decoder,
   1.127 +									FLAC__uint64 *stream_length,
   1.128 +									void *client_data) {
   1.129 +	FLAC_music *data = (FLAC_music*)client_data;
   1.130 +
   1.131 +	int pos = SDL_RWtell (data->rwops);
   1.132 +	int length = SDL_RWseek (data->rwops, 0, SEEK_END);
   1.133 +
   1.134 +	if (SDL_RWseek (data->rwops, pos, SEEK_SET) != pos || length < 0) {
   1.135 +		/* there was an error attempting to return the stream to the original
   1.136 +		 * position, or the length was invalid. */
   1.137 +		return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
   1.138 +	}
   1.139 +	else {
   1.140 +		*stream_length = (FLAC__uint64)length;
   1.141 +		return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
   1.142 +	}
   1.143 +}
   1.144 +
   1.145 +static FLAC__bool flac_eof_music_cb(
   1.146 +								const FLAC__StreamDecoder *decoder,
   1.147 +								void *client_data ) {
   1.148 +	FLAC_music *data = (FLAC_music*)client_data;
   1.149 +
   1.150 +	int pos = SDL_RWtell (data->rwops);
   1.151 +	int end = SDL_RWseek (data->rwops, 0, SEEK_END);
   1.152 +
   1.153 +	// was the original position equal to the end (a.k.a. the seek didn't move)?
   1.154 +	if (pos == end) {
   1.155 +		// must be EOF
   1.156 +		return true;
   1.157 +	}
   1.158 +	else {
   1.159 +		// not EOF, return to the original position
   1.160 +		SDL_RWseek (data->rwops, pos, SEEK_SET);
   1.161 +
   1.162 +		return false;
   1.163 +	}
   1.164 +}
   1.165 +
   1.166 +static FLAC__StreamDecoderWriteStatus flac_write_music_cb(
   1.167 +									const FLAC__StreamDecoder *decoder,
   1.168 +									const FLAC__Frame *frame,
   1.169 +									const FLAC__int32 *const buffer[],
   1.170 +									void *client_data) {
   1.171 +	FLAC_music *data = (FLAC_music *)client_data;
   1.172 +	size_t i;
   1.173 +
   1.174 +	if (data->flac_data.total_samples == 0) {
   1.175 +		SDL_SetError ("Given FLAC file does not specify its sample count.");
   1.176 +		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
   1.177 +	}
   1.178 +
   1.179 +	if (data->flac_data.channels != 2 ||
   1.180 +		data->flac_data.bits_per_sample != 16) {
   1.181 +		SDL_SetError("Current FLAC support is only for 16 bit Stereo files.");
   1.182 +		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
   1.183 +	}
   1.184 +
   1.185 +	for (i = 0; i < frame->header.blocksize; i++) {
   1.186 +		// make sure we still have at least two bytes that can be read (one for
   1.187 +		// each channel)
   1.188 +		if (data->flac_data.max_to_read >= 4) {
   1.189 +			// does the data block exist?
   1.190 +			if (!data->flac_data.data) {
   1.191 +				data->flac_data.data_len = data->flac_data.max_to_read;
   1.192 +				data->flac_data.data_read = 0;
   1.193 +
   1.194 +				// create it
   1.195 +				data->flac_data.data =
   1.196 +									(char *)malloc (data->flac_data.data_len);
   1.197 +
   1.198 +				if (!data->flac_data.data) {
   1.199 +					return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
   1.200 +				}
   1.201 +			}
   1.202 +
   1.203 +			FLAC__int16 i16;
   1.204 +			FLAC__uint16 ui16;
   1.205 +
   1.206 +			i16 = (FLAC__int16)buffer[0][i];
   1.207 +			ui16 = (FLAC__uint16)i16;
   1.208 +
   1.209 +			*((data->flac_data.data) + (data->flac_data.data_read++)) =
   1.210 +															(char)(ui16);
   1.211 +			*((data->flac_data.data) + (data->flac_data.data_read++)) =
   1.212 +															(char)(ui16 >> 8);
   1.213 +
   1.214 +			i16 = (FLAC__int16)buffer[1][i];
   1.215 +			ui16 = (FLAC__uint16)i16;
   1.216 +
   1.217 +			*((data->flac_data.data) + (data->flac_data.data_read++)) =
   1.218 +															(char)(ui16);
   1.219 +			*((data->flac_data.data) + (data->flac_data.data_read++)) =
   1.220 +															(char)(ui16 >> 8);
   1.221 +
   1.222 +			data->flac_data.max_to_read -= 4;
   1.223 +
   1.224 +			if (data->flac_data.max_to_read < 4) {
   1.225 +				// we need to set this so that the read halts from the
   1.226 +				// FLAC_getsome function.
   1.227 +				data->flac_data.max_to_read = 0;
   1.228 +			}
   1.229 +		}
   1.230 +		else {
   1.231 +			// we need to write to the overflow
   1.232 +			if (!data->flac_data.overflow) {
   1.233 +				data->flac_data.overflow_len =
   1.234 +											4 * (frame->header.blocksize - i);
   1.235 +				data->flac_data.overflow_read = 0;
   1.236 +
   1.237 +				// make it big enough for the rest of the block
   1.238 +				data->flac_data.overflow =
   1.239 +								(char *)malloc (data->flac_data.overflow_len);
   1.240 +
   1.241 +				if (!data->flac_data.overflow) {
   1.242 +					return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
   1.243 +				}
   1.244 +			}
   1.245 +
   1.246 +			FLAC__int16 i16;
   1.247 +			FLAC__uint16 ui16;
   1.248 +
   1.249 +			i16 = (FLAC__int16)buffer[0][i];
   1.250 +			ui16 = (FLAC__uint16)i16;
   1.251 +
   1.252 +			*((data->flac_data.overflow) + (data->flac_data.overflow_read++)) =
   1.253 +															(char)(ui16);
   1.254 +			*((data->flac_data.overflow) + (data->flac_data.overflow_read++)) =
   1.255 +															(char)(ui16 >> 8);
   1.256 +
   1.257 +			i16 = (FLAC__int16)buffer[1][i];
   1.258 +			ui16 = (FLAC__uint16)i16;
   1.259 +
   1.260 +			*((data->flac_data.overflow) + (data->flac_data.overflow_read++)) =
   1.261 +															(char)(ui16);
   1.262 +			*((data->flac_data.overflow) + (data->flac_data.overflow_read++)) =
   1.263 +															(char)(ui16 >> 8);
   1.264 +		}
   1.265 +	}
   1.266 +
   1.267 +	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
   1.268 +}
   1.269 +
   1.270 +static void flac_metadata_music_cb(
   1.271 +					const FLAC__StreamDecoder *decoder,
   1.272 +					const FLAC__StreamMetadata *metadata,
   1.273 +					void *client_data) {
   1.274 +	FLAC_music *data = (FLAC_music *)client_data;
   1.275 +
   1.276 +	if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
   1.277 +		data->flac_data.sample_rate = metadata->data.stream_info.sample_rate;
   1.278 +		data->flac_data.channels = metadata->data.stream_info.channels;
   1.279 +		data->flac_data.total_samples =
   1.280 +							metadata->data.stream_info.total_samples;
   1.281 +		data->flac_data.bits_per_sample =
   1.282 +							metadata->data.stream_info.bits_per_sample;
   1.283 +		data->flac_data.sample_size = data->flac_data.channels *
   1.284 +										((data->flac_data.bits_per_sample) / 8);
   1.285 +	}
   1.286 +}
   1.287 +
   1.288 +static void flac_error_music_cb(
   1.289 +				const FLAC__StreamDecoder *decoder,
   1.290 +				FLAC__StreamDecoderErrorStatus status,
   1.291 +				void *client_data) {
   1.292 +	// print an SDL error based on the error status
   1.293 +	switch (status) {
   1.294 +		case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC:
   1.295 +			SDL_SetError ("Error processing the FLAC file [LOST_SYNC].");
   1.296 +		break;
   1.297 +		case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER:
   1.298 +			SDL_SetError ("Error processing the FLAC file [BAD_HEADER].");
   1.299 +		break;
   1.300 +		case FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH:
   1.301 +			SDL_SetError ("Error processing the FLAC file [CRC_MISMATCH].");
   1.302 +		break;
   1.303 +		case FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM:
   1.304 +			SDL_SetError ("Error processing the FLAC file [UNPARSEABLE].");
   1.305 +		break;
   1.306 +		default:
   1.307 +			SDL_SetError ("Error processing the FLAC file [UNKNOWN].");
   1.308 +		break;
   1.309 +	}
   1.310 +}
   1.311 +
   1.312 +/* Load an FLAC stream from an SDL_RWops object */
   1.313 +FLAC_music *FLAC_new_RW(SDL_RWops *rw) {
   1.314 +	FLAC_music *music;
   1.315 +	int init_stage = 0;
   1.316 +	int was_error = 1;
   1.317 +
   1.318 +	music = (FLAC_music *)malloc ( sizeof (*music));
   1.319 +	if (music) {
   1.320 +		/* Initialize the music structure */
   1.321 +		memset (music, 0, (sizeof (*music)));
   1.322 +		FLAC_stop (music);
   1.323 +		FLAC_setvolume (music, MIX_MAX_VOLUME);
   1.324 +		music->section = -1;
   1.325 +		music->rwops = rw;
   1.326 +		music->flac_data.max_to_read = 0;
   1.327 +		music->flac_data.overflow = NULL;
   1.328 +		music->flac_data.overflow_len = 0;
   1.329 +		music->flac_data.overflow_read = 0;
   1.330 +		music->flac_data.data = NULL;
   1.331 +		music->flac_data.data_len = 0;
   1.332 +		music->flac_data.data_read = 0;
   1.333 +
   1.334 +		if (Mix_InitFLAC () >= 0) {
   1.335 +			init_stage++; // stage 1!
   1.336 +
   1.337 +			music->flac_decoder = flac.FLAC__stream_decoder_new ();
   1.338 +
   1.339 +			if (music->flac_decoder != NULL) {
   1.340 +				init_stage++; // stage 2!
   1.341 +
   1.342 +				if (flac.FLAC__stream_decoder_init_stream(
   1.343 +							music->flac_decoder,
   1.344 +							flac_read_music_cb, flac_seek_music_cb,
   1.345 +							flac_tell_music_cb, flac_length_music_cb,
   1.346 +							flac_eof_music_cb, flac_write_music_cb,
   1.347 +							flac_metadata_music_cb, flac_error_music_cb,
   1.348 +							music) == FLAC__STREAM_DECODER_INIT_STATUS_OK ) {
   1.349 +					init_stage++; // stage 3!
   1.350 +
   1.351 +					if (flac.FLAC__stream_decoder_process_until_end_of_metadata
   1.352 +											(music->flac_decoder)) {
   1.353 +						was_error = 0;
   1.354 +					}
   1.355 +				}
   1.356 +			}
   1.357 +		}
   1.358 +
   1.359 +		if (was_error) {
   1.360 +			switch (init_stage) {
   1.361 +				case 3:
   1.362 +					flac.FLAC__stream_decoder_finish( music->flac_decoder );
   1.363 +				case 2:
   1.364 +					flac.FLAC__stream_decoder_delete( music->flac_decoder );
   1.365 +				case 1:
   1.366 +					Mix_QuitFLAC();
   1.367 +				case 0:
   1.368 +					free(music);
   1.369 +					SDL_RWclose(rw);
   1.370 +					break;
   1.371 +			}
   1.372 +
   1.373 +			SDL_SetError ("There was an error in stage [%d] of FLAC init.",
   1.374 +							init_stage);
   1.375 +
   1.376 +			return NULL;
   1.377 +		}
   1.378 +	}
   1.379 +	else {
   1.380 +		SDL_OutOfMemory();
   1.381 +	}
   1.382 +
   1.383 +	return music;
   1.384 +}
   1.385 +
   1.386 +/* Start playback of a given FLAC stream */
   1.387 +void FLAC_play(FLAC_music *music) {
   1.388 +	music->playing = 1;
   1.389 +}
   1.390 +
   1.391 +/* Return non-zero if a stream is currently playing */
   1.392 +int FLAC_playing(FLAC_music *music) {
   1.393 +	return(music->playing);
   1.394 +}
   1.395 +
   1.396 +/* Read some FLAC stream data and convert it for output */
   1.397 +static void FLAC_getsome(FLAC_music *music) {
   1.398 +	int section;
   1.399 +	SDL_AudioCVT *cvt;
   1.400 +
   1.401 +	/* GET AUDIO wAVE DATA */
   1.402 +	// set the max number of characters to read
   1.403 +	music->flac_data.max_to_read = 8192;
   1.404 +
   1.405 +	// clear out the data buffer if it exists
   1.406 +	if (music->flac_data.data) {
   1.407 +		free (music->flac_data.data);
   1.408 +	}
   1.409 +
   1.410 +	music->flac_data.data_len = music->flac_data.max_to_read;
   1.411 +	music->flac_data.data_read = 0;
   1.412 +	music->flac_data.data = (char *)malloc (music->flac_data.data_len);
   1.413 +
   1.414 +	// we have data to read
   1.415 +	while(music->flac_data.max_to_read > 0) {
   1.416 +		// first check if there is data in the overflow from before
   1.417 +		if (music->flac_data.overflow) {
   1.418 +			size_t overflow_len = music->flac_data.overflow_read;
   1.419 +
   1.420 +			if (overflow_len > music->flac_data.max_to_read) {
   1.421 +				size_t overflow_extra_len = overflow_len -
   1.422 +												music->flac_data.max_to_read;
   1.423 +
   1.424 +				char* new_overflow = (char *)malloc (overflow_extra_len);
   1.425 +				memcpy (music->flac_data.data+music->flac_data.data_read,
   1.426 +					music->flac_data.overflow, music->flac_data.max_to_read);
   1.427 +				music->flac_data.data_read += music->flac_data.max_to_read;
   1.428 +				memcpy (new_overflow,
   1.429 +					music->flac_data.overflow + music->flac_data.max_to_read,
   1.430 +					overflow_extra_len);
   1.431 +				free (music->flac_data.overflow);
   1.432 +				music->flac_data.overflow = new_overflow;
   1.433 +				music->flac_data.overflow_len = overflow_extra_len;
   1.434 +				music->flac_data.overflow_read = 0;
   1.435 +				music->flac_data.max_to_read = 0;
   1.436 +			}
   1.437 +			else {
   1.438 +				memcpy (music->flac_data.data+music->flac_data.data_read,
   1.439 +					music->flac_data.overflow, overflow_len);
   1.440 +				music->flac_data.data_read += overflow_len;
   1.441 +				free (music->flac_data.overflow);
   1.442 +				music->flac_data.overflow = NULL;
   1.443 +				music->flac_data.overflow_len = 0;
   1.444 +				music->flac_data.overflow_read = 0;
   1.445 +				music->flac_data.max_to_read -= overflow_len;
   1.446 +			}
   1.447 +		}
   1.448 +		else {
   1.449 +			if (!flac.FLAC__stream_decoder_process_single (
   1.450 +														music->flac_decoder)) {
   1.451 +				music->flac_data.max_to_read = 0;
   1.452 +			}
   1.453 +
   1.454 +			if (flac.FLAC__stream_decoder_get_state (music->flac_decoder)
   1.455 +									== FLAC__STREAM_DECODER_END_OF_STREAM) {
   1.456 +				music->flac_data.max_to_read = 0;
   1.457 +			}
   1.458 +		}
   1.459 +	}
   1.460 +
   1.461 +	if (music->flac_data.data_read <= 0) {
   1.462 +		if (music->flac_data.data_read == 0) {
   1.463 +			music->playing = 0;
   1.464 +		}
   1.465 +		return;
   1.466 +	}
   1.467 +	cvt = &music->cvt;
   1.468 +	if (section != music->section) {
   1.469 +
   1.470 +		SDL_BuildAudioCVT (cvt, AUDIO_S16, (Uint8)music->flac_data.channels,
   1.471 +						(int)music->flac_data.sample_rate, mixer.format,
   1.472 +		                mixer.channels, mixer.freq);
   1.473 +		if (cvt->buf) {
   1.474 +			free (cvt->buf);
   1.475 +		}
   1.476 +		cvt->buf = (Uint8 *)malloc (music->flac_data.data_len * cvt->len_mult);
   1.477 +		music->section = section;
   1.478 +	}
   1.479 +	if (cvt->buf) {
   1.480 +		memcpy (cvt->buf, music->flac_data.data, music->flac_data.data_read);
   1.481 +		if (cvt->needed) {
   1.482 +			cvt->len = music->flac_data.data_read;
   1.483 +			SDL_ConvertAudio (cvt);
   1.484 +		}
   1.485 +		else {
   1.486 +			cvt->len_cvt = music->flac_data.data_read;
   1.487 +		}
   1.488 +		music->len_available = music->cvt.len_cvt;
   1.489 +		music->snd_available = music->cvt.buf;
   1.490 +	}
   1.491 +	else {
   1.492 +		SDL_SetError ("Out of memory");
   1.493 +		music->playing = 0;
   1.494 +	}
   1.495 +}
   1.496 +
   1.497 +/* Play some of a stream previously started with FLAC_play() */
   1.498 +int FLAC_playAudio(FLAC_music *music, Uint8 *snd, int len) {
   1.499 +	int mixable;
   1.500 +
   1.501 +	while ((len > 0) && music->playing) {
   1.502 +		if (!music->len_available) {
   1.503 +			FLAC_getsome (music);
   1.504 +		}
   1.505 +		mixable = len;
   1.506 +		if (mixable > music->len_available) {
   1.507 +			mixable = music->len_available;
   1.508 +		}
   1.509 +		if (music->volume == MIX_MAX_VOLUME) {
   1.510 +			memcpy (snd, music->snd_available, mixable);
   1.511 +		}
   1.512 +		else {
   1.513 +			SDL_MixAudio (snd, music->snd_available, mixable, music->volume);
   1.514 +		}
   1.515 +		music->len_available -= mixable;
   1.516 +		music->snd_available += mixable;
   1.517 +		len -= mixable;
   1.518 +		snd += mixable;
   1.519 +	}
   1.520 +
   1.521 +	return len;
   1.522 +}
   1.523 +
   1.524 +/* Stop playback of a stream previously started with FLAC_play() */
   1.525 +void FLAC_stop(FLAC_music *music) {
   1.526 +	music->playing = 0;
   1.527 +}
   1.528 +
   1.529 +/* Close the given FLAC_music object */
   1.530 +void FLAC_delete(FLAC_music *music) {
   1.531 +	if (music) {
   1.532 +		if (music->flac_decoder) {
   1.533 +			flac.FLAC__stream_decoder_finish (music->flac_decoder);
   1.534 +			flac.FLAC__stream_decoder_delete (music->flac_decoder);
   1.535 +		}
   1.536 +
   1.537 +		if (music->flac_data.data) {
   1.538 +			free (music->flac_data.data);
   1.539 +		}
   1.540 +
   1.541 +		if (music->flac_data.overflow) {
   1.542 +			free (music->flac_data.overflow);
   1.543 +		}
   1.544 +
   1.545 +		if (music->cvt.buf) {
   1.546 +			free (music->cvt.buf);
   1.547 +		}
   1.548 +
   1.549 +		free (music);
   1.550 +
   1.551 +		Mix_QuitFLAC ();
   1.552 +	}
   1.553 +}
   1.554 +
   1.555 +/* Jump (seek) to a given position (time is in seconds) */
   1.556 +void FLAC_jump_to_time(FLAC_music *music, double time) {
   1.557 +	if (music) {
   1.558 +		if (music->flac_decoder) {
   1.559 +			double seek_sample = music->flac_data.sample_rate * time;
   1.560 +
   1.561 +			// clear data if it has data
   1.562 +			if (music->flac_data.data) {
   1.563 +				free (music->flac_data.data);
   1.564 +				music->flac_data.data = NULL;
   1.565 +			}
   1.566 +
   1.567 +			// clear overflow if it has data
   1.568 +			if (music->flac_data.overflow) {
   1.569 +				free (music->flac_data.overflow);
   1.570 +				music->flac_data.overflow = NULL;
   1.571 +			}
   1.572 +
   1.573 +			if (!flac.FLAC__stream_decoder_seek_absolute (music->flac_decoder,
   1.574 +												(FLAC__uint64)seek_sample)) {
   1.575 +				if (flac.FLAC__stream_decoder_get_state (music->flac_decoder)
   1.576 +										== FLAC__STREAM_DECODER_SEEK_ERROR) {
   1.577 +					flac.FLAC__stream_decoder_flush (music->flac_decoder);
   1.578 +				}
   1.579 +
   1.580 +				SDL_SetError
   1.581 +					("Seeking of FLAC stream failed: libFLAC seek failed.");
   1.582 +			}
   1.583 +		}
   1.584 +		else {
   1.585 +			SDL_SetError
   1.586 +				("Seeking of FLAC stream failed: FLAC decoder was NULL.");
   1.587 +		}
   1.588 +	}
   1.589 +	else {
   1.590 +		SDL_SetError ("Seeking of FLAC stream failed: music was NULL.");
   1.591 +	}
   1.592 +}
   1.593 +
   1.594 +#endif /* FLAC_MUSIC */