music_flac.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 31 Dec 2011 10:27:48 -0500
changeset 518 8bc9b5fd2aae
parent 487 485a29de2b2d
child 521 565549e046b0
permissions -rw-r--r--
Happy New Year!
slouken@382
     1
/*
slouken@518
     2
  SDL_mixer:  An audio mixer library based on the SDL library
slouken@518
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@382
     4
slouken@518
     5
  This software is provided 'as-is', without any express or implied
slouken@518
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@518
     7
  arising from the use of this software.
slouken@382
     8
slouken@518
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@518
    10
  including commercial applications, and to alter it and redistribute it
slouken@518
    11
  freely, subject to the following restrictions:
slouken@382
    12
slouken@518
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@518
    14
     claim that you wrote the original software. If you use this software
slouken@518
    15
     in a product, an acknowledgment in the product documentation would be
slouken@518
    16
     appreciated but is not required.
slouken@518
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@518
    18
     misrepresented as being the original software.
slouken@518
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@382
    20
slouken@518
    21
  This file is used to support SDL_LoadMUS playback of FLAC files.
slouken@518
    22
  	~ Austen Dicken (admin@cvpcs.org)
slouken@382
    23
*/
slouken@382
    24
slouken@382
    25
#ifdef FLAC_MUSIC
slouken@382
    26
slouken@382
    27
#include <stdio.h>
slouken@382
    28
#include <stdlib.h>
slouken@382
    29
#include <string.h>
slouken@382
    30
slouken@382
    31
#include "SDL_mixer.h"
slouken@382
    32
#include "dynamic_flac.h"
slouken@382
    33
#include "music_flac.h"
slouken@382
    34
slouken@382
    35
/* This is the format of the audio mixer data */
slouken@382
    36
static SDL_AudioSpec mixer;
slouken@382
    37
slouken@382
    38
/* Initialize the FLAC player, with the given mixer settings
slouken@382
    39
   This function returns 0, or -1 if there was an error.
slouken@382
    40
 */
slouken@409
    41
int FLAC_init(SDL_AudioSpec *mixerfmt)
slouken@409
    42
{
slouken@382
    43
	mixer = *mixerfmt;
slouken@382
    44
	return(0);
slouken@382
    45
}
slouken@382
    46
slouken@382
    47
/* Set the volume for an FLAC stream */
slouken@409
    48
void FLAC_setvolume(FLAC_music *music, int volume)
slouken@409
    49
{
slouken@382
    50
	music->volume = volume;
slouken@382
    51
}
slouken@382
    52
slouken@382
    53
/* Load an FLAC stream from the given file */
slouken@409
    54
FLAC_music *FLAC_new(const char *file)
slouken@409
    55
{
slouken@382
    56
	SDL_RWops *rw;
slouken@382
    57
slouken@382
    58
	rw = SDL_RWFromFile (file, "rb");
slouken@382
    59
	if (rw == NULL) {
slouken@382
    60
		SDL_SetError ("Couldn't open %s", file);
slouken@382
    61
		return NULL;
slouken@382
    62
	}
slouken@382
    63
	return FLAC_new_RW (rw);
slouken@382
    64
}
slouken@382
    65
slouken@382
    66
static FLAC__StreamDecoderReadStatus flac_read_music_cb(
slouken@382
    67
									const FLAC__StreamDecoder *decoder,
slouken@382
    68
									FLAC__byte buffer[],
slouken@382
    69
									size_t *bytes,
slouken@409
    70
									void *client_data)
slouken@409
    71
{
slouken@382
    72
	FLAC_music *data = (FLAC_music*)client_data;
slouken@382
    73
slouken@382
    74
	// make sure there is something to be reading
slouken@382
    75
	if (*bytes > 0) {
slouken@382
    76
		*bytes = SDL_RWread (data->rwops, buffer, sizeof (FLAC__byte), *bytes);
slouken@382
    77
slouken@382
    78
		if (*bytes < 0) { // error in read
slouken@382
    79
			return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
slouken@382
    80
		}
slouken@382
    81
		else if (*bytes == 0 ) { // no data was read (EOF)
slouken@382
    82
			return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
slouken@382
    83
		}
slouken@382
    84
		else { // data was read, continue
slouken@382
    85
			return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
slouken@382
    86
		}
slouken@382
    87
	}
slouken@382
    88
	else {
slouken@382
    89
		return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
slouken@382
    90
	}
slouken@382
    91
}
slouken@382
    92
