main.c 6.5 KB

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