Mercurial > SDL_sound_CoreAudio
annotate decoders/mp3.c @ 76:73484e54d05e
Shouldn't compile skeleton.c.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Tue, 25 Sep 2001 16:49:40 +0000 |
parents | 40006625142a |
children | 25e515dfa18d |
rev | line source |
---|---|
14 | 1 /* |
2 * SDL_sound -- An abstract sound format decoding API. | |
3 * Copyright (C) 2001 Ryan C. Gordon. | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2.1 of the License, or (at your option) any later version. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Lesser General Public | |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 */ | |
19 | |
20 /* | |
21 * MPEG-1 Layer 3, or simply, "MP3", decoder for SDL_sound. | |
22 * | |
23 * This driver handles all those highly compressed songs you stole through | |
24 * Napster. :) It depends on the SMPEG library for decoding, which can | |
25 * be grabbed from: http://www.lokigames.com/development/smpeg.php3 | |
26 * | |
27 * Please see the file LICENSE in the source's root directory. | |
28 * | |
29 * This file written by Ryan C. Gordon. (icculus@clutteredmind.org) | |
30 */ | |
31 | |
64
40006625142a
Changes in preparation of autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents:
62
diff
changeset
|
32 #ifdef SOUND_SUPPORTS_MP3 |
40006625142a
Changes in preparation of autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents:
62
diff
changeset
|
33 |
14 | 34 #include <stdio.h> |
35 #include <stdlib.h> | |
36 #include <string.h> | |
37 #include <assert.h> | |
38 | |
39 #include "smpeg.h" | |
40 #include "SDL_sound.h" | |
41 #include "extra_rwops.h" | |
42 | |
43 #define __SDL_SOUND_INTERNAL__ | |
44 #include "SDL_sound_internal.h" | |
45 | |
46 | |
46
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
47 static int MP3_init(void); |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
48 static void MP3_quit(void); |
14 | 49 static int MP3_open(Sound_Sample *sample, const char *ext); |
50 static void MP3_close(Sound_Sample *sample); | |
51 static Uint32 MP3_read(Sound_Sample *sample); | |
52 | |
53 const Sound_DecoderFunctions __Sound_DecoderFunctions_MP3 = | |
54 { | |
55 { | |
56 "MP3", | |
57 "MPEG-1 Layer 3 audio through SMPEG", | |
58 "Ryan C. Gordon <icculus@clutteredmind.org>", | |
59 "http://www.icculus.org/SDL_sound/" | |
60 }, | |
61 | |
46
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
62 MP3_init, /* init() method */ |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
63 MP3_quit, /* quit() method */ |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
64 MP3_open, /* open() method */ |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
65 MP3_close, /* close() method */ |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
66 MP3_read /* read() method */ |
14 | 67 }; |
68 | |
46
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
69 |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
70 static int MP3_init(void) |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
71 { |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
72 return(1); /* always succeeds. */ |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
73 } /* MP3_init */ |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
74 |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
75 |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
76 static void MP3_quit(void) |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
77 { |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
78 /* it's a no-op. */ |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
79 } /* MP3_quit */ |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
80 |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
81 |
14 | 82 static __inline__ void output_version(void) |
83 { | |
84 static int first_time = 1; | |
85 | |
86 if (first_time) | |
87 { | |
88 SMPEG_version v; | |
89 SMPEG_VERSION(&v); | |
62
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
90 SNDDBG(("MP3: Compiled against SMPEG v%d.%d.%d.\n", |
14 | 91 v.major, v.minor, v.patch)); |
92 first_time = 0; | |
93 } /* if */ | |
94 } /* output_version */ | |
95 | |
96 | |
97 static int MP3_open(Sound_Sample *sample, const char *ext) | |
98 { | |
99 SMPEG *smpeg; | |
100 SMPEG_Info smpeg_info; | |
101 SDL_AudioSpec spec; | |
102 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
103 SDL_RWops *refCounter; | |
46
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
104 Uint8 mp3_magic[2]; |
14 | 105 |
106 output_version(); | |
107 | |
46
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
108 /* |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
109 * SMPEG appears to be far too greedy about what it accepts as input. |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
110 * This test was adapted from SDL_mixer. |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
111 */ |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
112 if (SDL_RWread(internal->rw, mp3_magic, sizeof (mp3_magic), 1) != 1) |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
113 { |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
114 Sound_SetError("MP3: Could not read MP3 magic."); |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
115 return(0); |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
116 } |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
117 if (mp3_magic[0] != 0xFF || (mp3_magic[1] & 0xF0) != 0xF0) |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
118 { |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
119 Sound_SetError("MP3: Not an MP3 stream."); |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
120 return(0); |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
121 } |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
122 SDL_RWseek(internal->rw, -sizeof (mp3_magic), SEEK_CUR); |
6e13fcc178da
No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
28
diff
changeset
|
123 |
14 | 124 refCounter = RWops_RWRefCounter_new(internal->rw); |
125 if (refCounter == NULL) | |
126 { | |
62
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
127 SNDDBG(("MP3: Failed to create reference counting RWops.\n")); |
14 | 128 return(0); |
129 } /* if */ | |
130 | |
131 /* replace original RWops. This is safe. Honest. :) */ | |
132 internal->rw = refCounter; | |
133 | |
134 /* | |
135 * increment the refcount, since SMPEG will nuke the RWops if it can't | |
136 * accept the contained data... | |
137 */ | |
138 RWops_RWRefCounter_addRef(refCounter); | |
139 smpeg = SMPEG_new_rwops(refCounter, &smpeg_info, 0); | |
140 | |
141 if (SMPEG_error(smpeg)) | |
142 { | |
143 Sound_SetError(SMPEG_error(smpeg)); | |
144 SMPEG_delete(smpeg); | |
145 return(0); | |
146 } /* if */ | |
147 | |
148 if (!smpeg_info.has_audio) | |
149 { | |
150 Sound_SetError("MP3: No audio stream found in data."); | |
151 SMPEG_delete(smpeg); | |
152 return(0); | |
153 } /* if */ | |
154 | |
62
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
155 SNDDBG(("MP3: Accepting data stream.\n")); |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
156 SNDDBG(("MP3: has_audio == {%s}.\n", smpeg_info.has_audio ? "TRUE" : "FALSE")); |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
157 SNDDBG(("MP3: has_video == {%s}.\n", smpeg_info.has_video ? "TRUE" : "FALSE")); |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
158 SNDDBG(("MP3: width == (%d).\n", smpeg_info.width)); |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
159 SNDDBG(("MP3: height == (%d).\n", smpeg_info.height)); |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
160 SNDDBG(("MP3: current_frame == (%d).\n", smpeg_info.current_frame)); |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
161 SNDDBG(("MP3: current_fps == (%f).\n", smpeg_info.current_fps)); |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
162 SNDDBG(("MP3: audio_string == [%s].\n", smpeg_info.audio_string)); |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
163 SNDDBG(("MP3: audio_current_frame == (%d).\n", smpeg_info.audio_current_frame)); |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
164 SNDDBG(("MP3: current_offset == (%d).\n", smpeg_info.current_offset)); |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
165 SNDDBG(("MP3: total_size == (%d).\n", smpeg_info.total_size)); |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
166 SNDDBG(("MP3: current_time == (%f).\n", smpeg_info.current_time)); |
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
54
diff
changeset
|
167 SNDDBG(("MP3: total_time == (%f).\n", smpeg_info.total_time)); |
14 | 168 |
169 SMPEG_enablevideo(smpeg, 0); | |
170 SMPEG_enableaudio(smpeg, 1); | |
171 SMPEG_loop(smpeg, 0); | |
172 | |
173 SMPEG_wantedSpec(smpeg, &spec); | |
54
6df2f69e037e
Fixes by Torbj�rn...now the thing actually works! :) See the CHANGELOG
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
174 /* |
6df2f69e037e
Fixes by Torbj�rn...now the thing actually works! :) See the CHANGELOG
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
175 * One of the MP3:s I tried wouldn't work unless I added this line |
6df2f69e037e
Fixes by Torbj�rn...now the thing actually works! :) See the CHANGELOG
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
176 * to tell SMPEG that yes, it may have the spec it wants. |
6df2f69e037e
Fixes by Torbj�rn...now the thing actually works! :) See the CHANGELOG
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
177 */ |
6df2f69e037e
Fixes by Torbj�rn...now the thing actually works! :) See the CHANGELOG
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
178 SMPEG_actualSpec(smpeg, &spec); |
14 | 179 sample->actual.format = spec.format; |
180 sample->actual.rate = spec.freq; | |
181 sample->actual.channels = spec.channels; | |
182 sample->flags = SOUND_SAMPLEFLAG_NONE; | |
183 internal->decoder_private = smpeg; | |
184 | |
185 SMPEG_play(smpeg); | |
186 return(1); | |
187 } /* MP3_open */ | |
188 | |
189 | |
190 static void MP3_close(Sound_Sample *sample) | |
191 { | |
192 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
193 SMPEG_delete((SMPEG *) internal->decoder_private); | |
194 } /* MP3_close */ | |
195 | |
196 | |
197 static Uint32 MP3_read(Sound_Sample *sample) | |
198 { | |
199 Uint32 retval; | |
200 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
201 SMPEG *smpeg = (SMPEG *) internal->decoder_private; | |
202 | |
54
6df2f69e037e
Fixes by Torbj�rn...now the thing actually works! :) See the CHANGELOG
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
203 /* |
6df2f69e037e
Fixes by Torbj�rn...now the thing actually works! :) See the CHANGELOG
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
204 * We have to clear the buffer because apparently SMPEG_playAudio() |
6df2f69e037e
Fixes by Torbj�rn...now the thing actually works! :) See the CHANGELOG
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
205 * will mix the decoded audio with whatever's already in it. Nasty. |
6df2f69e037e
Fixes by Torbj�rn...now the thing actually works! :) See the CHANGELOG
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
206 */ |
6df2f69e037e
Fixes by Torbj�rn...now the thing actually works! :) See the CHANGELOG
Ryan C. Gordon <icculus@icculus.org>
parents:
46
diff
changeset
|
207 memset(internal->buffer, 0, internal->buffer_size); |
14 | 208 retval = SMPEG_playAudio(smpeg, internal->buffer, internal->buffer_size); |
209 if (retval < internal->buffer_size) | |
210 { | |
211 char *errMsg = SMPEG_error(smpeg); | |
212 if (errMsg == NULL) | |
213 sample->flags |= SOUND_SAMPLEFLAG_EOF; | |
214 else | |
215 { | |
216 Sound_SetError(errMsg); | |
217 sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
218 } /* else */ | |
219 } /* if */ | |
220 | |
221 return(retval); | |
222 } /* MP3_read */ | |
223 | |
64
40006625142a
Changes in preparation of autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents:
62
diff
changeset
|
224 #endif /* SOUND_SUPPORTS_MP3 */ |
40006625142a
Changes in preparation of autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents:
62
diff
changeset
|
225 |
14 | 226 /* end of mp3.c ... */ |
227 |