slouken@382
    93
static FLAC__StreamDecoderSeekStatus flac_seek_music_cb(
slouken@382
    94
									const FLAC__StreamDecoder *decoder,
slouken@382
    95
									FLAC__uint64 absolute_byte_offset,
slouken@409
    96
									void *client_data)
slouken@409
    97
{
slouken@382
    98
	FLAC_music *data = (FLAC_music*)client_data;
slouken@382
    99
slouken@473
   100
	if (SDL_RWseek (data->rwops, absolute_byte_offset, RW_SEEK_SET) < 0) {
slouken@382
   101
		return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
slouken@382
   102
	}
slouken@382
   103
	else {
slouken@382
   104
		return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
slouken@382
   105
	}
slouken@382
   106
}
slouken@382
   107
slouken@382
   108
static FLAC__StreamDecoderTellStatus flac_tell_music_cb(
slouken@382
   109
									const FLAC__StreamDecoder *decoder,
slouken@382
   110
									FLAC__uint64 *absolute_byte_offset,
slouken@409
   111
									void *client_data )
slouken@409
   112
{
slouken@382
   113
	FLAC_music *data = (FLAC_music*)client_data;
slouken@382
   114
slouken@382
   115
	int pos = SDL_RWtell (data->rwops);
slouken@382
   116
slouken@382
   117
	if (pos < 0) {
slouken@382
   118
		return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
slouken@382
   119
	}
slouken@382
   120
	else {
slouken@382
   121
		*absolute_byte_offset = (FLAC__uint64)pos;
slouken@382
   122
		return FLAC__STREAM_DECODER_TELL_STATUS_OK;
slouken@382
   123
	}
slouken@382
   124
}
slouken@382
   125
slouken@382
   126
static FLAC__StreamDecoderLengthStatus flac_length_music_cb (
slouken@382
   127
									const FLAC__StreamDecoder *decoder,
slouken@382
   128
									FLAC__uint64 *stream_length,
slouken@409
   129
									void *client_data)
slouken@409
   130
{
slouken@382
   131
	FLAC_music *data = (FLAC_music*)client_data;
slouken@382
   132
slouken@382
   133
	int pos = SDL_RWtell (data->rwops);
slouken@473
   134
	int length = SDL_RWseek (data->rwops, 0, RW_SEEK_END);
slouken@382
   135
slouken@473
   136
	if (SDL_RWseek (data->rwops, pos, RW_SEEK_SET) != pos || length < 0) {
slouken@382
   137
		/* there was an error attempting to return the stream to the original
slouken@382
   138
		 * position, or the length was invalid. */
slouken@382
   139
		return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
slouken@382
   140
	}
slouken@382
   141
	else {
slouken@382
   142
		*stream_length = (FLAC__uint64)length;
slouken@382
   143
		return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
slouken@382
   144
	}
slouken@382
   145
}
slouken@382
   146
slouken@382
   147
static FLAC__bool flac_eof_music_cb(
slouken@382
   148
								const FLAC__StreamDecoder *decoder,
slouken@409
   149
								void *client_data )
