annotate decoders/flac.c @ 304:0cf2b2e4a259

Updated.
author Ryan C. Gordon <icculus@icculus.org>
date Sun, 21 Apr 2002 17:47:52 +0000
parents d3dc34315ac7
children c97be6e1bd27
rev   line source
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
1 /*
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
2 * SDL_sound -- An abstract sound format decoding API.
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
3 * Copyright (C) 2001 Ryan C. Gordon.
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
4 *
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
72ff7d3a25b6 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.
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
9 *
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
13 * Lesser General Public License for more details.
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
14 *
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
18 */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
19
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
20 /*
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
21 * FLAC decoder for SDL_sound.
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
22 *
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
23 * This driver handles FLAC audio, that is to say the Free Lossless Audio
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
24 * Codec. It depends on libFLAC for decoding, which can be grabbed from:
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
25 * http://flac.sourceforge.net
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
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
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
28 *
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
29 * This file written by Torbjörn Andersson. (d91tan@Update.UU.SE)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
30 */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
31
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
32 #if HAVE_CONFIG_H
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
33 # include <config.h>
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
34 #endif
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
35
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
36 #ifdef SOUND_SUPPORTS_FLAC
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
37
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
38 #include <stdio.h>
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
39 #include <stdlib.h>
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
40 #include <string.h>
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
41 #include <assert.h>
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
42
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
43 #include "SDL_sound.h"
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
44
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
45 #define __SDL_SOUND_INTERNAL__
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
46 #include "SDL_sound_internal.h"
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
47
231
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
48 /*
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
49 * libFLAC 1.0.1 added a seekable stream decoder, but if I understand the
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
50 * documentation correctly it's still much easier for us to handle the rewind
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
51 * method ourselves.
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
52 */
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
53
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
54 #include "FLAC/stream_decoder.h"
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
55
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
56
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
57 static int FLAC_init(void);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
58 static void FLAC_quit(void);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
59 static int FLAC_open(Sound_Sample *sample, const char *ext);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
60 static void FLAC_close(Sound_Sample *sample);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
61 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
62 static int FLAC_rewind(Sound_Sample *sample);
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
63
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
64 static const char *extensions_flac[] = { "FLAC", "FLA", NULL };
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
65
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
66 const Sound_DecoderFunctions __Sound_DecoderFunctions_FLAC =
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
67 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
68 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
69 extensions_flac,
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
70 "Free Lossless Audio Codec",
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
71 "Torbjörn Andersson <d91tan@Update.UU.SE>",
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
72 "http://flac.sourceforge.net/"
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
73 },
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
74
221
c9772a9f5271 Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 184
diff changeset
75 FLAC_init, /* init() method */
c9772a9f5271 Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 184
diff changeset
76 FLAC_quit, /* quit() method */
c9772a9f5271 Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 184
diff changeset
77 FLAC_open, /* open() method */
c9772a9f5271 Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 184
diff changeset
78 FLAC_close, /* close() method */
c9772a9f5271 Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 184
diff changeset
79 FLAC_read, /* read() method */
c9772a9f5271 Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 184
diff changeset
80 FLAC_rewind /* rewind() method */
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
81 };
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
82
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
83 /* This is what we store in our internal->decoder_private field. */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
84 typedef struct
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
85 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
86 FLAC__StreamDecoder *decoder;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
87 SDL_RWops *rw;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
88 Sound_Sample *sample;
231
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
89 Uint32 data_starting_offset;
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
90 Uint32 frame_size;
166
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
91 Uint8 is_flac;
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
92 } flac_t;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
93
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
94
166
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
95 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
96 {
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
97 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
98 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
99 free(f);
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
100 } /* free_flac */
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
101
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
102
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
103 static FLAC__StreamDecoderReadStatus FLAC_read_callback(
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
104 const FLAC__StreamDecoder *decoder, FLAC__byte buffer[],
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
105 unsigned int *bytes, void *client_data)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
106 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
107 flac_t *f = (flac_t *) client_data;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
108 Uint32 retval;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
109
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
110 #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
111 SNDDBG(("FLAC: Read callback.\n"));
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
112 #endif
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
113
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
114 retval = SDL_RWread(f->rw, (Uint8 *) buffer, 1, *bytes);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
115
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
116 if (retval == 0)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
117 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
118 *bytes = 0;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
119 f->sample->flags |= SOUND_SAMPLEFLAG_EOF;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
120 return(FLAC__STREAM_DECODER_READ_END_OF_STREAM);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
121 } /* if */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
122
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
123 if (retval == -1)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
124 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
125 *bytes = 0;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
126 f->sample->flags |= SOUND_SAMPLEFLAG_ERROR;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
127 return(FLAC__STREAM_DECODER_READ_ABORT);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
128 } /* if */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
129
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
130 if (retval < *bytes)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
131 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
132 *bytes = retval;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
133 f->sample->flags |= SOUND_SAMPLEFLAG_EAGAIN;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
134 } /* if */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
135
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
136 return(FLAC__STREAM_DECODER_READ_CONTINUE);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
137 } /* FLAC_read_callback */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
138
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
139
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
140 static FLAC__StreamDecoderWriteStatus FLAC_write_callback(
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
141 const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame,
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
142 const FLAC__int32 *buffer[], void *client_data)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
143 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
144 flac_t *f = (flac_t *) client_data;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
145 Uint32 i, j;
166
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
146 Uint32 sample;
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
147 Uint8 *dst;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
148
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
149 #if 0
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
150 SNDDBG(("FLAC: Write callback.\n"));
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
151 #endif
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
152
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
153 f->frame_size = frame->header.channels * frame->header.blocksize
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
154 * frame->header.bits_per_sample / 8;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
156 if (f->frame_size > f->sample->buffer_size)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
157 Sound_SetBufferSize(f->sample, f->frame_size);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
158
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
159 dst = f->sample->buffer;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
160
166
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
161 /* 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
162 * 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
163 * 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
164 * 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
165 */
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
166 if (f->sample->actual.format == AUDIO_S8)
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
167 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
168 for (i = 0; i < frame->header.blocksize; i++)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
169 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
170 {
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
171 sample = buffer[j][i];
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
172 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
173 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
174 *dst++ = sample & 0x00ff;
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
175 } /* for */
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
176 } /* if */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
177 else
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
178 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
179 for (i = 0; i < frame->header.blocksize; i++)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
180 for (j = 0; j < frame->header.channels; j++)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
181 {
166
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
182 sample = buffer[j][i];
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
183 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
184 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
185 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
186 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
187 *dst++ = (sample & 0xff00) >> 8;
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
188 *dst++ = sample & 0x00ff;
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
189 } /* for */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
190 } /* else */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
191
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
192 return(FLAC__STREAM_DECODER_WRITE_CONTINUE);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
193 } /* FLAC_write_callback */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
194
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
195
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
196 void FLAC_metadata_callback(
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
197 const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata,
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
198 void *client_data)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
199 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
200 flac_t *f = (flac_t *) client_data;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
201
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
202 SNDDBG(("FLAC: Metadata callback.\n"));
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
203
166
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
204 /* 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
205 * 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
206 */
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
207 if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
208 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
209 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
210
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
211 f->is_flac = 1;
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
212 f->sample->actual.channels = metadata->data.stream_info.channels;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
213 f->sample->actual.rate = metadata->data.stream_info.sample_rate;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
214
166
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
215 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
216 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
217 else
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
218 f->sample->actual.format = AUDIO_S8;
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
219 } /* if */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
220 } /* FLAC_metadata_callback */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
221
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
222
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
223 void FLAC_error_callback(
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
224 const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status,
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
225 void *client_data)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
226 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
227 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
228
166
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
229 /* !!! 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
230 Sound_SetError(FLAC__StreamDecoderErrorStatusString[status]);
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
231 f->sample->flags |= SOUND_SAMPLEFLAG_ERROR;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
232 } /* FLAC_error_callback */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
233
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
234
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
235 static int FLAC_init(void)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
236 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
237 return(1); /* always succeeds. */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
238 } /* FLAC_init */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
239
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
240
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
241 static void FLAC_quit(void)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
242 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
243 /* it's a no-op. */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
244 } /* FLAC_quit */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
245
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
246
182
3849438b735e Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents: 166
diff changeset
247 #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
248
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
249 static int FLAC_open(Sound_Sample *sample, const char *ext)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
250 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
251 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
252 SDL_RWops *rw = internal->rw;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
253 FLAC__StreamDecoder *decoder;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
254 flac_t *f;
166
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
255 int i;
182
3849438b735e Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents: 166
diff changeset
256 int has_extension = 0;
3849438b735e Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents: 166
diff changeset
257
3849438b735e Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents: 166
diff changeset
258 /*
3849438b735e Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents: 166
diff changeset
259 * 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
260 * 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
261 * 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
262 * 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
263 * 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
264 */
3849438b735e Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents: 166
diff changeset
265 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
266 {
3849438b735e Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents: 166
diff changeset
267 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
268 {
3849438b735e Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents: 166
diff changeset
269 has_extension = 1;
3849438b735e Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents: 166
diff changeset
270 break;
3849438b735e Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents: 166
diff changeset
271 } /* if */
3849438b735e Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents: 166
diff changeset
272 } /* for */
3849438b735e Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents: 166
diff changeset
273
3849438b735e Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents: 166
diff changeset
274 if (!has_extension)
3849438b735e Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents: 166
diff changeset
275 {
3849438b735e Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents: 166
diff changeset
276 int rc;
3849438b735e Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents: 166
diff changeset
277 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
278 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
279
3849438b735e Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents: 166
diff changeset
280 /* 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
281 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
282 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
283 } /* if */
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
284
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
285 f = (flac_t *) malloc(sizeof (flac_t));
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
286 BAIL_IF_MACRO(f == NULL, ERR_OUT_OF_MEMORY, 0);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
287
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
288 decoder = FLAC__stream_decoder_new();
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
289 if (decoder == NULL)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
290 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
291 Sound_SetError(ERR_OUT_OF_MEMORY);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
292 free(f);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
293 return(0);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
294 } /* if */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
295
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
296 FLAC__stream_decoder_set_read_callback(decoder, FLAC_read_callback);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
297 FLAC__stream_decoder_set_write_callback(decoder, FLAC_write_callback);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
298 FLAC__stream_decoder_set_metadata_callback(decoder, FLAC_metadata_callback);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
299 FLAC__stream_decoder_set_error_callback(decoder, FLAC_error_callback);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
300 FLAC__stream_decoder_set_client_data(decoder, f);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
301
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
302 f->rw = internal->rw;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
303 f->sample = sample;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
304 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
305 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
306 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
307
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
308 internal->decoder_private = f;
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
309 FLAC__stream_decoder_init(decoder);
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
310
182
3849438b735e Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents: 166
diff changeset
311 /*
231
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
312 * Annoyingly, the rewind method will put the FLAC decoder in a state
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
313 * where it expects to read metadata, so we have to set this marker
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
314 * before the metadata block.
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
315 */
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
316 f->data_starting_offset = SDL_RWtell(f->rw);
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
317
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
318 /*
182
3849438b735e Checks magic number again, unless the file extension is recognized.
Ryan C. Gordon <icculus@icculus.org>
parents: 166
diff changeset
319 * 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
320 * 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
321 * 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
322 * implemented.
166
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
323 */
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
324 if (!f->is_flac)
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
325 {
166
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
326 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
327
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
328 /* Still not FLAC? Give up. */
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
329 if (!f->is_flac)
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
330 {
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
331 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
332 free_flac(f);
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
333 return(0);
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
334 } /* if */
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
335 } /* if */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
336
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
337 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
338
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
339 sample->flags = SOUND_SAMPLEFLAG_NONE;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
340 return(1);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
341 } /* FLAC_open */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
342
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
343
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
344 static void FLAC_close(Sound_Sample *sample)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
345 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
346 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
347 flac_t *f = (flac_t *) internal->decoder_private;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
348
166
d8904267d23c Cleanups, fixes and enhancements by Torbj�rn Andersson.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
349 free_flac(f);
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
350 } /* FLAC_close */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
351
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
352
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
353 static Uint32 FLAC_read(Sound_Sample *sample)
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
354 {
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
355 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
356 flac_t *f = (flac_t *) internal->decoder_private;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
357 Uint32 len;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
358
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
359 if (!FLAC__stream_decoder_process_one_frame(f->decoder))
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
360 {
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
361 Sound_SetError("FLAC: Couldn't decode frame.");
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
362 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
363 return(0);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
364 } /* if */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
365
231
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
366 if (FLAC__stream_decoder_get_state(f->decoder) == FLAC__STREAM_DECODER_END_OF_STREAM)
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
367 {
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
368 sample->flags |= SOUND_SAMPLEFLAG_EOF;
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
369 return(0);
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
370 } /* if */
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
371
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
372 /* 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
373 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
374 return(0);
231
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
375
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
376 return(f->frame_size);
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
377 } /* FLAC_read */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
378
221
c9772a9f5271 Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 184
diff changeset
379
c9772a9f5271 Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 184
diff changeset
380 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
381 {
231
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
382 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
383 flac_t *f = (flac_t *) internal->decoder_private;
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
384 int rc = SDL_RWseek(f->rw, f->data_starting_offset, SEEK_SET);
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
385
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
386 BAIL_IF_MACRO(rc != f->data_starting_offset, ERR_IO_ERROR, 0);
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
387 BAIL_IF_MACRO(!FLAC__stream_decoder_reset(f->decoder),
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
388 "FLAC: could not reset decoder", 0);
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
389 FLAC__stream_decoder_process_metadata(f->decoder);
d3dc34315ac7 Rewind method implemented by Torbj�rn.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
390 return(1);
221
c9772a9f5271 Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 184
diff changeset
391 } /* FLAC_rewind */
c9772a9f5271 Initial implementation or stubs for rewind method. Other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 184
diff changeset
392
155
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
393 #endif /* SOUND_SUPPORTS_FLAC */
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
394
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
395
72ff7d3a25b6 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
396 /* end of flac.c ... */