Skip to content

Latest commit

 

History

History
236 lines (203 loc) · 6.52 KB

music_smpeg.c

File metadata and controls

236 lines (203 loc) · 6.52 KB
 
1
2
/*
SDL_mixer: An audio mixer library based on the SDL library
Mar 1, 2018
Mar 1, 2018
3
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
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
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_SMPEG
#include "SDL_loadso.h"
#include "music_smpeg.h"
#if SDL_VERSION_ATLEAST(2, 0, 0)
/* Forward declaration for SDL 2.0 because struct is not available there but
still used in a file included with smpeg.h. May not compile if missing. */
typedef struct SDL_Overlay SDL_Overlay;
#endif
#include "smpeg.h"
typedef struct {
int loaded;
void *handle;
void (*SMPEG_actualSpec)(SMPEG *mpeg, SDL_AudioSpec *spec);
void (*SMPEG_delete)(SMPEG* mpeg);
void (*SMPEG_enableaudio)(SMPEG* mpeg, int enable);
void (*SMPEG_enablevideo)(SMPEG* mpeg, int enable);
SMPEG* (*SMPEG_new_rwops)(SDL_RWops *src, SMPEG_Info* info, int freesrc, int sdl_audio);
void (*SMPEG_play)(SMPEG* mpeg);
int (*SMPEG_playAudio)(SMPEG *mpeg, Uint8 *stream, int len);
void (*SMPEG_rewind)(SMPEG* mpeg);
void (*SMPEG_setvolume)(SMPEG* mpeg, int volume);
void (*SMPEG_skip)(SMPEG* mpeg, float seconds);
SMPEGstatus (*SMPEG_status)(SMPEG* mpeg);
void (*SMPEG_stop)(SMPEG* mpeg);
} smpeg_loader;
static smpeg_loader smpeg = {
0, NULL
};
#ifdef SMPEG_DYNAMIC
Oct 21, 2017
Oct 21, 2017
58
59
60
61
62
63
64
#define FUNCTION_LOADER(FUNC, SIG) \
smpeg.FUNC = (SIG) SDL_LoadFunction(smpeg.handle, #FUNC); \
if (smpeg.FUNC == NULL) { SDL_UnloadObject(smpeg.handle); return -1; }
#else
#define FUNCTION_LOADER(FUNC, SIG) \
smpeg.FUNC = FUNC;
#endif
65
66
67
68
static int SMPEG_Load(void)
{
if (smpeg.loaded == 0) {
Oct 21, 2017
Oct 21, 2017
69
#ifdef SMPEG_DYNAMIC
70
71
72
73
smpeg.handle = SDL_LoadObject(SMPEG_DYNAMIC);
if (smpeg.handle == NULL) {
return -1;
}
Oct 21, 2017
Oct 21, 2017
74
#elif defined(__MACOSX__)
75
76
77
78
79
80
81
extern SMPEG* SMPEG_new_rwops(SDL_RWops*, SMPEG_Info*, int, int) __attribute__((weak_import));
if (SMPEG_new_rwops == NULL)
{
/* Missing weakly linked framework */
Mix_SetError("Missing smpeg2.framework");
return -1;
}
Oct 21, 2017
Oct 21, 2017
82
83
84
85
86
87
88
89
90
91
92
93
94
#endif
FUNCTION_LOADER(SMPEG_actualSpec, void (*)(SMPEG *, SDL_AudioSpec *))
FUNCTION_LOADER(SMPEG_delete, void (*)(SMPEG*))
FUNCTION_LOADER(SMPEG_enableaudio, void (*)(SMPEG*, int))
FUNCTION_LOADER(SMPEG_enablevideo, void (*)(SMPEG*, int))
FUNCTION_LOADER(SMPEG_new_rwops, SMPEG* (*)(SDL_RWops *, SMPEG_Info*, int, int))
FUNCTION_LOADER(SMPEG_play, void (*)(SMPEG*))
FUNCTION_LOADER(SMPEG_playAudio, int (*)(SMPEG *, Uint8 *, int))
FUNCTION_LOADER(SMPEG_rewind, void (*)(SMPEG*))
FUNCTION_LOADER(SMPEG_setvolume, void (*)(SMPEG*, int))
FUNCTION_LOADER(SMPEG_skip, void (*)(SMPEG*, float))
FUNCTION_LOADER(SMPEG_status, SMPEGstatus (*)(SMPEG*))
FUNCTION_LOADER(SMPEG_stop, void (*)(SMPEG*))
95
96
97
98
99
100
101
102
103
104
105
106
}
++smpeg.loaded;
return 0;
}
static void SMPEG_Unload(void)
{
if (smpeg.loaded == 0) {
return;
}
if (smpeg.loaded == 1) {
Oct 21, 2017
Oct 21, 2017
107
108
109
#ifdef SMPEG_DYNAMIC
SDL_UnloadObject(smpeg.handle);
#endif
110
111
112
113
114
}
--smpeg.loaded;
}
Oct 21, 2017
Oct 21, 2017
115
116
117
118
119
120
121
typedef struct
{
SMPEG *mp3;
SDL_RWops *src;
int freesrc;
} SMPEG_Music;
122
123
static void *SMPEG_CreateFromRW(SDL_RWops *src, int freesrc)
{
Oct 21, 2017
Oct 21, 2017
124
SMPEG_Music *music;
Oct 21, 2017
Oct 21, 2017
126
127
128
129
130
131
132
133
134
music = (SMPEG_Music *)SDL_calloc(1, sizeof(*music));
if (!music) {
SDL_OutOfMemory();
return NULL;
}
music->src = src;
music->mp3 = smpeg.SMPEG_new_rwops(src, &info, SDL_FALSE, 0);
135
136
if (!info.has_audio) {
Mix_SetError("MPEG file does not have any audio stream.");
Oct 21, 2017
Oct 21, 2017
137
138
smpeg.SMPEG_delete(music->mp3);
SDL_free(music);
139
140
141
return NULL;
}
smpeg.SMPEG_actualSpec(mp3, &music_spec);
Oct 21, 2017
Oct 21, 2017
142
143
144
music->freesrc = freesrc;
return music;
145
146
147
148
}
static void SMPEG_SetVolume(void *context, int volume)
{
Oct 21, 2017
Oct 21, 2017
149
150
SMPEG_Music *music = (SMPEG_Music *)context;
smpeg.SMPEG_setvolume(music->mp3,(int)(((float)volume/(float)MIX_MAX_VOLUME)*100.0));
151
152
153
154
}
static int SMPEG_Play(void *context)
{
Oct 21, 2017
Oct 21, 2017
155
156
157
158
159
SMPEG_Music *music = (SMPEG_Music *)context;
smpeg.SMPEG_enableaudio(music->mp3, 1);
smpeg.SMPEG_enablevideo(music->mp3, 0);
smpeg.SMPEG_rewind(music->mp3);
smpeg.SMPEG_play(music->mp3);
160
161
162
163
164
return 0;
}
static SDL_bool SMPEG_IsPlaying(void *context)
{
Oct 21, 2017
Oct 21, 2017
165
166
SMPEG_Music *music = (SMPEG_Music *)context;
return smpeg.SMPEG_status(music->mp3) == SMPEG_PLAYING ? SDL_TRUE : SDL_FALSE;
167
168
169
170
}
static int SMPEG_GetAudio(void *context, void *data, int bytes)
{
Oct 21, 2017
Oct 21, 2017
171
SMPEG_Music *music = (SMPEG_Music *)context;
172
173
Uint8 *stream = (Uint8 *)data;
int len = bytes;
Oct 21, 2017
Oct 21, 2017
174
175
176
int left = (len - smpeg.SMPEG_playAudio(music->mp3, stream, len));
if (left > 0) {
stream += (len - left);
177
178
179
180
181
return left;
}
static int SMPEG_Seek(void *context, double position)
{
Oct 21, 2017
Oct 21, 2017
182
183
184
SMPEG_Music *music = (SMPEG_Music *)context;
smpeg.SMPEG_rewind(music->mp3);
smpeg.SMPEG_play(music->mp3);
185
if (position > 0.0) {
Oct 21, 2017
Oct 21, 2017
186
smpeg.SMPEG_skip(music->mp3, (float)position);
187
188
189
190
191
192
}
return 0;
}
static void SMPEG_Stop(void *context)
{
Oct 21, 2017
Oct 21, 2017
193
194
SMPEG_Music *music = (SMPEG_Music *)context;
smpeg.SMPEG_stop(music->mp3);
195
196
197
198
}
static void SMPEG_Delete(void *context)
{
Oct 21, 2017
Oct 21, 2017
199
200
201
202
203
204
205
206
SMPEG_Music *music = (SMPEG_Music *)context;
smpeg.SMPEG_delete(music->mp3);
if (music->freesrc) {
SDL_RWclose(music->src);
}
SDL_free(music);
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
}
Mix_MusicInterface Mix_MusicInterface_SMPEG =
{
"SMPEG",
MIX_MUSIC_SMPEG,
MUS_MP3,
SDL_FALSE,
SDL_FALSE,
SMPEG_Load,
NULL, /* Open */
SMPEG_CreateFromRW,
NULL, /* CreateFromFile */
SMPEG_SetVolume,
SMPEG_Play,
SMPEG_IsPlaying,
SMPEG_GetAudio,
SMPEG_Seek,
NULL, /* Pause */
NULL, /* Resume */
SMPEG_Stop,
SMPEG_Delete,
NULL, /* Close */
SMPEG_Unload,
};
#endif /* MUSIC_MP3_SMPEG */
/* vi: set ts=4 sw=4 expandtab: */