slouken@409
   150
{
slouken@382
   151
	FLAC_music *data = (FLAC_music*)client_data;
slouken@382
   152
slouken@382
   153
	int pos = SDL_RWtell (data->rwops);
slouken@473
   154
	int end = SDL_RWseek (data->rwops, 0, RW_SEEK_END);
slouken@382
   155
slouken@382
   156
	// was the original position equal to the end (a.k.a. the seek didn't move)?
slouken@382
   157
	if (pos == end) {
slouken@382
   158
		// must be EOF
slouken@382
   159
		return true;
slouken@382
   160
	}
slouken@382
   161
	else {
slouken@382
   162
		// not EOF, return to the original position
slouken@473
   163
		SDL_RWseek (data->rwops, pos, RW_SEEK_SET);
slouken@382
   164
slouken@382
   165
		return false;
slouken@382
   166
	}
slouken@382
   167
}
slouken@382
   168
slouken@382
   169
static FLAC__StreamDecoderWriteStatus flac_write_music_cb(
slouken@382
   170
									const FLAC__StreamDecoder *decoder,
slouken@382
   171
									const FLAC__Frame *frame,
slouken@382
   172
									const FLAC__int32 *const buffer[],
slouken@409
   173
									void *client_data)
slouken@409
   174
{
slouken@382
   175
	FLAC_music *data = (FLAC_music *)client_data;
slouken@382
   176
	size_t i;
slouken@382
   177
slouken@382
   178
	if (data->flac_data.total_samples == 0) {
slouken@382
   179
		SDL_SetError ("Given FLAC file does not specify its sample count.");
slouken@382
   180
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
slouken@382
   181
	}
slouken@382
   182
slouken@382
   183
	if (data->flac_data.channels != 2 ||
slouken@382
   184
		data->flac_data.bits_per_sample != 16) {
slouken@382
   185
		SDL_SetError("Current FLAC support is only for 16 bit Stereo files.");
slouken@382
   186
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
slouken@382
   187
	}
slouken@382
   188
slouken@382
   189
	for (i = 0; i < frame->header.blocksize; i++) {
slouken@436
   190
		FLAC__int16 i16;
slouken@436
   191
		FLAC__uint16 ui16;
slouken@436
   192
slouken@382
   193
		// make sure we still have at least two bytes that can be read (one for
slouken@382
   194
		// each channel)
slouken@382
   195
		if (data->flac_data.max_to_read >= 4) {
slouken@382
   196
			// does the data block exist?
slouken@382
   197
			if (!data->flac_data.data) {
slouken@382
   198
				data->flac_data.data_len = data->flac_data.max_to_read;
slouken@382
   199
				data->flac_data.data_read = 0;
slouken@382
   200
slouken@382
   201
				// create it
slouken@382
   202
				data->flac_data.data =
slouken@382
   203
									(char *)malloc (data->flac_data.data_len);
slouken@382
   204
slouken@382
   205
				if (!data->flac_data.data) {
slouken@382
   206
					return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
slouken@382
   207
				}
slouken@382
   208
			}
slouken@382
   209
slouken@382
   210
			i16 = (FLAC__int16)buffer[0][i];
slouken@382
   211
			ui16 = (FLAC__uint16)i16;
slouken@382
   212
slouken@382
   213
			*((data->flac_data.data) + (data->flac_data.data_read++)) =
slouken@382
   214
															(char)(ui16);
slouken@382
   215
			*((data->flac_data.data) + (data->flac_data.data_read++)) =
slouken@382
   216
															(char)(ui16 >> 8);
slouken@382
   217
slouken@382
   218
			i16 = (FLAC__int16)buffer[1][i];
slouken@382
   219
			ui16 = (FLAC__uint16)i16;
slouken@382
   220
slouken@382
   221
			*((data->flac_data.data) + (data->flac_data.data_read++)) =
slouken@382
   222
															(char)(ui16);
slouken@382
   223
			*((data->flac_data.data) + (data->flac_data.data_read++)) =
slouken@382
   224
															(char)(ui16 >> 8);
slouken@382
   225
slouken@382
   226
			data->flac_data.max_to_read -= 4;
slouken@382
   227
slouken@382
   228
			if (data->flac_data.max_to_read < 4) {
slouken@382
   229
				// we need to set this so that the read halts from the
slouken@382
   230
				// FLAC_getsome function.
slouken@382
   231
				data->flac_data.max_to_read = 0;
slouken@382
   232
			}
slouken@382
   233
		}
slouken@382
   234
		else {
slouken@382
   235
			// we need to write to the overflow
slouken@382
   236
			if (!data->flac_data.overflow) {
slouken@382
   237
				data->flac_data.overflow_len =
slouken@382
   238
											4 * (frame->header.blocksize - i);
slouken@382
   239
				data->flac_data.overflow_read = 0;
slouken@382
   240
slouken@382
   241
				// make it big enough for the rest of the block
slouken@382
   242
				data->flac_data.overflow =
slouken@382
   243
								(char *)malloc (data->flac_data.overflow_len);
slouken@382
   244
slouken@382
   245
				if (!data->flac_data.overflow) {
slouken@382
   246
					return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
slouken@382
   247
				}
slouken@382
   248
			}
slouken@382
   249
slouken@382
   250
			i16 = (FLAC__int16)buffer[0][i];
slouken@382
   251
			ui16 = (FLAC__uint16)i16;
slouken@382
   252
slouken@382
   253
			*((data->flac_data.overflow) + (data->flac_data.overflow_read++)) =
slouken@382
   254
															(char)(ui16);
slouken@382
   255
			*((data->flac_data.overflow) + (data->flac_data.overflow_read++)) =
slouken@382
   256
															(char)(ui16 >> 8);
slouken@382
   257
slouken@382
   258
			i16 = (FLAC__int16)buffer[1][i];
slouken@382
   259
			ui16 = (FLAC__uint16)i16;
slouken@382
   260
slouken@382
   261
			*((data->flac_data.overflow) + (data->flac_data.overflow_read++)) =
slouken@382
   262
															(char)(ui16);
slouken@382
   263
			*((data->flac_data.overflow) + (data->flac_data.overflow_read++)) =
slouken@382
   264
															(char)(ui16 >> 8);
slouken@382
   265
		}
slouken@382
   266
	}
slouken@382
   267
slouken@382
   268
	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
slouken@382
   269
}
slouken@382
   270
