Skip to content

Latest commit

 

History

History
398 lines (328 loc) · 8.29 KB

music_mpg.c

File metadata and controls

398 lines (328 loc) · 8.29 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
SDL_mixer: An audio mixer library based on the SDL library
Copyright (C) 1997-2017 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.
*/
#ifdef MP3_MUSIC
#include "SDL_mixer.h"
#include "music_mpg.h"
#include "dynamic_mp3.h"
static int
snd_format_to_mpg123(uint16_t sdl_fmt)
{
switch (sdl_fmt)
{
case AUDIO_U8: return MPG123_ENC_UNSIGNED_8;
case AUDIO_U16SYS: return MPG123_ENC_UNSIGNED_16;
case AUDIO_S8: return MPG123_ENC_SIGNED_8;
case AUDIO_S16SYS: return MPG123_ENC_SIGNED_16;
}
return -1;
}
static Uint16
mpg123_format_to_sdl(int fmt)
{
switch (fmt)
{
case MPG123_ENC_UNSIGNED_8: return AUDIO_U8;
case MPG123_ENC_UNSIGNED_16: return AUDIO_U16SYS;
case MPG123_ENC_SIGNED_8: return AUDIO_S8;
case MPG123_ENC_SIGNED_16: return AUDIO_S16SYS;
}
return -1;
}
static char const*
mpg123_format_str(int fmt)
{
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)
#undef f
}
return "unknown";
}
static char const*
mpg_err(mpg123_handle* mpg, int code)
{
char const* err = "unknown error";
if (mpg && code == MPG123_ERR) {
err = mpg123.mpg123_strerror(mpg);
} else {
err = mpg123.mpg123_plain_strerror(code);
}
return err;
}
/* we're gonna override mpg123's I/O with these wrappers for RWops */
static
ssize_t rwops_read(void* p, void* dst, size_t n) {
Dec 14, 2019
Dec 14, 2019
86
return (ssize_t)MP3_RWread((struct mp3file_t *)p, dst, 1, n);
87
88
89
90
}
static
off_t rwops_seek(void* p, off_t offset, int whence) {
Dec 14, 2019
Dec 14, 2019
91
return (off_t)MP3_RWseek((struct mp3file_t *)p, (int)offset, whence);
92
93
94
95
96
97
98
99
100
101
102
103
104
105
}
static
void rwops_cleanup(void* p) {
(void)p;
/* do nothing, we will free the file later */
}
static int getsome(mpg_data* m);
mpg_data*
mpg_new_rw(SDL_RWops *src, SDL_AudioSpec* mixer, int freesrc)
{
int fmt, result;
Dec 14, 2019
Dec 14, 2019
106
int pos;
107
108
109
110
111
112
113
114
115
116
117
118
119
120
mpg_data* m = NULL;
if (!Mix_Init(MIX_INIT_MP3)) {
goto fail;
}
m = (mpg_data*)SDL_malloc(sizeof(mpg_data));
if (!m) {
SDL_OutOfMemory();
goto fail;
}
SDL_memset(m, 0, sizeof(mpg_data));
Dec 14, 2019
Dec 14, 2019
121
m->mp3file.rw = src;
122
123
m->freesrc = freesrc;
Dec 14, 2019
Dec 14, 2019
124
125
126
127
128
129
130
131
pos = SDL_RWtell(src);
m->mp3file.length = SDL_RWseek(src, 0, RW_SEEK_END);
SDL_RWseek(src, pos, RW_SEEK_SET);
if (mp3_skiptags(&m->mp3file) < 0) {
Mix_SetError("music_mpg: corrupt mp3 file (bad tags.)");
goto fail;
}
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
m->handle = mpg123.mpg123_new(0, &result);
if (result != MPG123_OK) {
goto fail;
}
result = mpg123.mpg123_replace_reader_handle(
m->handle,
rwops_read, rwops_seek, rwops_cleanup
);
if (result != MPG123_OK) {
goto fail;
}
result = mpg123.mpg123_format_none(m->handle);
if (result != MPG123_OK) {
goto fail;
}
fmt = snd_format_to_mpg123(mixer->format);
if (fmt == -1) {
goto fail;
}
result = mpg123.mpg123_format(m->handle, mixer->freq, mixer->channels, fmt);
if (result != MPG123_OK) {
goto fail;
}
Dec 14, 2019
Dec 14, 2019
160
result = mpg123.mpg123_open_handle(m->handle, &m->mp3file);
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
if (result != MPG123_OK) {
goto fail;
}
m->volume = MIX_MAX_VOLUME;
m->mixer = *mixer;
/* hacky: read until we can figure out the format then rewind */
while (!m->gotformat)
{
if (!getsome(m)) {
goto fail;
}
}
/* rewind */
mpg123.mpg123_seek(m->handle, 0, SEEK_SET);
m->len_available = 0;
m->snd_available = m->cvt.buf;
return m;
fail:
if (!m && freesrc) {
SDL_RWclose(src);
}
mpg_delete(m);
return NULL;
}
void
mpg_delete(mpg_data* m)
{
if (!m) {
return;
}
if (m->freesrc) {
Dec 14, 2019
Dec 14, 2019
200
SDL_RWclose(m->mp3file.rw);
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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
}
if (m->cvt.buf) {
SDL_free(m->cvt.buf);
}
mpg123.mpg123_close(m->handle);
mpg123.mpg123_delete(m->handle);
SDL_free(m);
}
void
mpg_start(mpg_data* m) {
m->playing = 1;
}
void
mpg_stop(mpg_data* m) {
m->playing = 0;
}
int
mpg_playing(mpg_data* m) {
return m->playing;
}
/*
updates the convert struct and buffer to match the format queried from
mpg123.
*/
static int
update_format(mpg_data* m)
{
int code;
long rate;
int channels, encoding;
Uint16 sdlfmt;
size_t bufsize;
m->gotformat = 1;
code =
mpg123.mpg123_getformat(
m->handle,
&rate, &channels, &encoding
);
if (code != MPG123_OK) {
Mix_SetError("mpg123_getformat: %s", mpg_err(m->handle, code));
return 0;
}
sdlfmt = mpg123_format_to_sdl(encoding);
if (sdlfmt == (Uint16)-1)
{
Mix_SetError(
"Format %s is not supported by SDL",
mpg123_format_str(encoding)
);
return 0;
}
SDL_BuildAudioCVT(
&m->cvt,
sdlfmt, channels, rate,
m->mixer.format,
m->mixer.channels,
m->mixer.freq
);
if (m->cvt.buf) {
SDL_free(m->cvt.buf);
}
bufsize = sizeof(m->buf) * m->cvt.len_mult;
m->cvt.buf = SDL_malloc(bufsize);
if (!m->cvt.buf)
{
Mix_SetError("Out of memory");
mpg_stop(m);
return 0;
}
return 1;
}
/* read some mp3 stream data and convert it for output */
static int
getsome(mpg_data* m)
{
int code;
size_t len = 0;
Uint8* data = m->buf;
const size_t cbdata = sizeof(m->buf);
SDL_AudioCVT* cvt = &m->cvt;
code = mpg123.mpg123_read(m->handle, data, cbdata, &len);
switch (code)
{
case MPG123_NEW_FORMAT:
if (!update_format(m)) {
return 0;
}
break;
case MPG123_DONE:
mpg_stop(m);
case MPG123_OK:
break;
default:
Mix_SetError("mpg123_read: %s", mpg_err(m->handle, code));
return 0;
}
Dec 14, 2019
Dec 14, 2019
317
318
319
320
321
322
if (!cvt->buf) {
/* not received MPG123_NEW_FORMAT... */
Mix_SetError("music_mpg: No format.");
return 0;
}
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
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
SDL_memcpy(cvt->buf, data, cbdata);
if (cvt->needed) {
/* we need to convert the audio to SDL's format */
cvt->len = len;
SDL_ConvertAudio(cvt);
}
else {
/* no conversion needed, just copy */
cvt->len_cvt = len;
}
m->len_available = cvt->len_cvt;
m->snd_available = cvt->buf;
return 1;
}
int
mpg_get_samples(mpg_data* m, Uint8 *stream, int len)
{
int mixable;
while (len > 0 && m->playing)
{
if (!m->len_available)
{
if (!getsome(m)) {
m->playing = 0;
return len;
}
}
mixable = len;
if (mixable > m->len_available) {
mixable = m->len_available;
}
if (m->volume == MIX_MAX_VOLUME) {
SDL_memcpy(stream, m->snd_available, mixable);
}
else {
SDL_MixAudio(
stream,
m->snd_available,
mixable,
m->volume
);
}
m->len_available -= mixable;
m->snd_available += mixable;
len -= mixable;
stream += mixable;
}
return len;
}
void
mpg_seek(mpg_data* m, double secs)
{
off_t offset = m->mixer.freq * secs;
if ((offset = mpg123.mpg123_seek(m->handle, offset, SEEK_SET)) < 0) {
Mix_SetError("mpg123_seek: %s", mpg_err(m->handle, -offset));
}
}
void
mpg_volume(mpg_data* m, int volume) {
m->volume = volume;
}
#endif /* MP3_MUSIC */