Mercurial > SDL_sound_CoreAudio
annotate decoders/flac.c @ 180:db3f0ee7aac0
Modified to more easily allow for different compression types.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Fri, 07 Dec 2001 20:31:20 +0000 |
parents | d8904267d23c |
children | 3849438b735e |
rev | line source |
---|---|
155 | 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 * FLAC decoder for SDL_sound. | |
22 * | |
23 * This driver handles FLAC audio, that is to say the Free Lossless Audio | |
24 * Codec. It depends on libFLAC for decoding, which can be grabbed from: | |
25 * http://flac.sourceforge.net | |
26 * | |
27 * Please see the file LICENSE in the source's root directory. | |
28 * | |
29 * This file written by Torbjörn Andersson. (d91tan@Update.UU.SE) | |
30 */ | |
31 | |
32 #if HAVE_CONFIG_H | |
33 # include <config.h> | |
34 #endif | |
35 | |
36 #ifdef SOUND_SUPPORTS_FLAC | |
37 | |
38 #include <stdio.h> | |
39 #include <stdlib.h> | |
40 #include <string.h> | |
41 #include <assert.h> | |
42 | |
43 #include "SDL_sound.h" | |
44 | |
45 #define __SDL_SOUND_INTERNAL__ | |
46 #include "SDL_sound_internal.h" | |
47 | |
164
77482005beb6
Now includes FLAC/stream_decoder.h instead of FLAC/all.h. More robust
Ryan C. Gordon <icculus@icculus.org>
parents:
155
diff
changeset
|
48 #include "FLAC/stream_decoder.h" |
155 | 49 |
50 | |
51 static int FLAC_init(void); | |
52 static void FLAC_quit(void); | |
53 static int FLAC_open(Sound_Sample *sample, const char *ext); | |
54 static void FLAC_close(Sound_Sample *sample); | |
55 static Uint32 FLAC_read(Sound_Sample *sample); | |
56 | |
57 static const char *extensions_flac[] = { "FLAC", "FLA", NULL }; | |
58 | |
59 const Sound_DecoderFunctions __Sound_DecoderFunctions_FLAC = | |
60 { | |
61 { | |
62 extensions_flac, | |
63 "Free Lossless Audio Codec", | |
64 "Torbjörn Andersson <d91tan@Update.UU.SE>", | |
65 "http://flac.sourceforge.net/" | |
66 }, | |
67 | |
68 FLAC_init, /* init() method */ | |
69 FLAC_quit, /* quit() method */ | |
70 FLAC_open, /* open() method */ | |
71 FLAC_close, /* close() method */ | |
72 FLAC_read /* read() method */ | |
73 }; | |
74 | |
75 /* This is what we store in our internal->decoder_private field. */ | |
76 typedef struct | |
77 { | |
78 FLAC__StreamDecoder *decoder; | |
79 SDL_RWops *rw; | |
80 Sound_Sample *sample; | |
81 Uint32 frame_size; | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
82 Uint8 is_flac; |
155 | 83 } flac_t; |
84 | |
85 | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
86 static void free_flac(flac_t *f) |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
87 { |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
88 FLAC__stream_decoder_finish(f->decoder); |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
89 FLAC__stream_decoder_delete(f->decoder); |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
90 free(f); |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
91 } /* free_flac */ |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
92 |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
93 |
155 | 94 static FLAC__StreamDecoderReadStatus FLAC_read_callback( |
95 const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], | |
96 unsigned int *bytes, void *client_data) | |
97 { | |
98 flac_t *f = (flac_t *) client_data; | |
99 Uint32 retval; | |
100 | |
101 #if 0 | |
164
77482005beb6
Now includes FLAC/stream_decoder.h instead of FLAC/all.h. More robust
Ryan C. Gordon <icculus@icculus.org>
parents:
155
diff
changeset
|
102 SNDDBG(("FLAC: Read callback.\n")); |
155 | 103 #endif |
104 | |
105 retval = SDL_RWread(f->rw, (Uint8 *) buffer, 1, *bytes); | |
106 | |
107 if (retval == 0) | |
108 { | |
109 *bytes = 0; | |
110 f->sample->flags |= SOUND_SAMPLEFLAG_EOF; | |
111 return(FLAC__STREAM_DECODER_READ_END_OF_STREAM); | |
112 } /* if */ | |
113 | |
114 if (retval == -1) | |
115 { | |
116 *bytes = 0; | |
117 f->sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
118 return(FLAC__STREAM_DECODER_READ_ABORT); | |
119 } /* if */ | |
120 | |
121 if (retval < *bytes) | |
122 { | |
123 *bytes = retval; | |
124 f->sample->flags |= SOUND_SAMPLEFLAG_EAGAIN; | |
125 } /* if */ | |
126 | |
127 return(FLAC__STREAM_DECODER_READ_CONTINUE); | |
128 } /* FLAC_read_callback */ | |
129 | |
130 | |
131 static FLAC__StreamDecoderWriteStatus FLAC_write_callback( | |
132 const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, | |
133 const FLAC__int32 *buffer[], void *client_data) | |
134 { | |
135 flac_t *f = (flac_t *) client_data; | |
136 Uint32 i, j; | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
137 Uint32 sample; |
155 | 138 Uint8 *dst; |
139 | |
140 #if 0 | |
141 SNDDBG(("FLAC: Write callback.\n")); | |
142 #endif | |
143 | |
144 f->frame_size = frame->header.channels * frame->header.blocksize | |
145 * frame->header.bits_per_sample / 8; | |
146 | |
147 if (f->frame_size > f->sample->buffer_size) | |
148 Sound_SetBufferSize(f->sample, f->frame_size); | |
149 | |
150 dst = f->sample->buffer; | |
151 | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
152 /* If the sample is neither exactly 8-bit nor 16-bit, it will have to |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
153 * be converted. Unfortunately the buffer is read-only, so we either |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
154 * have to check for each sample, or make a copy of the buffer. I'm |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
155 * not sure which way is best, so I've arbitrarily picked the former. |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
156 */ |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
157 if (f->sample->actual.format == AUDIO_S8) |
155 | 158 { |
159 for (i = 0; i < frame->header.blocksize; i++) | |
160 for (j = 0; j < frame->header.channels; j++) | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
161 { |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
162 sample = buffer[j][i]; |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
163 if (frame->header.bits_per_sample < 8) |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
164 sample <<= (8 - frame->header.bits_per_sample); |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
165 *dst++ = sample & 0x00ff; |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
166 } /* for */ |
155 | 167 } /* if */ |
168 else | |
169 { | |
170 for (i = 0; i < frame->header.blocksize; i++) | |
171 for (j = 0; j < frame->header.channels; j++) | |
172 { | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
173 sample = buffer[j][i]; |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
174 if (frame->header.bits_per_sample < 16) |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
175 sample <<= (16 - frame->header.bits_per_sample); |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
176 else if (frame->header.bits_per_sample > 16) |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
177 sample >>= (frame->header.bits_per_sample - 16); |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
178 *dst++ = (sample & 0xff00) >> 8; |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
179 *dst++ = sample & 0x00ff; |
155 | 180 } /* for */ |
181 } /* else */ | |
182 | |
183 return(FLAC__STREAM_DECODER_WRITE_CONTINUE); | |
184 } /* FLAC_write_callback */ | |
185 | |
186 | |
187 void FLAC_metadata_callback( | |
188 const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, | |
189 void *client_data) | |
190 { | |
191 flac_t *f = (flac_t *) client_data; | |
192 | |
193 SNDDBG(("FLAC: Metadata callback.\n")); | |
194 | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
195 /* There are several kinds of metadata, but STREAMINFO is the only |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
196 * one that always has to be there. |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
197 */ |
155 | 198 if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO) |
199 { | |
200 SNDDBG(("FLAC: Metadata is streaminfo.\n")); | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
201 |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
202 f->is_flac = 1; |
155 | 203 f->sample->actual.channels = metadata->data.stream_info.channels; |
204 f->sample->actual.rate = metadata->data.stream_info.sample_rate; | |
205 | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
206 if (metadata->data.stream_info.bits_per_sample > 8) |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
207 f->sample->actual.format = AUDIO_S16MSB; |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
208 else |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
209 f->sample->actual.format = AUDIO_S8; |
155 | 210 } /* if */ |
211 } /* FLAC_metadata_callback */ | |
212 | |
213 | |
214 void FLAC_error_callback( | |
215 const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, | |
216 void *client_data) | |
217 { | |
218 flac_t *f = (flac_t *) client_data; | |
164
77482005beb6
Now includes FLAC/stream_decoder.h instead of FLAC/all.h. More robust
Ryan C. Gordon <icculus@icculus.org>
parents:
155
diff
changeset
|
219 |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
220 /* !!! FIXME: Is every error really fatal? I don't know... */ |
164
77482005beb6
Now includes FLAC/stream_decoder.h instead of FLAC/all.h. More robust
Ryan C. Gordon <icculus@icculus.org>
parents:
155
diff
changeset
|
221 Sound_SetError(FLAC__StreamDecoderErrorStatusString[status]); |
155 | 222 f->sample->flags |= SOUND_SAMPLEFLAG_ERROR; |
223 } /* FLAC_error_callback */ | |
224 | |
225 | |
226 static int FLAC_init(void) | |
227 { | |
228 return(1); /* always succeeds. */ | |
229 } /* FLAC_init */ | |
230 | |
231 | |
232 static void FLAC_quit(void) | |
233 { | |
234 /* it's a no-op. */ | |
235 } /* FLAC_quit */ | |
236 | |
237 | |
238 static int FLAC_open(Sound_Sample *sample, const char *ext) | |
239 { | |
240 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
241 SDL_RWops *rw = internal->rw; | |
242 FLAC__StreamDecoder *decoder; | |
243 flac_t *f; | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
244 int i; |
155 | 245 |
246 f = (flac_t *) malloc(sizeof (flac_t)); | |
247 BAIL_IF_MACRO(f == NULL, ERR_OUT_OF_MEMORY, 0); | |
248 | |
249 decoder = FLAC__stream_decoder_new(); | |
250 if (decoder == NULL) | |
251 { | |
252 Sound_SetError(ERR_OUT_OF_MEMORY); | |
253 free(f); | |
254 return(0); | |
255 } /* if */ | |
256 | |
257 FLAC__stream_decoder_set_read_callback(decoder, FLAC_read_callback); | |
258 FLAC__stream_decoder_set_write_callback(decoder, FLAC_write_callback); | |
259 FLAC__stream_decoder_set_metadata_callback(decoder, FLAC_metadata_callback); | |
260 FLAC__stream_decoder_set_error_callback(decoder, FLAC_error_callback); | |
261 FLAC__stream_decoder_set_client_data(decoder, f); | |
262 | |
263 f->rw = internal->rw; | |
264 f->sample = sample; | |
265 f->decoder = decoder; | |
164
77482005beb6
Now includes FLAC/stream_decoder.h instead of FLAC/all.h. More robust
Ryan C. Gordon <icculus@icculus.org>
parents:
155
diff
changeset
|
266 f->sample->actual.format = 0; |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
267 f->is_flac = 0; |
155 | 268 |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
269 #if 0 |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
270 /* !!! FIXME: |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
271 * |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
272 * It should be possible to play a FLAC stream starting at any frame, |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
273 * but we can only do that if we know for sure that it is a FLAC |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
274 * stream. Otherwise we have to check for metadata, and then we need |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
275 * the entire stream, from the beginning. |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
276 * |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
277 * But getting this to work right seems to be an enormous pain in the |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
278 * butt for what is, at the moment, a very small gain. Maybe later. |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
279 */ |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
280 for (i = 0; extensions_flac[i] != NULL; i++) |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
281 if (__Sound_strcasecmp(ext, extensions_flac[i]) == 0) |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
282 { |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
283 f->is_flac = 1; |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
284 break; |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
285 } /* if */ |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
286 #endif |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
287 |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
288 internal->decoder_private = f; |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
289 FLAC__stream_decoder_init(decoder); |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
290 |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
291 /* If we are not sure this is a FLAC stream, check for the STREAMINFO |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
292 * metadata block. If not, we'd have to peek at the first audio frame |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
293 * and get the sound format from there but, as stated above, that is |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
294 * not yet implemented. |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
295 */ |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
296 if (!f->is_flac) |
155 | 297 { |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
298 FLAC__stream_decoder_process_metadata(decoder); |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
299 |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
300 /* Still not FLAC? Give up. */ |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
301 if (!f->is_flac) |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
302 { |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
303 Sound_SetError("FLAC: No metadata found. Not a FLAC stream?"); |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
304 free_flac(f); |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
305 return(0); |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
306 } /* if */ |
155 | 307 } /* if */ |
308 | |
164
77482005beb6
Now includes FLAC/stream_decoder.h instead of FLAC/all.h. More robust
Ryan C. Gordon <icculus@icculus.org>
parents:
155
diff
changeset
|
309 SNDDBG(("FLAC: Accepting data stream.\n")); |
77482005beb6
Now includes FLAC/stream_decoder.h instead of FLAC/all.h. More robust
Ryan C. Gordon <icculus@icculus.org>
parents:
155
diff
changeset
|
310 |
155 | 311 sample->flags = SOUND_SAMPLEFLAG_NONE; |
312 return(1); | |
313 } /* FLAC_open */ | |
314 | |
315 | |
316 static void FLAC_close(Sound_Sample *sample) | |
317 { | |
318 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
319 flac_t *f = (flac_t *) internal->decoder_private; | |
320 | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
321 free_flac(f); |
155 | 322 } /* FLAC_close */ |
323 | |
324 | |
325 static Uint32 FLAC_read(Sound_Sample *sample) | |
326 { | |
327 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
328 flac_t *f = (flac_t *) internal->decoder_private; | |
329 Uint32 len; | |
330 | |
331 if (FLAC__stream_decoder_get_state(f->decoder) == FLAC__STREAM_DECODER_END_OF_STREAM) | |
332 { | |
333 sample->flags |= SOUND_SAMPLEFLAG_EOF; | |
334 return(0); | |
335 } /* if */ | |
336 | |
337 if (!FLAC__stream_decoder_process_one_frame(f->decoder)) | |
338 { | |
164
77482005beb6
Now includes FLAC/stream_decoder.h instead of FLAC/all.h. More robust
Ryan C. Gordon <icculus@icculus.org>
parents:
155
diff
changeset
|
339 Sound_SetError("FLAC: Couldn't decode frame."); |
155 | 340 sample->flags |= SOUND_SAMPLEFLAG_ERROR; |
341 return(0); | |
342 } /* if */ | |
343 | |
164
77482005beb6
Now includes FLAC/stream_decoder.h instead of FLAC/all.h. More robust
Ryan C. Gordon <icculus@icculus.org>
parents:
155
diff
changeset
|
344 /* An error may have been signalled through the error callback. */ |
77482005beb6
Now includes FLAC/stream_decoder.h instead of FLAC/all.h. More robust
Ryan C. Gordon <icculus@icculus.org>
parents:
155
diff
changeset
|
345 if (sample->flags & SOUND_SAMPLEFLAG_ERROR) |
77482005beb6
Now includes FLAC/stream_decoder.h instead of FLAC/all.h. More robust
Ryan C. Gordon <icculus@icculus.org>
parents:
155
diff
changeset
|
346 return(0); |
77482005beb6
Now includes FLAC/stream_decoder.h instead of FLAC/all.h. More robust
Ryan C. Gordon <icculus@icculus.org>
parents:
155
diff
changeset
|
347 |
155 | 348 return(f->frame_size); |
349 } /* FLAC_read */ | |
350 | |
351 #endif /* SOUND_SUPPORTS_FLAC */ | |
352 | |
353 | |
354 /* end of flac.c ... */ |