annotate src/flac-1.2.1/examples/c/encode/file/main.c @ 141:1b5b6dfd0d0e

Add updated build of PortAudio for OSX
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 03 Jan 2017 15:10:52 +0000
parents 98c1576536ae
children
rev   line source
cannam@86 1 /* example_c_encode_file - Simple FLAC file encoder 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 encode a WAVE file to a FLAC
cannam@86 21 * file. It only supports 16-bit stereo files in canonical WAVE format.
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 <string.h>
cannam@86 34 #include "FLAC/metadata.h"
cannam@86 35 #include "FLAC/stream_encoder.h"
cannam@86 36
cannam@86 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);
cannam@86 38
cannam@86 39 #define READSIZE 1024
cannam@86 40
cannam@86 41 static unsigned total_samples = 0; /* can use a 32-bit number due to WAVE size limitations */
cannam@86 42 static FLAC__byte buffer[READSIZE/*samples*/ * 2/*bytes_per_sample*/ * 2/*channels*/]; /* we read the WAVE data into here */
cannam@86 43 static FLAC__int32 pcm[READSIZE/*samples*/ * 2/*channels*/];
cannam@86 44
cannam@86 45 int main(int argc, char *argv[])
cannam@86 46 {
cannam@86 47 FLAC__bool ok = true;
cannam@86 48 FLAC__StreamEncoder *encoder = 0;
cannam@86 49 FLAC__StreamEncoderInitStatus init_status;
cannam@86 50 FLAC__StreamMetadata *metadata[2];
cannam@86 51 FLAC__StreamMetadata_VorbisComment_Entry entry;
cannam@86 52 FILE *fin;
cannam@86 53 unsigned sample_rate = 0;
cannam@86 54 unsigned channels = 0;
cannam@86 55 unsigned bps = 0;
cannam@86 56
cannam@86 57 if(argc != 3) {
cannam@86 58 fprintf(stderr, "usage: %s infile.wav outfile.flac\n", argv[0]);
cannam@86 59 return 1;
cannam@86 60 }
cannam@86 61
cannam@86 62 if((fin = fopen(argv[1], "rb")) == NULL) {
cannam@86 63 fprintf(stderr, "ERROR: opening %s for output\n", argv[1]);
cannam@86 64 return 1;
cannam@86 65 }
cannam@86 66
cannam@86 67 /* read wav header and validate it */
cannam@86 68 if(
cannam@86 69 fread(buffer, 1, 44, fin) != 44 ||
cannam@86 70 memcmp(buffer, "RIFF", 4) ||
cannam@86 71 memcmp(buffer+8, "WAVEfmt \020\000\000\000\001\000\002\000", 16) ||
cannam@86 72 memcmp(buffer+32, "\004\000\020\000data", 8)
cannam@86 73 ) {
cannam@86 74 fprintf(stderr, "ERROR: invalid/unsupported WAVE file, only 16bps stereo WAVE in canonical form allowed\n");
cannam@86 75 fclose(fin);
cannam@86 76 return 1;
cannam@86 77 }
cannam@86 78 sample_rate = ((((((unsigned)buffer[27] << 8) | buffer[26]) << 8) | buffer[25]) << 8) | buffer[24];
cannam@86 79 channels = 2;
cannam@86 80 bps = 16;
cannam@86 81 total_samples = (((((((unsigned)buffer[43] << 8) | buffer[42]) << 8) | buffer[41]) << 8) | buffer[40]) / 4;
cannam@86 82
cannam@86 83 /* allocate the encoder */
cannam@86 84 if((encoder = FLAC__stream_encoder_new()) == NULL) {
cannam@86 85 fprintf(stderr, "ERROR: allocating encoder\n");
cannam@86 86 fclose(fin);
cannam@86 87 return 1;
cannam@86 88 }
cannam@86 89
cannam@86 90 ok &= FLAC__stream_encoder_set_verify(encoder, true);
cannam@86 91 ok &= FLAC__stream_encoder_set_compression_level(encoder, 5);
cannam@86 92 ok &= FLAC__stream_encoder_set_channels(encoder, channels);
cannam@86 93 ok &= FLAC__stream_encoder_set_bits_per_sample(encoder, bps);
cannam@86 94 ok &= FLAC__stream_encoder_set_sample_rate(encoder, sample_rate);
cannam@86 95 ok &= FLAC__stream_encoder_set_total_samples_estimate(encoder, total_samples);
cannam@86 96
cannam@86 97 /* now add some metadata; we'll add some tags and a padding block */
cannam@86 98 if(ok) {
cannam@86 99 if(
cannam@86 100 (metadata[0] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT)) == NULL ||
cannam@86 101 (metadata[1] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)) == NULL ||
cannam@86 102 /* there are many tag (vorbiscomment) functions but these are convenient for this particular use: */
cannam@86 103 !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "ARTIST", "Some Artist") ||
cannam@86 104 !FLAC__metadata_object_vorbiscomment_append_comment(metadata[0], entry, /*copy=*/false) || /* copy=false: let metadata object take control of entry's allocated string */
cannam@86 105 !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "YEAR", "1984") ||
cannam@86 106 !FLAC__metadata_object_vorbiscomment_append_comment(metadata[0], entry, /*copy=*/false)
cannam@86 107 ) {
cannam@86 108 fprintf(stderr, "ERROR: out of memory or tag error\n");
cannam@86 109 ok = false;
cannam@86 110 }
cannam@86 111
cannam@86 112 metadata[1]->length = 1234; /* set the padding length */
cannam@86 113
cannam@86 114 ok = FLAC__stream_encoder_set_metadata(encoder, metadata, 2);
cannam@86 115 }
cannam@86 116
cannam@86 117 /* initialize encoder */
cannam@86 118 if(ok) {
cannam@86 119 init_status = FLAC__stream_encoder_init_file(encoder, argv[2], progress_callback, /*client_data=*/NULL);
cannam@86 120 if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
cannam@86 121 fprintf(stderr, "ERROR: initializing encoder: %s\n", FLAC__StreamEncoderInitStatusString[init_status]);
cannam@86 122 ok = false;
cannam@86 123 }
cannam@86 124 }
cannam@86 125
cannam@86 126 /* read blocks of samples from WAVE file and feed to encoder */
cannam@86 127 if(ok) {
cannam@86 128 size_t left = (size_t)total_samples;
cannam@86 129 while(ok && left) {
cannam@86 130 size_t need = (left>READSIZE? (size_t)READSIZE : (size_t)left);
cannam@86 131 if(fread(buffer, channels*(bps/8), need, fin) != need) {
cannam@86 132 fprintf(stderr, "ERROR: reading from WAVE file\n");
cannam@86 133 ok = false;
cannam@86 134 }
cannam@86 135 else {
cannam@86 136 /* convert the packed little-endian 16-bit PCM samples from WAVE into an interleaved FLAC__int32 buffer for libFLAC */
cannam@86 137 size_t i;
cannam@86 138 for(i = 0; i < need*channels; i++) {
cannam@86 139 /* inefficient but simple and works on big- or little-endian machines */
cannam@86 140 pcm[i] = (FLAC__int32)(((FLAC__int16)(FLAC__int8)buffer[2*i+1] << 8) | (FLAC__int16)buffer[2*i]);
cannam@86 141 }
cannam@86 142 /* feed samples to encoder */
cannam@86 143 ok = FLAC__stream_encoder_process_interleaved(encoder, pcm, need);
cannam@86 144 }
cannam@86 145 left -= need;
cannam@86 146 }
cannam@86 147 }
cannam@86 148
cannam@86 149 ok &= FLAC__stream_encoder_finish(encoder);
cannam@86 150
cannam@86 151 fprintf(stderr, "encoding: %s\n", ok? "succeeded" : "FAILED");
cannam@86 152 fprintf(stderr, " state: %s\n", FLAC__StreamEncoderStateString[FLAC__stream_encoder_get_state(encoder)]);
cannam@86 153
cannam@86 154 /* now that encoding is finished, the metadata can be freed */
cannam@86 155 FLAC__metadata_object_delete(metadata[0]);
cannam@86 156 FLAC__metadata_object_delete(metadata[1]);
cannam@86 157
cannam@86 158 FLAC__stream_encoder_delete(encoder);
cannam@86 159 fclose(fin);
cannam@86 160
cannam@86 161 return 0;
cannam@86 162 }
cannam@86 163
cannam@86 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)
cannam@86 165 {
cannam@86 166 (void)encoder, (void)client_data;
cannam@86 167
cannam@86 168 #ifdef _MSC_VER
cannam@86 169 fprintf(stderr, "wrote %I64u bytes, %I64u/%u samples, %u/%u frames\n", bytes_written, samples_written, total_samples, frames_written, total_frames_estimate);
cannam@86 170 #else
cannam@86 171 fprintf(stderr, "wrote %llu bytes, %llu/%u samples, %u/%u frames\n", bytes_written, samples_written, total_samples, frames_written, total_frames_estimate);
cannam@86 172 #endif
cannam@86 173 }