This repository was archived by the owner on Jan 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFlacReader.c
302 lines (266 loc) · 9.15 KB
/
FlacReader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#if USE_FLAC == 2
#include "flac.c"
static const char* foxenFlacDecode(FILE* fp, uint32_t size, FaunBuffer* buf,
const void* preRead, size_t preReadLen)
{
const char* error = NULL;
const int bufSize = 256;
const int decodeSize = 512;
uint8_t* readBuf;
int32_t* decodeBuf;
size_t toRead, n;
uint32_t inUsed, procLen;
uint32_t bufPos = 0;
uint32_t frate;
uint32_t fchannels;
uint32_t frames = 0;
float* pcmOut = NULL;
float ds0, ds1;
fx_flac_state_t fstate;
fx_flac_t* flac = FX_FLAC_ALLOC_SUBSET_FORMAT_DAT();
decodeBuf = (int32_t*) malloc(decodeSize*sizeof(int32_t) + bufSize);
readBuf = (uint8_t*) (decodeBuf + decodeSize);
memcpy(readBuf, preRead, preReadLen);
bufPos = preReadLen;
if (size)
size -= preReadLen;
else
size = UINT32_MAX;
while (1) {
if (size) {
toRead = bufSize - bufPos;
if (toRead > 0) {
if (toRead > size)
toRead = size;
n = fread(readBuf + bufPos, 1, toRead, fp);
if (n == 0) {
size = 0; // Stop reading but continue decoding.
} else {
size -= n;
bufPos += n; // Advance the write cursor
}
}
}
inUsed = bufPos;
procLen = decodeSize;
fstate = fx_flac_process(flac, readBuf, &inUsed,
decodeBuf, &procLen);
if (fstate == FLAC_ERR) {
error = "FLAC decode failed";
break;
}
if (fstate == FLAC_END_OF_METADATA) {
frate = fx_flac_get_streaminfo(flac, FLAC_KEY_SAMPLE_RATE);
fchannels = fx_flac_get_streaminfo(flac, FLAC_KEY_N_CHANNELS);
frames = fx_flac_get_streaminfo(flac, FLAC_KEY_N_SAMPLES);
//printf("FLAC rate:%d channels:%d samples:%d\n",
// frate, fchannels, frames);
if (frate != 44100 && frate != 22050) {
error = "FLAC sample rate is unsupported";
break;
}
// NOTE: Zero for total samples denotes 'unknown' and is valid.
if (! frames) {
error = "FLAC total samples is unknown";
break;
}
_allocBufferVoice(buf, frames);
pcmOut = buf->sample.f32;
}
// Save decoded samples to PCM buffer.
if (pcmOut) {
if (frate == 22050) {
if (fchannels == 1) {
for (uint32_t i = 0; i < procLen; i++) {
ds0 = (decodeBuf[i] >> 16) / 32767.0f;
*pcmOut++ = ds0;
*pcmOut++ = ds0;
*pcmOut++ = ds0;
*pcmOut++ = ds0;
}
} else {
for (uint32_t i = 0; i < procLen; i++) {
ds0 = (decodeBuf[i] >> 16) / 32767.0f;
++i;
ds1 = (decodeBuf[i] >> 16) / 32767.0f;
*pcmOut++ = ds0;
*pcmOut++ = ds1;
*pcmOut++ = ds0;
*pcmOut++ = ds1;
}
}
} else {
if (fchannels == 1) {
for (uint32_t i = 0; i < procLen; i++) {
ds0 = (decodeBuf[i] >> 16) / 32767.0f;
*pcmOut++ = ds0;
*pcmOut++ = ds0;
}
} else {
for (uint32_t i = 0; i < procLen; i++)
*pcmOut++ = (decodeBuf[i] >> 16) / 32767.0f;
}
}
}
n = bufPos - inUsed;
if (n == 0) {
// Exit loop when both decoding & reading are done.
if (procLen == 0 && size == 0)
break;
} else if (inUsed) {
// Move unprocessed bytes to the beginning of readBuf.
//printf("KR unproc %ld pos:%d used:%d\n", n, bufPos, inUsed);
memmove(readBuf, readBuf + inUsed, n);
}
bufPos = n;
}
buf->used = frames;
free(decodeBuf);
free(flac);
return error;
}
#else
#include <FLAC/stream_decoder.h>
typedef struct {
FaunBuffer* buf;
float* pcmOut;
FILE* fp;
uint32_t length;
uint64_t totalSamples;
uint32_t rate;
uint16_t channels;
uint16_t bitsPerSample;
} FlacReader;
static FLAC__StreamDecoderReadStatus flacRead(const FLAC__StreamDecoder* fdec,
FLAC__byte buffer[], size_t* bytes, void* client_data)
{
FlacReader* rd = (FlacReader*) client_data;
size_t len = *bytes;
(void) fdec;
if (len > rd->length)
len = rd->length;
len = fread(buffer, 1, len, rd->fp);
rd->length -= len;
*bytes = len;
if (len > 0)
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
if (ferror(rd->fp))
return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
}
static void flacMetadata(const FLAC__StreamDecoder* fdec,
const FLAC__StreamMetadata* metadata, void* client_data)
{
FlacReader* rd = (FlacReader*) client_data;
(void)fdec;
if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
if (rd->pcmOut) {
fprintf(_errStream, "FLAC stream_info repeated\n");
return;
}
rd->totalSamples = metadata->data.stream_info.total_samples;
rd->rate = metadata->data.stream_info.sample_rate;
rd->channels = metadata->data.stream_info.channels;
rd->bitsPerSample = metadata->data.stream_info.bits_per_sample;
#if 0
printf("FLAC meta samples:%lu rate:%d chan:%d bps:%d\n",
rd->totalSamples, rd->rate, rd->channels, rd->bitsPerSample);
#endif
if (rd->rate != 44100 && rd->rate != 22050) {
fprintf(_errStream, "FLAC sample rate %d not handled\n", rd->rate);
return;
}
if (rd->bitsPerSample < 16) {
fprintf(_errStream, "FLAC bps %d not handled\n", rd->bitsPerSample);
return;
}
if (rd->totalSamples) {
_allocBufferVoice(rd->buf, rd->totalSamples);
rd->pcmOut = rd->buf->sample.f32;
}
}
}
static FLAC__StreamDecoderWriteStatus flacWrite(const FLAC__StreamDecoder* fdec,
const FLAC__Frame* frame, const FLAC__int32* const buffer[], void* client_data)
{
FlacReader* rd = (FlacReader*) client_data;
float* pcmOut = rd->pcmOut;
const FLAC__int32* chan0 = buffer[0];
const FLAC__int32* chan1;
size_t i, procLen = frame->header.blocksize;
int shift = rd->bitsPerSample - 16;
float ds0, ds1;
(void) fdec;
if (! pcmOut)
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
if (rd->rate == 22050) {
if (rd->channels == 1) {
for (i = 0; i < procLen; i++) {
ds0 = (chan0[i] >> shift) / 32767.0f;
*pcmOut++ = ds0;
*pcmOut++ = ds0;
*pcmOut++ = ds0;
*pcmOut++ = ds0;
}
} else {
chan1 = buffer[1];
for (i = 0; i < procLen; i++) {
ds0 = (chan0[i] >> shift) / 32767.0f;
ds1 = (chan1[i] >> shift) / 32767.0f;
*pcmOut++ = ds0;
*pcmOut++ = ds1;
*pcmOut++ = ds0;
*pcmOut++ = ds1;
}
}
} else {
if (rd->channels == 1) {
for (i = 0; i < procLen; i++) {
ds0 = (chan0[i] >> shift) / 32767.0f;
*pcmOut++ = ds0;
*pcmOut++ = ds0;
}
} else {
chan1 = buffer[1];
for (i = 0; i < procLen; i++) {
*pcmOut++ = (chan0[i] >> shift) / 32767.0f;
*pcmOut++ = (chan1[i] >> shift) / 32767.0f;
}
}
}
rd->pcmOut = pcmOut;
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}
static void flacError(const FLAC__StreamDecoder* fdec,
FLAC__StreamDecoderErrorStatus status, void* client_data)
{
(void) fdec;
(void) client_data;
fprintf(_errStream, "FLAC decode error: %s\n",
FLAC__StreamDecoderErrorStatusString[status]);
}
static const char* libFlacDecode(FILE* fp, uint32_t size, FaunBuffer* buf)
{
FLAC__StreamDecoder* dec;
const char* error = NULL;
FlacReader fr;
fr.buf = buf;
fr.pcmOut = NULL;
fr.fp = fp;
fr.length = size ? size : UINT32_MAX;
fr.totalSamples = 0;
fr.rate = 0;
dec = FLAC__stream_decoder_new();
if (FLAC__stream_decoder_init_stream(dec, flacRead, NULL, NULL, NULL, NULL,
flacWrite, flacMetadata, flacError, &fr)
== FLAC__STREAM_DECODER_INIT_STATUS_OK) {
if (FLAC__stream_decoder_process_until_end_of_stream(dec))
buf->used = fr.totalSamples;
else
error = "FLAC process failed";
} else
error = "FLAC decoder init failed";
FLAC__stream_decoder_delete(dec);
return error;
}
#endif