Skip to content

Latest commit

 

History

History
329 lines (269 loc) · 10.9 KB

load_flac.c

File metadata and controls

329 lines (269 loc) · 10.9 KB
 
Dec 31, 2011
Dec 31, 2011
2
SDL_mixer: An audio mixer library based on the SDL library
Feb 15, 2013
Feb 15, 2013
3
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
Dec 31, 2011
Dec 31, 2011
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
This is the source needed to decode a FLAC into a waveform.
May 22, 2013
May 22, 2013
22
~ Austen Dicken (admin@cvpcs.org).
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
*/
#ifdef FLAC_MUSIC
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SDL_mutex.h"
#include "SDL_endian.h"
#include "SDL_timer.h"
#include "SDL_mixer.h"
#include "dynamic_flac.h"
#include "load_flac.h"
#include <FLAC/stream_decoder.h>
typedef struct {
May 22, 2013
May 22, 2013
42
43
44
45
46
47
48
SDL_RWops* sdl_src;
SDL_AudioSpec* sdl_spec;
Uint8** sdl_audio_buf;
Uint32* sdl_audio_len;
int sdl_audio_read;
FLAC__uint64 flac_total_samples;
unsigned flac_bps;
49
50
51
} FLAC_SDL_Data;
static FLAC__StreamDecoderReadStatus flac_read_load_cb(
May 22, 2013
May 22, 2013
52
53
54
55
const FLAC__StreamDecoder *decoder,
FLAC__byte buffer[],
size_t *bytes,
void *client_data)
Oct 10, 2009
Oct 10, 2009
56
{
May 22, 2013
May 22, 2013
57
58
59
60
61
62
63
64
65
// make sure there is something to be reading
if (*bytes > 0) {
FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
*bytes = SDL_RWread (data->sdl_src, buffer, sizeof (FLAC__byte),
*bytes);
if (*bytes == 0) { // error or no data was read (EOF)
return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
Jun 1, 2013
Jun 1, 2013
66
} else { // data was read, continue
May 22, 2013
May 22, 2013
67
68
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
}
Jun 1, 2013
Jun 1, 2013
69
} else {
May 22, 2013
May 22, 2013
70
71
return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
}
72
73
74
}
static FLAC__StreamDecoderSeekStatus flac_seek_load_cb(
May 22, 2013
May 22, 2013
75
76
77
const FLAC__StreamDecoder *decoder,
FLAC__uint64 absolute_byte_offset,
void *client_data)
Oct 10, 2009
Oct 10, 2009
78
{
May 22, 2013
May 22, 2013
79
80
81
82
FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
if (SDL_RWseek (data->sdl_src, absolute_byte_offset, RW_SEEK_SET) < 0) {
return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
Jun 1, 2013
Jun 1, 2013
83
} else {
May 22, 2013
May 22, 2013
84
85
return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
}
86
87
88
}
static FLAC__StreamDecoderTellStatus flac_tell_load_cb(
May 22, 2013
May 22, 2013
89
90
91
const FLAC__StreamDecoder *decoder,
FLAC__uint64 *absolute_byte_offset,
void *client_data)
Oct 10, 2009
Oct 10, 2009
92
{
May 22, 2013
May 22, 2013
93
FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
Jun 1, 2013
Jun 1, 2013
95
Sint64 pos = SDL_RWtell (data->sdl_src);
May 22, 2013
May 22, 2013
97
98
if (pos < 0) {
return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
Jun 1, 2013
Jun 1, 2013
99
} else {
May 22, 2013
May 22, 2013
100
101
102
*absolute_byte_offset = (FLAC__uint64)pos;
return FLAC__STREAM_DECODER_TELL_STATUS_OK;
}
103
104
105
}
static FLAC__StreamDecoderLengthStatus flac_length_load_cb(
May 22, 2013
May 22, 2013
106
107
108
const FLAC__StreamDecoder *decoder,
FLAC__uint64 *stream_length,
void *client_data)
Oct 10, 2009
Oct 10, 2009
109
{
May 22, 2013
May 22, 2013
110
111
FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
Jun 1, 2013
Jun 1, 2013
112
113
Sint64 pos = SDL_RWtell (data->sdl_src);
Sint64 length = SDL_RWseek (data->sdl_src, 0, RW_SEEK_END);
May 22, 2013
May 22, 2013
114
115
116
117
118
if (SDL_RWseek (data->sdl_src, pos, RW_SEEK_SET) != pos || length < 0) {
/* there was an error attempting to return the stream to the original
* position, or the length was invalid. */
return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
Jun 1, 2013
Jun 1, 2013
119
} else {
May 22, 2013
May 22, 2013
120
121
122
*stream_length = (FLAC__uint64)length;
return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
}
123
124
125
}
static FLAC__bool flac_eof_load_cb(const FLAC__StreamDecoder *decoder,
May 22, 2013
May 22, 2013
126
void *client_data)
Oct 10, 2009
Oct 10, 2009
127
{
May 22, 2013
May 22, 2013
128
129
FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
Jun 1, 2013
Jun 1, 2013
130
131
Sint64 pos = SDL_RWtell (data->sdl_src);
Sint64 end = SDL_RWseek (data->sdl_src, 0, RW_SEEK_END);
May 22, 2013
May 22, 2013
132
133
134
135
136
// was the original position equal to the end (a.k.a. the seek didn't move)?
if (pos == end) {
// must be EOF
return true;
Jun 1, 2013
Jun 1, 2013
137
} else {
May 22, 2013
May 22, 2013
138
139
140
141
// not EOF, return to the original position
SDL_RWseek (data->sdl_src, pos, RW_SEEK_SET);
return false;
}
142
143
144
}
static FLAC__StreamDecoderWriteStatus flac_write_load_cb(
May 22, 2013
May 22, 2013
145
146
147
148
const FLAC__StreamDecoder *decoder,
const FLAC__Frame *frame,
const FLAC__int32 *const buffer[],
void *client_data)
Oct 10, 2009
Oct 10, 2009
149
{
May 22, 2013
May 22, 2013
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
size_t i;
Uint8 *buf;
if (data->flac_total_samples == 0) {
SDL_SetError ("Given FLAC file does not specify its sample count.");
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
if (data->sdl_spec->channels != 2 || data->flac_bps != 16) {
SDL_SetError ("Current FLAC support is only for 16 bit Stereo files.");
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
// check if it is the first audio frame so we can initialize the output
// buffer
if (frame->header.number.sample_number == 0) {
*(data->sdl_audio_len) = data->sdl_spec->size;
data->sdl_audio_read = 0;
*(data->sdl_audio_buf) = SDL_malloc (*(data->sdl_audio_len));
if (*(data->sdl_audio_buf) == NULL) {
SDL_SetError
("Unable to allocate memory to store the FLAC stream.");
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
}
buf = *(data->sdl_audio_buf);
for (i = 0; i < frame->header.blocksize; i++) {
FLAC__int16 i16;
FLAC__uint16 ui16;
i16 = (FLAC__int16)buffer[0][i];
ui16 = (FLAC__uint16)i16;
*(buf + (data->sdl_audio_read++)) = (char)(ui16);
*(buf + (data->sdl_audio_read++)) = (char)(ui16 >> 8);
i16 = (FLAC__int16)buffer[1][i];
ui16 = (FLAC__uint16)i16;
*(buf + (data->sdl_audio_read++)) = (char)(ui16);
*(buf + (data->sdl_audio_read++)) = (char)(ui16 >> 8);
}
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
198
199
200
}
static void flac_metadata_load_cb(
May 22, 2013
May 22, 2013
201
202
203
const FLAC__StreamDecoder *decoder,
const FLAC__StreamMetadata *metadata,
void *client_data)
Nov 8, 2009
Nov 8, 2009
204
{
May 22, 2013
May 22, 2013
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
FLAC_SDL_Data *data = (FLAC_SDL_Data *)client_data;
FLAC__uint64 total_samples;
unsigned bps;
if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
// save the metadata right now for use later on
*(data->sdl_audio_buf) = NULL;
*(data->sdl_audio_len) = 0;
memset (data->sdl_spec, '\0', sizeof (SDL_AudioSpec));
data->sdl_spec->format = AUDIO_S16;
data->sdl_spec->freq = (int)(metadata->data.stream_info.sample_rate);
data->sdl_spec->channels = (Uint8)(metadata->data.stream_info.channels);
data->sdl_spec->samples = 8192; /* buffer size */
total_samples = metadata->data.stream_info.total_samples;
bps = metadata->data.stream_info.bits_per_sample;
Jun 1, 2013
Jun 1, 2013
223
data->sdl_spec->size = (Uint32)(total_samples * data->sdl_spec->channels * (bps / 8));
May 22, 2013
May 22, 2013
224
225
226
data->flac_total_samples = total_samples;
data->flac_bps = bps;
}
227
228
229
}
static void flac_error_load_cb(
May 22, 2013
May 22, 2013
230
231
232
const FLAC__StreamDecoder *decoder,
FLAC__StreamDecoderErrorStatus status,
void *client_data)
Nov 8, 2009
Nov 8, 2009
233
{
May 22, 2013
May 22, 2013
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// print an SDL error based on the error status
switch (status) {
case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC:
SDL_SetError ("Error processing the FLAC file [LOST_SYNC].");
break;
case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER:
SDL_SetError ("Error processing the FLAC file [BAD_HEADER].");
break;
case FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH:
SDL_SetError ("Error processing the FLAC file [CRC_MISMATCH].");
break;
case FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM:
SDL_SetError ("Error processing the FLAC file [UNPARSEABLE].");
break;
default:
SDL_SetError ("Error processing the FLAC file [UNKNOWN].");
break;
}
252
253
254
255
}
/* don't call this directly; use Mix_LoadWAV_RW() for now. */
SDL_AudioSpec *Mix_LoadFLAC_RW (SDL_RWops *src, int freesrc,
Nov 8, 2009
Nov 8, 2009
256
257
SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
{
May 22, 2013
May 22, 2013
258
259
260
261
262
FLAC__StreamDecoder *decoder = 0;
FLAC__StreamDecoderInitStatus init_status;
int was_error = 1;
int was_init = 0;
Uint32 samplesize;
May 22, 2013
May 22, 2013
264
265
266
// create the client data passing information
FLAC_SDL_Data* client_data;
client_data = (FLAC_SDL_Data *)SDL_malloc (sizeof (FLAC_SDL_Data));
May 22, 2013
May 22, 2013
268
269
if ((!src) || (!audio_buf) || (!audio_len)) /* sanity checks. */
goto done;
May 22, 2013
May 22, 2013
271
272
if (!Mix_Init(MIX_INIT_FLAC))
goto done;
May 22, 2013
May 22, 2013
274
275
276
277
if ((decoder = flac.FLAC__stream_decoder_new ()) == NULL) {
SDL_SetError ("Unable to allocate FLAC decoder.");
goto done;
}
May 22, 2013
May 22, 2013
279
280
281
282
283
284
init_status = flac.FLAC__stream_decoder_init_stream (decoder,
flac_read_load_cb, flac_seek_load_cb,
flac_tell_load_cb, flac_length_load_cb,
flac_eof_load_cb, flac_write_load_cb,
flac_metadata_load_cb, flac_error_load_cb,
client_data);
May 22, 2013
May 22, 2013
286
287
288
289
if (init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
SDL_SetError ("Unable to initialize FLAC stream decoder.");
goto done;
}
May 22, 2013
May 22, 2013
291
was_init = 1;
May 22, 2013
May 22, 2013
293
294
295
296
client_data->sdl_src = src;
client_data->sdl_spec = spec;
client_data->sdl_audio_buf = audio_buf;
client_data->sdl_audio_len = audio_len;
May 22, 2013
May 22, 2013
298
299
300
301
if (!flac.FLAC__stream_decoder_process_until_end_of_stream (decoder)) {
SDL_SetError ("Unable to process FLAC file.");
goto done;
}
May 22, 2013
May 22, 2013
303
was_error = 0;
May 22, 2013
May 22, 2013
305
306
307
/* Don't return a buffer that isn't a multiple of samplesize */
samplesize = ((spec->format & 0xFF) / 8) * spec->channels;
*audio_len &= ~(samplesize - 1);
308
309
done:
May 22, 2013
May 22, 2013
310
311
312
313
314
315
316
317
if (was_init && decoder) {
flac.FLAC__stream_decoder_finish (decoder);
}
if (decoder) {
flac.FLAC__stream_decoder_delete (decoder);
}
Jun 2, 2013
Jun 2, 2013
318
319
if (freesrc && src) {
SDL_RWclose (src);
May 22, 2013
May 22, 2013
320
321
}
Jun 2, 2013
Jun 2, 2013
322
if (was_error) {
May 22, 2013
May 22, 2013
323
spec = NULL;
Jun 2, 2013
Jun 2, 2013
324
}
325
326
327
328
329
return spec;
}
#endif // FLAC_MUSIC