forked from WohlSoft/SDL-Mixer-X
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic_drmp3.c
307 lines (264 loc) · 8.41 KB
/
music_drmp3.c
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
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
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
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
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
/*
SDL_mixer: An audio mixer library based on the SDL library
Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
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 MUSIC_MP3_DRMP3
#include "music_drmp3.h"
#include "mp3utils.h"
#include "SDL.h"
#define DR_MP3_IMPLEMENTATION
#if defined(__GNUC__) && (__GNUC__ >= 4) && \
!(defined(_WIN32) || defined(__EMX__))
#define DRMP3_API __attribute__((visibility("hidden")))
#elif defined(__APPLE__)
#define DRMP3_API __private_extern__
#else
#define DRMP3_API /* just in case.. */
#endif
#define DR_MP3_NO_STDIO
#define DRMP3_ASSERT(expression)
#define DRMP3_COPY_MEMORY(dst, src, sz) SDL_memcpy((dst), (src), (sz))
#define DRMP3_MOVE_MEMORY(dst, src, sz) SDL_memmove((dst), (src), (sz))
#define DRMP3_ZERO_MEMORY(p, sz) SDL_memset((p), 0, (sz))
#define DRMP3_MALLOC(sz) SDL_malloc((sz))
#define DRMP3_REALLOC(p, sz) SDL_realloc((p), (sz))
#define DRMP3_FREE(p) SDL_free((p))
#include "dr_libs/dr_mp3.h"
typedef struct {
struct mp3file_t file;
drmp3 dec;
int play_count;
int freesrc;
int volume;
int status;
SDL_AudioStream *stream;
drmp3_int16 *buffer;
int buffer_size;
int channels;
Mix_MusicMetaTags tags;
} DRMP3_Music;
static size_t DRMP3_ReadCB(void *context, void *buf, size_t size)
{
DRMP3_Music *music = (DRMP3_Music *)context;
return MP3_RWread(&music->file, buf, 1, size);
}
static drmp3_bool32 DRMP3_SeekCB(void *context, int offset, drmp3_seek_origin origin)
{
DRMP3_Music *music = (DRMP3_Music *)context;
int whence = (origin == drmp3_seek_origin_start) ? RW_SEEK_SET : RW_SEEK_CUR;
if (MP3_RWseek(&music->file, offset, whence) < 0) {
return DRMP3_FALSE;
}
return DRMP3_TRUE;
}
static int DRMP3_Seek(void *context, double position);
static void *DRMP3_CreateFromRW(SDL_RWops *src, int freesrc)
{
DRMP3_Music *music;
music = (DRMP3_Music *)SDL_calloc(1, sizeof(DRMP3_Music));
if (!music) {
SDL_OutOfMemory();
return NULL;
}
music->volume = MIX_MAX_VOLUME;
if (MP3_RWinit(&music->file, src) < 0) {
SDL_free(music);
return NULL;
}
meta_tags_init(&music->tags);
if (mp3_read_tags(&music->tags, &music->file, SDL_FALSE) < 0) {
SDL_free(music);
Mix_SetError("music_drmp3: corrupt mp3 file (bad tags).");
return NULL;
}
MP3_RWseek(&music->file, 0, RW_SEEK_SET);
if (!drmp3_init(&music->dec, DRMP3_ReadCB, DRMP3_SeekCB, music, NULL)) {
SDL_free(music);
Mix_SetError("music_drmp3: corrupt mp3 file (bad stream).");
return NULL;
}
music->channels = music->dec.channels;
music->stream = SDL_NewAudioStream(AUDIO_S16SYS,
(Uint8)music->channels,
(int)music->dec.sampleRate,
music_spec.format,
music_spec.channels,
music_spec.freq);
if (!music->stream) {
SDL_OutOfMemory();
drmp3_uninit(&music->dec);
SDL_free(music);
return NULL;
}
music->buffer_size = music_spec.samples * sizeof(drmp3_int16) * music->channels;
music->buffer = (drmp3_int16*)SDL_calloc(1, music->buffer_size);
if (!music->buffer) {
drmp3_uninit(&music->dec);
SDL_OutOfMemory();
SDL_free(music);
return NULL;
}
music->freesrc = freesrc;
return music;
}
static void DRMP3_SetVolume(void *context, int volume)
{
DRMP3_Music *music = (DRMP3_Music *)context;
music->volume = volume;
}
static int DRMP3_GetVolume(void *context)
{
DRMP3_Music *music = (DRMP3_Music *)context;
return music->volume;
}
/* Starts the playback. */
static int DRMP3_Play(void *context, int play_count)
{
DRMP3_Music *music = (DRMP3_Music *)context;
music->play_count = play_count;
return DRMP3_Seek(music, 0.0);
}
static void DRMP3_Stop(void *context)
{
DRMP3_Music *music = (DRMP3_Music *)context;
SDL_AudioStreamClear(music->stream);
}
static int DRMP3_GetSome(void *context, void *data, int bytes, SDL_bool *done)
{
DRMP3_Music *music = (DRMP3_Music *)context;
int filled;
drmp3_uint64 amount;
if (music->stream) {
filled = SDL_AudioStreamGet(music->stream, data, bytes);
if (filled != 0) {
return filled;
}
}
if (!music->play_count) {
/* All done */
*done = SDL_TRUE;
return 0;
}
amount = drmp3_read_pcm_frames_s16(&music->dec, music_spec.samples, music->buffer);
if (amount > 0) {
if (SDL_AudioStreamPut(music->stream, music->buffer, (int)amount * sizeof(drmp3_int16) * music->channels) < 0) {
return -1;
}
} else {
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 (DRMP3_Play(music, play_count) < 0) {
return -1;
}
}
}
return 0;
}
static int DRMP3_GetAudio(void *context, void *data, int bytes)
{
DRMP3_Music *music = (DRMP3_Music *)context;
return music_pcm_getaudio(context, data, bytes, music->volume, DRMP3_GetSome);
}
static int DRMP3_Seek(void *context, double position)
{
DRMP3_Music *music = (DRMP3_Music *)context;
drmp3_uint64 destpos = (drmp3_uint64)(position * music->dec.sampleRate);
drmp3_seek_to_pcm_frame(&music->dec, destpos);
return 0;
}
static double DRMP3_Tell(void *context)
{
DRMP3_Music *music = (DRMP3_Music *)context;
return (double)music->dec.currentPCMFrame / music->dec.sampleRate;
}
static double DRMP3_Duration(void *context)
{
DRMP3_Music *music = (DRMP3_Music *)context;
drmp3_uint64 samples = drmp3_get_pcm_frame_count(&music->dec);
return (double)samples / music->dec.sampleRate;
}
static const char* DRMP3_GetMetaTag(void *context, Mix_MusicMetaTag tag_type)
{
DRMP3_Music *music = (DRMP3_Music *)context;
return meta_tags_get(&music->tags, tag_type);
}
static void DRMP3_Delete(void *context)
{
DRMP3_Music *music = (DRMP3_Music *)context;
drmp3_uninit(&music->dec);
meta_tags_clear(&music->tags);
if (music->stream) {
SDL_FreeAudioStream(music->stream);
}
if (music->buffer) {
SDL_free(music->buffer);
}
if (music->freesrc) {
SDL_RWclose(music->file.src);
}
SDL_free(music);
}
Mix_MusicInterface Mix_MusicInterface_DRMP3 =
{
"DRMP3",
MIX_MUSIC_DRMP3,
MUS_MP3,
SDL_FALSE,
SDL_FALSE,
NULL, /* Load */
NULL, /* Open */
DRMP3_CreateFromRW,
NULL, /* CreateFromRWex [MIXER-X]*/
NULL, /* CreateFromFile */
NULL, /* CreateFromFileEx [MIXER-X]*/
DRMP3_SetVolume,
DRMP3_GetVolume,
DRMP3_Play,
NULL, /* IsPlaying */
DRMP3_GetAudio,
NULL, /* Jump */
DRMP3_Seek,
DRMP3_Tell,
DRMP3_Duration,
NULL, /* SetTempo [MIXER-X] */
NULL, /* GetTempo [MIXER-X] */
NULL, /* SetSpeed [MIXER-X] */
NULL, /* GetSpeed [MIXER-X] */
NULL, /* SetPitch [MIXER-X] */
NULL, /* GetPitch [MIXER-X] */
NULL, /* GetTracksCount [MIXER-X] */
NULL, /* SetTrackMute [MIXER-X] */
NULL, /* LoopStart */
NULL, /* LoopEnd */
NULL, /* LoopLength */
DRMP3_GetMetaTag,
NULL, /* GetNumTracks */
NULL, /* StartTrack */
NULL, /* Pause */
NULL, /* Resume */
DRMP3_Stop,
DRMP3_Delete,
NULL, /* Close */
NULL /* Unload */
};
#endif /* MUSIC_MP3_DRMP3 */
/* vi: set ts=4 sw=4 expandtab: */