slouken@382
   271
static void flac_metadata_music_cb(
slouken@382
   272
					const FLAC__StreamDecoder *decoder,
slouken@382
   273
					const FLAC__StreamMetadata *metadata,
slouken@409
   274
					void *client_data)
slouken@409
   275
{
slouken@382
   276
	FLAC_music *data = (FLAC_music *)client_data;
slouken@382
   277
slouken@382
   278
	if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
slouken@382
   279
		data->flac_data.sample_rate = metadata->data.stream_info.sample_rate;
slouken@382
   280
		data->flac_data.channels = metadata->data.stream_info.channels;
slouken@382
   281
		data->flac_data.total_samples =
slouken@382
   282
							metadata->data.stream_info.total_samples;
slouken@382
   283
		data->flac_data.bits_per_sample =
slouken@382
   284
							metadata->data.stream_info.bits_per_sample;
slouken@382
   285
		data->flac_data.sample_size = data->flac_data.channels *
slouken@382
   286
										((data->flac_data.bits_per_sample) / 8);
slouken@382
   287
	}
slouken@382
   288
}
slouken@382
   289
slouken@382
   290
static void flac_error_music_cb(
slouken@382
   291
				const FLAC__StreamDecoder *decoder,
slouken@382
   292
				FLAC__StreamDecoderErrorStatus status,
slouken@409
   293
				void *client_data)
slouken@409
   294
{
slouken@382
   295
	// print an SDL error based on the error status
slouken@382
   296
	switch (status) {
slouken@382
   297
		case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC:
slouken@382
   298
			SDL_SetError ("Error processing the FLAC file [LOST_SYNC].");
slouken@382
   299
		break;
slouken@382
   300
		case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER:
slouken@382
   301
			SDL_SetError ("Error processing the FLAC file [BAD_HEADER].");
slouken@382
   302
		break;
slouken@382
   303
		case FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH:
slouken@382
   304
			SDL_SetError ("Error processing the FLAC file [CRC_MISMATCH].");
slouken@382
   305
		break;
slouken@382
   306
		case FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM:
slouken@382
   307
			SDL_SetError ("Error processing the FLAC file [UNPARSEABLE].");
slouken@382
   308
		break;
slouken@382
   309
		default:
slouken@382
   310
			SDL_SetError ("Error processing the FLAC file [UNKNOWN].");
slouken@382
   311
		break;
slouken@382
   312
	}
slouken@382
   313
}
slouken@382
   314
