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