load_flac.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 19 Oct 2009 01:03:40 +0000
changeset 462 028b317d01d2
parent 436 8ec227cabdd4
child 470 5cebd6c5be2d
permissions -rw-r--r--
Added the AudioUnit framework for the native MIDI code.
slouken@382
     1
/*
slouken@382
     2
    SDL_mixer:  An audio mixer library based on the SDL library
slouken@386
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@382
     4
slouken@382
     5
    This library is free software; you can redistribute it and/or
slouken@382
     6
    modify it under the terms of the GNU Library General Public
slouken@382
     7
    License as published by the Free Software Foundation; either
slouken@382
     8
    version 2 of the License, or (at your option) any later version.
slouken@382
     9
slouken@382
    10
    This library is distributed in the hope that it will be useful,
slouken@382
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@382
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@382
    13
    Library General Public License for more details.
slouken@382
    14
slouken@382
    15
    You should have received a copy of the GNU Library General Public
slouken@382
    16
    License along with this library; if not, write to the Free
slouken@382
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@382
    18
slouken@382
    19
    This is the source needed to decode a FLAC into a waveform.
slouken@382
    20
    										~ Austen Dicken (admin@cvpcs.org).
slouken@382
    21
*/
slouken@382
    22
slouken@382
    23
#ifdef FLAC_MUSIC
slouken@382
    24
slouken@382
    25
#include <stdio.h>
slouken@382
    26
#include <stdlib.h>
slouken@382
    27
#include <string.h>
slouken@382
    28
slouken@382
    29
#include "SDL_mutex.h"
slouken@382
    30
#include "SDL_endian.h"
slouken@382
    31
#include "SDL_timer.h"
slouken@382
    32
slouken@382
    33
#include "SDL_mixer.h"
slouken@382
    34
#include "dynamic_flac.h"
slouken@382
    35
#include "load_flac.h"
slouken@382
    36
slouken@382
    37
#include <FLAC/stream_decoder.h>
slouken@382
    38
slouken@382
    39
typedef struct {
slouken@382
    40
	SDL_RWops* sdl_src;
slouken@382
    41
	SDL_AudioSpec* sdl_spec;
slouken@382
    42
	Uint8** sdl_audio_buf;
slouken@382
    43
	Uint32* sdl_audio_len;
slouken@382
    44
	int sdl_audio_read;
slouken@382
    45
	FLAC__uint64 flac_total_samples;
slouken@382
    46
	unsigned flac_bps;
slouken@382
    47
} FLAC_SDL_Data;
slouken@382
    48
slouken@382
    49
static FLAC__StreamDecoderReadStatus flac_read_load_cb(
slouken@382
    50
									const FLAC__StreamDecoder *decoder,
slouken@382
    51
									FLAC__byte buffer[],
slouken@382
    52
									size_t *bytes,
slouken@436
    53
									void *client_data)
slouken@436
    54
{
slouken@382
    55
	// make sure there is something to be reading
slouken@382
    56
	if (*bytes > 0) {
slouken@382
    57
		FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
slouken@382
    58
slouken@382
    59
		*bytes = SDL_RWread (data->sdl_src, buffer, sizeof (FLAC__byte),
slouken@382
    60
								*bytes);
slouken@382
    61
slouken@382
    62
		if(*bytes < 0) { // error in read
slouken@382
    63
			return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
slouken@382
    64
		}
slouken@382
    65
		else if(*bytes == 0) { // no data was read (EOF)
slouken@382
    66
			return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
slouken@382
    67
		}
slouken@382
    68
		else { // data was read, continue
slouken@382
    69
			return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
slouken@382
    70
		}
slouken@382
    71
	}
slouken@382
    72
	else {
slouken@382
    73
		return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
slouken@382
    74
	}
slouken@382
    75
}
slouken@382
    76
slouken@382
    77
static FLAC__StreamDecoderSeekStatus flac_seek_load_cb(
slouken@382
    78
									const FLAC__StreamDecoder *decoder,
slouken@382
    79
									FLAC__uint64 absolute_byte_offset,
slouken@436
    80
									void *client_data)
slouken@436
    81
{
slouken@382
    82
	FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
slouken@382
    83
slouken@382
    84
	if (SDL_RWseek (data->sdl_src, absolute_byte_offset, SEEK_SET) < 0) {
slouken@382
    85
		return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
slouken@382
    86
	}
slouken@382
    87
	else {
slouken@382
    88
		return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
slouken@382
    89
	}
slouken@382
    90
}
slouken@382
    91