slouken@382
   315
/* Load an FLAC stream from an SDL_RWops object */
slouken@409
   316
FLAC_music *FLAC_new_RW(SDL_RWops *rw)
slouken@409
   317
{
slouken@382
   318
	FLAC_music *music;
slouken@382
   319
	int init_stage = 0;
slouken@382
   320
	int was_error = 1;
slouken@382
   321
slouken@382
   322
	music = (FLAC_music *)malloc ( sizeof (*music));
slouken@382
   323
	if (music) {
slouken@382
   324
		/* Initialize the music structure */
slouken@382
   325
		memset (music, 0, (sizeof (*music)));
slouken@382
   326
		FLAC_stop (music);
slouken@382
   327
		FLAC_setvolume (music, MIX_MAX_VOLUME);
slouken@382
   328
		music->section = -1;
slouken@382
   329
		music->rwops = rw;
slouken@382
   330
		music->flac_data.max_to_read = 0;
slouken@382
   331
		music->flac_data.overflow = NULL;
slouken@382
   332
		music->flac_data.overflow_len = 0;
slouken@382
   333
		music->flac_data.overflow_read = 0;
slouken@382
   334
		music->flac_data.data = NULL;
slouken@382
   335
		music->flac_data.data_len = 0;
slouken@382
   336
		music->flac_data.data_read = 0;
slouken@382
   337
slouken@470
   338
		if (Mix_Init(MIX_INIT_FLAC)) {
slouken@382
   339
			init_stage++; // stage 1!
slouken@382
   340
slouken@382
   341
			music->flac_decoder = flac.FLAC__stream_decoder_new ();
slouken@382
   342
slouken@382
   343
			if (music->flac_decoder != NULL) {
slouken@382
   344
				init_stage++; // stage 2!
slouken@382
   345
slouken@382
   346
				if (flac.FLAC__stream_decoder_init_stream(
slouken@382
   347
							music->flac_decoder,
slouken@382
   348
							flac_read_music_cb, flac_seek_music_cb,
slouken@382
   349
							flac_tell_music_cb, flac_length_music_cb,
slouken@382
   350
							flac_eof_music_cb, flac_write_music_cb,
slouken@382
   351
							flac_metadata_music_cb, flac_error_music_cb,
slouken@382
   352
							music) == FLAC__STREAM_DECODER_INIT_STATUS_OK ) {
slouken@382
   353
					init_stage++; // stage 3!
slouken@382
   354
slouken@382
   355
					if (flac.FLAC__stream_decoder_process_until_end_of_metadata
slouken@382
   356
											(music->flac_decoder)) {
slouken@382
   357
						was_error = 0;
slouken@470
   358
					} else {
slouken@470
   359
						SDL_SetError("FLAC__stream_decoder_process_until_end_of_metadata() failed");
slouken@382
   360
					}
slouken@470
   361
				} else {
slouken@470
   362
					SDL_SetError("FLAC__stream_decoder_init_stream() failed");
slouken@382
   363
				}
slouken@470
   364
			} else {
slouken@470
   365
				SDL_SetError("FLAC__stream_decoder_new() failed");
slouken@382
   366
			}
slouken@382
   367
		}
slouken@382
   368
slouken@382
   369
		if (was_error) {
slouken@382
   370
			switch (init_stage) {
slouken@382
   371
				case 3:
slouken@382
   372
					flac.FLAC__stream_decoder_finish( music->flac_decoder );
slouken@382
   373
				case 2:
slouken@382
   374
					flac.FLAC__stream_decoder_delete( music->flac_decoder );
slouken@382
   375
				case 1:
slouken@382
   376
				case 0:
slouken@382
   377
					free(music);
slouken@382
   378
					SDL_RWclose(rw);
slouken@382
   379
					break;
slouken@382
   380
			}
slouken@382
   381
			return NULL;
slouken@382
   382
		}
slouken@382
   383
	}
slouken@382
   384
	else {
slouken@382
   385
		SDL_OutOfMemory();
slouken@382
   386
	}
slouken@382
   387
slouken@382
   388
	return music;
slouken@382
   389
}
slouken@382
   390
