1 /* example_cpp_encode_file - Simple FLAC file encoder 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 encode a WAVE file to a FLAC
22 * file. It only supports 16-bit stereo files in canonical WAVE format.
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 #include <string.h>
35
36 #include "FLAC++/metadata.h"
37 #include "FLAC++/encoder.h"
38 #include "share/compat.h"
39
40 #include <cstring>
41
42 class OurEncoder: public FLAC::Encoder::File {
43 public:
OurEncoder()44 OurEncoder(): FLAC::Encoder::File() { }
45 protected:
46 virtual void progress_callback(FLAC__uint64 bytes_written, FLAC__uint64 samples_written, uint32_t frames_written, uint32_t total_frames_estimate);
47 };
48
49 #define READSIZE 1024
50
51 static uint32_t total_samples = 0; /* can use a 32-bit number due to WAVE size limitations */
52 static FLAC__byte buffer[READSIZE/*samples*/ * 2/*bytes_per_sample*/ * 2/*channels*/]; /* we read the WAVE data into here */
53 static FLAC__int32 pcm[READSIZE/*samples*/ * 2/*channels*/];
54
main(int argc,char * argv[])55 int main(int argc, char *argv[])
56 {
57 bool ok = true;
58 OurEncoder encoder;
59 FLAC__StreamEncoderInitStatus init_status;
60 FLAC__StreamMetadata *metadata[2];
61 FLAC__StreamMetadata_VorbisComment_Entry entry;
62 FILE *fin;
63 uint32_t sample_rate = 0;
64 uint32_t channels = 0;
65 uint32_t bps = 0;
66
67 if(argc != 3) {
68 fprintf(stderr, "usage: %s infile.wav outfile.flac\n", argv[0]);
69 return 1;
70 }
71
72 if((fin = fopen(argv[1], "rb")) == NULL) {
73 fprintf(stderr, "ERROR: opening %s for output\n", argv[1]);
74 return 1;
75 }
76
77 /* read wav header and validate it */
78 if(
79 fread(buffer, 1, 44, fin) != 44 ||
80 memcmp(buffer, "RIFF", 4) ||
81 memcmp(buffer+8, "WAVEfmt \020\000\000\000\001\000\002\000", 16) ||
82 memcmp(buffer+32, "\004\000\020\000data", 8)
83 ) {
84 fprintf(stderr, "ERROR: invalid/unsupported WAVE file, only 16bps stereo WAVE in canonical form allowed\n");
85 fclose(fin);
86 return 1;
87 }
88 sample_rate = ((((((uint32_t)buffer[27] << 8) | buffer[26]) << 8) | buffer[25]) << 8) | buffer[24];
89 channels = 2;
90 bps = 16;
91 total_samples = (((((((uint32_t)buffer[43] << 8) | buffer[42]) << 8) | buffer[41]) << 8) | buffer[40]) / 4;
92
93 /* check the encoder */
94 if(!encoder) {
95 fprintf(stderr, "ERROR: allocating encoder\n");
96 fclose(fin);
97 return 1;
98 }
99
100 ok &= encoder.set_verify(true);
101 ok &= encoder.set_compression_level(5);
102 ok &= encoder.set_channels(channels);
103 ok &= encoder.set_bits_per_sample(bps);
104 ok &= encoder.set_sample_rate(sample_rate);
105 ok &= encoder.set_total_samples_estimate(total_samples);
106
107 /* now add some metadata; we'll add some tags and a padding block */
108 if(ok) {
109 if(
110 (metadata[0] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT)) == NULL ||
111 (metadata[1] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)) == NULL ||
112 /* there are many tag (vorbiscomment) functions but these are convenient for this particular use: */
113 !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "ARTIST", "Some Artist") ||
114 !FLAC__metadata_object_vorbiscomment_append_comment(metadata[0], entry, /*copy=*/false) || /* copy=false: let metadata object take control of entry's allocated string */
115 !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "YEAR", "1984") ||
116 !FLAC__metadata_object_vorbiscomment_append_comment(metadata[0], entry, /*copy=*/false)
117 ) {
118 fprintf(stderr, "ERROR: out of memory or tag error\n");
119 ok = false;
120 } else {
121
122 metadata[1]->length = 1234; /* set the padding length */
123
124 ok = encoder.set_metadata(metadata, 2);
125
126 }
127 }
128
129 /* initialize encoder */
130 if(ok) {
131 init_status = encoder.init(argv[2]);
132 if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
133 fprintf(stderr, "ERROR: initializing encoder: %s\n", FLAC__StreamEncoderInitStatusString[init_status]);
134 ok = false;
135 }
136 }
137
138 /* read blocks of samples from WAVE file and feed to encoder */
139 if(ok) {
140 size_t left = (size_t)total_samples;
141 while(ok && left) {
142 size_t need = (left>READSIZE? (size_t)READSIZE : (size_t)left);
143 if(fread(buffer, channels*(bps/8), need, fin) != need) {
144 fprintf(stderr, "ERROR: reading from WAVE file\n");
145 ok = false;
146 }
147 else {
148 /* convert the packed little-endian 16-bit PCM samples from WAVE into an interleaved FLAC__int32 buffer for libFLAC */
149 size_t i;
150 for(i = 0; i < need*channels; i++) {
151 /* inefficient but simple and works on big- or little-endian machines */
152 pcm[i] = (FLAC__int32)(((FLAC__int16)(FLAC__int8)buffer[2*i+1] << 8) | (FLAC__int16)buffer[2*i]);
153 }
154 /* feed samples to encoder */
155 ok = encoder.process_interleaved(pcm, need);
156 }
157 left -= need;
158 }
159 }
160
161 ok &= encoder.finish();
162
163 fprintf(stderr, "encoding: %s\n", ok? "succeeded" : "FAILED");
164 fprintf(stderr, " state: %s\n", encoder.get_state().resolved_as_cstring(encoder));
165
166 /* now that encoding is finished, the metadata can be freed */
167 FLAC__metadata_object_delete(metadata[0]);
168 FLAC__metadata_object_delete(metadata[1]);
169
170 fclose(fin);
171
172 return 0;
173 }
174
progress_callback(FLAC__uint64 bytes_written,FLAC__uint64 samples_written,uint32_t frames_written,uint32_t total_frames_estimate)175 void OurEncoder::progress_callback(FLAC__uint64 bytes_written, FLAC__uint64 samples_written, uint32_t frames_written, uint32_t total_frames_estimate)
176 {
177 fprintf(stderr, "wrote %" PRIu64 " bytes, %" PRIu64 "/%u samples, %u/%u frames\n", bytes_written, samples_written, total_samples, frames_written, total_frames_estimate);
178 }
179