slouken@382
    92
static FLAC__StreamDecoderTellStatus flac_tell_load_cb(
slouken@382
    93
									const FLAC__StreamDecoder *decoder,
slouken@382
    94
									FLAC__uint64 *absolute_byte_offset,
slouken@436
    95
									void *client_data)
slouken@436
    96
{
slouken@382
    97
	FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
slouken@382
    98
slouken@382
    99
	int pos = SDL_RWtell (data->sdl_src);
slouken@382
   100
slouken@382
   101
	if (pos < 0) {
slouken@382
   102
		return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
slouken@382
   103
	}
slouken@382
   104
	else {
slouken@382
   105
		*absolute_byte_offset = (FLAC__uint64)pos;
slouken@382
   106
		return FLAC__STREAM_DECODER_TELL_STATUS_OK;
slouken@382
   107
	}
slouken@382
   108
}
slouken@382
   109
slouken@382
   110
static FLAC__StreamDecoderLengthStatus flac_length_load_cb(
slouken@382
   111
									const FLAC__StreamDecoder *decoder,
slouken@382
   112
									FLAC__uint64 *stream_length,
slouken@436
   113
									void *client_data)
slouken@436
   114
{
slouken@382
   115
	FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
slouken@382
   116
slouken@382
   117
	int pos = SDL_RWtell (data->sdl_src);
slouken@382
   118
	int length = SDL_RWseek (data->sdl_src, 0, SEEK_END);
slouken@382
   119
slouken@382
   120
	if (SDL_RWseek (data->sdl_src, pos, SEEK_SET) != pos || length < 0) {
slouken@382
   121
		/* there was an error attempting to return the stream to the original
slouken@382
   122
		 * position, or the length was invalid. */
slouken@382
   123
		return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
slouken@382
   124
	}
slouken@382
   125
	else {
slouken@382
   126
		*stream_length = (FLAC__uint64)length;
slouken@382
   127
		return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
slouken@382
   128
	}
slouken@382
   129
}
slouken@382
   130
slouken@382
   131
static FLAC__bool flac_eof_load_cb(const FLAC__StreamDecoder *decoder,
slouken@436
   132
									void *client_data)
slouken@436
   133
{
slouken@382
   134
	FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
slouken@382
   135
slouken@382
   136
	int pos = SDL_RWtell (data->sdl_src);
slouken@382
   137
	int end = SDL_RWseek (data->sdl_src, 0, SEEK_END);
slouken@382
   138
slouken@382
   139
	// was the original position equal to the end (a.k.a. the seek didn't move)?
slouken@382
   140
	if (pos == end) {
slouken@382
   141
		// must be EOF
slouken@382
   142
		return true;
slouken@382
   143
	}
slouken@382
   144
	else {
slouken@382
   145
		// not EOF, return to the original position
slouken@382
   146
		SDL_RWseek (data->sdl_src, pos, SEEK_SET);
slouken@382
   147
slouken@382
   148
		return false;
slouken@382
   149
	}
slouken@382
   150
}
slouken@382
   151
slouken@382
   152
static FLAC__StreamDecoderWriteStatus flac_write_load_cb(
slouken@382
   153
									const FLAC__StreamDecoder *decoder,
slouken@382
   154
									const FLAC__Frame *frame,
slouken@382
   155
									const FLAC__int32 *const buffer[],
slouken@436
   156
									void *client_data)
