annotate src/flac-1.2.1/examples/cpp/decode/file/main.cpp @ 86:98c1576536ae

Bring in flac, ogg, vorbis
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 19 Mar 2013 17:37:49 +0000
parents
children
rev   line source
cannam@86 1 /* example_cpp_decode_file - Simple FLAC file decoder using libFLAC
cannam@86 2 * Copyright (C) 2007 Josh Coalson
cannam@86 3 *
cannam@86 4 * This program is free software; you can redistribute it and/or
cannam@86 5 * modify it under the terms of the GNU General Public License
cannam@86 6 * as published by the Free Software Foundation; either version 2
cannam@86 7 * of the License, or (at your option) any later version.
cannam@86 8 *
cannam@86 9 * This program is distributed in the hope that it will be useful,
cannam@86 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cannam@86 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cannam@86 12 * GNU General Public License for more details.
cannam@86 13 *
cannam@86 14 * You should have received a copy of the GNU General Public License
cannam@86 15 * along with this program; if not, write to the Free Software
cannam@86 16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
cannam@86 17 */
cannam@86 18
cannam@86 19 /*
cannam@86 20 * This example shows how to use libFLAC++ to decode a FLAC file to a WAVE
cannam@86 21 * file. It only supports 16-bit stereo files.
cannam@86 22 *
cannam@86 23 * Complete API documentation can be found at:
cannam@86 24 * http://flac.sourceforge.net/api/
cannam@86 25 */
cannam@86 26
cannam@86 27 #if HAVE_CONFIG_H
cannam@86 28 # include <config.h>
cannam@86 29 #endif
cannam@86 30
cannam@86 31 #include <stdio.h>
cannam@86 32 #include <stdlib.h>
cannam@86 33 #include "FLAC++/decoder.h"
cannam@86 34
cannam@86 35 static FLAC__uint64 total_samples = 0;
cannam@86 36 static unsigned sample_rate = 0;
cannam@86 37 static unsigned channels = 0;
cannam@86 38 static unsigned bps = 0;
cannam@86 39
cannam@86 40 static bool write_little_endian_uint16(FILE *f, FLAC__uint16 x)
cannam@86 41 {
cannam@86 42 return
cannam@86 43 fputc(x, f) != EOF &&
cannam@86 44 fputc(x >> 8, f) != EOF
cannam@86 45 ;
cannam@86 46 }
cannam@86 47
cannam@86 48 static bool write_little_endian_int16(FILE *f, FLAC__int16 x)
cannam@86 49 {
cannam@86 50 return write_little_endian_uint16(f, (FLAC__uint16)x);
cannam@86 51 }
cannam@86 52
cannam@86 53 static bool write_little_endian_uint32(FILE *f, FLAC__uint32 x)
cannam@86 54 {
cannam@86 55 return
cannam@86 56 fputc(x, f) != EOF &&
cannam@86 57 fputc(x >> 8, f) != EOF &&
cannam@86 58 fputc(x >> 16, f) != EOF &&
cannam@86 59 fputc(x >> 24, f) != EOF
cannam@86 60 ;
cannam@86 61 }
cannam@86 62
cannam@86 63 class OurDecoder: public FLAC::Decoder::File {
cannam@86 64 public:
cannam@86 65 OurDecoder(FILE *f_): FLAC::Decoder::File(), f(f_) { }
cannam@86 66 protected:
cannam@86 67 FILE *f;
cannam@86 68
cannam@86 69 virtual ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
cannam@86 70 virtual void metadata_callback(const ::FLAC__StreamMetadata *metadata);
cannam@86 71 virtual void error_callback(::FLAC__StreamDecoderErrorStatus status);
cannam@86 72 };
cannam@86 73
cannam@86 74 int main(int argc, char *argv[])
cannam@86 75 {
cannam@86 76 bool ok = true;
cannam@86 77 FILE *fout;
cannam@86 78
cannam@86 79 if(argc != 3) {
cannam@86 80 fprintf(stderr, "usage: %s infile.flac outfile.wav\n", argv[0]);
cannam@86 81 return 1;
cannam@86 82 }
cannam@86 83
cannam@86 84 if((fout = fopen(argv[2], "wb")) == NULL) {
cannam@86 85 fprintf(stderr, "ERROR: opening %s for output\n", argv[2]);
cannam@86 86 return 1;
cannam@86 87 }
cannam@86 88
cannam@86 89 OurDecoder decoder(fout);
cannam@86 90
cannam@86 91 if(!decoder) {
cannam@86 92 fprintf(stderr, "ERROR: allocating decoder\n");
cannam@86 93 fclose(fout);
cannam@86 94 return 1;
cannam@86 95 }
cannam@86 96
cannam@86 97 (void)decoder.set_md5_checking(true);
cannam@86 98
cannam@86 99 FLAC__StreamDecoderInitStatus init_status = decoder.init(argv[1]);
cannam@86 100 if(init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
cannam@86 101 fprintf(stderr, "ERROR: initializing decoder: %s\n", FLAC__StreamDecoderInitStatusString[init_status]);
cannam@86 102 ok = false;
cannam@86 103 }
cannam@86 104
cannam@86 105 if(ok) {
cannam@86 106 ok = decoder.process_until_end_of_stream();
cannam@86 107 fprintf(stderr, "decoding: %s\n", ok? "succeeded" : "FAILED");
cannam@86 108 fprintf(stderr, " state: %s\n", decoder.get_state().resolved_as_cstring(decoder));
cannam@86 109 }
cannam@86 110
cannam@86 111 fclose(fout);
cannam@86 112
cannam@86 113 return 0;
cannam@86 114 }
cannam@86 115
cannam@86 116 ::FLAC__StreamDecoderWriteStatus OurDecoder::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[])
cannam@86 117 {
cannam@86 118 const FLAC__uint32 total_size = (FLAC__uint32)(total_samples * channels * (bps/8));
cannam@86 119 size_t i;
cannam@86 120
cannam@86 121 if(total_samples == 0) {
cannam@86 122 fprintf(stderr, "ERROR: this example only works for FLAC files that have a total_samples count in STREAMINFO\n");
cannam@86 123 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
cannam@86 124 }
cannam@86 125 if(channels != 2 || bps != 16) {
cannam@86 126 fprintf(stderr, "ERROR: this example only supports 16bit stereo streams\n");
cannam@86 127 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
cannam@86 128 }
cannam@86 129
cannam@86 130 /* write WAVE header before we write the first frame */
cannam@86 131 if(frame->header.number.sample_number == 0) {
cannam@86 132 if(
cannam@86 133 fwrite("RIFF", 1, 4, f) < 4 ||
cannam@86 134 !write_little_endian_uint32(f, total_size + 36) ||
cannam@86 135 fwrite("WAVEfmt ", 1, 8, f) < 8 ||
cannam@86 136 !write_little_endian_uint32(f, 16) ||
cannam@86 137 !write_little_endian_uint16(f, 1) ||
cannam@86 138 !write_little_endian_uint16(f, (FLAC__uint16)channels) ||
cannam@86 139 !write_little_endian_uint32(f, sample_rate) ||
cannam@86 140 !write_little_endian_uint32(f, sample_rate * channels * (bps/8)) ||
cannam@86 141 !write_little_endian_uint16(f, (FLAC__uint16)(channels * (bps/8))) || /* block align */
cannam@86 142 !write_little_endian_uint16(f, (FLAC__uint16)bps) ||
cannam@86 143 fwrite("data", 1, 4, f) < 4 ||
cannam@86 144 !write_little_endian_uint32(f, total_size)
cannam@86 145 ) {
cannam@86 146 fprintf(stderr, "ERROR: write error\n");
cannam@86 147 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
cannam@86 148 }
cannam@86 149 }
cannam@86 150
cannam@86 151 /* write decoded PCM samples */
cannam@86 152 for(i = 0; i < frame->header.blocksize; i++) {
cannam@86 153 if(
cannam@86 154 !write_little_endian_int16(f, (FLAC__int16)buffer[0][i]) || /* left channel */
cannam@86 155 !write_little_endian_int16(f, (FLAC__int16)buffer[1][i]) /* right channel */
cannam@86 156 ) {
cannam@86 157 fprintf(stderr, "ERROR: write error\n");
cannam@86 158 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
cannam@86 159 }
cannam@86 160 }
cannam@86 161
cannam@86 162 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
cannam@86 163 }
cannam@86 164
cannam@86 165 void OurDecoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
cannam@86 166 {
cannam@86 167 /* print some stats */
cannam@86 168 if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
cannam@86 169 /* save for later */
cannam@86 170 total_samples = metadata->data.stream_info.total_samples;
cannam@86 171 sample_rate = metadata->data.stream_info.sample_rate;
cannam@86 172 channels = metadata->data.stream_info.channels;
cannam@86 173 bps = metadata->data.stream_info.bits_per_sample;
cannam@86 174
cannam@86 175 fprintf(stderr, "sample rate : %u Hz\n", sample_rate);
cannam@86 176 fprintf(stderr, "channels : %u\n", channels);
cannam@86 177 fprintf(stderr, "bits per sample: %u\n", bps);
cannam@86 178 #ifdef _MSC_VER
cannam@86 179 fprintf(stderr, "total samples : %I64u\n", total_samples);
cannam@86 180 #else
cannam@86 181 fprintf(stderr, "total samples : %llu\n", total_samples);
cannam@86 182 #endif
cannam@86 183 }
cannam@86 184 }
cannam@86 185
cannam@86 186 void OurDecoder::error_callback(::FLAC__StreamDecoderErrorStatus status)
cannam@86 187 {
cannam@86 188 fprintf(stderr, "Got error callback: %s\n", FLAC__StreamDecoderErrorStatusString[status]);
cannam@86 189 }