Mercurial > SDL_sound_CoreAudio
annotate decoders/flac.c @ 535:45ee760a6f5a stable-1.0
Converted all text encoding from ISO-8859-1 to UTF-8.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Thu, 17 Apr 2008 18:06:53 +0000 |
parents | c5f184b4b2cd |
children |
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 * | |
184
47cc2de2ae36
Changed reference to "LICENSE" file to "COPYING".
Ryan C. Gordon <icculus@icculus.org>
parents:
182
diff
changeset
|
27 * Please see the file COPYING in the source's root directory. |
155 | 28 * |
535
45ee760a6f5a
Converted all text encoding from ISO-8859-1 to UTF-8.
Ryan C. Gordon <icculus@icculus.org>
parents:
515
diff
changeset
|
29 * This file written by Torbjörn Andersson. (d91tan@Update.UU.SE) |
155 | 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 | |
42 #include "SDL_sound.h" | |
43 | |
44 #define __SDL_SOUND_INTERNAL__ | |
45 #include "SDL_sound_internal.h" | |
46 | |
515
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
47 #include <FLAC/export.h> |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
48 |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
49 /* FLAC 1.1.3 has FLAC_API_VERSION_CURRENT == 8 */ |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
50 #if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT < 8 |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
51 #define LEGACY_FLAC |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
52 #else |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
53 #undef LEGACY_FLAC |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
54 #endif |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
55 |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
56 #ifdef LEGACY_FLAC |
443 | 57 #include <FLAC/seekable_stream_decoder.h> |
312 | 58 |
59 #define D_END_OF_STREAM FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM | |
60 | |
61 #define d_new() FLAC__seekable_stream_decoder_new() | |
62 #define d_init(x) FLAC__seekable_stream_decoder_init(x) | |
422
5b06e23d934e
Removed backwards compatible API nastiness from FLAC decoder.
Ryan C. Gordon <icculus@icculus.org>
parents:
400
diff
changeset
|
63 #define d_process_metadata(x) FLAC__seekable_stream_decoder_process_until_end_of_metadata(x) |
5b06e23d934e
Removed backwards compatible API nastiness from FLAC decoder.
Ryan C. Gordon <icculus@icculus.org>
parents:
400
diff
changeset
|
64 #define d_process_one_frame(x) FLAC__seekable_stream_decoder_process_single(x) |
312 | 65 #define d_get_state(x) FLAC__seekable_stream_decoder_get_state(x) |
66 #define d_finish(x) FLAC__seekable_stream_decoder_finish(x) | |
67 #define d_delete(x) FLAC__seekable_stream_decoder_delete(x) | |
68 #define d_set_read_callback(x, y) FLAC__seekable_stream_decoder_set_read_callback(x, y) | |
69 #define d_set_write_callback(x, y) FLAC__seekable_stream_decoder_set_write_callback(x, y) | |
70 #define d_set_metadata_callback(x, y) FLAC__seekable_stream_decoder_set_metadata_callback(x, y) | |
71 #define d_set_error_callback(x, y) FLAC__seekable_stream_decoder_set_error_callback(x, y) | |
72 #define d_set_client_data(x, y) FLAC__seekable_stream_decoder_set_client_data(x, y) | |
73 | |
74 typedef FLAC__SeekableStreamDecoder decoder_t; | |
75 typedef FLAC__SeekableStreamDecoderReadStatus d_read_status_t; | |
76 | |
77 #define D_SEEK_STATUS_OK FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK | |
78 #define D_SEEK_STATUS_ERROR FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR | |
79 #define D_TELL_STATUS_OK FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK | |
80 #define D_TELL_STATUS_ERROR FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR | |
81 #define D_LENGTH_STATUS_OK FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK | |
82 #define D_LENGTH_STATUS_ERROR FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_ERROR | |
83 | |
84 #define d_set_seek_callback(x, y) FLAC__seekable_stream_decoder_set_seek_callback(x, y) | |
85 #define d_set_tell_callback(x, y) FLAC__seekable_stream_decoder_set_tell_callback(x, y) | |
86 #define d_set_length_callback(x, y) FLAC__seekable_stream_decoder_set_length_callback(x, y) | |
87 #define d_set_eof_callback(x, y) FLAC__seekable_stream_decoder_set_eof_callback(x, y) | |
88 #define d_seek_absolute(x, y) FLAC__seekable_stream_decoder_seek_absolute(x, y) | |
89 | |
90 typedef FLAC__SeekableStreamDecoderSeekStatus d_seek_status_t; | |
91 typedef FLAC__SeekableStreamDecoderTellStatus d_tell_status_t; | |
92 typedef FLAC__SeekableStreamDecoderLengthStatus d_length_status_t; | |
515
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
93 #else |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
94 #include <FLAC/stream_decoder.h> |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
95 |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
96 #define D_END_OF_STREAM FLAC__STREAM_DECODER_END_OF_STREAM |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
97 |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
98 #define d_new() FLAC__stream_decoder_new() |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
99 #define d_process_metadata(x) FLAC__stream_decoder_process_until_end_of_metadata(x) |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
100 #define d_process_one_frame(x) FLAC__stream_decoder_process_single(x) |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
101 #define d_get_state(x) FLAC__stream_decoder_get_state(x) |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
102 #define d_finish(x) FLAC__stream_decoder_finish(x) |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
103 #define d_delete(x) FLAC__stream_decoder_delete(x) |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
104 |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
105 typedef FLAC__StreamDecoder decoder_t; |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
106 typedef FLAC__StreamDecoderReadStatus d_read_status_t; |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
107 |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
108 #define D_SEEK_STATUS_OK FLAC__STREAM_DECODER_SEEK_STATUS_OK |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
109 #define D_SEEK_STATUS_ERROR FLAC__STREAM_DECODER_SEEK_STATUS_ERROR |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
110 #define D_TELL_STATUS_OK FLAC__STREAM_DECODER_TELL_STATUS_OK |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
111 #define D_TELL_STATUS_ERROR FLAC__STREAM_DECODER_TELL_STATUS_ERROR |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
112 #define D_LENGTH_STATUS_OK FLAC__STREAM_DECODER_LENGTH_STATUS_OK |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
113 #define D_LENGTH_STATUS_ERROR FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
114 |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
115 #define d_seek_absolute(x, y) FLAC__stream_decoder_seek_absolute(x, y) |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
116 |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
117 typedef FLAC__StreamDecoderSeekStatus d_seek_status_t; |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
118 typedef FLAC__StreamDecoderTellStatus d_tell_status_t; |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
119 typedef FLAC__StreamDecoderLengthStatus d_length_status_t; |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
120 #endif |
312 | 121 |
393
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
122 #define D_WRITE_CONTINUE FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
123 #define D_READ_END_OF_STREAM FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
124 #define D_READ_ABORT FLAC__STREAM_DECODER_READ_STATUS_ABORT |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
125 #define D_READ_CONTINUE FLAC__STREAM_DECODER_READ_STATUS_CONTINUE |
312 | 126 |
127 #define d_error_status_string FLAC__StreamDecoderErrorStatusString | |
128 | |
393
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
129 typedef FLAC__StreamDecoderErrorStatus d_error_status_t; |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
130 typedef FLAC__StreamMetadata d_metadata_t; |
312 | 131 typedef FLAC__StreamDecoderWriteStatus d_write_status_t; |
132 | |
155 | 133 |
134 static int FLAC_init(void); | |
135 static void FLAC_quit(void); | |
136 static int FLAC_open(Sound_Sample *sample, const char *ext); | |
137 static void FLAC_close(Sound_Sample *sample); | |
138 static Uint32 FLAC_read(Sound_Sample *sample); | |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
184
diff
changeset
|
139 static int FLAC_rewind(Sound_Sample *sample); |
306
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
140 static int FLAC_seek(Sound_Sample *sample, Uint32 ms); |
155 | 141 |
142 static const char *extensions_flac[] = { "FLAC", "FLA", NULL }; | |
143 | |
144 const Sound_DecoderFunctions __Sound_DecoderFunctions_FLAC = | |
145 { | |
146 { | |
147 extensions_flac, | |
148 "Free Lossless Audio Codec", | |
535
45ee760a6f5a
Converted all text encoding from ISO-8859-1 to UTF-8.
Ryan C. Gordon <icculus@icculus.org>
parents:
515
diff
changeset
|
149 "Torbjörn Andersson <d91tan@Update.UU.SE>", |
155 | 150 "http://flac.sourceforge.net/" |
151 }, | |
152 | |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
184
diff
changeset
|
153 FLAC_init, /* init() method */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
184
diff
changeset
|
154 FLAC_quit, /* quit() method */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
184
diff
changeset
|
155 FLAC_open, /* open() method */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
184
diff
changeset
|
156 FLAC_close, /* close() method */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
184
diff
changeset
|
157 FLAC_read, /* read() method */ |
306
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
158 FLAC_rewind, /* rewind() method */ |
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
159 FLAC_seek /* seek() method */ |
155 | 160 }; |
161 | |
162 /* This is what we store in our internal->decoder_private field. */ | |
163 typedef struct | |
164 { | |
312 | 165 decoder_t *decoder; |
155 | 166 SDL_RWops *rw; |
167 Sound_Sample *sample; | |
168 Uint32 frame_size; | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
169 Uint8 is_flac; |
312 | 170 Uint32 stream_length; |
155 | 171 } flac_t; |
172 | |
173 | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
174 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
|
175 { |
312 | 176 d_finish(f->decoder); |
177 d_delete(f->decoder); | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
178 free(f); |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
179 } /* free_flac */ |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
180 |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
181 |
515
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
182 #ifdef LEGACY_FLAC |
312 | 183 static d_read_status_t read_callback( |
184 const decoder_t *decoder, FLAC__byte buffer[], | |
155 | 185 unsigned int *bytes, void *client_data) |
515
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
186 #else |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
187 static d_read_status_t read_callback( |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
188 const decoder_t *decoder, FLAC__byte buffer[], |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
189 size_t *bytes, void *client_data) |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
190 #endif |
155 | 191 { |
192 flac_t *f = (flac_t *) client_data; | |
193 Uint32 retval; | |
194 | |
195 retval = SDL_RWread(f->rw, (Uint8 *) buffer, 1, *bytes); | |
196 | |
197 if (retval == 0) | |
198 { | |
199 *bytes = 0; | |
200 f->sample->flags |= SOUND_SAMPLEFLAG_EOF; | |
312 | 201 return(D_READ_END_OF_STREAM); |
155 | 202 } /* if */ |
203 | |
204 if (retval == -1) | |
205 { | |
206 *bytes = 0; | |
207 f->sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
312 | 208 return(D_READ_ABORT); |
155 | 209 } /* if */ |
210 | |
211 if (retval < *bytes) | |
212 { | |
213 *bytes = retval; | |
214 f->sample->flags |= SOUND_SAMPLEFLAG_EAGAIN; | |
215 } /* if */ | |
216 | |
312 | 217 return(D_READ_CONTINUE); |
218 } /* read_callback */ | |
155 | 219 |
220 | |
312 | 221 static d_write_status_t write_callback( |
222 const decoder_t *decoder, const FLAC__Frame *frame, | |
393
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
223 const FLAC__int32 * const buffer[], |
b12c4483815e
Handles all versions of libFLAC up to version 1.0.3, now.
Ryan C. Gordon <icculus@icculus.org>
parents:
387
diff
changeset
|
224 void *client_data) |
155 | 225 { |
226 flac_t *f = (flac_t *) client_data; | |
227 Uint32 i, j; | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
228 Uint32 sample; |
155 | 229 Uint8 *dst; |
230 | |
231 f->frame_size = frame->header.channels * frame->header.blocksize | |
232 * frame->header.bits_per_sample / 8; | |
233 | |
234 if (f->frame_size > f->sample->buffer_size) | |
235 Sound_SetBufferSize(f->sample, f->frame_size); | |
236 | |
237 dst = f->sample->buffer; | |
238 | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
239 /* 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
|
240 * 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
|
241 * 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
|
242 * 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
|
243 */ |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
244 if (f->sample->actual.format == AUDIO_S8) |
155 | 245 { |
246 for (i = 0; i < frame->header.blocksize; i++) | |
247 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
|
248 { |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
249 sample = buffer[j][i]; |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
250 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
|
251 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
|
252 *dst++ = sample & 0x00ff; |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
253 } /* for */ |
155 | 254 } /* if */ |
255 else | |
256 { | |
257 for (i = 0; i < frame->header.blocksize; i++) | |
258 for (j = 0; j < frame->header.channels; j++) | |
259 { | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
260 sample = buffer[j][i]; |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
261 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
|
262 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
|
263 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
|
264 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
|
265 *dst++ = (sample & 0xff00) >> 8; |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
266 *dst++ = sample & 0x00ff; |
155 | 267 } /* for */ |
268 } /* else */ | |
269 | |
312 | 270 return(D_WRITE_CONTINUE); |
271 } /* write_callback */ | |
155 | 272 |
273 | |
312 | 274 static void metadata_callback( |
275 const decoder_t *decoder, | |
276 const d_metadata_t *metadata, | |
155 | 277 void *client_data) |
278 { | |
279 flac_t *f = (flac_t *) client_data; | |
312 | 280 |
155 | 281 SNDDBG(("FLAC: Metadata callback.\n")); |
282 | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
283 /* 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
|
284 * 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
|
285 */ |
155 | 286 if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO) |
287 { | |
288 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
|
289 |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
290 f->is_flac = 1; |
155 | 291 f->sample->actual.channels = metadata->data.stream_info.channels; |
292 f->sample->actual.rate = metadata->data.stream_info.sample_rate; | |
293 | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
294 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
|
295 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
|
296 else |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
297 f->sample->actual.format = AUDIO_S8; |
155 | 298 } /* if */ |
312 | 299 } /* metadata_callback */ |
155 | 300 |
301 | |
312 | 302 static void error_callback( |
303 const decoder_t *decoder, | |
304 d_error_status_t status, | |
155 | 305 void *client_data) |
306 { | |
307 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
|
308 |
387
fb519e6028e3
Changed all the Sound_SetError() calls to __Sound_SetError (or BAIL*_MACRO)
Ryan C. Gordon <icculus@icculus.org>
parents:
377
diff
changeset
|
309 __Sound_SetError(d_error_status_string[status]); |
155 | 310 f->sample->flags |= SOUND_SAMPLEFLAG_ERROR; |
312 | 311 } /* error_callback */ |
312 | |
313 | |
314 static d_seek_status_t seek_callback( | |
315 const decoder_t *decoder, | |
316 FLAC__uint64 absolute_byte_offset, | |
317 void *client_data) | |
318 { | |
319 flac_t *f = (flac_t *) client_data; | |
320 | |
321 if (SDL_RWseek(f->rw, absolute_byte_offset, SEEK_SET) >= 0) | |
322 { | |
323 return(D_SEEK_STATUS_OK); | |
324 } /* if */ | |
325 | |
326 return(D_SEEK_STATUS_ERROR); | |
327 } /* seek_callback*/ | |
328 | |
329 | |
330 static d_tell_status_t tell_callback( | |
331 const decoder_t *decoder, | |
332 FLAC__uint64 *absolute_byte_offset, | |
333 void *client_data) | |
334 { | |
335 flac_t *f = (flac_t *) client_data; | |
400 | 336 int pos; |
312 | 337 |
338 pos = SDL_RWtell(f->rw); | |
339 | |
340 if (pos < 0) | |
341 { | |
342 return(D_TELL_STATUS_ERROR); | |
343 } /* if */ | |
155 | 344 |
312 | 345 *absolute_byte_offset = pos; |
346 return(D_TELL_STATUS_OK); | |
347 } /* tell_callback */ | |
348 | |
349 | |
350 static d_length_status_t length_callback( | |
351 const decoder_t *decoder, | |
352 FLAC__uint64 *stream_length, | |
353 void *client_data) | |
354 { | |
355 flac_t *f = (flac_t *) client_data; | |
356 | |
357 if (f->sample->flags & SOUND_SAMPLEFLAG_CANSEEK) | |
358 { | |
359 *stream_length = f->stream_length; | |
360 return(D_LENGTH_STATUS_OK); | |
361 } /* if */ | |
362 | |
363 return(D_LENGTH_STATUS_ERROR); | |
364 } /* length_callback */ | |
365 | |
366 | |
367 static FLAC__bool eof_callback( | |
368 const decoder_t *decoder, | |
369 void *client_data) | |
370 { | |
371 flac_t *f = (flac_t *) client_data; | |
400 | 372 int pos; |
312 | 373 |
374 /* Maybe we could check for SOUND_SAMPLEFLAG_EOF here instead? */ | |
375 pos = SDL_RWtell(f->rw); | |
376 | |
377 if (pos >= 0 && pos >= f->stream_length) | |
378 { | |
379 return(true); | |
380 } /* if */ | |
381 | |
382 return(false); | |
383 } /* eof_callback */ | |
384 | |
155 | 385 |
386 static int FLAC_init(void) | |
387 { | |
388 return(1); /* always succeeds. */ | |
389 } /* FLAC_init */ | |
390 | |
391 | |
392 static void FLAC_quit(void) | |
393 { | |
394 /* it's a no-op. */ | |
395 } /* FLAC_quit */ | |
396 | |
397 | |
182
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
398 #define FLAC_MAGIC 0x43614C66 /* "fLaC" in ASCII. */ |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
399 |
155 | 400 static int FLAC_open(Sound_Sample *sample, const char *ext) |
401 { | |
402 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
403 SDL_RWops *rw = internal->rw; | |
312 | 404 decoder_t *decoder; |
155 | 405 flac_t *f; |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
406 int i; |
182
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
407 int has_extension = 0; |
312 | 408 Uint32 pos; |
182
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
409 |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
410 /* |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
411 * If the extension is "flac", we'll believe that this is really meant |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
412 * to be a FLAC stream, and will try to grok it from existing metadata. |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
413 * metadata searching can be a very expensive operation, however, so |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
414 * unless the user swears that it is a FLAC stream through the extension, |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
415 * we decide what to do based on the existance of a 32-bit magic number. |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
416 */ |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
417 for (i = 0; extensions_flac[i] != NULL; i++) |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
418 { |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
419 if (__Sound_strcasecmp(ext, extensions_flac[i]) == 0) |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
420 { |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
421 has_extension = 1; |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
422 break; |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
423 } /* if */ |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
424 } /* for */ |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
425 |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
426 if (!has_extension) |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
427 { |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
428 int rc; |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
429 Uint32 flac_magic = SDL_ReadLE32(rw); |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
430 BAIL_IF_MACRO(flac_magic != FLAC_MAGIC, "FLAC: Not a FLAC stream.", 0); |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
431 |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
432 /* move back over magic number for metadata scan... */ |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
433 rc = SDL_RWseek(internal->rw, -sizeof (flac_magic), SEEK_CUR); |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
434 BAIL_IF_MACRO(rc < 0, ERR_IO_ERROR, 0); |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
435 } /* if */ |
155 | 436 |
437 f = (flac_t *) malloc(sizeof (flac_t)); | |
438 BAIL_IF_MACRO(f == NULL, ERR_OUT_OF_MEMORY, 0); | |
439 | |
312 | 440 decoder = d_new(); |
155 | 441 if (decoder == NULL) |
442 { | |
443 free(f); | |
387
fb519e6028e3
Changed all the Sound_SetError() calls to __Sound_SetError (or BAIL*_MACRO)
Ryan C. Gordon <icculus@icculus.org>
parents:
377
diff
changeset
|
444 BAIL_MACRO(ERR_OUT_OF_MEMORY, 0); |
155 | 445 } /* if */ |
446 | |
515
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
447 #ifdef LEGACY_FLAC |
312 | 448 d_set_read_callback(decoder, read_callback); |
449 d_set_write_callback(decoder, write_callback); | |
450 d_set_metadata_callback(decoder, metadata_callback); | |
451 d_set_error_callback(decoder, error_callback); | |
452 d_set_seek_callback(decoder, seek_callback); | |
453 d_set_tell_callback(decoder, tell_callback); | |
454 d_set_length_callback(decoder, length_callback); | |
455 d_set_eof_callback(decoder, eof_callback); | |
422
5b06e23d934e
Removed backwards compatible API nastiness from FLAC decoder.
Ryan C. Gordon <icculus@icculus.org>
parents:
400
diff
changeset
|
456 |
312 | 457 d_set_client_data(decoder, f); |
515
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
458 #endif |
155 | 459 |
460 f->rw = internal->rw; | |
461 f->sample = sample; | |
462 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
|
463 f->sample->actual.format = 0; |
182
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
464 f->is_flac = 0 /* !!! FIXME: should be "has_extension", not "0". */; |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
465 |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
466 internal->decoder_private = f; |
515
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
467 /* really should check the init return value here: */ |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
468 #ifdef LEGACY_FLAC |
312 | 469 d_init(decoder); |
515
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
470 #else |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
471 FLAC__stream_decoder_init_stream(decoder, read_callback, seek_callback, |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
472 tell_callback, length_callback, |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
473 eof_callback, write_callback, |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
474 metadata_callback, error_callback, f); |
c5f184b4b2cd
Updated to libFLAC 1.1.3 API.
Ryan C. Gordon <icculus@icculus.org>
parents:
443
diff
changeset
|
475 #endif |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
476 |
400 | 477 sample->flags = SOUND_SAMPLEFLAG_NONE; |
478 | |
479 pos = SDL_RWtell(f->rw); | |
480 if (SDL_RWseek(f->rw, 0, SEEK_END) > 0) | |
481 { | |
482 f->stream_length = SDL_RWtell(f->rw); | |
483 if (SDL_RWseek(f->rw, pos, SEEK_SET) == -1) | |
484 { | |
485 free_flac(f); | |
486 BAIL_MACRO(ERR_IO_ERROR, 0); | |
487 } /* if */ | |
488 sample->flags = SOUND_SAMPLEFLAG_CANSEEK; | |
489 } /* if */ | |
490 | |
231
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
491 /* |
182
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
492 * If we are not sure this is a FLAC stream, check for the STREAMINFO |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
493 * metadata block. If not, we'd have to peek at the first audio frame |
182
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
494 * and get the sound format from there, but that is not yet |
3849438b735e
Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents:
166
diff
changeset
|
495 * implemented. |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
496 */ |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
497 if (!f->is_flac) |
155 | 498 { |
312 | 499 d_process_metadata(decoder); |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
500 |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
501 /* Still not FLAC? Give up. */ |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
502 if (!f->is_flac) |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
503 { |
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
504 free_flac(f); |
387
fb519e6028e3
Changed all the Sound_SetError() calls to __Sound_SetError (or BAIL*_MACRO)
Ryan C. Gordon <icculus@icculus.org>
parents:
377
diff
changeset
|
505 BAIL_MACRO("FLAC: No metadata found. Not a FLAC stream?", 0); |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
506 } /* if */ |
155 | 507 } /* if */ |
508 | |
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
|
509 SNDDBG(("FLAC: Accepting data stream.\n")); |
155 | 510 return(1); |
511 } /* FLAC_open */ | |
512 | |
513 | |
514 static void FLAC_close(Sound_Sample *sample) | |
515 { | |
516 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
517 flac_t *f = (flac_t *) internal->decoder_private; | |
518 | |
166
d8904267d23c
Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents:
164
diff
changeset
|
519 free_flac(f); |
155 | 520 } /* FLAC_close */ |
521 | |
522 | |
523 static Uint32 FLAC_read(Sound_Sample *sample) | |
524 { | |
525 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; | |
526 flac_t *f = (flac_t *) internal->decoder_private; | |
527 Uint32 len; | |
528 | |
312 | 529 if (!d_process_one_frame(f->decoder)) |
155 | 530 { |
531 sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
387
fb519e6028e3
Changed all the Sound_SetError() calls to __Sound_SetError (or BAIL*_MACRO)
Ryan C. Gordon <icculus@icculus.org>
parents:
377
diff
changeset
|
532 BAIL_MACRO("FLAC: Couldn't decode frame.", 0); |
155 | 533 } /* if */ |
534 | |
312 | 535 if (d_get_state(f->decoder) == D_END_OF_STREAM) |
231
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
536 { |
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
537 sample->flags |= SOUND_SAMPLEFLAG_EOF; |
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
538 return(0); |
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
539 } /* if */ |
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
540 |
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
|
541 /* 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
|
542 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
|
543 return(0); |
231
d3dc34315ac7
Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents:
221
diff
changeset
|
544 |
155 | 545 return(f->frame_size); |
546 } /* FLAC_read */ | |
547 | |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
184
diff
changeset
|
548 |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
184
diff
changeset
|
549 static int FLAC_rewind(Sound_Sample *sample) |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
184
diff
changeset
|
550 { |
312 | 551 return FLAC_seek(sample, 0); |
221
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
184
diff
changeset
|
552 } /* FLAC_rewind */ |
c9772a9f5271
Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents:
184
diff
changeset
|
553 |
306
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
554 |
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
555 static int FLAC_seek(Sound_Sample *sample, Uint32 ms) |
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
556 { |
312 | 557 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; |
558 flac_t *f = (flac_t *) internal->decoder_private; | |
559 | |
560 d_seek_absolute(f->decoder, (ms * sample->actual.rate) / 1000); | |
561 return(1); | |
306
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
562 } /* FLAC_seek */ |
c97be6e1bd27
Added framework for Sound_Seek() support.
Ryan C. Gordon <icculus@icculus.org>
parents:
231
diff
changeset
|
563 |
155 | 564 #endif /* SOUND_SUPPORTS_FLAC */ |
565 | |
566 /* end of flac.c ... */ |