Skip to content

Latest commit

 

History

History
226 lines (186 loc) · 4.86 KB

load_mp3.c

File metadata and controls

226 lines (186 loc) · 4.86 KB
 
1
2
/*
SDL_mixer: An audio mixer library based on the SDL library
Jan 2, 2017
Jan 2, 2017
3
Copyright (C) 1997-2017 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
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 is the source needed to decode an MP3 into a waveform.
*/
/* $Id$ */
Jul 20, 2017
Jul 20, 2017
26
#if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC) || defined(MP3_MPG_MUSIC)
27
28
29
30
31
32
33
34
35
#include "SDL_mixer.h"
#include "load_mp3.h"
#if defined(MP3_MUSIC)
#include "dynamic_mp3.h"
#elif defined(MP3_MAD_MUSIC)
#include "music_mad.h"
Jul 20, 2017
Jul 20, 2017
36
37
#elif defined(MP3_MPG_MUSIC)
#include "music_mpg.h"
38
39
40
41
42
43
44
45
46
47
48
#endif
SDL_AudioSpec *Mix_LoadMP3_RW(SDL_RWops *src, int freesrc, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
{
/* note: spec is initialized to mixer spec */
#if defined(MP3_MUSIC)
SMPEG* mp3;
SMPEG_Info info;
#elif defined(MP3_MAD_MUSIC)
mad_data *mp3_mad;
Jul 20, 2017
Jul 20, 2017
49
50
#elif defined(MP3_MPG_MUSIC)
mpg_data *mp3_mpg;
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
#endif
long samplesize;
int read_len;
const Uint32 chunk_len = 4096;
int err = 0;
if ((!src) || (!spec) || (!audio_buf) || (!audio_len))
{
return NULL;
}
if (!err)
{
*audio_len = 0;
*audio_buf = (Uint8*) SDL_malloc(chunk_len);
err = (*audio_buf == NULL);
}
if (!err)
{
err = ((Mix_Init(MIX_INIT_MP3) & MIX_INIT_MP3) == 0);
}
if (!err)
{
#if defined(MP3_MUSIC)
mp3 = smpeg.SMPEG_new_rwops(src, &info, freesrc, 0);
err = (mp3 == NULL);
#elif defined(MP3_MAD_MUSIC)
mp3_mad = mad_openFileRW(src, spec, freesrc);
err = (mp3_mad == NULL);
Jul 20, 2017
Jul 20, 2017
82
83
84
#elif defined(MP3_MPG_MUSIC)
mp3_mpg = mpg_new_rw(src, spec, freesrc);
err = (mp3_mpg == NULL);
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
#endif
}
#if defined(MP3_MUSIC)
if (!err)
{
err = !info.has_audio;
}
#endif
if (!err)
{
#if defined(MP3_MUSIC)
smpeg.SMPEG_actualSpec(mp3, spec);
smpeg.SMPEG_enableaudio(mp3, 1);
smpeg.SMPEG_enablevideo(mp3, 0);
smpeg.SMPEG_play(mp3);
/* read once for audio length */
while ((read_len = smpeg.SMPEG_playAudio(mp3, *audio_buf, chunk_len)) > 0)
{
*audio_len += read_len;
}
smpeg.SMPEG_stop(mp3);
#elif defined(MP3_MAD_MUSIC)
mad_start(mp3_mad);
/* read once for audio length */
while ((read_len = mad_getSamples(mp3_mad, *audio_buf, chunk_len)) > 0)
{
*audio_len += read_len;
}
mad_stop(mp3_mad);
Jul 20, 2017
Jul 20, 2017
126
127
128
129
130
131
132
133
134
135
136
137
#elif defined(MP3_MPG_MUSIC)
mpg_start(mp3_mpg);
/* read once for audio length */
while ((read_len = mpg_get_samples(mp3_mpg, *audio_buf, chunk_len)) > 0)
{
*audio_len += read_len;
}
mpg_stop(mp3_mpg);
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
#endif
err = (read_len < 0);
}
if (!err)
{
/* reallocate, if needed */
if ((*audio_len > 0) && (*audio_len != chunk_len))
{
*audio_buf = (Uint8*) SDL_realloc(*audio_buf, *audio_len);
err = (*audio_buf == NULL);
}
}
if (!err)
{
/* read again for audio buffer, if needed */
if (*audio_len > chunk_len)
{
#if defined(MP3_MUSIC)
smpeg.SMPEG_rewind(mp3);
smpeg.SMPEG_play(mp3);
err = (*audio_len != smpeg.SMPEG_playAudio(mp3, *audio_buf, *audio_len));
smpeg.SMPEG_stop(mp3);
#elif defined(MP3_MAD_MUSIC)
mad_seek(mp3_mad, 0);
mad_start(mp3_mad);
err = (*audio_len != mad_getSamples(mp3_mad, *audio_buf, *audio_len));
mad_stop(mp3_mad);
Jul 20, 2017
Jul 20, 2017
168
169
170
171
172
#elif defined(MP3_MPG_MUSIC)
mpg_seek(mp3_mpg, 0);
mpg_start(mp3_mpg);
err = (*audio_len != mpg_get_samples(mp3_mpg, *audio_buf, *audio_len));
mpg_stop(mp3_mpg);
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
#endif
}
}
if (!err)
{
/* Don't return a buffer that isn't a multiple of samplesize */
samplesize = ((spec->format & 0xFF)/8)*spec->channels;
*audio_len &= ~(samplesize-1);
}
#if defined(MP3_MUSIC)
if (mp3)
{
smpeg.SMPEG_delete(mp3); mp3 = NULL;
/* Deleting the MP3 closed the source if desired */
freesrc = SDL_FALSE;
}
#elif defined(MP3_MAD_MUSIC)
if (mp3_mad)
{
mad_closeFile(mp3_mad); mp3_mad = NULL;
/* Deleting the MP3 closed the source if desired */
freesrc = SDL_FALSE;
}
Jul 20, 2017
Jul 20, 2017
198
199
200
201
202
203
204
#elif defined(MP3_MPG_MUSIC)
if (mp3_mpg)
{
mpg_delete(mp3_mpg); mp3_mpg = NULL;
/* Deleting the MP3 closed the source if desired */
freesrc = SDL_FALSE;
}
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#endif
if (freesrc)
{
SDL_RWclose(src); src = NULL;
}
/* handle error */
if (err)
{
if (*audio_buf != NULL)
{
SDL_free(*audio_buf); *audio_buf = NULL;
}
*audio_len = 0;
spec = NULL;
}
return spec;
}
#endif