annotate src/flac-1.2.1/examples/c/encode/file/main.c @ 1:05aa0afa9217

Bring in flac, ogg, vorbis
author Chris Cannam
date Tue, 19 Mar 2013 17:37:49 +0000
parents
children
rev   line source
Chris@1 1 /* example_c_encode_file - Simple FLAC file encoder using libFLAC
Chris@1 2 * Copyright (C) 2007 Josh Coalson
Chris@1 3 *
Chris@1 4 * This program is free software; you can redistribute it and/or
Chris@1 5 * modify it under the terms of the GNU General Public License
Chris@1 6 * as published by the Free Software Foundation; either version 2
Chris@1 7 * of the License, or (at your option) any later version.
Chris@1 8 *
Chris@1 9 * This program is distributed in the hope that it will be useful,
Chris@1 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1 12 * GNU General Public License for more details.
Chris@1 13 *
Chris@1 14 * You should have received a copy of the GNU General Public License
Chris@1 15 * along with this program; if not, write to the Free Software
Chris@1 16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Chris@1 17 */
Chris@1 18
Chris@1 19 /*
Chris@1 20 * This example shows how to use libFLAC to encode a WAVE file to a FLAC
Chris@1 21 * file. It only supports 16-bit stereo files in canonical WAVE format.
Chris@1 22 *
Chris@1 23 * Complete API documentation can be found at:
Chris@1 24 * http://flac.sourceforge.net/api/
Chris@1 25 */
Chris@1 26
Chris@1 27 #if HAVE_CONFIG_H
Chris@1 28 # include <config.h>
Chris@1 29 #endif
Chris@1 30
Chris@1 31 #include <stdio.h>
Chris@1 32 #include <stdlib.h>
Chris@1 33 #include <string.h>
Chris@1 34 #include "FLAC/metadata.h"
Chris@1 35 #include "FLAC/stream_encoder.h"
Chris@1 36
Chris@1 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);
Chris@1 38
Chris@1 39 #define READSIZE 1024
Chris@1 40
Chris@1 41 static unsigned total_samples = 0; /* can use a 32-bit number due to WAVE size limitations */
Chris@1 42 static FLAC__byte buffer[READSIZE/*samples*/ * 2/*bytes_per_sample*/ * 2/*channels*/]; /* we read the WAVE data into here */
Chris@1 43 static FLAC__int32 pcm[READSIZE/*samples*/ * 2/*channels*/];
Chris@1 44
Chris@1 45 int main(int argc, char *argv[])
Chris@1 46 {
Chris@1 47 FLAC__bool ok = true;
Chris@1 48 FLAC__StreamEncoder *encoder = 0;
Chris@1 49 FLAC__StreamEncoderInitStatus init_status;
Chris@1 50 FLAC__StreamMetadata *metadata[2];
Chris@1 51 FLAC__StreamMetadata_VorbisComment_Entry entry;
Chris@1 52 FILE *fin;
Chris@1 53 unsigned sample_rate = 0;
Chris@1 54 unsigned channels = 0;
Chris@1 55 unsigned bps = 0;
Chris@1 56
Chris@1 57 if(argc != 3) {
Chris@1 58 fprintf(stderr, "usage: %s infile.wav outfile.flac\n", argv[0]);
Chris@1 59 return 1;
Chris@1 60 }
Chris@1 61
Chris@1 62 if((fin = fopen(argv[1], "rb")) == NULL) {
Chris@1 63 fprintf(stderr, "ERROR: opening %s for output\n", argv[1]);
Chris@1 64 return 1;
Chris@1 65 }
Chris@1 66
Chris@1 67 /* read wav header and validate it */
Chris@1 68 if(
Chris@1 69 fread(buffer, 1, 44, fin) != 44 ||
Chris@1 70 memcmp(buffer, "RIFF", 4) ||
Chris@1 71 memcmp(buffer+8, "WAVEfmt \020\000\000\000\001\000\002\000", 16) ||
Chris@1 72 memcmp(buffer+32, "\004\000\020\000data", 8)
Chris@1 73 ) {
Chris@1 74 fprintf(stderr, "ERROR: invalid/unsupported WAVE file, only 16bps stereo WAVE in canonical form allowed\n");
Chris@1 75 fclose(fin);
Chris@1 76 return 1;
Chris@1 77 }
Chris@1 78 sample_rate = ((((((unsigned)buffer[27] << 8) | buffer[26]) << 8) | buffer[25]) << 8) | buffer[24];
Chris@1 79 channels = 2;
Chris@1 80 bps = 16;
Chris@1 81 total_samples = (((((((unsigned)buffer[43] << 8) | buffer[42]) << 8) | buffer[41]) << 8) | buffer[40]) / 4;
Chris@1 82
Chris@1 83 /* allocate the encoder */
Chris@1 84 if((encoder = FLAC__stream_encoder_new()) == NULL) {
Chris@1 85 fprintf(stderr, "ERROR: allocating encoder\n");
Chris@1 86 fclose(fin);
Chris@1 87 return 1;
Chris@1 88 }
Chris@1 89
Chris@1 90 ok &= FLAC__stream_encoder_set_verify(encoder, true);
Chris@1 91 ok &= FLAC__stream_encoder_set_compression_level(encoder, 5);
Chris@1 92 ok &= FLAC__stream_encoder_set_channels(encoder, channels);
Chris@1 93 ok &= FLAC__stream_encoder_set_bits_per_sample(encoder, bps);
Chris@1 94 ok &= FLAC__stream_encoder_set_sample_rate(encoder, sample_rate);
Chris@1 95 ok &= FLAC__stream_encoder_set_total_samples_estimate(encoder, total_samples);
Chris@1 96
Chris@1 97 /* now add some metadata; we'll add some tags and a padding block */
Chris@1 98 if(ok) {
Chris@1 99 if(
Chris@1 100 (metadata[0] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT)) == NULL ||
Chris@1 101 (metadata[1] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)) == NULL ||
Chris@1 102 /* there are many tag (vorbiscomment) functions but these are convenient for this particular use: */
Chris@1 103 !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "ARTIST", "Some Artist") ||
Chris@1 104 !FLAC__metadata_object_vorbiscomment_append_comment(metadata[0], entry, /*copy=*/false) || /* copy=false: let metadata object take control of entry's allocated string */
Chris@1 105 !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "YEAR", "1984") ||
Chris@1 106 !FLAC__metadata_object_vorbiscomment_append_comment(metadata[0], entry, /*copy=*/false)
Chris@1 107 ) {
Chris@1 108 fprintf(stderr, "ERROR: out of memory or tag error\n");
Chris@1 109 ok = false;
Chris@1 110 }
Chris@1 111
Chris@1 112 metadata[1]->length = 1234; /* set the padding length */
Chris@1 113
Chris@1 114 ok = FLAC__stream_encoder_set_metadata(encoder, metadata, 2);
Chris@1 115 }
Chris@1 116
Chris@1 117 /* initialize encoder */
Chris@1 118 if(ok) {
Chris@1 119 init_status = FLAC__stream_encoder_init_file(encoder, argv[2], progress_callback, /*client_data=*/NULL);
Chris@1 120 if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
Chris@1 121 fprintf(stderr, "ERROR: initializing encoder: %s\n", FLAC__StreamEncoderInitStatusString[init_status]);
Chris@1 122 ok = false;
Chris@1 123 }
Chris@1 124 }
Chris@1 125
Chris@1 126 /* read blocks of samples from WAVE file and feed to encoder */
Chris@1 127 if(ok) {
Chris@1 128 size_t left = (size_t)total_samples;
Chris@1 129 while(ok && left) {
Chris@1 130 size_t need = (left>READSIZE? (size_t)READSIZE : (size_t)left);
Chris@1 131 if(fread(buffer, channels*(bps/8), need, fin) != need) {
Chris@1 132 fprintf(stderr, "ERROR: reading from WAVE file\n");
Chris@1 133 ok = false;
Chris@1 134 }
Chris@1 135 else {
Chris@1 136 /* convert the packed little-endian 16-bit PCM samples from WAVE into an interleaved FLAC__int32 buffer for libFLAC */
Chris@1 137 size_t i;
Chris@1 138 for(i = 0; i < need*channels; i++) {
Chris@1 139 /* inefficient but simple and works on big- or little-endian machines */
Chris@1 140 pcm[i] = (FLAC__int32)(((FLAC__int16)(FLAC__int8)buffer[2*i+1] << 8) | (FLAC__int16)buffer[2*i]);
Chris@1 141 }
Chris@1 142 /* feed samples to encoder */
Chris@1 143 ok = FLAC__stream_encoder_process_interleaved(encoder, pcm, need);
Chris@1 144 }
Chris@1 145 left -= need;
Chris@1 146 }
Chris@1 147 }
Chris@1 148
Chris@1 149 ok &= FLAC__stream_encoder_finish(encoder);
Chris@1 150
Chris@1 151 fprintf(stderr, "encoding: %s\n", ok? "succeeded" : "FAILED");
Chris@1 152 fprintf(stderr, " state: %s\n", FLAC__StreamEncoderStateString[FLAC__stream_encoder_get_state(encoder)]);
Chris@1 153
Chris@1 154 /* now that encoding is finished, the metadata can be freed */
Chris@1 155 FLAC__metadata_object_delete(metadata[0]);
Chris@1 156 FLAC__metadata_object_delete(metadata[1]);
Chris@1 157
Chris@1 158 FLAC__stream_encoder_delete(encoder);
Chris@1 159 fclose(fin);
Chris@1 160
Chris@1 161 return 0;
Chris@1 162 }
Chris@1 163
Chris@1 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)
Chris@1 165 {
Chris@1 166 (void)encoder, (void)client_data;
Chris@1 167
Chris@1 168 #ifdef _MSC_VER
Chris@1 169 fprintf(stderr, "wrote %I64u bytes, %I64u/%u samples, %u/%u frames\n", bytes_written, samples_written, total_samples, frames_written, total_frames_estimate);
Chris@1 170 #else
Chris@1 171 fprintf(stderr, "wrote %llu bytes, %llu/%u samples, %u/%u frames\n", bytes_written, samples_written, total_samples, frames_written, total_frames_estimate);
Chris@1 172 #endif
Chris@1 173 }