slouken@382
   391
/* Start playback of a given FLAC stream */
slouken@409
   392
void FLAC_play(FLAC_music *music)
slouken@409
   393
{
slouken@382
   394
	music->playing = 1;
slouken@382
   395
}
slouken@382
   396
slouken@382
   397
/* Return non-zero if a stream is currently playing */
slouken@409
   398
int FLAC_playing(FLAC_music *music)
slouken@409
   399
{
slouken@382
   400
	return(music->playing);
slouken@382
   401
}
slouken@382
   402
slouken@382
   403
/* Read some FLAC stream data and convert it for output */
slouken@409
   404
static void FLAC_getsome(FLAC_music *music)
slouken@409
   405
{
slouken@382
   406
	SDL_AudioCVT *cvt;
slouken@382
   407
slouken@382
   408
	/* GET AUDIO wAVE DATA */
slouken@382
   409
	// set the max number of characters to read
slouken@382
   410
	music->flac_data.max_to_read = 8192;
slouken@382
   411
slouken@382
   412
	// clear out the data buffer if it exists
slouken@382
   413
	if (music->flac_data.data) {
slouken@382
   414
		free (music->flac_data.data);
slouken@382
   415
	}
slouken@382
   416
slouken@382
   417
	music->flac_data.data_len = music->flac_data.max_to_read;
slouken@382
   418
	music->flac_data.data_read = 0;
slouken@382
   419
	music->flac_data.data = (char *)malloc (music->flac_data.data_len);
slouken@382
   420
slouken@382
   421
	// we have data to read
slouken@382
   422
	while(music->flac_data.max_to_read > 0) {
slouken@382
   423
		// first check if there is data in the overflow from before
slouken@382
   424
		if (music->flac_data.overflow) {
slouken@382
   425
			size_t overflow_len = music->flac_data.overflow_read;
slouken@382
   426
slouken@382
   427
			if (overflow_len > music->flac_data.max_to_read) {
slouken@382
   428
				size_t overflow_extra_len = overflow_len -
slouken@382
   429
												music->flac_data.max_to_read;
slouken@382
   430
slouken@382
   431
				char* new_overflow = (char *)malloc (overflow_extra_len);
slouken@382
   432
				memcpy (music->flac_data.data+music->flac_data.data_read,
slouken@382
   433
					music->flac_data.overflow, music->flac_data.max_to_read);
slouken@382
   434
				music->flac_data.data_read += music->flac_data.max_to_read;
slouken@382
   435
				memcpy (new_overflow,
slouken@382
   436
					music->flac_data.overflow + music->flac_data.max_to_read,
slouken@382
   437
					overflow_extra_len);
slouken@382
   438
				free (music->flac_data.overflow);
slouken@382
   439
				music->flac_data.overflow = new_overflow;
slouken@382
   440
				music->flac_data.overflow_len = overflow_extra_len;
slouken@382
   441
				music->flac_data.overflow_read = 0;
slouken@382
   442
				music->flac_data.max_to_read = 0;
slouken@382
   443
			}
slouken@382
   444
			else {
slouken@382
   445
				memcpy (music->flac_data.data+music->flac_data.data_read,
slouken@382
   446
					music->flac_data.overflow, overflow_len);
slouken@382
   447
				music->flac_data.data_read += overflow_len;
slouken@382
   448
				free (music->flac_data.overflow);
slouken@382
   449
				music->flac_data.overflow = NULL;
slouken@382
   450
				music->flac_data.overflow_len = 0;
slouken@382
   451
				music->flac_data.overflow_read = 0;
slouken@382
   452
				music->flac_data.max_to_read -= overflow_len;
slouken@382
   453
			}
slouken@382
   454
		}
slouken@382
   455
		else {
slouken@382
   456
			if (!flac.FLAC__stream_decoder_process_single (
slouken@382
   457
														music->flac_decoder)) {
slouken@382
   458
				music->flac_data.max_to_read = 0;
slouken@382
   459
			}
slouken@382
   460
slouken@382
   461
			if (flac.FLAC__stream_decoder_get_state (music->flac_decoder)
slouken@382
   462
									== FLAC__STREAM_DECODER_END_OF_STREAM) {
slouken@382
   463
				music->flac_data.max_to_read = 0;
slouken@382
   464
			}
slouken@382
   465
		}
slouken@382
   466
	}
slouken@382
   467
slouken@382
   468
	if (music->flac_data.data_read <= 0) {
slouken@382
   469
		if (music->flac_data.data_read == 0) {
slouken@382
   470
			music->playing = 0;
slouken@382
   471
		}
slouken@382
   472
		return;
slouken@382
   473
	}
slouken@382
   474
	cvt = &music->cvt;
slouken@487
   475
	if (music->section < 0) {
slouken@382
   476
slouken@382
   477
		SDL_BuildAudioCVT (cvt, AUDIO_S16, (Uint8)music->flac_data.channels,
slouken@382
   478
						(int)music->flac_data.sample_rate, mixer.format,
slouken@382
   479
		                mixer.channels, mixer.freq);
slouken@382
   480
		if (cvt->buf) {
slouken@382
   481
			free (cvt->buf);
slouken@382
   482
		}
slouken@382
   483
		cvt->buf = (Uint8 *)malloc (music->flac_data.data_len * cvt->len_mult);
slouken@487
   484
		music->section = 0;
slouken@382
   485
	}
slouken@382
   486
	if (cvt->buf) {
slouken@382
   487
		memcpy (cvt->buf, music->flac_data.data, music->flac_data.data_read);
slouken@382
   488
		if (cvt->needed) {
slouken@382
   489
			cvt->len = music->flac_data.data_read;
slouken@382
   490
			SDL_ConvertAudio (cvt);
slouken@382
   491
		}
slouken@382
   492
		else {
slouken@382
   493
			cvt->len_cvt = music->flac_data.data_read;
slouken@382
   494
		}
slouken@382
   495
		music->len_available = music->cvt.len_cvt;
slouken@382
   496
		music->snd_available = music->cvt.buf;
slouken@382
   497
	}
slouken@382
   498
	else {
slouken@382
   499
		SDL_SetError ("Out of memory");
slouken@382
   500
		music->playing = 0;
slouken@382
   501
	}
slouken@382
   502
}
slouken@382
   503