slouken@436
   157
{
slouken@382
   158
	FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
slouken@382
   159
	size_t i;
slouken@436
   160
	Uint8 *buf;
slouken@382
   161
slouken@382
   162
	if (data->flac_total_samples == 0) {
slouken@382
   163
		SDL_SetError ("Given FLAC file does not specify its sample count.");
slouken@382
   164
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
slouken@382
   165
	}
slouken@382
   166
slouken@382
   167
	if (data->sdl_spec->channels != 2 || data->flac_bps != 16) {
slouken@382
   168
		SDL_SetError ("Current FLAC support is only for 16 bit Stereo files.");
slouken@382
   169
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
slouken@382
   170
	}
slouken@382
   171
slouken@382
   172
	// check if it is the first audio frame so we can initialize the output
slouken@382
   173
	// buffer
slouken@382
   174
	if (frame->header.number.sample_number == 0) {
slouken@382
   175
		*(data->sdl_audio_len) = data->sdl_spec->size;
slouken@382
   176
		data->sdl_audio_read = 0;
slouken@382
   177
    	*(data->sdl_audio_buf) = malloc (*(data->sdl_audio_len));
slouken@382
   178
slouken@382
   179
    	if (*(data->sdl_audio_buf) == NULL) {
slouken@382
   180
    		SDL_SetError
slouken@382
   181
					("Unable to allocate memory to store the FLAC stream.");
slouken@382
   182
    		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
slouken@382
   183
    	}
slouken@382
   184
	}
slouken@382
   185
slouken@436
   186
	buf = *(data->sdl_audio_buf);
slouken@382
   187
slouken@382
   188
	for (i = 0; i < frame->header.blocksize; i++) {
slouken@382
   189
		FLAC__int16 i16;
slouken@382
   190
		FLAC__uint16 ui16;
slouken@382
   191
slouken@382
   192
		i16 = (FLAC__int16)buffer[0][i];
slouken@382
   193
		ui16 = (FLAC__uint16)i16;
slouken@382
   194
slouken@382
   195
		*(buf + (data->sdl_audio_read++)) = (char)(ui16);
slouken@382
   196
		*(buf + (data->sdl_audio_read++)) = (char)(ui16 >> 8);
slouken@382
   197
slouken@382
   198
		i16 = (FLAC__int16)buffer[1][i];
slouken@382
   199
		ui16 = (FLAC__uint16)i16;
slouken@382
   200
slouken@382
   201
		*(buf + (data->sdl_audio_read++)) = (char)(ui16);
slouken@382
   202
		*(buf + (data->sdl_audio_read++)) = (char)(ui16 >> 8);
slouken@382
   203
	}
slouken@382
   204
slouken@382
   205
	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
slouken@382
   206
}
slouken@382
   207
slouken@382
   208
static void flac_metadata_load_cb(
slouken@382
   209
					const FLAC__StreamDecoder *decoder,
slouken@382
   210
					const FLAC__StreamMetadata *metadata,
slouken@382
   211
					void *client_data) {
slouken@382
   212
	FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
slouken@382
   213
	FLAC__uint64 total_samples;
slouken@382
   214
	unsigned bps;
slouken@382
   215
slouken@382
   216
	if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
slouken@382
   217
		// save the metadata right now for use later on
slouken@382
   218
		*(data->sdl_audio_buf) = NULL;
slouken@382
   219
		*(data->sdl_audio_len) = 0;
slouken@382
   220
		memset (data->sdl_spec, '\0', sizeof (SDL_AudioSpec));
slouken@382
   221
slouken@382
   222
		data->sdl_spec->format = AUDIO_S16;
slouken@382
   223
		data->sdl_spec->freq = (int)(metadata->data.stream_info.sample_rate);
slouken@382
   224
		data->sdl_spec->channels = (Uint8)(metadata->data.stream_info.channels);
slouken@382
   225
		data->sdl_spec->samples = 8192; /* buffer size */
slouken@382
   226
slouken@382
   227
		total_samples = metadata->data.stream_info.total_samples;
slouken@382
   228
		bps = metadata->data.stream_info.bits_per_sample;
slouken@382
   229
slouken@382
   230
		data->sdl_spec->size = total_samples * data->sdl_spec->channels *
slouken@382
   231
								(bps / 8);
slouken@382
   232
		data->flac_total_samples = total_samples;
slouken@382
   233
		data->flac_bps = bps;
slouken@382
   234
	}
slouken@382
   235
}
slouken@382
   236
slouken@382
   237
static void flac_error_load_cb(
slouken@382
   238
				const FLAC__StreamDecoder *decoder,
slouken@382
   239
				FLAC__StreamDecoderErrorStatus status,
slouken@382
   240
				void *client_data) {
slouken@382
   241
	// print an SDL error based on the error status
slouken@382
   242
	switch (status) {
slouken@382
   243
		case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC:
slouken@382
   244
			SDL_SetError ("Error processing the FLAC file [LOST_SYNC].");
slouken@382
   245
		break;
slouken@382
   246
		case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER:
slouken@382
   247
			SDL_SetError ("Error processing the FLAC file [BAD_HEADER].");
slouken@382
   248
		break;
slouken@382
   249
		case FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH:
slouken@382
   250
			SDL_SetError ("Error processing the FLAC file [CRC_MISMATCH].");
slouken@382
   251
		break;
slouken@382
   252
		case FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM:
slouken@382
   253
			SDL_SetError ("Error processing the FLAC file [UNPARSEABLE].");
slouken@382
   254
		break;
slouken@382
   255
		default:
slouken@382
   256
			SDL_SetError ("Error processing the FLAC file [UNKNOWN].");
slouken@382
   257
		break;
slouken@382
   258
	}
slouken@382
   259
}
slouken@382
   260
