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