1
/* example_c_encode_file - Simple FLAC file encoder using libFLAC
2
* Copyright (C) 2007 Josh Coalson
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.
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.
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.
20
* This example shows how to use libFLAC to encode a WAVE file to a FLAC
21
* file. It only supports 16-bit stereo files in canonical WAVE format.
23
* Complete API documentation can be found at:
24
* http://flac.sourceforge.net/api/
34
#include "FLAC/metadata.h"
35
#include "FLAC/stream_encoder.h"
37
static void progress_callback(const FLAC__StreamEncoder *encoder, FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate, void *client_data);
41
static unsigned total_samples = 0; /* can use a 32-bit number due to WAVE size limitations */
42
static FLAC__byte buffer[READSIZE/*samples*/ * 2/*bytes_per_sample*/ * 2/*channels*/]; /* we read the WAVE data into here */
43
static FLAC__int32 pcm[READSIZE/*samples*/ * 2/*channels*/];
45
int main(int argc, char *argv[])
48
FLAC__StreamEncoder *encoder = 0;
49
FLAC__StreamEncoderInitStatus init_status;
50
FLAC__StreamMetadata *metadata[2];
51
FLAC__StreamMetadata_VorbisComment_Entry entry;
53
unsigned sample_rate = 0;
54
unsigned channels = 0;
58
fprintf(stderr, "usage: %s infile.wav outfile.flac\n", argv[0]);
62
if((fin = fopen(argv[1], "rb")) == NULL) {
63
fprintf(stderr, "ERROR: opening %s for output\n", argv[1]);
67
/* read wav header and validate it */
69
fread(buffer, 1, 44, fin) != 44 ||
70
memcmp(buffer, "RIFF", 4) ||
71
memcmp(buffer+8, "WAVEfmt \020\000\000\000\001\000\002\000", 16) ||
72
memcmp(buffer+32, "\004\000\020\000data", 8)
74
fprintf(stderr, "ERROR: invalid/unsupported WAVE file, only 16bps stereo WAVE in canonical form allowed\n");
78
sample_rate = ((((((unsigned)buffer[27] << 8) | buffer[26]) << 8) | buffer[25]) << 8) | buffer[24];
81
total_samples = (((((((unsigned)buffer[43] << 8) | buffer[42]) << 8) | buffer[41]) << 8) | buffer[40]) / 4;
83
/* allocate the encoder */
84
if((encoder = FLAC__stream_encoder_new()) == NULL) {
85
fprintf(stderr, "ERROR: allocating encoder\n");
90
ok &= FLAC__stream_encoder_set_verify(encoder, true);
91
ok &= FLAC__stream_encoder_set_compression_level(encoder, 5);
92
ok &= FLAC__stream_encoder_set_channels(encoder, channels);
93
ok &= FLAC__stream_encoder_set_bits_per_sample(encoder, bps);
94
ok &= FLAC__stream_encoder_set_sample_rate(encoder, sample_rate);
95
ok &= FLAC__stream_encoder_set_total_samples_estimate(encoder, total_samples);
97
/* now add some metadata; we'll add some tags and a padding block */
100
(metadata[0] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT)) == NULL ||
101
(metadata[1] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)) == NULL ||
102
/* there are many tag (vorbiscomment) functions but these are convenient for this particular use: */
103
!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "ARTIST", "Some Artist") ||
104
!FLAC__metadata_object_vorbiscomment_append_comment(metadata[0], entry, /*copy=*/false) || /* copy=false: let metadata object take control of entry's allocated string */
105
!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "YEAR", "1984") ||
106
!FLAC__metadata_object_vorbiscomment_append_comment(metadata[0], entry, /*copy=*/false)
108
fprintf(stderr, "ERROR: out of memory or tag error\n");
112
metadata[1]->length = 1234; /* set the padding length */
114
ok = FLAC__stream_encoder_set_metadata(encoder, metadata, 2);
117
/* initialize encoder */
119
init_status = FLAC__stream_encoder_init_file(encoder, argv[2], progress_callback, /*client_data=*/NULL);
120
if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
121
fprintf(stderr, "ERROR: initializing encoder: %s\n", FLAC__StreamEncoderInitStatusString[init_status]);
126
/* read blocks of samples from WAVE file and feed to encoder */
128
size_t left = (size_t)total_samples;
130
size_t need = (left>READSIZE? (size_t)READSIZE : (size_t)left);
131
if(fread(buffer, channels*(bps/8), need, fin) != need) {
132
fprintf(stderr, "ERROR: reading from WAVE file\n");
136
/* convert the packed little-endian 16-bit PCM samples from WAVE into an interleaved FLAC__int32 buffer for libFLAC */
138
for(i = 0; i < need*channels; i++) {
139
/* inefficient but simple and works on big- or little-endian machines */
140
pcm[i] = (FLAC__int32)(((FLAC__int16)(FLAC__int8)buffer[2*i+1] << 8) | (FLAC__int16)buffer[2*i]);
142
/* feed samples to encoder */
143
ok = FLAC__stream_encoder_process_interleaved(encoder, pcm, need);
149
ok &= FLAC__stream_encoder_finish(encoder);
151
fprintf(stderr, "encoding: %s\n", ok? "succeeded" : "FAILED");
152
fprintf(stderr, " state: %s\n", FLAC__StreamEncoderStateString[FLAC__stream_encoder_get_state(encoder)]);
154
/* now that encoding is finished, the metadata can be freed */
155
FLAC__metadata_object_delete(metadata[0]);
156
FLAC__metadata_object_delete(metadata[1]);
158
FLAC__stream_encoder_delete(encoder);
164
void progress_callback(const FLAC__StreamEncoder *encoder, FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate, void *client_data)
166
(void)encoder, (void)client_data;
169
fprintf(stderr, "wrote %I64u bytes, %I64u/%u samples, %u/%u frames\n", bytes_written, samples_written, total_samples, frames_written, total_frames_estimate);
171
fprintf(stderr, "wrote %llu bytes, %llu/%u samples, %u/%u frames\n", bytes_written, samples_written, total_samples, frames_written, total_frames_estimate);