slouken@382
   504
/* Play some of a stream previously started with FLAC_play() */
slouken@409
   505
int FLAC_playAudio(FLAC_music *music, Uint8 *snd, int len)
slouken@409
   506
{
slouken@382
   507
	int mixable;
slouken@382
   508
slouken@382
   509
	while ((len > 0) && music->playing) {
slouken@382
   510
		if (!music->len_available) {
slouken@382
   511
			FLAC_getsome (music);
slouken@382
   512
		}
slouken@382
   513
		mixable = len;
slouken@382
   514
		if (mixable > music->len_available) {
slouken@382
   515
			mixable = music->len_available;
slouken@382
   516
		}
slouken@382
   517
		if (music->volume == MIX_MAX_VOLUME) {
slouken@382
   518
			memcpy (snd, music->snd_available, mixable);
slouken@382
   519
		}
slouken@382
   520
		else {
slouken@382
   521
			SDL_MixAudio (snd, music->snd_available, mixable, music->volume);
slouken@382
   522
		}
slouken@382
   523
		music->len_available -= mixable;
slouken@382
   524
		music->snd_available += mixable;
slouken@382
   525
		len -= mixable;
slouken@382
   526
		snd += mixable;
slouken@382
   527
	}
slouken@382
   528
slouken@382
   529
	return len;
slouken@382
   530
}
slouken@382
   531
