Skip to content

Latest commit

 

History

History
592 lines (513 loc) · 16 KB

music_flac.c

File metadata and controls

592 lines (513 loc) · 16 KB
 
Dec 31, 2011
Dec 31, 2011
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
SDL_mixer: An audio mixer library based on the SDL library
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
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.
~ Austen Dicken (admin@cvpcs.org)
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
*/
#ifdef FLAC_MUSIC
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SDL_mixer.h"
#include "dynamic_flac.h"
#include "music_flac.h"
/* This is the format of the audio mixer data */
static SDL_AudioSpec mixer;
/* Initialize the FLAC player, with the given mixer settings
This function returns 0, or -1 if there was an error.
*/
Oct 3, 2009
Oct 3, 2009
41
42
int FLAC_init(SDL_AudioSpec *mixerfmt)
{
43
44
45
46
47
mixer = *mixerfmt;
return(0);
}
/* Set the volume for an FLAC stream */
Oct 3, 2009
Oct 3, 2009
48
49
void FLAC_setvolume(FLAC_music *music, int volume)
{
50
51
52
53
54
55
56
music->volume = volume;
}
static FLAC__StreamDecoderReadStatus flac_read_music_cb(
const FLAC__StreamDecoder *decoder,
FLAC__byte buffer[],
size_t *bytes,
Oct 3, 2009
Oct 3, 2009
57
58
void *client_data)
{
59
60
61
62
FLAC_music *data = (FLAC_music*)client_data;
// make sure there is something to be reading
if (*bytes > 0) {
Sep 5, 2019
Sep 5, 2019
63
int nbytes = SDL_RWread (data->rwops, buffer, sizeof (FLAC__byte), *bytes);
Sep 5, 2019
Sep 5, 2019
65
if (nbytes < 0) { // error in read
66
67
return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
}
Sep 5, 2019
Sep 5, 2019
68
69
*bytes = nbytes;
if (nbytes == 0) { // no data was read (EOF)
70
71
72
73
74
75
76
77
78
79
80
81
82
83
return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
}
else { // data was read, continue
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
}
}
else {
return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
}
}
static FLAC__StreamDecoderSeekStatus flac_seek_music_cb(
const FLAC__StreamDecoder *decoder,
FLAC__uint64 absolute_byte_offset,
Oct 3, 2009
Oct 3, 2009
84
85
void *client_data)
{
86
87
FLAC_music *data = (FLAC_music*)client_data;
Nov 8, 2009
Nov 8, 2009
88
if (SDL_RWseek (data->rwops, absolute_byte_offset, RW_SEEK_SET) < 0) {
89
90
91
92
93
94
95
96
97
98
return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
}
else {
return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
}
}
static FLAC__StreamDecoderTellStatus flac_tell_music_cb(
const FLAC__StreamDecoder *decoder,
FLAC__uint64 *absolute_byte_offset,
Oct 3, 2009
Oct 3, 2009
99
100
void *client_data )
{
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
FLAC_music *data = (FLAC_music*)client_data;
int pos = SDL_RWtell (data->rwops);
if (pos < 0) {
return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
}
else {
*absolute_byte_offset = (FLAC__uint64)pos;
return FLAC__STREAM_DECODER_TELL_STATUS_OK;
}
}
static FLAC__StreamDecoderLengthStatus flac_length_music_cb (
const FLAC__StreamDecoder *decoder,
FLAC__uint64 *stream_length,
Oct 3, 2009
Oct 3, 2009
116
117
void *client_data)
{
118
119
FLAC_music *data = (FLAC_music*)client_data;
int pos = SDL_RWtell (data->rwops);
Nov 8, 2009
Nov 8, 2009
120
int length = SDL_RWseek (data->rwops, 0, RW_SEEK_END);
Nov 8, 2009
Nov 8, 2009
122
if (SDL_RWseek (data->rwops, pos, RW_SEEK_SET) != pos || length < 0) {
123
124
125
126
127
128
129
130
131
132
133
134
/* 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;
}
else {
*stream_length = (FLAC__uint64)length;
return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
}
}
static FLAC__bool flac_eof_music_cb(
const FLAC__StreamDecoder *decoder,
Oct 3, 2009
Oct 3, 2009
135
136
void *client_data )
{
137
138
FLAC_music *data = (FLAC_music*)client_data;
int pos = SDL_RWtell (data->rwops);
Nov 8, 2009
Nov 8, 2009
139
int end = SDL_RWseek (data->rwops, 0, RW_SEEK_END);
140
141
142
143
144
145
146
147
// was the original position equal to the end (a.k.a. the seek didn't move)?
if (pos == end) {
// must be EOF
return true;
}
else {
// not EOF, return to the original position
Nov 8, 2009
Nov 8, 2009
148
SDL_RWseek (data->rwops, pos, RW_SEEK_SET);
149
150
151
152
153
154
155
156
157
return false;
}
}
static FLAC__StreamDecoderWriteStatus flac_write_music_cb(
const FLAC__StreamDecoder *decoder,
const FLAC__Frame *frame,
const FLAC__int32 *const buffer[],
Oct 3, 2009
Oct 3, 2009
158
159
void *client_data)
{
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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++) {
Oct 10, 2009
Oct 10, 2009
175
176
177
FLAC__int16 i16;
FLAC__uint16 ui16;
178
179
180
181
182
183
184
185
186
187
// 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 =
Jan 13, 2012
Jan 13, 2012
188
(char *)SDL_malloc (data->flac_data.data_len);
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
223
224
225
226
227
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;
}
}
else {
// we need to write to the overflow
if (!data->flac_data.overflow) {
data->flac_data.overflow_len =
4 * (frame->header.blocksize - i);
data->flac_data.overflow_read = 0;
// make it big enough for the rest of the block
data->flac_data.overflow =
Jan 13, 2012
Jan 13, 2012
228
(char *)SDL_malloc (data->flac_data.overflow_len);
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
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;
}
static void flac_metadata_music_cb(
const FLAC__StreamDecoder *decoder,
const FLAC__StreamMetadata *metadata,
Oct 3, 2009
Oct 3, 2009
259
260
void *client_data)
{
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
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);
}
}
static void flac_error_music_cb(
const FLAC__StreamDecoder *decoder,
FLAC__StreamDecoderErrorStatus status,
Oct 3, 2009
Oct 3, 2009
278
279
void *client_data)
{
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// 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;
}
}
/* Load an FLAC stream from an SDL_RWops object */
Dec 31, 2011
Dec 31, 2011
301
FLAC_music *FLAC_new_RW(SDL_RWops *rw, int freerw)
Oct 3, 2009
Oct 3, 2009
302
{
303
304
305
306
FLAC_music *music;
int init_stage = 0;
int was_error = 1;
Jan 4, 2012
Jan 4, 2012
307
308
309
310
311
312
313
if (!Mix_Init(MIX_INIT_FLAC)) {
if (freerw) {
SDL_RWclose(rw);
}
return NULL;
}
Jan 13, 2012
Jan 13, 2012
314
music = (FLAC_music *)SDL_malloc ( sizeof (*music));
315
316
317
318
319
320
321
if (music) {
/* Initialize the music structure */
memset (music, 0, (sizeof (*music)));
FLAC_stop (music);
FLAC_setvolume (music, MIX_MAX_VOLUME);
music->section = -1;
music->rwops = rw;
Dec 31, 2011
Dec 31, 2011
322
music->freerw = freerw;
323
324
325
326
327
328
329
330
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;
Jan 4, 2012
Jan 4, 2012
331
init_stage++; // stage 1!
Jan 4, 2012
Jan 4, 2012
333
music->flac_decoder = flac.FLAC__stream_decoder_new ();
Jan 4, 2012
Jan 4, 2012
335
336
if (music->flac_decoder != NULL) {
init_stage++; // stage 2!
Jan 4, 2012
Jan 4, 2012
338
339
340
341
342
343
344
345
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,
music) == FLAC__STREAM_DECODER_INIT_STATUS_OK ) {
init_stage++; // stage 3!
Jan 4, 2012
Jan 4, 2012
347
348
349
if (flac.FLAC__stream_decoder_process_until_end_of_metadata
(music->flac_decoder)) {
was_error = 0;
Nov 8, 2009
Nov 8, 2009
350
} else {
Jan 4, 2012
Jan 4, 2012
351
SDL_SetError("FLAC__stream_decoder_process_until_end_of_metadata() failed");
Nov 8, 2009
Nov 8, 2009
353
} else {
Jan 4, 2012
Jan 4, 2012
354
SDL_SetError("FLAC__stream_decoder_init_stream() failed");
Jan 4, 2012
Jan 4, 2012
356
357
} else {
SDL_SetError("FLAC__stream_decoder_new() failed");
358
359
360
361
362
363
364
365
366
367
}
if (was_error) {
switch (init_stage) {
case 3:
flac.FLAC__stream_decoder_finish( music->flac_decoder );
case 2:
flac.FLAC__stream_decoder_delete( music->flac_decoder );
case 1:
case 0:
Jan 13, 2012
Jan 13, 2012
368
SDL_free(music);
Dec 31, 2011
Dec 31, 2011
369
370
371
if (freerw) {
SDL_RWclose(rw);
}
372
373
374
375
break;
}
return NULL;
}
Dec 31, 2011
Dec 31, 2011
376
} else {
377
SDL_OutOfMemory();
Dec 31, 2011
Dec 31, 2011
378
379
380
381
if (freerw) {
SDL_RWclose(rw);
}
return NULL;
382
383
384
385
386
387
}
return music;
}
/* Start playback of a given FLAC stream */
Oct 3, 2009
Oct 3, 2009
388
389
void FLAC_play(FLAC_music *music)
{
390
391
392
393
music->playing = 1;
}
/* Return non-zero if a stream is currently playing */
Oct 3, 2009
Oct 3, 2009
394
395
int FLAC_playing(FLAC_music *music)
{
396
397
398
399
return(music->playing);
}
/* Read some FLAC stream data and convert it for output */
Oct 3, 2009
Oct 3, 2009
400
401
static void FLAC_getsome(FLAC_music *music)
{
402
403
SDL_AudioCVT *cvt;
Jan 1, 2012
Jan 1, 2012
404
/* GET AUDIO WAVE DATA */
405
406
407
408
// 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;
Jan 1, 2012
Jan 1, 2012
409
if (!music->flac_data.data) {
Jan 13, 2012
Jan 13, 2012
410
music->flac_data.data = (char *)SDL_malloc (music->flac_data.data_len);
Jan 1, 2012
Jan 1, 2012
411
}
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// 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;
if (overflow_len > music->flac_data.max_to_read) {
size_t overflow_extra_len = overflow_len -
music->flac_data.max_to_read;
memcpy (music->flac_data.data+music->flac_data.data_read,
music->flac_data.overflow, music->flac_data.max_to_read);
music->flac_data.data_read += music->flac_data.max_to_read;
Jan 1, 2012
Jan 1, 2012
426
memcpy (music->flac_data.overflow,
427
428
429
music->flac_data.overflow + music->flac_data.max_to_read,
overflow_extra_len);
music->flac_data.overflow_len = overflow_extra_len;
Jan 1, 2012
Jan 1, 2012
430
music->flac_data.overflow_read = overflow_extra_len;
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
music->flac_data.max_to_read = 0;
}
else {
memcpy (music->flac_data.data+music->flac_data.data_read,
music->flac_data.overflow, overflow_len);
music->flac_data.data_read += overflow_len;
free (music->flac_data.overflow);
music->flac_data.overflow = NULL;
music->flac_data.overflow_len = 0;
music->flac_data.overflow_read = 0;
music->flac_data.max_to_read -= overflow_len;
}
}
else {
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) {
music->playing = 0;
}
return;
}
cvt = &music->cvt;
Apr 14, 2010
Apr 14, 2010
464
if (music->section < 0) {
465
466
SDL_BuildAudioCVT (cvt, AUDIO_S16, (Uint8)music->flac_data.channels,
(int)music->flac_data.sample_rate, mixer.format,
Sep 5, 2019
Sep 5, 2019
467
mixer.channels, mixer.freq);
468
469
470
if (cvt->buf) {
free (cvt->buf);
}
Jan 13, 2012
Jan 13, 2012
471
cvt->buf = (Uint8 *)SDL_malloc (music->flac_data.data_len * cvt->len_mult);
Apr 14, 2010
Apr 14, 2010
472
music->section = 0;
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
}
if (cvt->buf) {
memcpy (cvt->buf, music->flac_data.data, music->flac_data.data_read);
if (cvt->needed) {
cvt->len = music->flac_data.data_read;
SDL_ConvertAudio (cvt);
}
else {
cvt->len_cvt = music->flac_data.data_read;
}
music->len_available = music->cvt.len_cvt;
music->snd_available = music->cvt.buf;
}
else {
SDL_SetError ("Out of memory");
music->playing = 0;
}
}
/* Play some of a stream previously started with FLAC_play() */
Oct 3, 2009
Oct 3, 2009
493
494
int FLAC_playAudio(FLAC_music *music, Uint8 *snd, int len)
{
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
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) {
memcpy (snd, music->snd_available, mixable);
}
else {
SDL_MixAudio (snd, music->snd_available, mixable, music->volume);
}
music->len_available -= mixable;
music->snd_available += mixable;
len -= mixable;
snd += mixable;
}
return len;
}
/* Stop playback of a stream previously started with FLAC_play() */
Oct 3, 2009
Oct 3, 2009
521
522
void FLAC_stop(FLAC_music *music)
{
523
524
525
526
music->playing = 0;
}
/* Close the given FLAC_music object */
Oct 3, 2009
Oct 3, 2009
527
528
void FLAC_delete(FLAC_music *music)
{
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
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) {
free (music->flac_data.data);
}
if (music->flac_data.overflow) {
free (music->flac_data.overflow);
}
if (music->cvt.buf) {
free (music->cvt.buf);
}
Dec 31, 2011
Dec 31, 2011
547
548
549
if (music->freerw) {
SDL_RWclose(music->rwops);
}
550
551
552
553
554
free (music);
}
}
/* Jump (seek) to a given position (time is in seconds) */
Oct 3, 2009
Oct 3, 2009
555
556
void FLAC_jump_to_time(FLAC_music *music, double time)
{
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
if (music) {
if (music->flac_decoder) {
double seek_sample = music->flac_data.sample_rate * time;
// clear data if it has data
if (music->flac_data.data) {
free (music->flac_data.data);
music->flac_data.data = NULL;
}
// clear overflow if it has data
if (music->flac_data.overflow) {
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);
}
Sep 5, 2019
Sep 5, 2019
580
SDL_SetError("Seeking of FLAC stream failed: libFLAC seek failed.");
581
582
583
}
}
else {
Sep 5, 2019
Sep 5, 2019
584
SDL_SetError("Seeking of FLAC stream failed: FLAC decoder was NULL.");
585
586
587
588
589
590
591
592
}
}
else {
SDL_SetError ("Seeking of FLAC stream failed: music was NULL.");
}
}
#endif /* FLAC_MUSIC */