cannam@86: /* example_cpp_decode_file - Simple FLAC file decoder using libFLAC cannam@86: * Copyright (C) 2007 Josh Coalson cannam@86: * cannam@86: * This program is free software; you can redistribute it and/or cannam@86: * modify it under the terms of the GNU General Public License cannam@86: * as published by the Free Software Foundation; either version 2 cannam@86: * of the License, or (at your option) any later version. cannam@86: * cannam@86: * This program is distributed in the hope that it will be useful, cannam@86: * but WITHOUT ANY WARRANTY; without even the implied warranty of cannam@86: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the cannam@86: * GNU General Public License for more details. cannam@86: * cannam@86: * You should have received a copy of the GNU General Public License cannam@86: * along with this program; if not, write to the Free Software cannam@86: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. cannam@86: */ cannam@86: cannam@86: /* cannam@86: * This example shows how to use libFLAC++ to decode a FLAC file to a WAVE cannam@86: * file. It only supports 16-bit stereo files. cannam@86: * cannam@86: * Complete API documentation can be found at: cannam@86: * http://flac.sourceforge.net/api/ cannam@86: */ cannam@86: cannam@86: #if HAVE_CONFIG_H cannam@86: # include cannam@86: #endif cannam@86: cannam@86: #include cannam@86: #include cannam@86: #include "FLAC++/decoder.h" cannam@86: cannam@86: static FLAC__uint64 total_samples = 0; cannam@86: static unsigned sample_rate = 0; cannam@86: static unsigned channels = 0; cannam@86: static unsigned bps = 0; cannam@86: cannam@86: static bool write_little_endian_uint16(FILE *f, FLAC__uint16 x) cannam@86: { cannam@86: return cannam@86: fputc(x, f) != EOF && cannam@86: fputc(x >> 8, f) != EOF cannam@86: ; cannam@86: } cannam@86: cannam@86: static bool write_little_endian_int16(FILE *f, FLAC__int16 x) cannam@86: { cannam@86: return write_little_endian_uint16(f, (FLAC__uint16)x); cannam@86: } cannam@86: cannam@86: static bool write_little_endian_uint32(FILE *f, FLAC__uint32 x) cannam@86: { cannam@86: return cannam@86: fputc(x, f) != EOF && cannam@86: fputc(x >> 8, f) != EOF && cannam@86: fputc(x >> 16, f) != EOF && cannam@86: fputc(x >> 24, f) != EOF cannam@86: ; cannam@86: } cannam@86: cannam@86: class OurDecoder: public FLAC::Decoder::File { cannam@86: public: cannam@86: OurDecoder(FILE *f_): FLAC::Decoder::File(), f(f_) { } cannam@86: protected: cannam@86: FILE *f; cannam@86: cannam@86: virtual ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]); cannam@86: virtual void metadata_callback(const ::FLAC__StreamMetadata *metadata); cannam@86: virtual void error_callback(::FLAC__StreamDecoderErrorStatus status); cannam@86: }; cannam@86: cannam@86: int main(int argc, char *argv[]) cannam@86: { cannam@86: bool ok = true; cannam@86: FILE *fout; cannam@86: cannam@86: if(argc != 3) { cannam@86: fprintf(stderr, "usage: %s infile.flac outfile.wav\n", argv[0]); cannam@86: return 1; cannam@86: } cannam@86: cannam@86: if((fout = fopen(argv[2], "wb")) == NULL) { cannam@86: fprintf(stderr, "ERROR: opening %s for output\n", argv[2]); cannam@86: return 1; cannam@86: } cannam@86: cannam@86: OurDecoder decoder(fout); cannam@86: cannam@86: if(!decoder) { cannam@86: fprintf(stderr, "ERROR: allocating decoder\n"); cannam@86: fclose(fout); cannam@86: return 1; cannam@86: } cannam@86: cannam@86: (void)decoder.set_md5_checking(true); cannam@86: cannam@86: FLAC__StreamDecoderInitStatus init_status = decoder.init(argv[1]); cannam@86: if(init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) { cannam@86: fprintf(stderr, "ERROR: initializing decoder: %s\n", FLAC__StreamDecoderInitStatusString[init_status]); cannam@86: ok = false; cannam@86: } cannam@86: cannam@86: if(ok) { cannam@86: ok = decoder.process_until_end_of_stream(); cannam@86: fprintf(stderr, "decoding: %s\n", ok? "succeeded" : "FAILED"); cannam@86: fprintf(stderr, " state: %s\n", decoder.get_state().resolved_as_cstring(decoder)); cannam@86: } cannam@86: cannam@86: fclose(fout); cannam@86: cannam@86: return 0; cannam@86: } cannam@86: cannam@86: ::FLAC__StreamDecoderWriteStatus OurDecoder::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]) cannam@86: { cannam@86: const FLAC__uint32 total_size = (FLAC__uint32)(total_samples * channels * (bps/8)); cannam@86: size_t i; cannam@86: cannam@86: if(total_samples == 0) { cannam@86: fprintf(stderr, "ERROR: this example only works for FLAC files that have a total_samples count in STREAMINFO\n"); cannam@86: return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; cannam@86: } cannam@86: if(channels != 2 || bps != 16) { cannam@86: fprintf(stderr, "ERROR: this example only supports 16bit stereo streams\n"); cannam@86: return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; cannam@86: } cannam@86: cannam@86: /* write WAVE header before we write the first frame */ cannam@86: if(frame->header.number.sample_number == 0) { cannam@86: if( cannam@86: fwrite("RIFF", 1, 4, f) < 4 || cannam@86: !write_little_endian_uint32(f, total_size + 36) || cannam@86: fwrite("WAVEfmt ", 1, 8, f) < 8 || cannam@86: !write_little_endian_uint32(f, 16) || cannam@86: !write_little_endian_uint16(f, 1) || cannam@86: !write_little_endian_uint16(f, (FLAC__uint16)channels) || cannam@86: !write_little_endian_uint32(f, sample_rate) || cannam@86: !write_little_endian_uint32(f, sample_rate * channels * (bps/8)) || cannam@86: !write_little_endian_uint16(f, (FLAC__uint16)(channels * (bps/8))) || /* block align */ cannam@86: !write_little_endian_uint16(f, (FLAC__uint16)bps) || cannam@86: fwrite("data", 1, 4, f) < 4 || cannam@86: !write_little_endian_uint32(f, total_size) cannam@86: ) { cannam@86: fprintf(stderr, "ERROR: write error\n"); cannam@86: return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; cannam@86: } cannam@86: } cannam@86: cannam@86: /* write decoded PCM samples */ cannam@86: for(i = 0; i < frame->header.blocksize; i++) { cannam@86: if( cannam@86: !write_little_endian_int16(f, (FLAC__int16)buffer[0][i]) || /* left channel */ cannam@86: !write_little_endian_int16(f, (FLAC__int16)buffer[1][i]) /* right channel */ cannam@86: ) { cannam@86: fprintf(stderr, "ERROR: write error\n"); cannam@86: return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; cannam@86: } cannam@86: } cannam@86: cannam@86: return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; cannam@86: } cannam@86: cannam@86: void OurDecoder::metadata_callback(const ::FLAC__StreamMetadata *metadata) cannam@86: { cannam@86: /* print some stats */ cannam@86: if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) { cannam@86: /* save for later */ cannam@86: total_samples = metadata->data.stream_info.total_samples; cannam@86: sample_rate = metadata->data.stream_info.sample_rate; cannam@86: channels = metadata->data.stream_info.channels; cannam@86: bps = metadata->data.stream_info.bits_per_sample; cannam@86: cannam@86: fprintf(stderr, "sample rate : %u Hz\n", sample_rate); cannam@86: fprintf(stderr, "channels : %u\n", channels); cannam@86: fprintf(stderr, "bits per sample: %u\n", bps); cannam@86: #ifdef _MSC_VER cannam@86: fprintf(stderr, "total samples : %I64u\n", total_samples); cannam@86: #else cannam@86: fprintf(stderr, "total samples : %llu\n", total_samples); cannam@86: #endif cannam@86: } cannam@86: } cannam@86: cannam@86: void OurDecoder::error_callback(::FLAC__StreamDecoderErrorStatus status) cannam@86: { cannam@86: fprintf(stderr, "Got error callback: %s\n", FLAC__StreamDecoderErrorStatusString[status]); cannam@86: }