+/** @brief Metadata callback for FLAC decoder
+ *
+ * This is a no-op here.
+ */
+static void flac_metadata(const FLAC__FileDecoder attribute((unused)) *decoder,
+ const FLAC__StreamMetadata attribute((unused)) *metadata,
+ void attribute((unused)) *client_data) {
+}
+
+/** @brief Error callback for FLAC decoder */
+static void flac_error(const FLAC__FileDecoder attribute((unused)) *decoder,
+ FLAC__StreamDecoderErrorStatus status,
+ void attribute((unused)) *client_data) {
+ fatal(0, "error decoding %s: %s", path,
+ FLAC__StreamDecoderErrorStatusString[status]);
+}
+
+/** @brief Write callback for FLAC decoder */
+static FLAC__StreamDecoderWriteStatus flac_write
+ (const FLAC__FileDecoder attribute((unused)) *decoder,
+ const FLAC__Frame *frame,
+ const FLAC__int32 *const buffer[],
+ void attribute((unused)) *client_data) {
+ size_t n, c;
+
+ output_header(frame->header.sample_rate,
+ frame->header.channels,
+ frame->header.bits_per_sample,
+ (frame->header.channels * frame->header.blocksize
+ * frame->header.bits_per_sample) / 8,
+ ENDIAN_BIG);
+ for(n = 0; n < frame->header.blocksize; ++n) {
+ for(c = 0; c < frame->header.channels; ++c) {
+ switch(frame->header.bits_per_sample) {
+ case 8: output_8(buffer[c][n]); break;
+ case 16: output_16(buffer[c][n]); break;
+ case 24: output_24(buffer[c][n]); break;
+ case 32: output_32(buffer[c][n]); break;
+ }
+ }
+ }
+ return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
+}
+
+
+/** @brief FLAC file decoder */
+static void decode_flac(void) {
+ FLAC__FileDecoder *fd = 0;
+ FLAC__FileDecoderState fs;
+
+ if(!(fd = FLAC__file_decoder_new()))
+ fatal(0, "FLAC__file_decoder_new failed");
+ if(!(FLAC__file_decoder_set_filename(fd, path)))
+ fatal(0, "FLAC__file_set_filename failed");
+ FLAC__file_decoder_set_metadata_callback(fd, flac_metadata);
+ FLAC__file_decoder_set_error_callback(fd, flac_error);
+ FLAC__file_decoder_set_write_callback(fd, flac_write);
+ if((fs = FLAC__file_decoder_init(fd)))
+ fatal(0, "FLAC__file_decoder_init: %s", FLAC__FileDecoderStateString[fs]);
+ FLAC__file_decoder_process_until_end_of_file(fd);
+}
+