Skip to content

Latest commit

 

History

History
744 lines (649 loc) · 24.8 KB

music_flac.c

File metadata and controls

744 lines (649 loc) · 24.8 KB
 
Dec 31, 2011
Dec 31, 2011
2
SDL_mixer: An audio mixer library based on the SDL library
Jan 5, 2019
Jan 5, 2019
3
Copyright (C) 1997-2019 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 21, 2017
Oct 21, 2017
27
#include "SDL_assert.h"
Oct 17, 2017
Oct 17, 2017
28
#include "SDL_loadso.h"
29
30
31
#include "music_flac.h"
Oct 17, 2017
Oct 17, 2017
32
33
34
35
36
37
#include <FLAC/stream_decoder.h>
typedef struct {
int loaded;
void *handle;
Oct 18, 2017
Oct 18, 2017
38
FLAC__StreamDecoder *(*FLAC__stream_decoder_new)(void);
Oct 17, 2017
Oct 17, 2017
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
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);
Nov 17, 2019
Nov 17, 2019
64
65
66
67
68
FLAC__uint64 (*FLAC__stream_decoder_get_total_samples)(
const FLAC__StreamDecoder *decoder);
FLAC__bool (*FLAC__stream_decoder_set_metadata_respond)(
FLAC__StreamDecoder *decoder,
FLAC__MetadataType type);
Oct 17, 2017
Oct 17, 2017
69
70
71
72
73
74
75
} flac_loader;
static flac_loader flac = {
0, NULL
};
#ifdef FLAC_DYNAMIC
Oct 21, 2017
Oct 21, 2017
76
77
78
79
80
81
82
#define FUNCTION_LOADER(FUNC, SIG) \
flac.FUNC = (SIG) SDL_LoadFunction(flac.handle, #FUNC); \
if (flac.FUNC == NULL) { SDL_UnloadObject(flac.handle); return -1; }
#else
#define FUNCTION_LOADER(FUNC, SIG) \
flac.FUNC = FUNC;
#endif
Oct 17, 2017
Oct 17, 2017
83
Oct 18, 2017
Oct 18, 2017
84
static int FLAC_Load(void)
Oct 17, 2017
Oct 17, 2017
85
86
{
if (flac.loaded == 0) {
Oct 21, 2017
Oct 21, 2017
87
#ifdef FLAC_DYNAMIC
Oct 17, 2017
Oct 17, 2017
88
89
90
91
flac.handle = SDL_LoadObject(FLAC_DYNAMIC);
if (flac.handle == NULL) {
return -1;
}
Oct 21, 2017
Oct 21, 2017
92
93
94
95
96
97
#elif defined(__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");
Oct 17, 2017
Oct 17, 2017
98
99
return -1;
}
Oct 21, 2017
Oct 21, 2017
100
101
102
103
104
#endif
FUNCTION_LOADER(FLAC__stream_decoder_new, FLAC__StreamDecoder *(*)(void))
FUNCTION_LOADER(FLAC__stream_decoder_delete, void (*)(FLAC__StreamDecoder *))
FUNCTION_LOADER(FLAC__stream_decoder_init_stream, FLAC__StreamDecoderInitStatus (*)(
Oct 17, 2017
Oct 17, 2017
105
106
107
108
109
110
111
112
113
114
FLAC__StreamDecoder *,
FLAC__StreamDecoderReadCallback,
FLAC__StreamDecoderSeekCallback,
FLAC__StreamDecoderTellCallback,
FLAC__StreamDecoderLengthCallback,
FLAC__StreamDecoderEofCallback,
FLAC__StreamDecoderWriteCallback,
FLAC__StreamDecoderMetadataCallback,
FLAC__StreamDecoderErrorCallback,
void *))
Oct 21, 2017
Oct 21, 2017
115
116
117
118
119
120
121
FUNCTION_LOADER(FLAC__stream_decoder_finish, FLAC__bool (*)(FLAC__StreamDecoder *))
FUNCTION_LOADER(FLAC__stream_decoder_flush, FLAC__bool (*)(FLAC__StreamDecoder *))
FUNCTION_LOADER(FLAC__stream_decoder_process_single, FLAC__bool (*)(FLAC__StreamDecoder *))
FUNCTION_LOADER(FLAC__stream_decoder_process_until_end_of_metadata, FLAC__bool (*)(FLAC__StreamDecoder *))
FUNCTION_LOADER(FLAC__stream_decoder_process_until_end_of_stream, FLAC__bool (*)(FLAC__StreamDecoder *))
FUNCTION_LOADER(FLAC__stream_decoder_seek_absolute, FLAC__bool (*)(FLAC__StreamDecoder *, FLAC__uint64))
FUNCTION_LOADER(FLAC__stream_decoder_get_state, FLAC__StreamDecoderState (*)(const FLAC__StreamDecoder *decoder))
Nov 17, 2019
Nov 17, 2019
122
123
124
125
126
FUNCTION_LOADER(FLAC__stream_decoder_get_total_samples,
FLAC__uint64 (*)(const FLAC__StreamDecoder *))
FUNCTION_LOADER(FLAC__stream_decoder_set_metadata_respond,
FLAC__bool (*)(FLAC__StreamDecoder *,
FLAC__MetadataType))
Oct 17, 2017
Oct 17, 2017
127
128
}
++flac.loaded;
Oct 17, 2017
Oct 17, 2017
130
131
132
133
return 0;
}
static void FLAC_Unload(void)
Oct 3, 2009
Oct 3, 2009
134
{
Oct 17, 2017
Oct 17, 2017
135
136
137
138
if (flac.loaded == 0) {
return;
}
if (flac.loaded == 1) {
Oct 21, 2017
Oct 21, 2017
139
#ifdef FLAC_DYNAMIC
Oct 17, 2017
Oct 17, 2017
140
SDL_UnloadObject(flac.handle);
Oct 21, 2017
Oct 21, 2017
141
#endif
Oct 17, 2017
Oct 17, 2017
142
143
}
--flac.loaded;
144
145
}
Oct 17, 2017
Oct 17, 2017
146
147
typedef struct {
Oct 21, 2017
Oct 21, 2017
148
149
150
int volume;
int play_count;
FLAC__StreamDecoder *flac_decoder;
Oct 17, 2017
Oct 17, 2017
151
152
153
154
155
unsigned sample_rate;
unsigned channels;
unsigned bits_per_sample;
SDL_RWops *src;
int freesrc;
Oct 21, 2017
Oct 21, 2017
156
SDL_AudioStream *stream;
Nov 17, 2019
Nov 17, 2019
157
int loop;
Dec 19, 2019
Dec 19, 2019
158
159
FLAC__int64 pcm_pos;
FLAC__int64 full_length;
Nov 17, 2019
Nov 17, 2019
160
SDL_bool loop_flag;
Dec 19, 2019
Dec 19, 2019
161
162
163
FLAC__int64 loop_start;
FLAC__int64 loop_end;
FLAC__int64 loop_len;
Oct 21, 2017
Oct 21, 2017
164
} FLAC_Music;
Oct 17, 2017
Oct 17, 2017
165
166
Oct 21, 2017
Oct 21, 2017
167
168
static int FLAC_Seek(void *context, double position);
169
static FLAC__StreamDecoderReadStatus flac_read_music_cb(
May 22, 2013
May 22, 2013
170
171
172
173
const FLAC__StreamDecoder *decoder,
FLAC__byte buffer[],
size_t *bytes,
void *client_data)
Oct 3, 2009
Oct 3, 2009
174
{
Oct 21, 2017
Oct 21, 2017
175
FLAC_Music *data = (FLAC_Music*)client_data;
May 22, 2013
May 22, 2013
176
Dec 17, 2019
Dec 17, 2019
177
178
(void)decoder;
Oct 21, 2017
Oct 21, 2017
179
/* make sure there is something to be reading */
May 22, 2013
May 22, 2013
180
if (*bytes > 0) {
Jun 2, 2013
Jun 2, 2013
181
*bytes = SDL_RWread (data->src, buffer, sizeof (FLAC__byte), *bytes);
May 22, 2013
May 22, 2013
182
Oct 21, 2017
Oct 21, 2017
183
if (*bytes == 0) { /* error or no data was read (EOF) */
May 22, 2013
May 22, 2013
184
return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
Oct 21, 2017
Oct 21, 2017
185
} else { /* data was read, continue */
May 22, 2013
May 22, 2013
186
187
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
}
Jun 1, 2013
Jun 1, 2013
188
} else {
May 22, 2013
May 22, 2013
189
190
return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
}
191
192
193
}
static FLAC__StreamDecoderSeekStatus flac_seek_music_cb(
May 22, 2013
May 22, 2013
194
195
196
const FLAC__StreamDecoder *decoder,
FLAC__uint64 absolute_byte_offset,
void *client_data)
Oct 3, 2009
Oct 3, 2009
197
{
Oct 21, 2017
Oct 21, 2017
198
FLAC_Music *data = (FLAC_Music*)client_data;
May 22, 2013
May 22, 2013
199
Dec 17, 2019
Dec 17, 2019
200
201
202
(void)decoder;
if (SDL_RWseek(data->src, (Sint64)absolute_byte_offset, RW_SEEK_SET) < 0) {
May 22, 2013
May 22, 2013
203
return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
Jun 1, 2013
Jun 1, 2013
204
} else {
May 22, 2013
May 22, 2013
205
206
return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
}
207
208
209
}
static FLAC__StreamDecoderTellStatus flac_tell_music_cb(
May 22, 2013
May 22, 2013
210
211
const FLAC__StreamDecoder *decoder,
FLAC__uint64 *absolute_byte_offset,
Oct 17, 2017
Oct 17, 2017
212
void *client_data)
Oct 3, 2009
Oct 3, 2009
213
{
Oct 21, 2017
Oct 21, 2017
214
FLAC_Music *data = (FLAC_Music*)client_data;
Oct 21, 2017
Oct 21, 2017
216
Sint64 pos = SDL_RWtell(data->src);
Dec 17, 2019
Dec 17, 2019
218
219
(void)decoder;
May 22, 2013
May 22, 2013
220
221
if (pos < 0) {
return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
Jun 1, 2013
Jun 1, 2013
222
} else {
May 22, 2013
May 22, 2013
223
224
225
*absolute_byte_offset = (FLAC__uint64)pos;
return FLAC__STREAM_DECODER_TELL_STATUS_OK;
}
226
227
}
Oct 21, 2017
Oct 21, 2017
228
static FLAC__StreamDecoderLengthStatus flac_length_music_cb(
May 22, 2013
May 22, 2013
229
230
231
const FLAC__StreamDecoder *decoder,
FLAC__uint64 *stream_length,
void *client_data)
Oct 3, 2009
Oct 3, 2009
232
{
Oct 21, 2017
Oct 21, 2017
233
FLAC_Music *data = (FLAC_Music*)client_data;
May 22, 2013
May 22, 2013
234
Oct 21, 2017
Oct 21, 2017
235
236
Sint64 pos = SDL_RWtell(data->src);
Sint64 length = SDL_RWseek(data->src, 0, RW_SEEK_END);
May 22, 2013
May 22, 2013
237
Dec 17, 2019
Dec 17, 2019
238
239
(void)decoder;
Oct 21, 2017
Oct 21, 2017
240
if (SDL_RWseek(data->src, pos, RW_SEEK_SET) != pos || length < 0) {
May 22, 2013
May 22, 2013
241
242
243
/* 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
244
} else {
May 22, 2013
May 22, 2013
245
246
247
*stream_length = (FLAC__uint64)length;
return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
}
248
249
250
}
static FLAC__bool flac_eof_music_cb(
May 22, 2013
May 22, 2013
251
const FLAC__StreamDecoder *decoder,
Oct 17, 2017
Oct 17, 2017
252
void *client_data)
Oct 3, 2009
Oct 3, 2009
253
{
Oct 21, 2017
Oct 21, 2017
254
FLAC_Music *data = (FLAC_Music*)client_data;
May 22, 2013
May 22, 2013
255
Oct 21, 2017
Oct 21, 2017
256
257
Sint64 pos = SDL_RWtell(data->src);
Sint64 end = SDL_RWseek(data->src, 0, RW_SEEK_END);
May 22, 2013
May 22, 2013
258
Dec 17, 2019
Dec 17, 2019
259
260
(void)decoder;
Oct 21, 2017
Oct 21, 2017
261
/* was the original position equal to the end (a.k.a. the seek didn't move)? */
May 22, 2013
May 22, 2013
262
if (pos == end) {
Oct 21, 2017
Oct 21, 2017
263
/* must be EOF */
May 22, 2013
May 22, 2013
264
return true;
Jun 1, 2013
Jun 1, 2013
265
} else {
Oct 21, 2017
Oct 21, 2017
266
267
/* not EOF, return to the original position */
SDL_RWseek(data->src, pos, RW_SEEK_SET);
May 22, 2013
May 22, 2013
268
269
return false;
}
270
271
272
}
static FLAC__StreamDecoderWriteStatus flac_write_music_cb(
May 22, 2013
May 22, 2013
273
274
275
276
const FLAC__StreamDecoder *decoder,
const FLAC__Frame *frame,
const FLAC__int32 *const buffer[],
void *client_data)
Oct 3, 2009
Oct 3, 2009
277
{
Oct 21, 2017
Oct 21, 2017
278
279
FLAC_Music *music = (FLAC_Music *)client_data;
Sint16 *data;
Oct 21, 2017
Oct 21, 2017
280
unsigned int i, j, channels;
Nov 17, 2019
Nov 17, 2019
281
int shift_amount = 0, amount;
May 22, 2013
May 22, 2013
282
Dec 17, 2019
Dec 17, 2019
283
284
(void)decoder;
Oct 21, 2017
Oct 21, 2017
285
if (!music->stream) {
May 22, 2013
May 22, 2013
286
287
288
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
Oct 21, 2017
Oct 21, 2017
289
290
291
292
293
294
295
296
297
298
299
300
switch (music->bits_per_sample) {
case 16:
shift_amount = 0;
break;
case 20:
shift_amount = 4;
break;
case 24:
shift_amount = 8;
break;
default:
SDL_SetError("FLAC decoder doesn't support %d bits_per_sample", music->bits_per_sample);
May 22, 2013
May 22, 2013
301
302
303
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
Oct 21, 2017
Oct 21, 2017
304
305
306
307
308
309
if (music->channels == 3) {
/* We'll just drop the center channel for now */
channels = 2;
} else {
channels = music->channels;
}
May 22, 2013
May 22, 2013
310
Oct 21, 2017
Oct 21, 2017
311
312
313
314
315
316
317
318
data = SDL_stack_alloc(Sint16, (frame->header.blocksize * channels));
if (!data) {
SDL_SetError("Couldn't allocate %d bytes stack memory", (int)(frame->header.blocksize * channels * sizeof(*data)));
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
if (music->channels == 3) {
Sint16 *dst = data;
for (i = 0; i < frame->header.blocksize; ++i) {
Dec 17, 2019
Dec 17, 2019
319
320
Sint16 FL = (Sint16)(buffer[0][i] >> shift_amount);
Sint16 FR = (Sint16)(buffer[1][i] >> shift_amount);
Oct 21, 2017
Oct 21, 2017
321
322
323
324
325
326
327
328
329
Sint16 FCmix = (Sint16)((buffer[2][i] >> shift_amount) * 0.5f);
int sample;
sample = (FL + FCmix);
if (sample > SDL_MAX_SINT16) {
*dst = SDL_MAX_SINT16;
} else if (sample < SDL_MIN_SINT16) {
*dst = SDL_MIN_SINT16;
} else {
Dec 17, 2019
Dec 17, 2019
330
*dst = (Sint16)sample;
Oct 21, 2017
Oct 21, 2017
331
332
}
++dst;
May 22, 2013
May 22, 2013
333
Oct 21, 2017
Oct 21, 2017
334
335
336
337
338
339
sample = (FR + FCmix);
if (sample > SDL_MAX_SINT16) {
*dst = SDL_MAX_SINT16;
} else if (sample < SDL_MIN_SINT16) {
*dst = SDL_MIN_SINT16;
} else {
Dec 17, 2019
Dec 17, 2019
340
*dst = (Sint16)sample;
May 22, 2013
May 22, 2013
341
}
Oct 21, 2017
Oct 21, 2017
342
343
344
345
346
347
++dst;
}
} else {
for (i = 0; i < channels; ++i) {
Sint16 *dst = data + i;
for (j = 0; j < frame->header.blocksize; ++j) {
Dec 17, 2019
Dec 17, 2019
348
*dst = (Sint16)(buffer[i][j] >> shift_amount);
Oct 21, 2017
Oct 21, 2017
349
dst += channels;
May 22, 2013
May 22, 2013
350
351
352
}
}
}
Dec 17, 2019
Dec 17, 2019
353
amount = (int)(frame->header.blocksize * channels * sizeof(*data));
Dec 19, 2019
Dec 19, 2019
354
music->pcm_pos += (FLAC__int64) frame->header.blocksize;
Dec 20, 2019
Dec 20, 2019
355
if (music->loop && (music->play_count != 1) &&
Nov 26, 2019
Nov 26, 2019
356
(music->pcm_pos >= music->loop_end)) {
Dec 23, 2019
Dec 23, 2019
357
amount -= (music->pcm_pos - music->loop_end) * channels * (int)sizeof(*data);
Nov 17, 2019
Nov 17, 2019
358
359
360
361
music->loop_flag = SDL_TRUE;
}
SDL_AudioStreamPut(music->stream, data, amount);
Oct 21, 2017
Oct 21, 2017
362
SDL_stack_free(data);
May 22, 2013
May 22, 2013
363
364
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
365
366
}
Nov 17, 2019
Nov 17, 2019
367
368
/* Parse time string of the form HH:MM:SS.mmm and return equivalent sample
* position */
Dec 19, 2019
Dec 19, 2019
369
static FLAC__int64 parse_time(char *time, unsigned samplerate_hz)
Nov 17, 2019
Nov 17, 2019
370
371
{
char *num_start, *p;
Dec 19, 2019
Dec 19, 2019
372
373
FLAC__int64 result = 0;
char c; int val;
Nov 17, 2019
Nov 17, 2019
374
375
376
/* Time is directly expressed as a sample position */
if (SDL_strchr(time, ':') == NULL) {
Dec 19, 2019
Dec 19, 2019
377
return SDL_strtoll(time, NULL, 10);
Nov 17, 2019
Nov 17, 2019
378
379
380
381
382
383
384
385
}
result = 0;
num_start = time;
for (p = time; *p != '\0'; ++p) {
if (*p == '.' || *p == ':') {
c = *p; *p = '\0';
Dec 19, 2019
Dec 19, 2019
386
387
388
if ((val = SDL_atoi(num_start)) < 0)
return -1;
result = result * 60 + val;
Nov 17, 2019
Nov 17, 2019
389
390
391
392
393
num_start = p + 1;
*p = c;
}
if (*p == '.') {
Dec 19, 2019
Dec 19, 2019
394
395
396
double val_f = SDL_atof(p);
if (val_f < 0) return -1;
return result * samplerate_hz + (FLAC__int64) (val_f * samplerate_hz);
Nov 17, 2019
Nov 17, 2019
397
398
399
}
}
Dec 19, 2019
Dec 19, 2019
400
401
if ((val = SDL_atoi(num_start)) < 0) return -1;
return (result * 60 + val) * samplerate_hz;
Nov 17, 2019
Nov 17, 2019
402
403
}
404
static void flac_metadata_music_cb(
May 22, 2013
May 22, 2013
405
406
407
const FLAC__StreamDecoder *decoder,
const FLAC__StreamMetadata *metadata,
void *client_data)
Oct 3, 2009
Oct 3, 2009
408
{
Oct 21, 2017
Oct 21, 2017
409
FLAC_Music *music = (FLAC_Music *)client_data;
Nov 17, 2019
Nov 17, 2019
410
const FLAC__StreamMetadata_VorbisComment *vc;
Oct 21, 2017
Oct 21, 2017
411
int channels;
Nov 17, 2019
Nov 17, 2019
412
413
414
415
unsigned rate;
char *param, *argument, *value;
SDL_bool is_loop_length = SDL_FALSE;
Dec 17, 2019
Dec 17, 2019
416
417
(void)decoder;
Nov 17, 2019
Nov 17, 2019
418
419
420
421
if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
music->sample_rate = metadata->data.stream_info.sample_rate;
music->channels = metadata->data.stream_info.channels;
music->bits_per_sample = metadata->data.stream_info.bits_per_sample;
Dec 20, 2019
Dec 20, 2019
422
/*printf("FLAC: Sample rate = %d, channels = %d, bits_per_sample = %d\n", music->sample_rate, music->channels, music->bits_per_sample);*/
Nov 17, 2019
Nov 17, 2019
423
424
425
426
427
428
429
/* SDL's channel mapping and FLAC channel mapping are the same,
except for 3 channels: SDL is FL FR LFE and FLAC is FL FR FC
*/
if (music->channels == 3) {
channels = 2;
} else {
Dec 17, 2019
Dec 17, 2019
430
channels = (int)music->channels;
Nov 17, 2019
Nov 17, 2019
431
}
Oct 21, 2017
Oct 21, 2017
432
Nov 17, 2019
Nov 17, 2019
433
434
/* We check for NULL stream later when we get data */
SDL_assert(!music->stream);
Dec 17, 2019
Dec 17, 2019
435
music->stream = SDL_NewAudioStream(AUDIO_S16SYS, (Uint8)channels, (int)music->sample_rate,
Nov 17, 2019
Nov 17, 2019
436
437
music_spec.format, music_spec.channels, music_spec.freq);
} else if (metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
Dec 17, 2019
Dec 17, 2019
438
FLAC__uint32 i;
Nov 17, 2019
Nov 17, 2019
439
Nov 17, 2019
Nov 17, 2019
440
441
442
vc = &metadata->data.vorbis_comment;
rate = music->sample_rate;
Nov 17, 2019
Nov 17, 2019
443
for (i = 0; i < vc->num_comments; ++i) {
Nov 18, 2019
Nov 18, 2019
444
param = SDL_strdup((const char *) vc->comments[i].entry);
Nov 17, 2019
Nov 17, 2019
445
446
447
448
449
450
451
452
argument = param;
value = SDL_strchr(param, '=');
if (value == NULL) {
value = param + SDL_strlen(param);
} else {
*(value++) = '\0';
}
Oct 21, 2017
Oct 21, 2017
453
Nov 17, 2019
Nov 17, 2019
454
455
456
/* Want to match LOOP-START, LOOP_START, etc. Remove - or _ from
* string if it is present at position 4. */
if ((argument[4] == '_') || (argument[4] == '-')) {
Dec 19, 2019
Dec 19, 2019
457
SDL_memmove(argument + 4, argument + 5, SDL_strlen(argument) - 4);
Nov 17, 2019
Nov 17, 2019
458
459
460
461
462
}
if (SDL_strcasecmp(argument, "LOOPSTART") == 0)
music->loop_start = parse_time(value, rate);
else if (SDL_strcasecmp(argument, "LOOPLENGTH") == 0) {
Dec 19, 2019
Dec 19, 2019
463
music->loop_len = SDL_strtoll(value, NULL, 10);
Nov 17, 2019
Nov 17, 2019
464
465
466
is_loop_length = SDL_TRUE;
} else if (SDL_strcasecmp(argument, "LOOPEND") == 0) {
music->loop_end = parse_time(value, rate);
Dec 17, 2019
Dec 17, 2019
467
is_loop_length = SDL_FALSE;
Nov 17, 2019
Nov 17, 2019
468
}
Dec 19, 2019
Dec 19, 2019
469
470
471
472
473
474
475
if (music->loop_start < 0 || music->loop_len < 0 || music->loop_end < 0) {
music->loop_start = 0;
music->loop_len = 0;
music->loop_end = 0;
SDL_free(param);
break; /* ignore tag. */
}
Nov 17, 2019
Nov 17, 2019
476
477
478
479
480
481
482
483
SDL_free(param);
}
if (is_loop_length) {
music->loop_end = music->loop_start + music->loop_len;
} else {
music->loop_len = music->loop_end - music->loop_start;
}
Oct 21, 2017
Oct 21, 2017
484
}
485
486
487
}
static void flac_error_music_cb(
May 22, 2013
May 22, 2013
488
489
490
const FLAC__StreamDecoder *decoder,
FLAC__StreamDecoderErrorStatus status,
void *client_data)
Oct 3, 2009
Oct 3, 2009
491
{
Dec 17, 2019
Dec 17, 2019
492
493
494
(void)decoder;
(void)client_data;
Oct 21, 2017
Oct 21, 2017
495
/* print an SDL error based on the error status */
May 22, 2013
May 22, 2013
496
switch (status) {
Oct 21, 2017
Oct 21, 2017
497
498
case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC:
SDL_SetError("Error processing the FLAC file [LOST_SYNC].");
May 22, 2013
May 22, 2013
499
break;
Oct 21, 2017
Oct 21, 2017
500
501
case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER:
SDL_SetError("Error processing the FLAC file [BAD_HEADER].");
May 22, 2013
May 22, 2013
502
break;
Oct 21, 2017
Oct 21, 2017
503
504
case FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH:
SDL_SetError("Error processing the FLAC file [CRC_MISMATCH].");
May 22, 2013
May 22, 2013
505
break;
Oct 21, 2017
Oct 21, 2017
506
507
case FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM:
SDL_SetError("Error processing the FLAC file [UNPARSEABLE].");
May 22, 2013
May 22, 2013
508
break;
Oct 21, 2017
Oct 21, 2017
509
510
default:
SDL_SetError("Error processing the FLAC file [UNKNOWN].");
May 22, 2013
May 22, 2013
511
512
break;
}
513
514
515
}
/* Load an FLAC stream from an SDL_RWops object */
Oct 17, 2017
Oct 17, 2017
516
static void *FLAC_CreateFromRW(SDL_RWops *src, int freesrc)
Oct 3, 2009
Oct 3, 2009
517
{
Oct 21, 2017
Oct 21, 2017
518
FLAC_Music *music;
May 22, 2013
May 22, 2013
519
520
int init_stage = 0;
int was_error = 1;
Dec 19, 2019
Dec 19, 2019
521
FLAC__int64 full_length;
May 22, 2013
May 22, 2013
522
Oct 21, 2017
Oct 21, 2017
523
524
525
music = (FLAC_Music *)SDL_calloc(1, sizeof(*music));
if (!music) {
SDL_OutOfMemory();
May 22, 2013
May 22, 2013
526
527
return NULL;
}
Oct 21, 2017
Oct 21, 2017
528
529
530
531
532
533
music->src = src;
music->volume = MIX_MAX_VOLUME;
music->flac_decoder = flac.FLAC__stream_decoder_new();
if (music->flac_decoder) {
init_stage++; /* stage 1! */
Nov 17, 2019
Nov 17, 2019
534
535
flac.FLAC__stream_decoder_set_metadata_respond(music->flac_decoder,
FLAC__METADATA_TYPE_VORBIS_COMMENT);
Oct 21, 2017
Oct 21, 2017
536
537
538
539
540
541
542
543
544
545
546
547
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 2! */
if (flac.FLAC__stream_decoder_process_until_end_of_metadata(music->flac_decoder)) {
was_error = 0;
May 22, 2013
May 22, 2013
548
} else {
Oct 21, 2017
Oct 21, 2017
549
SDL_SetError("FLAC__stream_decoder_process_until_end_of_metadata() failed");
May 22, 2013
May 22, 2013
550
551
}
} else {
Oct 21, 2017
Oct 21, 2017
552
SDL_SetError("FLAC__stream_decoder_init_stream() failed");
May 22, 2013
May 22, 2013
553
}
Oct 21, 2017
Oct 21, 2017
554
555
556
} else {
SDL_SetError("FLAC__stream_decoder_new() failed");
}
May 22, 2013
May 22, 2013
557
Oct 21, 2017
Oct 21, 2017
558
559
560
if (was_error) {
switch (init_stage) {
case 2:
Dec 18, 2019
Dec 18, 2019
561
flac.FLAC__stream_decoder_finish(music->flac_decoder); /* fallthrough */
Oct 21, 2017
Oct 21, 2017
562
case 1:
Dec 18, 2019
Dec 18, 2019
563
flac.FLAC__stream_decoder_delete(music->flac_decoder); /* fallthrough */
Oct 21, 2017
Oct 21, 2017
564
565
566
case 0:
SDL_free(music);
break;
May 22, 2013
May 22, 2013
567
}
Oct 21, 2017
Oct 21, 2017
568
return NULL;
May 22, 2013
May 22, 2013
569
}
Oct 21, 2017
Oct 21, 2017
570
Nov 17, 2019
Nov 17, 2019
571
572
573
/* loop_start, loop_end and loop_len get set by metadata callback if tags
* are present in metadata.
*/
Dec 19, 2019
Dec 19, 2019
574
575
576
full_length = (FLAC__int64) flac.FLAC__stream_decoder_get_total_samples(music->flac_decoder);
if ((music->loop_end > 0) && (music->loop_end <= full_length) &&
(music->loop_start < music->loop_end)) {
Nov 17, 2019
Nov 17, 2019
577
578
579
music->loop = 1;
}
Dec 17, 2019
Dec 17, 2019
580
music->full_length = full_length;
Oct 21, 2017
Oct 21, 2017
581
music->freesrc = freesrc;
May 22, 2013
May 22, 2013
582
return music;
583
584
}
Oct 17, 2017
Oct 17, 2017
585
586
587
/* Set the volume for an FLAC stream */
static void FLAC_SetVolume(void *context, int volume)
{
Oct 21, 2017
Oct 21, 2017
588
FLAC_Music *music = (FLAC_Music *)context;
Oct 17, 2017
Oct 17, 2017
589
590
591
music->volume = volume;
}
Dec 23, 2019
Dec 23, 2019
592
593
594
595
596
597
598
/* Get the volume for an FLAC stream */
static int FLAC_GetVolume(void *context)
{
FLAC_Music *music = (FLAC_Music *)context;
return music->volume;
}
599
/* Start playback of a given FLAC stream */
Oct 21, 2017
Oct 21, 2017
600
static int FLAC_Play(void *context, int play_count)
Oct 3, 2009
Oct 3, 2009
601
{
Oct 21, 2017
Oct 21, 2017
602
603
604
FLAC_Music *music = (FLAC_Music *)context;
music->play_count = play_count;
return FLAC_Seek(music, 0.0);
605
606
607
}
/* Read some FLAC stream data and convert it for output */
Oct 21, 2017
Oct 21, 2017
608
static int FLAC_GetSome(void *context, void *data, int bytes, SDL_bool *done)
Oct 3, 2009
Oct 3, 2009
609
{
Oct 21, 2017
Oct 21, 2017
610
611
FLAC_Music *music = (FLAC_Music *)context;
int filled;
May 22, 2013
May 22, 2013
612
Oct 21, 2017
Oct 21, 2017
613
614
615
filled = SDL_AudioStreamGet(music->stream, data, bytes);
if (filled != 0) {
return filled;
May 22, 2013
May 22, 2013
616
617
}
Oct 21, 2017
Oct 21, 2017
618
619
620
621
if (!music->play_count) {
/* All done */
*done = SDL_TRUE;
return 0;
May 22, 2013
May 22, 2013
622
}
Oct 21, 2017
Oct 21, 2017
623
624
625
626
if (!flac.FLAC__stream_decoder_process_single(music->flac_decoder)) {
SDL_SetError("FLAC__stream_decoder_process_single() failed");
return -1;
May 22, 2013
May 22, 2013
627
}
Oct 21, 2017
Oct 21, 2017
628
Nov 17, 2019
Nov 17, 2019
629
630
if (music->loop_flag) {
music->pcm_pos = music->loop_start;
Dec 23, 2019
Dec 23, 2019
631
if (flac.FLAC__stream_decoder_seek_absolute(music->flac_decoder, (FLAC__uint64)music->loop_start) ==
Nov 17, 2019
Nov 17, 2019
632
633
634
635
636
FLAC__STREAM_DECODER_SEEK_ERROR) {
SDL_SetError("FLAC__stream_decoder_seek_absolute() failed");
flac.FLAC__stream_decoder_flush(music->flac_decoder);
return -1;
} else {
Nov 26, 2019
Nov 26, 2019
637
638
639
640
641
int play_count = -1;
if (music->play_count > 0) {
play_count = (music->play_count - 1);
}
music->play_count = play_count;
Nov 17, 2019
Nov 17, 2019
642
643
644
645
music->loop_flag = SDL_FALSE;
}
}
Oct 21, 2017
Oct 21, 2017
646
647
648
649
if (flac.FLAC__stream_decoder_get_state(music->flac_decoder) == FLAC__STREAM_DECODER_END_OF_STREAM) {
if (music->play_count == 1) {
music->play_count = 0;
SDL_AudioStreamFlush(music->stream);
Oct 17, 2017
Oct 17, 2017
650
} else {
Oct 21, 2017
Oct 21, 2017
651
652
653
654
655
656
657
int play_count = -1;
if (music->play_count > 0) {
play_count = (music->play_count - 1);
}
if (FLAC_Play(music, play_count) < 0) {
return -1;
}
May 22, 2013
May 22, 2013
658
659
}
}
Oct 21, 2017
Oct 21, 2017
660
return 0;
661
662
663
}
/* Play some of a stream previously started with FLAC_play() */
Oct 17, 2017
Oct 17, 2017
664
static int FLAC_GetAudio(void *context, void *data, int bytes)
Oct 3, 2009
Oct 3, 2009
665
{
Oct 21, 2017
Oct 21, 2017
666
667
FLAC_Music *music = (FLAC_Music *)context;
return music_pcm_getaudio(context, data, bytes, music->volume, FLAC_GetSome);
668
669
}
Oct 17, 2017
Oct 17, 2017
670
671
672
/* Jump (seek) to a given position (position is in seconds) */
static int FLAC_Seek(void *context, double position)
{
Oct 21, 2017
Oct 21, 2017
673
FLAC_Music *music = (FLAC_Music *)context;
Nov 26, 2019
Nov 26, 2019
674
FLAC__uint64 seek_sample = (FLAC__uint64) (music->sample_rate * position);
Oct 17, 2017
Oct 17, 2017
675
Nov 18, 2019
Nov 18, 2019
676
SDL_AudioStreamClear(music->stream);
Dec 19, 2019
Dec 19, 2019
677
music->pcm_pos = (FLAC__int64) seek_sample;
Nov 26, 2019
Nov 26, 2019
678
if (!flac.FLAC__stream_decoder_seek_absolute(music->flac_decoder, seek_sample)) {
Oct 21, 2017
Oct 21, 2017
679
680
if (flac.FLAC__stream_decoder_get_state(music->flac_decoder) == FLAC__STREAM_DECODER_SEEK_ERROR) {
flac.FLAC__stream_decoder_flush(music->flac_decoder);
Oct 17, 2017
Oct 17, 2017
681
682
683
684
685
686
687
688
}
SDL_SetError("Seeking of FLAC stream failed: libFLAC seek failed.");
return -1;
}
return 0;
}
Dec 17, 2019
Dec 17, 2019
689
690
691
692
693
694
695
/* Return music duration in seconds */
static double FLAC_Duration(void *context)
{
FLAC_Music *music = (FLAC_Music *)context;
return (double)music->full_length / music->sample_rate;
}
Oct 21, 2017
Oct 21, 2017
696
/* Close the given FLAC_Music object */
Oct 17, 2017
Oct 17, 2017
697
static void FLAC_Delete(void *context)
Oct 3, 2009
Oct 3, 2009
698
{
Oct 21, 2017
Oct 21, 2017
699
FLAC_Music *music = (FLAC_Music *)context;
May 22, 2013
May 22, 2013
700
701
if (music) {
if (music->flac_decoder) {
Oct 21, 2017
Oct 21, 2017
702
703
flac.FLAC__stream_decoder_finish(music->flac_decoder);
flac.FLAC__stream_decoder_delete(music->flac_decoder);
May 22, 2013
May 22, 2013
704
}
Oct 21, 2017
Oct 21, 2017
705
706
if (music->stream) {
SDL_FreeAudioStream(music->stream);
May 22, 2013
May 22, 2013
707
}
Jun 2, 2013
Jun 2, 2013
708
709
if (music->freesrc) {
SDL_RWclose(music->src);
May 22, 2013
May 22, 2013
710
}
Oct 21, 2017
Oct 21, 2017
711
SDL_free(music);
May 22, 2013
May 22, 2013
712
}
713
714
}
Oct 17, 2017
Oct 17, 2017
715
Mix_MusicInterface Mix_MusicInterface_FLAC =
Oct 3, 2009
Oct 3, 2009
716
{
Oct 17, 2017
Oct 17, 2017
717
718
719
720
721
722
723
724
725
726
727
"FLAC",
MIX_MUSIC_FLAC,
MUS_FLAC,
SDL_FALSE,
SDL_FALSE,
FLAC_Load,
NULL, /* Open */
FLAC_CreateFromRW,
NULL, /* CreateFromFile */
FLAC_SetVolume,
Dec 23, 2019
Dec 23, 2019
728
FLAC_GetVolume,
Oct 17, 2017
Oct 17, 2017
729
FLAC_Play,
Oct 21, 2017
Oct 21, 2017
730
NULL, /* IsPlaying */
Oct 17, 2017
Oct 17, 2017
731
732
FLAC_GetAudio,
FLAC_Seek,
Dec 17, 2019
Dec 17, 2019
733
FLAC_Duration,
Oct 17, 2017
Oct 17, 2017
734
735
NULL, /* Pause */
NULL, /* Resume */
Oct 21, 2017
Oct 21, 2017
736
NULL, /* Stop */
Oct 17, 2017
Oct 17, 2017
737
738
FLAC_Delete,
NULL, /* Close */
Nov 26, 2019
Nov 26, 2019
739
FLAC_Unload
Oct 17, 2017
Oct 17, 2017
740
741
742
743
744
};
#endif /* MUSIC_FLAC */
/* vi: set ts=4 sw=4 expandtab: */