Skip to content

Latest commit

 

History

History
516 lines (447 loc) · 15.2 KB

music_mpg123.c

File metadata and controls

516 lines (447 loc) · 15.2 KB
 
Jul 20, 2017
Jul 20, 2017
1
2
/*
SDL_mixer: An audio mixer library based on the SDL library
Jan 17, 2020
Jan 17, 2020
3
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
Jul 20, 2017
Jul 20, 2017
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.
*/
Oct 17, 2017
Oct 17, 2017
22
/* This file supports playing MP3 files with mpg123 */
Jul 20, 2017
Jul 20, 2017
23
Oct 17, 2017
Oct 17, 2017
24
25
#ifdef MUSIC_MP3_MPG123
Nov 17, 2019
Nov 17, 2019
26
#include <stdio.h> /* For SEEK_SET */
Oct 18, 2017
Oct 18, 2017
27
Oct 21, 2017
Oct 21, 2017
28
#include "SDL_assert.h"
Oct 18, 2017
Oct 18, 2017
29
30
#include "SDL_loadso.h"
Oct 17, 2017
Oct 17, 2017
31
#include "music_mpg123.h"
Dec 10, 2019
Dec 10, 2019
32
#include "mp3utils.h"
Oct 17, 2017
Oct 17, 2017
33
34
35
#include <mpg123.h>
Oct 18, 2017
Oct 18, 2017
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
typedef struct {
int loaded;
void *handle;
int (*mpg123_close)(mpg123_handle *mh);
void (*mpg123_delete)(mpg123_handle *mh);
void (*mpg123_exit)(void);
int (*mpg123_format)( mpg123_handle *mh, long rate, int channels, int encodings );
int (*mpg123_format_none)(mpg123_handle *mh);
int (*mpg123_getformat)( mpg123_handle *mh, long *rate, int *channels, int *encoding );
int (*mpg123_init)(void);
mpg123_handle *(*mpg123_new)(const char* decoder, int *error);
int (*mpg123_open_handle)(mpg123_handle *mh, void *iohandle);
const char* (*mpg123_plain_strerror)(int errcode);
Oct 21, 2017
Oct 21, 2017
51
void (*mpg123_rates)(const long **list, size_t *number);
Oct 18, 2017
Oct 18, 2017
52
53
54
int (*mpg123_read)(mpg123_handle *mh, unsigned char *outmemory, size_t outmemsize, size_t *done );
int (*mpg123_replace_reader_handle)( mpg123_handle *mh, ssize_t (*r_read) (void *, void *, size_t), off_t (*r_lseek)(void *, off_t, int), void (*cleanup)(void*) );
off_t (*mpg123_seek)( mpg123_handle *mh, off_t sampleoff, int whence );
Dec 23, 2019
Dec 23, 2019
55
off_t (*mpg123_tell)( mpg123_handle *mh);
Dec 17, 2019
Dec 17, 2019
56
off_t (*mpg123_length)(mpg123_handle *mh);
Oct 18, 2017
Oct 18, 2017
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const char* (*mpg123_strerror)(mpg123_handle *mh);
} mpg123_loader;
static mpg123_loader mpg123 = {
0, NULL
};
#ifdef MPG123_DYNAMIC
#define FUNCTION_LOADER(FUNC, SIG) \
mpg123.FUNC = (SIG) SDL_LoadFunction(mpg123.handle, #FUNC); \
if (mpg123.FUNC == NULL) { SDL_UnloadObject(mpg123.handle); return -1; }
#else
#define FUNCTION_LOADER(FUNC, SIG) \
mpg123.FUNC = FUNC;
#endif
Oct 18, 2017
Oct 18, 2017
73
static int MPG123_Load(void)
Oct 18, 2017
Oct 18, 2017
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
{
if (mpg123.loaded == 0) {
#ifdef MPG123_DYNAMIC
mpg123.handle = SDL_LoadObject(MPG123_DYNAMIC);
if (mpg123.handle == NULL) {
return -1;
}
#elif defined(__MACOSX__)
extern int mpg123_init(void) __attribute__((weak_import));
if (mpg123_init == NULL)
{
/* Missing weakly linked framework */
Mix_SetError("Missing mpg123.framework");
return -1;
}
#endif
FUNCTION_LOADER(mpg123_close, int (*)(mpg123_handle *mh))
FUNCTION_LOADER(mpg123_delete, void (*)(mpg123_handle *mh))
FUNCTION_LOADER(mpg123_exit, void (*)(void))
FUNCTION_LOADER(mpg123_format, int (*)( mpg123_handle *mh, long rate, int channels, int encodings ))
FUNCTION_LOADER(mpg123_format_none, int (*)(mpg123_handle *mh))
FUNCTION_LOADER(mpg123_getformat, int (*)( mpg123_handle *mh, long *rate, int *channels, int *encoding ))
FUNCTION_LOADER(mpg123_init, int (*)(void))
FUNCTION_LOADER(mpg123_new, mpg123_handle *(*)(const char* decoder, int *error))
FUNCTION_LOADER(mpg123_open_handle, int (*)(mpg123_handle *mh, void *iohandle))
FUNCTION_LOADER(mpg123_plain_strerror, const char* (*)(int errcode))
Dec 10, 2019
Dec 10, 2019
100
FUNCTION_LOADER(mpg123_rates, void (*)(const long **list, size_t *number))
Oct 18, 2017
Oct 18, 2017
101
102
103
FUNCTION_LOADER(mpg123_read, int (*)(mpg123_handle *mh, unsigned char *outmemory, size_t outmemsize, size_t *done ))
FUNCTION_LOADER(mpg123_replace_reader_handle, int (*)( mpg123_handle *mh, ssize_t (*r_read) (void *, void *, size_t), off_t (*r_lseek)(void *, off_t, int), void (*cleanup)(void*) ))
FUNCTION_LOADER(mpg123_seek, off_t (*)( mpg123_handle *mh, off_t sampleoff, int whence ))
Dec 23, 2019
Dec 23, 2019
104
FUNCTION_LOADER(mpg123_tell, off_t (*)( mpg123_handle *mh))
Dec 17, 2019
Dec 17, 2019
105
FUNCTION_LOADER(mpg123_length, off_t (*)(mpg123_handle *mh))
Oct 18, 2017
Oct 18, 2017
106
107
108
109
110
111
112
FUNCTION_LOADER(mpg123_strerror, const char* (*)(mpg123_handle *mh))
}
++mpg123.loaded;
return 0;
}
Oct 18, 2017
Oct 18, 2017
113
static void MPG123_Unload(void)
Oct 18, 2017
Oct 18, 2017
114
115
116
117
118
119
120
121
122
123
124
125
126
{
if (mpg123.loaded == 0) {
return;
}
if (mpg123.loaded == 1) {
#ifdef MPG123_DYNAMIC
SDL_UnloadObject(mpg123.handle);
#endif
}
--mpg123.loaded;
}
Oct 17, 2017
Oct 17, 2017
127
128
typedef struct
{
Dec 10, 2019
Dec 10, 2019
129
struct mp3file_t mp3file;
Oct 21, 2017
Oct 21, 2017
130
int play_count;
Oct 17, 2017
Oct 17, 2017
131
132
133
134
int freesrc;
int volume;
mpg123_handle* handle;
Oct 21, 2017
Oct 21, 2017
135
136
137
SDL_AudioStream *stream;
unsigned char *buffer;
size_t buffer_size;
Dec 17, 2019
Dec 17, 2019
138
long sample_rate;
Dec 17, 2019
Dec 17, 2019
139
off_t total_length;
Oct 21, 2017
Oct 21, 2017
140
} MPG123_Music;
Oct 17, 2017
Oct 17, 2017
141
Jul 20, 2017
Jul 20, 2017
142
Oct 21, 2017
Oct 21, 2017
143
144
static int MPG123_Seek(void *context, double secs);
static void MPG123_Delete(void *context);
Jul 20, 2017
Jul 20, 2017
145
Dec 17, 2019
Dec 17, 2019
146
Oct 21, 2017
Oct 21, 2017
147
static int mpg123_format_to_sdl(int fmt)
Jul 20, 2017
Jul 20, 2017
148
149
150
{
switch (fmt)
{
Oct 21, 2017
Oct 21, 2017
151
152
153
154
155
156
157
case MPG123_ENC_SIGNED_8: return AUDIO_S8;
case MPG123_ENC_UNSIGNED_8: return AUDIO_U8;
case MPG123_ENC_SIGNED_16: return AUDIO_S16SYS;
case MPG123_ENC_UNSIGNED_16: return AUDIO_U16SYS;
case MPG123_ENC_SIGNED_32: return AUDIO_S32SYS;
case MPG123_ENC_FLOAT_32: return AUDIO_F32SYS;
default: return -1;
Jul 20, 2017
Jul 20, 2017
158
159
160
}
}
Dec 17, 2019
Dec 17, 2019
161
162
/*#define DEBUG_MPG123*/
#ifdef DEBUG_MPG123
Oct 21, 2017
Oct 21, 2017
163
static const char *mpg123_format_str(int fmt)
Jul 20, 2017
Jul 20, 2017
164
165
166
167
168
169
170
171
172
{
switch (fmt)
{
#define f(x) case x: return #x;
f(MPG123_ENC_UNSIGNED_8)
f(MPG123_ENC_UNSIGNED_16)
f(MPG123_ENC_SIGNED_8)
f(MPG123_ENC_SIGNED_16)
f(MPG123_ENC_SIGNED_32)
Oct 21, 2017
Oct 21, 2017
173
f(MPG123_ENC_FLOAT_32)
Jul 20, 2017
Jul 20, 2017
174
175
176
177
#undef f
}
return "unknown";
}
Dec 17, 2019
Dec 17, 2019
178
#endif
Jul 20, 2017
Jul 20, 2017
179
Oct 21, 2017
Oct 21, 2017
180
static char const* mpg_err(mpg123_handle* mpg, int result)
Jul 20, 2017
Jul 20, 2017
181
182
183
{
char const* err = "unknown error";
Oct 21, 2017
Oct 21, 2017
184
if (mpg && result == MPG123_ERR) {
Oct 18, 2017
Oct 18, 2017
185
err = mpg123.mpg123_strerror(mpg);
Jul 20, 2017
Jul 20, 2017
186
} else {
Oct 21, 2017
Oct 21, 2017
187
err = mpg123.mpg123_plain_strerror(result);
Jul 20, 2017
Jul 20, 2017
188
189
190
191
192
}
return err;
}
/* we're gonna override mpg123's I/O with these wrappers for RWops */
Oct 21, 2017
Oct 21, 2017
193
194
static ssize_t rwops_read(void* p, void* dst, size_t n)
{
Dec 10, 2019
Dec 10, 2019
195
return (ssize_t)MP3_RWread((struct mp3file_t *)p, dst, 1, n);
Jul 20, 2017
Jul 20, 2017
196
197
}
Oct 21, 2017
Oct 21, 2017
198
199
static off_t rwops_seek(void* p, off_t offset, int whence)
{
Dec 10, 2019
Dec 10, 2019
200
return (off_t)MP3_RWseek((struct mp3file_t *)p, (Sint64)offset, whence);
Jul 20, 2017
Jul 20, 2017
201
202
}
Oct 21, 2017
Oct 21, 2017
203
204
static void rwops_cleanup(void* p)
{
Jul 20, 2017
Jul 20, 2017
205
206
207
208
(void)p;
/* do nothing, we will free the file later */
}
Oct 17, 2017
Oct 17, 2017
209
210
211
static int MPG123_Open(const SDL_AudioSpec *spec)
{
Nov 17, 2019
Nov 17, 2019
212
(void)spec;
Oct 18, 2017
Oct 18, 2017
213
if (mpg123.mpg123_init() != MPG123_OK) {
Oct 17, 2017
Oct 17, 2017
214
215
216
217
218
219
220
Mix_SetError("mpg123_init() failed");
return -1;
}
return 0;
}
static void *MPG123_CreateFromRW(SDL_RWops *src, int freesrc)
Jul 20, 2017
Jul 20, 2017
221
{
Oct 21, 2017
Oct 21, 2017
222
MPG123_Music *music;
Dec 17, 2019
Dec 17, 2019
223
224
int result, format, channels, encoding;
long rate;
Oct 21, 2017
Oct 21, 2017
225
226
const long *rates;
size_t i, num_rates;
Jul 20, 2017
Jul 20, 2017
227
Oct 21, 2017
Oct 21, 2017
228
229
230
231
music = (MPG123_Music*)SDL_calloc(1, sizeof(*music));
if (!music) {
return NULL;
}
Dec 10, 2019
Dec 10, 2019
232
music->mp3file.src = src;
Oct 21, 2017
Oct 21, 2017
233
234
music->volume = MIX_MAX_VOLUME;
Dec 10, 2019
Dec 10, 2019
235
music->mp3file.length = SDL_RWsize(src);
Dec 20, 2019
Dec 20, 2019
236
if (mp3_skiptags(&music->mp3file, SDL_TRUE) < 0) {
Dec 10, 2019
Dec 10, 2019
237
238
239
240
241
SDL_free(music);
Mix_SetError("music_mpg123: corrupt mp3 file (bad tags.)");
return NULL;
}
Oct 21, 2017
Oct 21, 2017
242
243
244
245
246
247
248
/* Just assume 16-bit 2 channel audio for now */
music->buffer_size = music_spec.samples * sizeof(Sint16) * 2;
music->buffer = (unsigned char *)SDL_malloc(music->buffer_size);
if (!music->buffer) {
MPG123_Delete(music);
SDL_OutOfMemory();
return NULL;
Jul 20, 2017
Jul 20, 2017
249
250
}
Oct 21, 2017
Oct 21, 2017
251
music->handle = mpg123.mpg123_new(0, &result);
Jul 20, 2017
Jul 20, 2017
252
if (result != MPG123_OK) {
Oct 21, 2017
Oct 21, 2017
253
254
255
MPG123_Delete(music);
Mix_SetError("mpg123_new failed");
return NULL;
Jul 20, 2017
Jul 20, 2017
256
257
}
Oct 18, 2017
Oct 18, 2017
258
result = mpg123.mpg123_replace_reader_handle(
Oct 21, 2017
Oct 21, 2017
259
music->handle,
Jul 20, 2017
Jul 20, 2017
260
rwops_read, rwops_seek, rwops_cleanup
Oct 21, 2017
Oct 21, 2017
261
);
Jul 20, 2017
Jul 20, 2017
262
if (result != MPG123_OK) {
Oct 21, 2017
Oct 21, 2017
263
264
265
MPG123_Delete(music);
Mix_SetError("mpg123_replace_reader_handle: %s", mpg_err(music->handle, result));
return NULL;
Jul 20, 2017
Jul 20, 2017
266
267
}
Oct 21, 2017
Oct 21, 2017
268
result = mpg123.mpg123_format_none(music->handle);
Jul 20, 2017
Jul 20, 2017
269
if (result != MPG123_OK) {
Oct 21, 2017
Oct 21, 2017
270
271
272
MPG123_Delete(music);
Mix_SetError("mpg123_format_none: %s", mpg_err(music->handle, result));
return NULL;
Jul 20, 2017
Jul 20, 2017
273
274
}
Oct 21, 2017
Oct 21, 2017
275
276
277
278
279
280
281
282
283
284
285
mpg123.mpg123_rates(&rates, &num_rates);
for (i = 0; i < num_rates; ++i) {
const int channels = (MPG123_MONO|MPG123_STEREO);
const int formats = (MPG123_ENC_SIGNED_8 |
MPG123_ENC_UNSIGNED_8 |
MPG123_ENC_SIGNED_16 |
MPG123_ENC_UNSIGNED_16 |
MPG123_ENC_SIGNED_32 |
MPG123_ENC_FLOAT_32);
mpg123.mpg123_format(music->handle, rates[i], channels, formats);
Jul 20, 2017
Jul 20, 2017
286
287
}
Dec 10, 2019
Dec 10, 2019
288
result = mpg123.mpg123_open_handle(music->handle, &music->mp3file);
Jul 20, 2017
Jul 20, 2017
289
if (result != MPG123_OK) {
Oct 21, 2017
Oct 21, 2017
290
291
292
MPG123_Delete(music);
Mix_SetError("mpg123_open_handle: %s", mpg_err(music->handle, result));
return NULL;
Jul 20, 2017
Jul 20, 2017
293
294
}
Dec 17, 2019
Dec 17, 2019
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
result = mpg123.mpg123_getformat(music->handle, &rate, &channels, &encoding);
if (result != MPG123_OK) {
MPG123_Delete(music);
Mix_SetError("mpg123_getformat: %s", mpg_err(music->handle, result));
return NULL;
}
#ifdef DEBUG_MPG123
printf("MPG123 format: %s, channels: %d, rate: %ld\n",
mpg123_format_str(encoding), channels, rate);
#endif
format = mpg123_format_to_sdl(encoding);
SDL_assert(format != -1);
music->sample_rate = rate;
music->stream = SDL_NewAudioStream((SDL_AudioFormat)format, (Uint8)channels, (int)rate,
music_spec.format, music_spec.channels, music_spec.freq);
if (!music->stream) {
MPG123_Delete(music);
return NULL;
}
Dec 17, 2019
Dec 17, 2019
317
318
music->total_length = mpg123.mpg123_length(music->handle);
Oct 21, 2017
Oct 21, 2017
319
320
music->freesrc = freesrc;
return music;
Jul 20, 2017
Jul 20, 2017
321
322
}
Oct 21, 2017
Oct 21, 2017
323
static void MPG123_SetVolume(void *context, int volume)
Oct 17, 2017
Oct 17, 2017
324
{
Oct 21, 2017
Oct 21, 2017
325
326
MPG123_Music *music = (MPG123_Music *)context;
music->volume = volume;
Jul 20, 2017
Jul 20, 2017
327
328
}
Dec 23, 2019
Dec 23, 2019
329
330
331
332
333
334
static int MPG123_GetVolume(void *context)
{
MPG123_Music *music = (MPG123_Music *)context;
return music->volume;
}
Oct 21, 2017
Oct 21, 2017
335
static int MPG123_Play(void *context, int play_count)
Oct 17, 2017
Oct 17, 2017
336
{
Oct 21, 2017
Oct 21, 2017
337
338
339
MPG123_Music *music = (MPG123_Music *)context;
music->play_count = play_count;
return MPG123_Seek(music, 0.0);
Jul 20, 2017
Jul 20, 2017
340
341
}
Oct 21, 2017
Oct 21, 2017
342
343
/* read some mp3 stream data and convert it for output */
static int MPG123_GetSome(void *context, void *data, int bytes, SDL_bool *done)
Jul 20, 2017
Jul 20, 2017
344
{
Oct 21, 2017
Oct 21, 2017
345
346
347
MPG123_Music *music = (MPG123_Music *)context;
int filled, result;
size_t amount;
Jul 20, 2017
Jul 20, 2017
348
long rate;
Oct 21, 2017
Oct 21, 2017
349
int channels, encoding, format;
Jul 20, 2017
Jul 20, 2017
350
Oct 21, 2017
Oct 21, 2017
351
352
353
354
355
if (music->stream) {
filled = SDL_AudioStreamGet(music->stream, data, bytes);
if (filled != 0) {
return filled;
}
Jul 20, 2017
Jul 20, 2017
356
357
}
Oct 21, 2017
Oct 21, 2017
358
359
360
if (!music->play_count) {
/* All done */
*done = SDL_TRUE;
Jul 20, 2017
Jul 20, 2017
361
362
363
return 0;
}
Oct 21, 2017
Oct 21, 2017
364
365
366
367
368
result = mpg123.mpg123_read(music->handle, music->buffer, music->buffer_size, &amount);
switch (result) {
case MPG123_OK:
if (SDL_AudioStreamPut(music->stream, music->buffer, (int)amount) < 0) {
return -1;
Jul 20, 2017
Jul 20, 2017
369
}
Oct 21, 2017
Oct 21, 2017
370
break;
Jul 20, 2017
Jul 20, 2017
371
Oct 21, 2017
Oct 21, 2017
372
373
374
375
376
case MPG123_NEW_FORMAT:
result = mpg123.mpg123_getformat(music->handle, &rate, &channels, &encoding);
if (result != MPG123_OK) {
Mix_SetError("mpg123_getformat: %s", mpg_err(music->handle, result));
return -1;
Jul 20, 2017
Jul 20, 2017
377
}
Dec 17, 2019
Dec 17, 2019
378
379
380
381
#ifdef DEBUG_MPG123
printf("MPG123 format: %s, channels: %d, rate: %ld\n",
mpg123_format_str(encoding), channels, rate);
#endif
Jul 20, 2017
Jul 20, 2017
382
Oct 21, 2017
Oct 21, 2017
383
384
format = mpg123_format_to_sdl(encoding);
SDL_assert(format != -1);
Jul 20, 2017
Jul 20, 2017
385
Dec 22, 2019
Dec 22, 2019
386
387
388
if (music->stream) {
SDL_FreeAudioStream(music->stream);
}
Nov 17, 2019
Nov 17, 2019
389
music->stream = SDL_NewAudioStream((SDL_AudioFormat)format, (Uint8)channels, (int)rate,
Oct 21, 2017
Oct 21, 2017
390
391
392
music_spec.format, music_spec.channels, music_spec.freq);
if (!music->stream) {
return -1;
Jul 20, 2017
Jul 20, 2017
393
}
Dec 17, 2019
Dec 17, 2019
394
music->sample_rate = rate;
Oct 21, 2017
Oct 21, 2017
395
396
397
398
399
400
401
402
403
404
405
406
407
408
break;
case MPG123_DONE:
if (music->play_count == 1) {
music->play_count = 0;
SDL_AudioStreamFlush(music->stream);
} else {
int play_count = -1;
if (music->play_count > 0) {
play_count = (music->play_count - 1);
}
if (MPG123_Play(music, play_count) < 0) {
return -1;
}
Jul 20, 2017
Jul 20, 2017
409
}
Oct 21, 2017
Oct 21, 2017
410
411
412
413
break;
default:
Mix_SetError("mpg123_read: %s", mpg_err(music->handle, result));
return -1;
Jul 20, 2017
Jul 20, 2017
414
}
Oct 21, 2017
Oct 21, 2017
415
416
417
418
419
420
return 0;
}
static int MPG123_GetAudio(void *context, void *data, int bytes)
{
MPG123_Music *music = (MPG123_Music *)context;
return music_pcm_getaudio(context, data, bytes, music->volume, MPG123_GetSome);
Jul 20, 2017
Jul 20, 2017
421
422
}
Oct 17, 2017
Oct 17, 2017
423
static int MPG123_Seek(void *context, double secs)
Jul 20, 2017
Jul 20, 2017
424
{
Oct 21, 2017
Oct 21, 2017
425
MPG123_Music *music = (MPG123_Music *)context;
Dec 17, 2019
Dec 17, 2019
426
off_t offset = (off_t)(music->sample_rate * secs);
Jul 20, 2017
Jul 20, 2017
427
Oct 21, 2017
Oct 21, 2017
428
429
if ((offset = mpg123.mpg123_seek(music->handle, offset, SEEK_SET)) < 0) {
return Mix_SetError("mpg123_seek: %s", mpg_err(music->handle, (int)-offset));
Jul 20, 2017
Jul 20, 2017
430
}
Oct 17, 2017
Oct 17, 2017
431
return 0;
Jul 20, 2017
Jul 20, 2017
432
433
}
Dec 23, 2019
Dec 23, 2019
434
435
436
437
438
439
440
441
442
443
444
445
446
static double MPG123_Tell(void *context)
{
MPG123_Music *music = (MPG123_Music *)context;
off_t offset = 0;
if (!music->sample_rate) {
return 0.0;
}
if ((offset = mpg123.mpg123_tell(music->handle)) < 0) {
return Mix_SetError("mpg123_tell: %s", mpg_err(music->handle, (int)-offset));
}
return (double)offset / music->sample_rate;
}
Dec 17, 2019
Dec 17, 2019
447
448
449
450
451
452
453
454
455
456
/* Return music duration in seconds */
static double MPG123_Duration(void *context)
{
MPG123_Music *music = (MPG123_Music *)context;
if (music->total_length < 0) {
return -1.0;
}
return (double)music->total_length / music->sample_rate;
}
Oct 17, 2017
Oct 17, 2017
457
458
static void MPG123_Delete(void *context)
{
Oct 21, 2017
Oct 21, 2017
459
MPG123_Music *music = (MPG123_Music *)context;
Jul 20, 2017
Jul 20, 2017
460
Oct 21, 2017
Oct 21, 2017
461
462
463
if (music->handle) {
mpg123.mpg123_close(music->handle);
mpg123.mpg123_delete(music->handle);
Oct 17, 2017
Oct 17, 2017
464
}
Oct 21, 2017
Oct 21, 2017
465
466
if (music->stream) {
SDL_FreeAudioStream(music->stream);
Oct 17, 2017
Oct 17, 2017
467
}
Oct 21, 2017
Oct 21, 2017
468
469
470
471
if (music->buffer) {
SDL_free(music->buffer);
}
if (music->freesrc) {
Dec 10, 2019
Dec 10, 2019
472
SDL_RWclose(music->mp3file.src);
Oct 21, 2017
Oct 21, 2017
473
474
}
SDL_free(music);
Oct 17, 2017
Oct 17, 2017
475
476
477
478
}
static void MPG123_Close(void)
{
Oct 18, 2017
Oct 18, 2017
479
mpg123.mpg123_exit();
Oct 17, 2017
Oct 17, 2017
480
481
482
483
484
485
486
487
488
489
}
Mix_MusicInterface Mix_MusicInterface_MPG123 =
{
"MPG123",
MIX_MUSIC_MPG123,
MUS_MP3,
SDL_FALSE,
SDL_FALSE,
Oct 18, 2017
Oct 18, 2017
490
MPG123_Load,
Oct 17, 2017
Oct 17, 2017
491
492
493
494
MPG123_Open,
MPG123_CreateFromRW,
NULL, /* CreateFromFile */
MPG123_SetVolume,
Dec 23, 2019
Dec 23, 2019
495
MPG123_GetVolume,
Oct 17, 2017
Oct 17, 2017
496
MPG123_Play,
Oct 21, 2017
Oct 21, 2017
497
NULL, /* IsPlaying */
Oct 17, 2017
Oct 17, 2017
498
499
MPG123_GetAudio,
MPG123_Seek,
Dec 23, 2019
Dec 23, 2019
500
MPG123_Tell,
Dec 17, 2019
Dec 17, 2019
501
MPG123_Duration,
Dec 23, 2019
Dec 23, 2019
502
503
504
NULL, /* LoopStart */
NULL, /* LoopEnd */
NULL, /* LoopLength */
Dec 23, 2019
Dec 23, 2019
505
NULL, /* GetMetaTag */
Oct 17, 2017
Oct 17, 2017
506
507
NULL, /* Pause */
NULL, /* Resume */
Oct 21, 2017
Oct 21, 2017
508
NULL, /* Stop */
Oct 17, 2017
Oct 17, 2017
509
510
MPG123_Delete,
MPG123_Close,
Oct 18, 2017
Oct 18, 2017
511
MPG123_Unload
Oct 17, 2017
Oct 17, 2017
512
513
514
515
516
};
#endif /* MUSIC_MP3_MPG123 */
/* vi: set ts=4 sw=4 expandtab: */