Skip to content

Latest commit

 

History

History
807 lines (707 loc) · 28.1 KB

music_flac.c

File metadata and controls

807 lines (707 loc) · 28.1 KB
 
Dec 31, 2011
Dec 31, 2011
2
SDL_mixer: An audio mixer library based on the SDL library
Jan 2, 2017
Jan 2, 2017
3
Copyright (C) 1997-2017 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 file is used to support SDL_LoadMUS playback of FLAC files.
May 22, 2013
May 22, 2013
22
~ Austen Dicken (admin@cvpcs.org)
23
24
*/
Oct 17, 2017
Oct 17, 2017
25
#ifdef MUSIC_FLAC
Oct 17, 2017
Oct 17, 2017
27
#include "SDL_loadso.h"
28
29
30
#include "music_flac.h"
Oct 17, 2017
Oct 17, 2017
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <FLAC/stream_decoder.h>
typedef struct {
int loaded;
void *handle;
FLAC__StreamDecoder *(*FLAC__stream_decoder_new)();
void (*FLAC__stream_decoder_delete)(FLAC__StreamDecoder *decoder);
FLAC__StreamDecoderInitStatus (*FLAC__stream_decoder_init_stream)(
FLAC__StreamDecoder *decoder,
FLAC__StreamDecoderReadCallback read_callback,
FLAC__StreamDecoderSeekCallback seek_callback,
FLAC__StreamDecoderTellCallback tell_callback,
FLAC__StreamDecoderLengthCallback length_callback,
FLAC__StreamDecoderEofCallback eof_callback,
FLAC__StreamDecoderWriteCallback write_callback,
FLAC__StreamDecoderMetadataCallback metadata_callback,
FLAC__StreamDecoderErrorCallback error_callback,
void *client_data);
FLAC__bool (*FLAC__stream_decoder_finish)(FLAC__StreamDecoder *decoder);
FLAC__bool (*FLAC__stream_decoder_flush)(FLAC__StreamDecoder *decoder);
FLAC__bool (*FLAC__stream_decoder_process_single)(
FLAC__StreamDecoder *decoder);
FLAC__bool (*FLAC__stream_decoder_process_until_end_of_metadata)(
FLAC__StreamDecoder *decoder);
FLAC__bool (*FLAC__stream_decoder_process_until_end_of_stream)(
FLAC__StreamDecoder *decoder);
FLAC__bool (*FLAC__stream_decoder_seek_absolute)(
FLAC__StreamDecoder *decoder,
FLAC__uint64 sample);
FLAC__StreamDecoderState (*FLAC__stream_decoder_get_state)(
const FLAC__StreamDecoder *decoder);
} flac_loader;
static flac_loader flac = {
0, NULL
};
#ifdef FLAC_DYNAMIC
Oct 18, 2017
Oct 18, 2017
71
static int FLAC_Load(void)
Oct 17, 2017
Oct 17, 2017
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
{
if (flac.loaded == 0) {
flac.handle = SDL_LoadObject(FLAC_DYNAMIC);
if (flac.handle == NULL) {
return -1;
}
flac.FLAC__stream_decoder_new =
(FLAC__StreamDecoder *(*)())
SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_new");
if (flac.FLAC__stream_decoder_new == NULL) {
SDL_UnloadObject(flac.handle);
return -1;
}
flac.FLAC__stream_decoder_delete =
(void (*)(FLAC__StreamDecoder *))
SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_delete");
if (flac.FLAC__stream_decoder_delete == NULL) {
SDL_UnloadObject(flac.handle);
return -1;
}
flac.FLAC__stream_decoder_init_stream =
(FLAC__StreamDecoderInitStatus (*)(
FLAC__StreamDecoder *,
FLAC__StreamDecoderReadCallback,
FLAC__StreamDecoderSeekCallback,
FLAC__StreamDecoderTellCallback,
FLAC__StreamDecoderLengthCallback,
FLAC__StreamDecoderEofCallback,
FLAC__StreamDecoderWriteCallback,
FLAC__StreamDecoderMetadataCallback,
FLAC__StreamDecoderErrorCallback,
void *))
SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_init_stream");
if (flac.FLAC__stream_decoder_init_stream == NULL) {
SDL_UnloadObject(flac.handle);
return -1;
}
flac.FLAC__stream_decoder_finish =
(FLAC__bool (*)(FLAC__StreamDecoder *))
SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_finish");
if (flac.FLAC__stream_decoder_finish == NULL) {
SDL_UnloadObject(flac.handle);
return -1;
}
flac.FLAC__stream_decoder_flush =
(FLAC__bool (*)(FLAC__StreamDecoder *))
SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_flush");
if (flac.FLAC__stream_decoder_flush == NULL) {
SDL_UnloadObject(flac.handle);
return -1;
}
flac.FLAC__stream_decoder_process_single =
(FLAC__bool (*)(FLAC__StreamDecoder *))
SDL_LoadFunction(flac.handle,
"FLAC__stream_decoder_process_single");
if (flac.FLAC__stream_decoder_process_single == NULL) {
SDL_UnloadObject(flac.handle);
return -1;
}
flac.FLAC__stream_decoder_process_until_end_of_metadata =
(FLAC__bool (*)(FLAC__StreamDecoder *))
SDL_LoadFunction(flac.handle,
"FLAC__stream_decoder_process_until_end_of_metadata");
if (flac.FLAC__stream_decoder_process_until_end_of_metadata == NULL) {
SDL_UnloadObject(flac.handle);
return -1;
}
flac.FLAC__stream_decoder_process_until_end_of_stream =
(FLAC__bool (*)(FLAC__StreamDecoder *))
SDL_LoadFunction(flac.handle,
"FLAC__stream_decoder_process_until_end_of_stream");
if (flac.FLAC__stream_decoder_process_until_end_of_stream == NULL) {
SDL_UnloadObject(flac.handle);
return -1;
}
flac.FLAC__stream_decoder_seek_absolute =
(FLAC__bool (*)(FLAC__StreamDecoder *, FLAC__uint64))
SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_seek_absolute");
if (flac.FLAC__stream_decoder_seek_absolute == NULL) {
SDL_UnloadObject(flac.handle);
return -1;
}
flac.FLAC__stream_decoder_get_state =
(FLAC__StreamDecoderState (*)(const FLAC__StreamDecoder *decoder))
SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_get_state");
if (flac.FLAC__stream_decoder_get_state == NULL) {
SDL_UnloadObject(flac.handle);
return -1;
}
}
++flac.loaded;
Oct 17, 2017
Oct 17, 2017
164
165
166
167
return 0;
}
static void FLAC_Unload(void)
Oct 3, 2009
Oct 3, 2009
168
{
Oct 17, 2017
Oct 17, 2017
169
170
171
172
173
174
175
if (flac.loaded == 0) {
return;
}
if (flac.loaded == 1) {
SDL_UnloadObject(flac.handle);
}
--flac.loaded;
176
177
}
Oct 17, 2017
Oct 17, 2017
178
179
180
#else /* !FLAC_DYNAMIC */
static int FLAC_Load(void)
Oct 3, 2009
Oct 3, 2009
181
{
Oct 17, 2017
Oct 17, 2017
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
if (flac.loaded == 0) {
#ifdef __MACOSX__
extern FLAC__StreamDecoder *FLAC__stream_decoder_new(void) __attribute__((weak_import));
if (FLAC__stream_decoder_new == NULL)
{
/* Missing weakly linked framework */
Mix_SetError("Missing FLAC.framework");
return -1;
}
#endif // __MACOSX__
flac.FLAC__stream_decoder_new = FLAC__stream_decoder_new;
flac.FLAC__stream_decoder_delete = FLAC__stream_decoder_delete;
flac.FLAC__stream_decoder_init_stream =
FLAC__stream_decoder_init_stream;
flac.FLAC__stream_decoder_finish = FLAC__stream_decoder_finish;
flac.FLAC__stream_decoder_flush = FLAC__stream_decoder_flush;
flac.FLAC__stream_decoder_process_single =
FLAC__stream_decoder_process_single;
flac.FLAC__stream_decoder_process_until_end_of_metadata =
FLAC__stream_decoder_process_until_end_of_metadata;
flac.FLAC__stream_decoder_process_until_end_of_stream =
FLAC__stream_decoder_process_until_end_of_stream;
flac.FLAC__stream_decoder_seek_absolute =
FLAC__stream_decoder_seek_absolute;
flac.FLAC__stream_decoder_get_state =
FLAC__stream_decoder_get_state;
}
++flac.loaded;
return 0;
}
static void FLAC_Unload(void)
{
if (flac.loaded == 0) {
return;
}
if (flac.loaded == 1) {
}
--flac.loaded;
223
224
}
Oct 17, 2017
Oct 17, 2017
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#endif /* FLAC_DYNAMIC */
typedef struct {
FLAC__uint64 sample_size;
unsigned sample_rate;
unsigned channels;
unsigned bits_per_sample;
FLAC__uint64 total_samples;
// the following are used to handle the callback nature of the writer
int max_to_read;
char *data; // pointer to beginning of data array
int data_len; // size of data array
int data_read; // amount of data array used
char *overflow; // pointer to beginning of overflow array
int overflow_len; // size of overflow array
int overflow_read; // amount of overflow array used
} FLAC_Data;
typedef struct {
SDL_bool playing;
int volume;
int section;
FLAC__StreamDecoder *flac_decoder;
FLAC_Data flac_data;
SDL_RWops *src;
int freesrc;
SDL_AudioCVT cvt;
int len_available;
Uint8 *snd_available;
} FLAC_music;
259
static FLAC__StreamDecoderReadStatus flac_read_music_cb(
May 22, 2013
May 22, 2013
260
261
262
263
const FLAC__StreamDecoder *decoder,
FLAC__byte buffer[],
size_t *bytes,
void *client_data)
Oct 3, 2009
Oct 3, 2009
264
{
May 22, 2013
May 22, 2013
265
266
267
268
FLAC_music *data = (FLAC_music*)client_data;
// make sure there is something to be reading
if (*bytes > 0) {
Jun 2, 2013
Jun 2, 2013
269
*bytes = SDL_RWread (data->src, buffer, sizeof (FLAC__byte), *bytes);
May 22, 2013
May 22, 2013
270
Oct 17, 2017
Oct 17, 2017
271
if (*bytes == 0) { // error or no data was read (EOF)
May 22, 2013
May 22, 2013
272
return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
Jun 1, 2013
Jun 1, 2013
273
} else { // data was read, continue
May 22, 2013
May 22, 2013
274
275
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
}
Jun 1, 2013
Jun 1, 2013
276
} else {
May 22, 2013
May 22, 2013
277
278
return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
}
279
280
281
}
static FLAC__StreamDecoderSeekStatus flac_seek_music_cb(
May 22, 2013
May 22, 2013
282
283
284
const FLAC__StreamDecoder *decoder,
FLAC__uint64 absolute_byte_offset,
void *client_data)
Oct 3, 2009
Oct 3, 2009
285
{
May 22, 2013
May 22, 2013
286
287
FLAC_music *data = (FLAC_music*)client_data;
Jun 2, 2013
Jun 2, 2013
288
if (SDL_RWseek (data->src, absolute_byte_offset, RW_SEEK_SET) < 0) {
May 22, 2013
May 22, 2013
289
return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
Jun 1, 2013
Jun 1, 2013
290
} else {
May 22, 2013
May 22, 2013
291
292
return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
}
293
294
295
}
static FLAC__StreamDecoderTellStatus flac_tell_music_cb(
May 22, 2013
May 22, 2013
296
297
const FLAC__StreamDecoder *decoder,
FLAC__uint64 *absolute_byte_offset,
Oct 17, 2017
Oct 17, 2017
298
void *client_data)
Oct 3, 2009
Oct 3, 2009
299
{
May 22, 2013
May 22, 2013
300
FLAC_music *data = (FLAC_music*)client_data;
Jun 2, 2013
Jun 2, 2013
302
Sint64 pos = SDL_RWtell (data->src);
May 22, 2013
May 22, 2013
304
305
if (pos < 0) {
return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
Jun 1, 2013
Jun 1, 2013
306
} else {
May 22, 2013
May 22, 2013
307
308
309
*absolute_byte_offset = (FLAC__uint64)pos;
return FLAC__STREAM_DECODER_TELL_STATUS_OK;
}
310
311
312
}
static FLAC__StreamDecoderLengthStatus flac_length_music_cb (
May 22, 2013
May 22, 2013
313
314
315
const FLAC__StreamDecoder *decoder,
FLAC__uint64 *stream_length,
void *client_data)
Oct 3, 2009
Oct 3, 2009
316
{
May 22, 2013
May 22, 2013
317
318
FLAC_music *data = (FLAC_music*)client_data;
Jun 2, 2013
Jun 2, 2013
319
320
Sint64 pos = SDL_RWtell (data->src);
Sint64 length = SDL_RWseek (data->src, 0, RW_SEEK_END);
May 22, 2013
May 22, 2013
321
Jun 2, 2013
Jun 2, 2013
322
if (SDL_RWseek (data->src, pos, RW_SEEK_SET) != pos || length < 0) {
May 22, 2013
May 22, 2013
323
324
325
/* 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
326
} else {
May 22, 2013
May 22, 2013
327
328
329
*stream_length = (FLAC__uint64)length;
return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
}
330
331
332
}
static FLAC__bool flac_eof_music_cb(
May 22, 2013
May 22, 2013
333
const FLAC__StreamDecoder *decoder,
Oct 17, 2017
Oct 17, 2017
334
void *client_data)
Oct 3, 2009
Oct 3, 2009
335
{
May 22, 2013
May 22, 2013
336
337
FLAC_music *data = (FLAC_music*)client_data;
Jun 2, 2013
Jun 2, 2013
338
339
Sint64 pos = SDL_RWtell (data->src);
Sint64 end = SDL_RWseek (data->src, 0, RW_SEEK_END);
May 22, 2013
May 22, 2013
340
341
342
343
344
// 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
345
} else {
May 22, 2013
May 22, 2013
346
// not EOF, return to the original position
Jun 2, 2013
Jun 2, 2013
347
SDL_RWseek (data->src, pos, RW_SEEK_SET);
May 22, 2013
May 22, 2013
348
349
return false;
}
350
351
352
}
static FLAC__StreamDecoderWriteStatus flac_write_music_cb(
May 22, 2013
May 22, 2013
353
354
355
356
const FLAC__StreamDecoder *decoder,
const FLAC__Frame *frame,
const FLAC__int32 *const buffer[],
void *client_data)
Oct 3, 2009
Oct 3, 2009
357
{
May 22, 2013
May 22, 2013
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
FLAC_music *data = (FLAC_music *)client_data;
size_t i;
if (data->flac_data.total_samples == 0) {
SDL_SetError ("Given FLAC file does not specify its sample count.");
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
if (data->flac_data.channels != 2 ||
data->flac_data.bits_per_sample != 16) {
SDL_SetError("Current FLAC support is only for 16 bit Stereo files.");
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
for (i = 0; i < frame->header.blocksize; i++) {
FLAC__int16 i16;
FLAC__uint16 ui16;
// make sure we still have at least two bytes that can be read (one for
// each channel)
if (data->flac_data.max_to_read >= 4) {
// does the data block exist?
if (!data->flac_data.data) {
data->flac_data.data_len = data->flac_data.max_to_read;
data->flac_data.data_read = 0;
// create it
data->flac_data.data =
(char *)SDL_malloc (data->flac_data.data_len);
if (!data->flac_data.data) {
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
}
i16 = (FLAC__int16)buffer[0][i];
ui16 = (FLAC__uint16)i16;
*((data->flac_data.data) + (data->flac_data.data_read++)) =
(char)(ui16);
*((data->flac_data.data) + (data->flac_data.data_read++)) =
(char)(ui16 >> 8);
i16 = (FLAC__int16)buffer[1][i];
ui16 = (FLAC__uint16)i16;
*((data->flac_data.data) + (data->flac_data.data_read++)) =
(char)(ui16);
*((data->flac_data.data) + (data->flac_data.data_read++)) =
(char)(ui16 >> 8);
data->flac_data.max_to_read -= 4;
if (data->flac_data.max_to_read < 4) {
// we need to set this so that the read halts from the
// FLAC_getsome function.
data->flac_data.max_to_read = 0;
}
Oct 17, 2017
Oct 17, 2017
416
} else {
May 22, 2013
May 22, 2013
417
418
// we need to write to the overflow
if (!data->flac_data.overflow) {
Oct 13, 2017
Oct 13, 2017
419
data->flac_data.overflow_len = (int)(4 * (frame->header.blocksize - i));
May 22, 2013
May 22, 2013
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
data->flac_data.overflow_read = 0;
// make it big enough for the rest of the block
data->flac_data.overflow =
(char *)SDL_malloc (data->flac_data.overflow_len);
if (!data->flac_data.overflow) {
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
}
i16 = (FLAC__int16)buffer[0][i];
ui16 = (FLAC__uint16)i16;
*((data->flac_data.overflow) + (data->flac_data.overflow_read++)) =
(char)(ui16);
*((data->flac_data.overflow) + (data->flac_data.overflow_read++)) =
(char)(ui16 >> 8);
i16 = (FLAC__int16)buffer[1][i];
ui16 = (FLAC__uint16)i16;
*((data->flac_data.overflow) + (data->flac_data.overflow_read++)) =
(char)(ui16);
*((data->flac_data.overflow) + (data->flac_data.overflow_read++)) =
(char)(ui16 >> 8);
}
}
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
450
451
452
}
static void flac_metadata_music_cb(
May 22, 2013
May 22, 2013
453
454
455
const FLAC__StreamDecoder *decoder,
const FLAC__StreamMetadata *metadata,
void *client_data)
Oct 3, 2009
Oct 3, 2009
456
{
May 22, 2013
May 22, 2013
457
458
459
460
461
462
463
464
465
466
467
468
FLAC_music *data = (FLAC_music *)client_data;
if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
data->flac_data.sample_rate = metadata->data.stream_info.sample_rate;
data->flac_data.channels = metadata->data.stream_info.channels;
data->flac_data.total_samples =
metadata->data.stream_info.total_samples;
data->flac_data.bits_per_sample =
metadata->data.stream_info.bits_per_sample;
data->flac_data.sample_size = data->flac_data.channels *
((data->flac_data.bits_per_sample) / 8);
}
469
470
471
}
static void flac_error_music_cb(
May 22, 2013
May 22, 2013
472
473
474
const FLAC__StreamDecoder *decoder,
FLAC__StreamDecoderErrorStatus status,
void *client_data)
Oct 3, 2009
Oct 3, 2009
475
{
May 22, 2013
May 22, 2013
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// 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;
}
494
495
496
}
/* Load an FLAC stream from an SDL_RWops object */
Oct 17, 2017
Oct 17, 2017
497
static void *FLAC_CreateFromRW(SDL_RWops *src, int freesrc)
Oct 3, 2009
Oct 3, 2009
498
{
May 22, 2013
May 22, 2013
499
500
501
502
503
504
505
506
FLAC_music *music;
int init_stage = 0;
int was_error = 1;
if (!Mix_Init(MIX_INIT_FLAC)) {
return NULL;
}
Oct 17, 2017
Oct 17, 2017
507
music = (FLAC_music *)SDL_calloc(1, sizeof (*music));
May 22, 2013
May 22, 2013
508
509
if (music) {
/* Initialize the music structure */
Oct 17, 2017
Oct 17, 2017
510
music->volume = MIX_MAX_VOLUME;
May 22, 2013
May 22, 2013
511
music->section = -1;
Jun 2, 2013
Jun 2, 2013
512
513
music->src = src;
music->freesrc = freesrc;
May 22, 2013
May 22, 2013
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
music->flac_data.max_to_read = 0;
music->flac_data.overflow = NULL;
music->flac_data.overflow_len = 0;
music->flac_data.overflow_read = 0;
music->flac_data.data = NULL;
music->flac_data.data_len = 0;
music->flac_data.data_read = 0;
init_stage++; // stage 1!
music->flac_decoder = flac.FLAC__stream_decoder_new ();
if (music->flac_decoder != NULL) {
init_stage++; // stage 2!
if (flac.FLAC__stream_decoder_init_stream(
music->flac_decoder,
flac_read_music_cb, flac_seek_music_cb,
flac_tell_music_cb, flac_length_music_cb,
flac_eof_music_cb, flac_write_music_cb,
flac_metadata_music_cb, flac_error_music_cb,
Oct 17, 2017
Oct 17, 2017
535
music) == FLAC__STREAM_DECODER_INIT_STATUS_OK) {
May 22, 2013
May 22, 2013
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
init_stage++; // stage 3!
if (flac.FLAC__stream_decoder_process_until_end_of_metadata
(music->flac_decoder)) {
was_error = 0;
} else {
SDL_SetError("FLAC__stream_decoder_process_until_end_of_metadata() failed");
}
} else {
SDL_SetError("FLAC__stream_decoder_init_stream() failed");
}
} else {
SDL_SetError("FLAC__stream_decoder_new() failed");
}
if (was_error) {
switch (init_stage) {
case 3:
Oct 17, 2017
Oct 17, 2017
554
flac.FLAC__stream_decoder_finish(music->flac_decoder);
May 22, 2013
May 22, 2013
555
case 2:
Oct 17, 2017
Oct 17, 2017
556
flac.FLAC__stream_decoder_delete(music->flac_decoder);
May 22, 2013
May 22, 2013
557
558
559
560
561
562
563
564
565
566
567
case 1:
case 0:
SDL_free(music);
break;
}
return NULL;
}
} else {
SDL_OutOfMemory();
}
return music;
568
569
}
Oct 17, 2017
Oct 17, 2017
570
571
572
573
574
575
576
/* Set the volume for an FLAC stream */
static void FLAC_SetVolume(void *context, int volume)
{
FLAC_music *music = (FLAC_music *)context;
music->volume = volume;
}
577
/* Start playback of a given FLAC stream */
Oct 17, 2017
Oct 17, 2017
578
static int FLAC_Play(void *context)
Oct 3, 2009
Oct 3, 2009
579
{
Oct 17, 2017
Oct 17, 2017
580
581
582
FLAC_music *music = (FLAC_music *)context;
music->playing = SDL_TRUE;
return 0;
583
584
585
}
/* Return non-zero if a stream is currently playing */
Oct 17, 2017
Oct 17, 2017
586
static SDL_bool FLAC_IsPlaying(void *context)
Oct 3, 2009
Oct 3, 2009
587
{
Oct 17, 2017
Oct 17, 2017
588
589
FLAC_music *music = (FLAC_music *)context;
return music->playing;
590
591
592
}
/* Read some FLAC stream data and convert it for output */
Oct 3, 2009
Oct 3, 2009
593
594
static void FLAC_getsome(FLAC_music *music)
{
May 22, 2013
May 22, 2013
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
SDL_AudioCVT *cvt;
/* GET AUDIO WAVE DATA */
// set the max number of characters to read
music->flac_data.max_to_read = 8192;
music->flac_data.data_len = music->flac_data.max_to_read;
music->flac_data.data_read = 0;
if (!music->flac_data.data) {
music->flac_data.data = (char *)SDL_malloc (music->flac_data.data_len);
}
// we have data to read
while(music->flac_data.max_to_read > 0) {
// first check if there is data in the overflow from before
if (music->flac_data.overflow) {
size_t overflow_len = music->flac_data.overflow_read;
Jun 12, 2013
Jun 12, 2013
612
if (overflow_len > (size_t)music->flac_data.max_to_read) {
May 22, 2013
May 22, 2013
613
614
615
size_t overflow_extra_len = overflow_len -
music->flac_data.max_to_read;
Jun 1, 2013
Jun 1, 2013
616
SDL_memcpy (music->flac_data.data+music->flac_data.data_read,
May 22, 2013
May 22, 2013
617
618
music->flac_data.overflow, music->flac_data.max_to_read);
music->flac_data.data_read += music->flac_data.max_to_read;
Jun 1, 2013
Jun 1, 2013
619
SDL_memcpy (music->flac_data.overflow,
May 22, 2013
May 22, 2013
620
621
music->flac_data.overflow + music->flac_data.max_to_read,
overflow_extra_len);
Oct 13, 2017
Oct 13, 2017
622
623
music->flac_data.overflow_len = (int)overflow_extra_len;
music->flac_data.overflow_read = (int)overflow_extra_len;
May 22, 2013
May 22, 2013
624
music->flac_data.max_to_read = 0;
Jun 1, 2013
Jun 1, 2013
625
626
} else {
SDL_memcpy (music->flac_data.data+music->flac_data.data_read,
May 22, 2013
May 22, 2013
627
music->flac_data.overflow, overflow_len);
Oct 13, 2017
Oct 13, 2017
628
music->flac_data.data_read += (int)overflow_len;
Jun 1, 2013
Jun 1, 2013
629
SDL_free (music->flac_data.overflow);
May 22, 2013
May 22, 2013
630
631
632
music->flac_data.overflow = NULL;
music->flac_data.overflow_len = 0;
music->flac_data.overflow_read = 0;
Oct 13, 2017
Oct 13, 2017
633
music->flac_data.max_to_read -= (int)overflow_len;
May 22, 2013
May 22, 2013
634
}
Oct 17, 2017
Oct 17, 2017
635
} else {
May 22, 2013
May 22, 2013
636
637
638
639
640
641
642
643
644
645
646
647
648
649
if (!flac.FLAC__stream_decoder_process_single (
music->flac_decoder)) {
music->flac_data.max_to_read = 0;
}
if (flac.FLAC__stream_decoder_get_state (music->flac_decoder)
== FLAC__STREAM_DECODER_END_OF_STREAM) {
music->flac_data.max_to_read = 0;
}
}
}
if (music->flac_data.data_read <= 0) {
if (music->flac_data.data_read == 0) {
Oct 17, 2017
Oct 17, 2017
650
music->playing = SDL_FALSE;
May 22, 2013
May 22, 2013
651
652
653
654
655
656
}
return;
}
cvt = &music->cvt;
if (music->section < 0) {
SDL_BuildAudioCVT (cvt, AUDIO_S16, (Uint8)music->flac_data.channels,
Oct 17, 2017
Oct 17, 2017
657
658
659
660
(int)music->flac_data.sample_rate,
music_spec.format,
music_spec.channels,
music_spec.freq);
May 22, 2013
May 22, 2013
661
if (cvt->buf) {
Jun 1, 2013
Jun 1, 2013
662
SDL_free (cvt->buf);
May 22, 2013
May 22, 2013
663
664
665
666
667
}
cvt->buf = (Uint8 *)SDL_malloc (music->flac_data.data_len * cvt->len_mult);
music->section = 0;
}
if (cvt->buf) {
Jun 1, 2013
Jun 1, 2013
668
SDL_memcpy (cvt->buf, music->flac_data.data, music->flac_data.data_read);
May 22, 2013
May 22, 2013
669
670
671
if (cvt->needed) {
cvt->len = music->flac_data.data_read;
SDL_ConvertAudio (cvt);
Oct 17, 2017
Oct 17, 2017
672
} else {
May 22, 2013
May 22, 2013
673
674
675
676
cvt->len_cvt = music->flac_data.data_read;
}
music->len_available = music->cvt.len_cvt;
music->snd_available = music->cvt.buf;
Oct 17, 2017
Oct 17, 2017
677
} else {
May 22, 2013
May 22, 2013
678
SDL_SetError ("Out of memory");
Oct 17, 2017
Oct 17, 2017
679
music->playing = SDL_FALSE;
May 22, 2013
May 22, 2013
680
}
681
682
683
}
/* Play some of a stream previously started with FLAC_play() */
Oct 17, 2017
Oct 17, 2017
684
static int FLAC_GetAudio(void *context, void *data, int bytes)
Oct 3, 2009
Oct 3, 2009
685
{
Oct 17, 2017
Oct 17, 2017
686
687
688
FLAC_music *music = (FLAC_music *)context;
Uint8 *snd = (Uint8 *)data;
int len = bytes;
May 22, 2013
May 22, 2013
689
690
691
692
693
694
695
696
697
698
699
int mixable;
while ((len > 0) && music->playing) {
if (!music->len_available) {
FLAC_getsome (music);
}
mixable = len;
if (mixable > music->len_available) {
mixable = music->len_available;
}
if (music->volume == MIX_MAX_VOLUME) {
Jun 1, 2013
Jun 1, 2013
700
SDL_memcpy (snd, music->snd_available, mixable);
Oct 17, 2017
Oct 17, 2017
701
702
} else {
SDL_MixAudioFormat(snd, music->snd_available, music_spec.format, mixable, music->volume);
May 22, 2013
May 22, 2013
703
704
705
706
707
708
709
710
}
music->len_available -= mixable;
music->snd_available += mixable;
len -= mixable;
snd += mixable;
}
return len;
711
712
}
Oct 17, 2017
Oct 17, 2017
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
/* Jump (seek) to a given position (position is in seconds) */
static int FLAC_Seek(void *context, double position)
{
FLAC_music *music = (FLAC_music *)context;
double seek_sample = music->flac_data.sample_rate * position;
// clear data if it has data
if (music->flac_data.data) {
SDL_free (music->flac_data.data);
music->flac_data.data = NULL;
}
// clear overflow if it has data
if (music->flac_data.overflow) {
SDL_free (music->flac_data.overflow);
music->flac_data.overflow = NULL;
}
if (!flac.FLAC__stream_decoder_seek_absolute (music->flac_decoder,
(FLAC__uint64)seek_sample)) {
if (flac.FLAC__stream_decoder_get_state (music->flac_decoder)
== FLAC__STREAM_DECODER_SEEK_ERROR) {
flac.FLAC__stream_decoder_flush (music->flac_decoder);
}
SDL_SetError("Seeking of FLAC stream failed: libFLAC seek failed.");
return -1;
}
return 0;
}
744
/* Stop playback of a stream previously started with FLAC_play() */
Oct 17, 2017
Oct 17, 2017
745
static void FLAC_Stop(void *context)
Oct 3, 2009
Oct 3, 2009
746
{
Oct 17, 2017
Oct 17, 2017
747
748
FLAC_music *music = (FLAC_music *)context;
music->playing = SDL_FALSE;
749
750
751
}
/* Close the given FLAC_music object */
Oct 17, 2017
Oct 17, 2017
752
static void FLAC_Delete(void *context)
Oct 3, 2009
Oct 3, 2009
753
{
Oct 17, 2017
Oct 17, 2017
754
FLAC_music *music = (FLAC_music *)context;
May 22, 2013
May 22, 2013
755
756
757
758
759
760
761
if (music) {
if (music->flac_decoder) {
flac.FLAC__stream_decoder_finish (music->flac_decoder);
flac.FLAC__stream_decoder_delete (music->flac_decoder);
}
if (music->flac_data.data) {
Jun 1, 2013
Jun 1, 2013
762
SDL_free (music->flac_data.data);
May 22, 2013
May 22, 2013
763
764
765
}
if (music->flac_data.overflow) {
Jun 1, 2013
Jun 1, 2013
766
SDL_free (music->flac_data.overflow);
May 22, 2013
May 22, 2013
767
768
769
}
if (music->cvt.buf) {
Jun 1, 2013
Jun 1, 2013
770
SDL_free (music->cvt.buf);
May 22, 2013
May 22, 2013
771
772
}
Jun 2, 2013
Jun 2, 2013
773
774
if (music->freesrc) {
SDL_RWclose(music->src);
May 22, 2013
May 22, 2013
775
}
Jun 1, 2013
Jun 1, 2013
776
SDL_free (music);
May 22, 2013
May 22, 2013
777
}
778
779
}
Oct 17, 2017
Oct 17, 2017
780
Mix_MusicInterface Mix_MusicInterface_FLAC =
Oct 3, 2009
Oct 3, 2009
781
{
Oct 17, 2017
Oct 17, 2017
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
"FLAC",
MIX_MUSIC_FLAC,
MUS_FLAC,
SDL_FALSE,
SDL_FALSE,
FLAC_Load,
NULL, /* Open */
FLAC_CreateFromRW,
NULL, /* CreateFromFile */
FLAC_SetVolume,
FLAC_Play,
FLAC_IsPlaying,
FLAC_GetAudio,
FLAC_Seek,
NULL, /* Pause */
NULL, /* Resume */
FLAC_Stop,
FLAC_Delete,
NULL, /* Close */
FLAC_Unload,
};
#endif /* MUSIC_FLAC */
/* vi: set ts=4 sw=4 expandtab: */