slouken@382
   532
/* Stop playback of a stream previously started with FLAC_play() */
slouken@409
   533
void FLAC_stop(FLAC_music *music)
slouken@409
   534
{
slouken@382
   535
	music->playing = 0;
slouken@382
   536
}
slouken@382
   537
slouken@382
   538
/* Close the given FLAC_music object */
slouken@409
   539
void FLAC_delete(FLAC_music *music)
slouken@409
   540
{
slouken@382
   541
	if (music) {
slouken@382
   542
		if (music->flac_decoder) {
slouken@382
   543
			flac.FLAC__stream_decoder_finish (music->flac_decoder);
slouken@382
   544
			flac.FLAC__stream_decoder_delete (music->flac_decoder);
slouken@382
   545
		}
slouken@382
   546
slouken@382
   547
		if (music->flac_data.data) {
slouken@382
   548
			free (music->flac_data.data);
slouken@382
   549
		}
slouken@382
   550
slouken@382
   551
		if (music->flac_data.overflow) {
slouken@382
   552
			free (music->flac_data.overflow);
slouken@382
   553
		}
slouken@382
   554
slouken@382
   555
		if (music->cvt.buf) {
slouken@382
   556
			free (music->cvt.buf);
slouken@382
   557
		}
slouken@382
   558
slouken@382
   559
		free (music);
slouken@382
   560
	}
slouken@382
   561
}
slouken@382
   562
slouken@382
   563
/* Jump (seek) to a given position (time is in seconds) */
slouken@409
   564
void FLAC_jump_to_time(FLAC_music *music, double time)
slouken@409
   565
{
slouken@382
   566
	if (music) {
slouken@382
   567
		if (music->flac_decoder) {
slouken@382
   568
			double seek_sample = music->flac_data.sample_rate * time;
slouken@382
   569
slouken@382
   570
			// clear data if it has data
slouken@382
   571
			if (music->flac_data.data) {
slouken@382
   572
				free (music->flac_data.data);
slouken@382
   573
				music->flac_data.data = NULL;
slouken@382
   574
			}
slouken@382
   575
slouken@382
   576
			// clear overflow if it has data
slouken@382
   577
			if (music->flac_data.overflow) {
slouken@382
   578
				free (music->flac_data.overflow);
slouken@382
   579
				music->flac_data.overflow = NULL;
slouken@382
   580
			}
slouken@382
   581
slouken@382
   582
			if (!flac.FLAC__stream_decoder_seek_absolute (music->flac_decoder,
slouken@382
   583
												(FLAC__uint64)seek_sample)) {
slouken@382
   584
				if (flac.FLAC__stream_decoder_get_state (music->flac_decoder)
slouken@382
   585
										== FLAC__STREAM_DECODER_SEEK_ERROR) {
slouken@382
   586
					flac.FLAC__stream_decoder_flush (music->flac_decoder);
slouken@382
   587
				}
slouken@382
   588
slouken@382
   589
				SDL_SetError
slouken@382
   590
					("Seeking of FLAC stream failed: libFLAC seek failed.");
slouken@382
   591
			}
slouken@382
   592
		}
slouken@382
   593
		else {
slouken@382
   594
			SDL_SetError
slouken@382
   595
				("Seeking of FLAC stream failed: FLAC decoder was NULL.");
slouken@382
   596
		}
slouken@382
   597
	}
slouken@382
   598
	else {
slouken@382
   599
		SDL_SetError ("Seeking of FLAC stream failed: music was NULL.");
slouken@382
   600
	}
slouken@382
   601
}
slouken@382
   602
slouken@382
   603
#endif /* FLAC_MUSIC */