cannam@86
|
1 /* example_c_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/stream_decoder.h"
|
cannam@86
|
34
|
cannam@86
|
35 static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
|
cannam@86
|
36 static void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
|
cannam@86
|
37 static void error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
|
cannam@86
|
38
|
cannam@86
|
39 static FLAC__uint64 total_samples = 0;
|
cannam@86
|
40 static unsigned sample_rate = 0;
|
cannam@86
|
41 static unsigned channels = 0;
|
cannam@86
|
42 static unsigned bps = 0;
|
cannam@86
|
43
|
cannam@86
|
44 static FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 x)
|
cannam@86
|
45 {
|
cannam@86
|
46 return
|
cannam@86
|
47 fputc(x, f) != EOF &&
|
cannam@86
|
48 fputc(x >> 8, f) != EOF
|
cannam@86
|
49 ;
|
cannam@86
|
50 }
|
cannam@86
|
51
|
cannam@86
|
52 static FLAC__bool write_little_endian_int16(FILE *f, FLAC__int16 x)
|
cannam@86
|
53 {
|
cannam@86
|
54 return write_little_endian_uint16(f, (FLAC__uint16)x);
|
cannam@86
|
55 }
|
cannam@86
|
56
|
cannam@86
|
57 static FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 x)
|
cannam@86
|
58 {
|
cannam@86
|
59 return
|
cannam@86
|
60 fputc(x, f) != EOF &&
|
cannam@86
|
61 fputc(x >> 8, f) != EOF &&
|
cannam@86
|
62 fputc(x >> 16, f) != EOF &&
|
cannam@86
|
63 fputc(x >> 24, f) != EOF
|
cannam@86
|
64 ;
|
cannam@86
|
65 }
|
cannam@86
|
66
|
cannam@86
|
67 int main(int argc, char *argv[])
|
cannam@86
|
68 {
|
cannam@86
|
69 FLAC__bool ok = true;
|
cannam@86
|
70 FLAC__StreamDecoder *decoder = 0;
|
cannam@86
|
71 FLAC__StreamDecoderInitStatus init_status;
|
cannam@86
|
72 FILE *fout;
|
cannam@86
|
73
|
cannam@86
|
74 if(argc != 3) {
|
cannam@86
|
75 fprintf(stderr, "usage: %s infile.flac outfile.wav\n", argv[0]);
|
cannam@86
|
76 return 1;
|
cannam@86
|
77 }
|
cannam@86
|
78
|
cannam@86
|
79 if((fout = fopen(argv[2], "wb")) == NULL) {
|
cannam@86
|
80 fprintf(stderr, "ERROR: opening %s for output\n", argv[2]);
|
cannam@86
|
81 return 1;
|
cannam@86
|
82 }
|
cannam@86
|
83
|
cannam@86
|
84 if((decoder = FLAC__stream_decoder_new()) == NULL) {
|
cannam@86
|
85 fprintf(stderr, "ERROR: allocating decoder\n");
|
cannam@86
|
86 fclose(fout);
|
cannam@86
|
87 return 1;
|
cannam@86
|
88 }
|
cannam@86
|
89
|
cannam@86
|
90 (void)FLAC__stream_decoder_set_md5_checking(decoder, true);
|
cannam@86
|
91
|
cannam@86
|
92 init_status = FLAC__stream_decoder_init_file(decoder, argv[1], write_callback, metadata_callback, error_callback, /*client_data=*/fout);
|
cannam@86
|
93 if(init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
|
cannam@86
|
94 fprintf(stderr, "ERROR: initializing decoder: %s\n", FLAC__StreamDecoderInitStatusString[init_status]);
|
cannam@86
|
95 ok = false;
|
cannam@86
|
96 }
|
cannam@86
|
97
|
cannam@86
|
98 if(ok) {
|
cannam@86
|
99 ok = FLAC__stream_decoder_process_until_end_of_stream(decoder);
|
cannam@86
|
100 fprintf(stderr, "decoding: %s\n", ok? "succeeded" : "FAILED");
|
cannam@86
|
101 fprintf(stderr, " state: %s\n", FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(decoder)]);
|
cannam@86
|
102 }
|
cannam@86
|
103
|
cannam@86
|
104 FLAC__stream_decoder_delete(decoder);
|
cannam@86
|
105 fclose(fout);
|
cannam@86
|
106
|
cannam@86
|
107 return 0;
|
cannam@86
|
108 }
|
cannam@86
|
109
|
cannam@86
|
110 FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
|
cannam@86
|
111 {
|
cannam@86
|
112 FILE *f = (FILE*)client_data;
|
cannam@86
|
113 const FLAC__uint32 total_size = (FLAC__uint32)(total_samples * channels * (bps/8));
|
cannam@86
|
114 size_t i;
|
cannam@86
|
115
|
cannam@86
|
116 (void)decoder;
|
cannam@86
|
117
|
cannam@86
|
118 if(total_samples == 0) {
|
cannam@86
|
119 fprintf(stderr, "ERROR: this example only works for FLAC files that have a total_samples count in STREAMINFO\n");
|
cannam@86
|
120 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
cannam@86
|
121 }
|
cannam@86
|
122 if(channels != 2 || bps != 16) {
|
cannam@86
|
123 fprintf(stderr, "ERROR: this example only supports 16bit stereo streams\n");
|
cannam@86
|
124 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
cannam@86
|
125 }
|
cannam@86
|
126
|
cannam@86
|
127 /* write WAVE header before we write the first frame */
|
cannam@86
|
128 if(frame->header.number.sample_number == 0) {
|
cannam@86
|
129 if(
|
cannam@86
|
130 fwrite("RIFF", 1, 4, f) < 4 ||
|
cannam@86
|
131 !write_little_endian_uint32(f, total_size + 36) ||
|
cannam@86
|
132 fwrite("WAVEfmt ", 1, 8, f) < 8 ||
|
cannam@86
|
133 !write_little_endian_uint32(f, 16) ||
|
cannam@86
|
134 !write_little_endian_uint16(f, 1) ||
|
cannam@86
|
135 !write_little_endian_uint16(f, (FLAC__uint16)channels) ||
|
cannam@86
|
136 !write_little_endian_uint32(f, sample_rate) ||
|
cannam@86
|
137 !write_little_endian_uint32(f, sample_rate * channels * (bps/8)) ||
|
cannam@86
|
138 !write_little_endian_uint16(f, (FLAC__uint16)(channels * (bps/8))) || /* block align */
|
cannam@86
|
139 !write_little_endian_uint16(f, (FLAC__uint16)bps) ||
|
cannam@86
|
140 fwrite("data", 1, 4, f) < 4 ||
|
cannam@86
|
141 !write_little_endian_uint32(f, total_size)
|
cannam@86
|
142 ) {
|
cannam@86
|
143 fprintf(stderr, "ERROR: write error\n");
|
cannam@86
|
144 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
cannam@86
|
145 }
|
cannam@86
|
146 }
|
cannam@86
|
147
|
cannam@86
|
148 /* write decoded PCM samples */
|
cannam@86
|
149 for(i = 0; i < frame->header.blocksize; i++) {
|
cannam@86
|
150 if(
|
cannam@86
|
151 !write_little_endian_int16(f, (FLAC__int16)buffer[0][i]) || /* left channel */
|
cannam@86
|
152 !write_little_endian_int16(f, (FLAC__int16)buffer[1][i]) /* right channel */
|
cannam@86
|
153 ) {
|
cannam@86
|
154 fprintf(stderr, "ERROR: write error\n");
|
cannam@86
|
155 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
cannam@86
|
156 }
|
cannam@86
|
157 }
|
cannam@86
|
158
|
cannam@86
|
159 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
cannam@86
|
160 }
|
cannam@86
|
161
|
cannam@86
|
162 void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
|
cannam@86
|
163 {
|
cannam@86
|
164 (void)decoder, (void)client_data;
|
cannam@86
|
165
|
cannam@86
|
166 /* print some stats */
|
cannam@86
|
167 if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
|
cannam@86
|
168 /* save for later */
|
cannam@86
|
169 total_samples = metadata->data.stream_info.total_samples;
|
cannam@86
|
170 sample_rate = metadata->data.stream_info.sample_rate;
|
cannam@86
|
171 channels = metadata->data.stream_info.channels;
|
cannam@86
|
172 bps = metadata->data.stream_info.bits_per_sample;
|
cannam@86
|
173
|
cannam@86
|
174 fprintf(stderr, "sample rate : %u Hz\n", sample_rate);
|
cannam@86
|
175 fprintf(stderr, "channels : %u\n", channels);
|
cannam@86
|
176 fprintf(stderr, "bits per sample: %u\n", bps);
|
cannam@86
|
177 #ifdef _MSC_VER
|
cannam@86
|
178 fprintf(stderr, "total samples : %I64u\n", total_samples);
|
cannam@86
|
179 #else
|
cannam@86
|
180 fprintf(stderr, "total samples : %llu\n", total_samples);
|
cannam@86
|
181 #endif
|
cannam@86
|
182 }
|
cannam@86
|
183 }
|
cannam@86
|
184
|
cannam@86
|
185 void error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
|
cannam@86
|
186 {
|
cannam@86
|
187 (void)decoder, (void)client_data;
|
cannam@86
|
188
|
cannam@86
|
189 fprintf(stderr, "Got error callback: %s\n", FLAC__StreamDecoderErrorStatusString[status]);
|
cannam@86
|
190 }
|