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