comparison decoders/mpglib.c @ 293:ee6e1f8bfae9

Can determine audio format correctly (mostly correctly?) now. Other cleanups.
author Ryan C. Gordon <icculus@icculus.org>
date Sun, 17 Mar 2002 21:17:09 +0000
parents 493dd0173f3d
children c97be6e1bd27
comparison
equal deleted inserted replaced
292:a9e211c3faa4 293:ee6e1f8bfae9
107 107
108 static int MPGLIB_open(Sound_Sample *sample, const char *ext) 108 static int MPGLIB_open(Sound_Sample *sample, const char *ext)
109 { 109 {
110 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; 110 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
111 mpglib_t *mpg = NULL; 111 mpglib_t *mpg = NULL;
112 int rc;
112 113
113 /* 114 /*
114 * If I understand things correctly, MP3 files don't really have any 115 * If I understand things correctly, MP3 files don't really have any
115 * magic header we can check for. The MP3 player is expected to just 116 * magic header we can check for. The MP3 player is expected to just
116 * pick the first thing that looks like a valid frame and start 117 * pick the first thing that looks like a valid frame and start
134 if (__Sound_strcasecmp(ext, "MP3") != 0) 135 if (__Sound_strcasecmp(ext, "MP3") != 0)
135 { 136 {
136 Uint8 mp3_magic[2]; 137 Uint8 mp3_magic[2];
137 138
138 if (SDL_RWread(internal->rw, mp3_magic, sizeof (mp3_magic), 1) != 1) 139 if (SDL_RWread(internal->rw, mp3_magic, sizeof (mp3_magic), 1) != 1)
139 { 140 BAIL_MACRO("MP3: Could not read MP3 magic.", 0);
140 Sound_SetError("MP3: Could not read MP3 magic.");
141 return(0);
142 } /*if */
143 141
144 if (mp3_magic[0] != 0xFF || (mp3_magic[1] & 0xF0) != 0xF0) 142 if (mp3_magic[0] != 0xFF || (mp3_magic[1] & 0xF0) != 0xF0)
145 { 143 BAIL_MACRO("MP3: Not an MP3 stream.", 0);
146 Sound_SetError("MP3: Not an MP3 stream.");
147 return(0);
148 } /* if */
149 144
150 /* !!! FIXME: If the seek fails, we'll probably miss a frame */ 145 /* !!! FIXME: If the seek fails, we'll probably miss a frame */
151 SDL_RWseek(internal->rw, -sizeof (mp3_magic), SEEK_CUR); 146 SDL_RWseek(internal->rw, -sizeof (mp3_magic), SEEK_CUR);
152 } /* if */ 147 } /* if */
153 148
154 mpg = (mpglib_t *) malloc(sizeof (mpglib_t)); 149 mpg = (mpglib_t *) malloc(sizeof (mpglib_t));
155 BAIL_IF_MACRO(mpg == NULL, ERR_OUT_OF_MEMORY, 0); 150 BAIL_IF_MACRO(mpg == NULL, ERR_OUT_OF_MEMORY, 0);
156 mpg->outpos = mpg->outleft = 0; 151 memset(mpg, '\0', sizeof (mpglib_t));
157 InitMP3(&mpg->mp); 152 InitMP3(&mpg->mp);
153
154 rc = SDL_RWread(internal->rw, mpg->inbuf, 1, sizeof (mpg->inbuf));
155 if (rc <= 0)
156 {
157 free(mpg);
158 BAIL_MACRO("MPGLIB: Failed to read any data at all", 0);
159 } /* if */
160
161 if (decodeMP3(&mpg->mp, mpg->inbuf, rc,
162 mpg->outbuf, sizeof (mpg->outbuf),
163 &mpg->outleft) == MP3_ERR)
164 {
165 free(mpg);
166 BAIL_MACRO("MPGLIB: Not an MP3 stream?", 0);
167 } /* if */
158 168
159 SNDDBG(("MPGLIB: Accepting data stream.\n")); 169 SNDDBG(("MPGLIB: Accepting data stream.\n"));
160 170
161 /* !!! FIXME: Determine what format mpglib is spitting out... */ 171 /* !!! FIXME: Determine what format mpglib is spitting out... */
162 internal->decoder_private = mpg; 172 internal->decoder_private = mpg;
163 sample->actual.rate = 44100; 173 sample->actual.rate = mpglib_freqs[mpg->mp.fr.sampling_frequency];
164 sample->actual.channels = 2; 174 sample->actual.channels = mpg->mp.fr.stereo;
165 sample->actual.format = AUDIO_S16LSB; 175 sample->actual.format = AUDIO_S16LSB; /* !!! FIXME: Is this right? */
166 sample->flags = SOUND_SAMPLEFLAG_NONE; 176 sample->flags = SOUND_SAMPLEFLAG_NONE;
167 177
168 return(1); /* we'll handle this data. */ 178 return(1); /* we'll handle this data. */
169 } /* MPGLIB_open */ 179 } /* MPGLIB_open */
170 180