main.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* example_cpp_decode_file - Simple FLAC file decoder 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 decode a FLAC file to a WAVE
  21. * file. It only supports 16-bit stereo files.
  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 "FLAC++/decoder.h"
  32. #include "share/compat.h"
  33. static FLAC__uint64 total_samples = 0;
  34. static uint32_t sample_rate = 0;
  35. static uint32_t channels = 0;
  36. static uint32_t bps = 0;
  37. static bool write_little_endian_uint16(FILE *f, FLAC__uint16 x)
  38. {
  39. return
  40. fputc(x, f) != EOF &&
  41. fputc(x >> 8, f) != EOF
  42. ;
  43. }
  44. static bool write_little_endian_int16(FILE *f, FLAC__int16 x)
  45. {
  46. return write_little_endian_uint16(f, (FLAC__uint16)x);
  47. }
  48. static bool write_little_endian_uint32(FILE *f, FLAC__uint32 x)
  49. {
  50. return
  51. fputc(x, f) != EOF &&
  52. fputc(x >> 8, f) != EOF &&
  53. fputc(x >> 16, f) != EOF &&
  54. fputc(x >> 24, f) != EOF
  55. ;
  56. }
  57. class OurDecoder: public FLAC::Decoder::File {
  58. public:
  59. OurDecoder(FILE *f_): FLAC::Decoder::File(), f(f_) { }
  60. protected:
  61. FILE *f;
  62. virtual ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
  63. virtual void metadata_callback(const ::FLAC__StreamMetadata *metadata);
  64. virtual void error_callback(::FLAC__StreamDecoderErrorStatus status);
  65. private:
  66. OurDecoder(const OurDecoder&);
  67. OurDecoder&operator=(const OurDecoder&);
  68. };
  69. int main(int argc, char *argv[])
  70. {
  71. bool ok = true;
  72. FILE *fout;
  73. if(argc != 3) {
  74. fprintf(stderr, "usage: %s infile.flac outfile.wav\n", argv[0]);
  75. return 1;
  76. }
  77. if((fout = fopen(argv[2], "wb")) == NULL) {
  78. fprintf(stderr, "ERROR: opening %s for output\n", argv[2]);
  79. return 1;
  80. }
  81. OurDecoder decoder(fout);
  82. if(!decoder) {
  83. fprintf(stderr, "ERROR: allocating decoder\n");
  84. fclose(fout);
  85. return 1;
  86. }
  87. (void)decoder.set_md5_checking(true);
  88. FLAC__StreamDecoderInitStatus init_status = decoder.init(argv[1]);
  89. if(init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
  90. fprintf(stderr, "ERROR: initializing decoder: %s\n", FLAC__StreamDecoderInitStatusString[init_status]);
  91. ok = false;
  92. }
  93. if(ok) {
  94. ok = decoder.process_until_end_of_stream();
  95. fprintf(stderr, "decoding: %s\n", ok? "succeeded" : "FAILED");
  96. fprintf(stderr, " state: %s\n", decoder.get_state().resolved_as_cstring(decoder));
  97. }
  98. fclose(fout);
  99. return 0;
  100. }
  101. ::FLAC__StreamDecoderWriteStatus OurDecoder::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[])
  102. {
  103. const FLAC__uint32 total_size = (FLAC__uint32)(total_samples * channels * (bps/8));
  104. size_t i;
  105. // Update data
  106. channels = OurDecoder::get_channels();
  107. bps = OurDecoder::get_bits_per_sample();
  108. if(total_samples == 0) {
  109. fprintf(stderr, "ERROR: this example only works for FLAC files that have a total_samples count in STREAMINFO\n");
  110. return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
  111. }
  112. if(channels != 2 || bps != 16) {
  113. fprintf(stderr, "ERROR: this example only supports 16bit stereo streams\n");
  114. return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
  115. }
  116. /* write WAVE header before we write the first frame */
  117. if(frame->header.number.sample_number == 0) {
  118. if(
  119. fwrite("RIFF", 1, 4, f) < 4 ||
  120. !write_little_endian_uint32(f, total_size + 36) ||
  121. fwrite("WAVEfmt ", 1, 8, f) < 8 ||
  122. !write_little_endian_uint32(f, 16) ||
  123. !write_little_endian_uint16(f, 1) ||
  124. !write_little_endian_uint16(f, (FLAC__uint16)channels) ||
  125. !write_little_endian_uint32(f, sample_rate) ||
  126. !write_little_endian_uint32(f, sample_rate * channels * (bps/8)) ||
  127. !write_little_endian_uint16(f, (FLAC__uint16)(channels * (bps/8))) || /* block align */
  128. !write_little_endian_uint16(f, (FLAC__uint16)bps) ||
  129. fwrite("data", 1, 4, f) < 4 ||
  130. !write_little_endian_uint32(f, total_size)
  131. ) {
  132. fprintf(stderr, "ERROR: write error\n");
  133. return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
  134. }
  135. }
  136. /* write decoded PCM samples */
  137. for(i = 0; i < frame->header.blocksize; i++) {
  138. if(
  139. !write_little_endian_int16(f, (FLAC__int16)buffer[0][i]) || /* left channel */
  140. !write_little_endian_int16(f, (FLAC__int16)buffer[1][i]) /* right channel */
  141. ) {
  142. fprintf(stderr, "ERROR: write error\n");
  143. return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
  144. }
  145. }
  146. return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
  147. }
  148. void OurDecoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
  149. {
  150. /* print some stats */
  151. if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
  152. /* save for later */
  153. total_samples = metadata->data.stream_info.total_samples;
  154. sample_rate = metadata->data.stream_info.sample_rate;
  155. channels = metadata->data.stream_info.channels;
  156. bps = metadata->data.stream_info.bits_per_sample;
  157. fprintf(stderr, "sample rate : %u Hz\n", sample_rate);
  158. fprintf(stderr, "channels : %u\n", channels);
  159. fprintf(stderr, "bits per sample: %u\n", bps);
  160. fprintf(stderr, "total samples : %" PRIu64 "\n", total_samples);
  161. }
  162. }
  163. void OurDecoder::error_callback(::FLAC__StreamDecoderErrorStatus status)
  164. {
  165. fprintf(stderr, "Got error callback: %s\n", FLAC__StreamDecoderErrorStatusString[status]);
  166. }