slouken@382
   261
/* don't call this directly; use Mix_LoadWAV_RW() for now. */
slouken@382
   262
SDL_AudioSpec *Mix_LoadFLAC_RW (SDL_RWops *src, int freesrc,
slouken@382
   263
        SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len) {
slouken@382
   264
	FLAC__StreamDecoder *decoder = 0;
slouken@382
   265
	FLAC__StreamDecoderInitStatus init_status;
slouken@382
   266
	int was_error = 1;
slouken@382
   267
	int was_init = 0;
slouken@382
   268
	Uint32 samplesize;
slouken@382
   269
slouken@382
   270
	// create the client data passing information
slouken@382
   271
	FLAC_SDL_Data* client_data;
slouken@382
   272
	client_data = (FLAC_SDL_Data *)malloc (sizeof (FLAC_SDL_Data));
slouken@382
   273
slouken@382
   274
    if ((!src) || (!audio_buf) || (!audio_len))   /* sanity checks. */
slouken@382
   275
        goto done;
slouken@382
   276
slouken@382
   277
    if (Mix_InitFLAC() < 0)
slouken@382
   278
        goto done;
slouken@382
   279
slouken@382
   280
	if ((decoder = flac.FLAC__stream_decoder_new ()) == NULL) {
slouken@382
   281
		SDL_SetError ("Unable to allocate FLAC decoder.");
slouken@382
   282
		goto done;
slouken@382
   283
	}
slouken@382
   284
slouken@382
   285
	init_status = flac.FLAC__stream_decoder_init_stream (decoder,
slouken@382
   286
								flac_read_load_cb, flac_seek_load_cb,
slouken@382
   287
								flac_tell_load_cb, flac_length_load_cb,
slouken@382
   288
								flac_eof_load_cb, flac_write_load_cb,
slouken@382
   289
								flac_metadata_load_cb, flac_error_load_cb,
slouken@382
   290
								client_data);
slouken@382
   291
slouken@382
   292
	if (init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
slouken@382
   293
		SDL_SetError ("Unable to initialize FLAC stream decoder.");
slouken@382
   294
		goto done;
slouken@382
   295
	}
slouken@382
   296
slouken@382
   297
	was_init = 1;
slouken@382
   298
slouken@382
   299
	client_data->sdl_src = src;
slouken@382
   300
	client_data->sdl_spec = spec;
slouken@382
   301
	client_data->sdl_audio_buf = audio_buf;
slouken@382
   302
	client_data->sdl_audio_len = audio_len;
slouken@382
   303
slouken@382
   304
	if (!flac.FLAC__stream_decoder_process_until_end_of_stream (decoder)) {
slouken@382
   305
		SDL_SetError ("Unable to process FLAC file.");
slouken@382
   306
		goto done;
slouken@382
   307
	}
slouken@382
   308
slouken@382
   309
	was_error = 0;
slouken@382
   310
slouken@382
   311
    /* Don't return a buffer that isn't a multiple of samplesize */
slouken@382
   312
    samplesize = ((spec->format & 0xFF) / 8) * spec->channels;
slouken@382
   313
    *audio_len &= ~(samplesize - 1);
slouken@382
   314
slouken@382
   315
done:
slouken@382
   316
	if(was_init && decoder) {
slouken@382
   317
		flac.FLAC__stream_decoder_finish (decoder);
slouken@382
   318
	}
slouken@382
   319
slouken@382
   320
	if(decoder) {
slouken@382
   321
		flac.FLAC__stream_decoder_delete (decoder);
slouken@382
   322
	}
slouken@382
   323
slouken@382
   324
    if (src) {
slouken@382
   325
        if (freesrc)
slouken@382
   326
            SDL_RWclose (src);
slouken@382
   327
        else
slouken@382
   328
            SDL_RWseek (src, 0, SEEK_SET);
slouken@382
   329
    }
slouken@382
   330
slouken@382
   331
    if (was_error)
slouken@382
   332
        spec = NULL;
slouken@382
   333
slouken@382
   334
    Mix_QuitFLAC ();
slouken@382
   335
slouken@382
   336
    return spec;
slouken@382
   337
}
slouken@382
   338
slouken@382
   339
#endif // FLAC_MUSIC