annotate decoders/mp3.c @ 166:d8904267d23c

Cleanups, fixes and enhancements by Torbj�rn Andersson.
author Ryan C. Gordon <icculus@icculus.org>
date Mon, 26 Nov 2001 04:32:55 +0000
parents 1df5c106504e
children 47cc2de2ae36
rev   line source
14
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
1 /*
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
2 * SDL_sound -- An abstract sound format decoding API.
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
3 * Copyright (C) 2001 Ryan C. Gordon.
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
4 *
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
8 * version 2.1 of the License, or (at your option) any later version.
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
9 *
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
13 * Lesser General Public License for more details.
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
14 *
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
18 */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
19
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
20 /*
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
21 * MPEG-1 Layer 3, or simply, "MP3", decoder for SDL_sound.
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
22 *
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
23 * This driver handles all those highly compressed songs you stole through
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
24 * Napster. :) It depends on the SMPEG library for decoding, which can
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
25 * be grabbed from: http://www.lokigames.com/development/smpeg.php3
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
26 *
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
27 * Please see the file LICENSE in the source's root directory.
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
28 *
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
29 * This file written by Ryan C. Gordon. (icculus@clutteredmind.org)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
30 */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
31
106
40de367eb59e Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents: 104
diff changeset
32 #if HAVE_CONFIG_H
40de367eb59e Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents: 104
diff changeset
33 # include <config.h>
40de367eb59e Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents: 104
diff changeset
34 #endif
100
6d9fdec2f708 added config.h, added --enable-debug flag, various other changes to the build system
fingolfin
parents: 96
diff changeset
35
104
103cfcb3c014 Updated to fix build system problem.
Ryan C. Gordon <icculus@icculus.org>
parents: 100
diff changeset
36 #ifdef SOUND_SUPPORTS_MP3
103cfcb3c014 Updated to fix build system problem.
Ryan C. Gordon <icculus@icculus.org>
parents: 100
diff changeset
37
14
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
38 #include <stdio.h>
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
39 #include <stdlib.h>
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
40 #include <string.h>
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
41 #include <assert.h>
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
42
106
40de367eb59e Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents: 104
diff changeset
43 #include "SDL_sound.h"
40de367eb59e Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents: 104
diff changeset
44
40de367eb59e Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents: 104
diff changeset
45 #define __SDL_SOUND_INTERNAL__
40de367eb59e Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents: 104
diff changeset
46 #include "SDL_sound_internal.h"
40de367eb59e Changing my include structure to do this right.
Ryan C. Gordon <icculus@icculus.org>
parents: 104
diff changeset
47
14
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
48 #include "smpeg.h"
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
49 #include "extra_rwops.h"
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
50
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
51
46
6e13fcc178da No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
52 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
53 static void MP3_quit(void);
14
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
54 static int MP3_open(Sound_Sample *sample, const char *ext);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
55 static void MP3_close(Sound_Sample *sample);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
56 static Uint32 MP3_read(Sound_Sample *sample);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
57
149
1df5c106504e Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
58 static const char *extensions_smpeg[] = { "MP3", "MPEG", "MPG", NULL };
14
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
59 const Sound_DecoderFunctions __Sound_DecoderFunctions_MP3 =
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
60 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
61 {
149
1df5c106504e Decoders can now list multiple file extensions.
Ryan C. Gordon <icculus@icculus.org>
parents: 106
diff changeset
62 extensions_smpeg,
14
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
63 "MPEG-1 Layer 3 audio through SMPEG",
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
64 "Ryan C. Gordon <icculus@clutteredmind.org>",
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
65 "http://www.icculus.org/SDL_sound/"
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
66 },
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
67
46
6e13fcc178da No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
68 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
69 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
70 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
71 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
72 MP3_read /* read() method */
14
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
73 };
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
74
46
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 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
77 {
6e13fcc178da No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
78 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
79 } /* MP3_init */
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
6e13fcc178da No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
82 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
83 {
6e13fcc178da No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
84 /* 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
85 } /* MP3_quit */
6e13fcc178da No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
86
6e13fcc178da No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
87
14
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
88 static __inline__ void output_version(void)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
89 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
90 static int first_time = 1;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
91
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
92 if (first_time)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
93 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
94 SMPEG_version v;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
95 SMPEG_VERSION(&v);
62
b13fafb976be Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents: 54
diff changeset
96 SNDDBG(("MP3: Compiled against SMPEG v%d.%d.%d.\n",
14
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
97 v.major, v.minor, v.patch));
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
98 first_time = 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
99 } /* if */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
100 } /* output_version */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
101
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
102
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
103 static int MP3_open(Sound_Sample *sample, const char *ext)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
104 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
105 SMPEG *smpeg;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
106 SMPEG_Info smpeg_info;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
107 SDL_AudioSpec spec;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
108 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
109 SDL_RWops *refCounter;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
110
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
111 output_version();
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
112
46
6e13fcc178da No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
113 /*
96
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
114 * If I understand things correctly, MP3 files don't really have any
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
115 * magic header we can check for. The MP3 player is expected to just
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
116 * pick the first thing that looks like a valid frame and start
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
117 * playing from there.
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
118 *
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
119 * So here's what we do: If the caller insists that this is really
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
120 * MP3 we'll take his word for it. Otherwise, use the same test as
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
121 * SDL_mixer does and check if the stream starts with something that
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
122 * looks like a frame.
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
123 *
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
124 * A frame begins with 11 bits of frame sync (all bits must be set),
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
125 * followed by a two-bit MPEG Audio version ID:
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
126 *
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
127 * 00 - MPEG Version 2.5 (later extension of MPEG 2)
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
128 * 01 - reserved
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
129 * 10 - MPEG Version 2 (ISO/IEC 13818-3)
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
130 * 11 - MPEG Version 1 (ISO/IEC 11172-3)
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
131 *
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
132 * Apparently we don't handle MPEG Version 2.5.
46
6e13fcc178da No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
133 */
96
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
134 if (__Sound_strcasecmp(ext, "MP3") != 0)
46
6e13fcc178da No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
135 {
96
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
136 Uint8 mp3_magic[2];
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
137
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
138 if (SDL_RWread(internal->rw, mp3_magic, sizeof (mp3_magic), 1) != 1)
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
139 {
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
140 Sound_SetError("MP3: Could not read MP3 magic.");
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
141 return(0);
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
142 } /*if */
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
143
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
144 if (mp3_magic[0] != 0xFF || (mp3_magic[1] & 0xF0) != 0xF0)
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
145 {
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
146 Sound_SetError("MP3: Not an MP3 stream.");
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
147 return(0);
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
148 } /* if */
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
149
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
150 /* !!! FIXME: If the seek fails, we'll probably miss a frame */
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
151 SDL_RWseek(internal->rw, -sizeof (mp3_magic), SEEK_CUR);
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
152 } /* if */
46
6e13fcc178da No longer grabs non-MP3 streams. Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
153
14
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
154 refCounter = RWops_RWRefCounter_new(internal->rw);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
155 if (refCounter == NULL)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
156 {
62
b13fafb976be Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents: 54
diff changeset
157 SNDDBG(("MP3: Failed to create reference counting RWops.\n"));
14
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
158 return(0);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
159 } /* if */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
160
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
161 /* replace original RWops. This is safe. Honest. :) */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
162 internal->rw = refCounter;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
163
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
164 /*
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
165 * increment the refcount, since SMPEG will nuke the RWops if it can't
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
166 * accept the contained data...
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
167 */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
168 RWops_RWRefCounter_addRef(refCounter);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
169 smpeg = SMPEG_new_rwops(refCounter, &smpeg_info, 0);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
170
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
171 if (SMPEG_error(smpeg))
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
172 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
173 Sound_SetError(SMPEG_error(smpeg));
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
174 SMPEG_delete(smpeg);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
175 return(0);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
176 } /* if */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
177
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
178 if (!smpeg_info.has_audio)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
179 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
180 Sound_SetError("MP3: No audio stream found in data.");
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
181 SMPEG_delete(smpeg);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
182 return(0);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
183 } /* if */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
184
62
b13fafb976be Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents: 54
diff changeset
185 SNDDBG(("MP3: Accepting data stream.\n"));
b13fafb976be Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents: 54
diff changeset
186 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
187 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
188 SNDDBG(("MP3: width == (%d).\n", smpeg_info.width));
b13fafb976be Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents: 54
diff changeset
189 SNDDBG(("MP3: height == (%d).\n", smpeg_info.height));
b13fafb976be Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents: 54
diff changeset
190 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
191 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
192 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
193 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
194 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
195 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
196 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
197 SNDDBG(("MP3: total_time == (%f).\n", smpeg_info.total_time));
14
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
198
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
199 SMPEG_enablevideo(smpeg, 0);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
200 SMPEG_enableaudio(smpeg, 1);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
201 SMPEG_loop(smpeg, 0);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
202
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
203 SMPEG_wantedSpec(smpeg, &spec);
96
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
204
54
6df2f69e037e Fixes by Torbj�rn...now the thing actually works! :) See the CHANGELOG
Ryan C. Gordon <icculus@icculus.org>
parents: 46
diff changeset
205 /*
96
25e515dfa18d Fix for MP3 detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 64
diff changeset
206 * One of the MP3s I tried wouldn't work unless I added this line
54
6df2f69e037e Fixes by Torbj�rn...now the thing actually works! :) See the CHANGELOG
Ryan C. Gordon <icculus@icculus.org>
parents: 46
diff changeset
207 * 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
208 */
6df2f69e037e Fixes by Torbj�rn...now the thing actually works! :) See the CHANGELOG
Ryan C. Gordon <icculus@icculus.org>
parents: 46
diff changeset
209 SMPEG_actualSpec(smpeg, &spec);
14
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
210 sample->actual.format = spec.format;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
211 sample->actual.rate = spec.freq;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
212 sample->actual.channels = spec.channels;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
213 sample->flags = SOUND_SAMPLEFLAG_NONE;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
214 internal->decoder_private = smpeg;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
215
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
216 SMPEG_play(smpeg);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
217 return(1);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
218 } /* MP3_open */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
219
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
220
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
221 static void MP3_close(Sound_Sample *sample)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
222 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
223 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
224 SMPEG_delete((SMPEG *) internal->decoder_private);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
225 } /* MP3_close */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
226
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
227
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
228 static Uint32 MP3_read(Sound_Sample *sample)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
229 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
230 Uint32 retval;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
231 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
232 SMPEG *smpeg = (SMPEG *) internal->decoder_private;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
233
54
6df2f69e037e Fixes by Torbj�rn...now the thing actually works! :) See the CHANGELOG
Ryan C. Gordon <icculus@icculus.org>
parents: 46
diff changeset
234 /*
6df2f69e037e Fixes by Torbj�rn...now the thing actually works! :) See the CHANGELOG
Ryan C. Gordon <icculus@icculus.org>
parents: 46
diff changeset
235 * 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
236 * 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
237 */
6df2f69e037e Fixes by Torbj�rn...now the thing actually works! :) See the CHANGELOG
Ryan C. Gordon <icculus@icculus.org>
parents: 46
diff changeset
238 memset(internal->buffer, 0, internal->buffer_size);
14
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
239 retval = SMPEG_playAudio(smpeg, internal->buffer, internal->buffer_size);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
240 if (retval < internal->buffer_size)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
241 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
242 char *errMsg = SMPEG_error(smpeg);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
243 if (errMsg == NULL)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
244 sample->flags |= SOUND_SAMPLEFLAG_EOF;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
245 else
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
246 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
247 Sound_SetError(errMsg);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
248 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
249 } /* else */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
250 } /* if */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
251
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
252 return(retval);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
253 } /* MP3_read */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
254
64
40006625142a Changes in preparation of autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents: 62
diff changeset
255 #endif /* SOUND_SUPPORTS_MP3 */
40006625142a Changes in preparation of autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents: 62
diff changeset
256
14
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
257 /* end of mp3.c ... */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
258