sample.cpp
Upload User: wstnjxml
Upload Date: 2014-04-03
Package Size: 7248k
Code Size: 9k
Category:

Windows CE

Development Platform:

C/C++

  1. /*
  2.   Copyright (c) 2005, The Musepack Development Team
  3.   All rights reserved.
  4.   Redistribution and use in source and binary forms, with or without
  5.   modification, are permitted provided that the following conditions are
  6.   met:
  7.   * Redistributions of source code must retain the above copyright
  8.   notice, this list of conditions and the following disclaimer.
  9.   * Redistributions in binary form must reproduce the above
  10.   copyright notice, this list of conditions and the following
  11.   disclaimer in the documentation and/or other materials provided
  12.   with the distribution.
  13.   * Neither the name of the The Musepack Development Team nor the
  14.   names of its contributors may be used to endorse or promote
  15.   products derived from this software without specific prior
  16.   written permission.
  17.   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18.   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19.   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20.   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21.   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22.   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23.   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24.   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25.   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26.   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27.   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdio.h>
  30. #include <assert.h>
  31. #include <time.h>
  32. #include "musepack/musepack.h"
  33. /*
  34.   The data bundle we pass around with our reader to store file
  35.   position and size etc. 
  36. */
  37. typedef struct reader_data_t {
  38.     FILE *file;
  39.     long size;
  40.     mpc_bool_t seekable;
  41. } reader_data;
  42. /*
  43.   Our implementations of the mpc_reader callback functions.
  44. */
  45. mpc_int32_t
  46. read_impl(void *data, void *ptr, mpc_int32_t size)
  47. {
  48.     reader_data *d = (reader_data *) data;
  49.     return fread(ptr, 1, size, d->file);
  50. }
  51. mpc_bool_t
  52. seek_impl(void *data, mpc_int32_t offset)
  53. {
  54.     reader_data *d = (reader_data *) data;
  55.     return d->seekable ? !fseek(d->file, offset, SEEK_SET) : false;
  56. }
  57. mpc_int32_t
  58. tell_impl(void *data)
  59. {
  60.     reader_data *d = (reader_data *) data;
  61.     return ftell(d->file);
  62. }
  63. mpc_int32_t
  64. get_size_impl(void *data)
  65. {
  66.     reader_data *d = (reader_data *) data;
  67.     return d->size;
  68. }
  69. mpc_bool_t
  70. canseek_impl(void *data)
  71. {
  72.     reader_data *d = (reader_data *) data;
  73.     return d->seekable;
  74. }
  75. #define WFX_SIZE (2+2+4+4+2+2)
  76. #ifdef MPC_FIXED_POINT
  77. static int
  78. shift_signed(MPC_SAMPLE_FORMAT val, int shift)
  79. {
  80.     if (shift > 0)
  81.         val <<= shift;
  82.     else if (shift < 0)
  83.         val >>= -shift;
  84.     return (int)val;
  85. }
  86. #endif
  87. class WavWriter {
  88.   public:
  89.     WavWriter(FILE * p_output, unsigned p_nch, unsigned p_bps,
  90.               unsigned p_srate)
  91.     : m_file(p_output), m_nch(p_nch), m_bps(p_bps), m_srate(p_srate) {
  92.         assert(m_bps == 16 || m_bps == 24);
  93.         WriteRaw("RIFF", 4);
  94.         WriteDword(0);          //fix this in destructor
  95.         WriteRaw("WAVE", 4);
  96.         WriteRaw("fmt ", 4);
  97.         WriteDword(WFX_SIZE);
  98.         WriteWord(1);           //WAVE_FORMAT_PCM
  99.         WriteWord(m_nch);
  100.         WriteDword(m_srate);
  101.         WriteDword(m_srate * m_nch * (m_bps >> 3));
  102.         WriteWord(m_nch * (m_bps >> 3));
  103.         WriteWord(m_bps);
  104.         /*
  105.            WORD  wFormatTag; 
  106.            WORD  nChannels; 
  107.            DWORD nSamplesPerSec; 
  108.            DWORD nAvgBytesPerSec; 
  109.            WORD  nBlockAlign; 
  110.            WORD  wBitsPerSample; 
  111.          */
  112.         WriteRaw("data", 4);
  113.         WriteDword(0);          //fix this in destructor
  114.         m_data_bytes_written = 0;
  115.     } mpc_bool_t WriteSamples(const MPC_SAMPLE_FORMAT * p_buffer, unsigned p_size) {
  116.         unsigned n;
  117.         int clip_min = -1 << (m_bps - 1),
  118.             clip_max = (1 << (m_bps - 1)) - 1, float_scale = 1 << (m_bps - 1);
  119.         for (n = 0; n < p_size; n++) {
  120.             int val;
  121. #ifdef MPC_FIXED_POINT
  122.             val =
  123.                 shift_signed(p_buffer[n],
  124.                              m_bps - MPC_FIXED_POINT_SCALE_SHIFT);
  125. #else
  126.             val = (int)(p_buffer[n] * float_scale);
  127. #endif
  128.             if (val < clip_min)
  129.                 val = clip_min;
  130.             else if (val > clip_max)
  131.                 val = clip_max;
  132.             if (!WriteInt(val, m_bps))
  133.                 return false;
  134.         }
  135.         m_data_bytes_written += p_size * (m_bps >> 3);
  136.         return true;
  137.     }
  138.     ~WavWriter() {
  139.         if (m_data_bytes_written & 1) {
  140.             char blah = 0;
  141.             WriteRaw(&blah, 1);
  142.             m_data_bytes_written++;
  143.         }
  144.         Seek(4);
  145.         WriteDword((unsigned long)(m_data_bytes_written + 4 + 8 + WFX_SIZE +
  146.                                    8));
  147.         Seek(8 + 4 + 8 + WFX_SIZE + 4);
  148.         WriteDword(m_data_bytes_written);
  149.     }
  150.   private:
  151.     mpc_bool_t Seek(unsigned p_offset) {
  152.         return !fseek(m_file, p_offset, SEEK_SET);
  153.     }
  154.     mpc_bool_t WriteRaw(const void *p_buffer, unsigned p_bytes) {
  155.         return fwrite(p_buffer, 1, p_bytes, m_file) == p_bytes;
  156.     }
  157.     mpc_bool_t WriteDword(unsigned long p_val) {
  158.         return WriteInt(p_val, 32);
  159.     }
  160.     mpc_bool_t WriteWord(unsigned short p_val) {
  161.         return WriteInt(p_val, 16);
  162.     }
  163.     // write a little-endian number properly
  164.     mpc_bool_t WriteInt(unsigned int p_val, unsigned p_width_bits) {
  165.         unsigned char temp;
  166.         unsigned shift = 0;
  167.         assert((p_width_bits % 8) == 0);
  168.         do {
  169.             temp = (unsigned char)((p_val >> shift) & 0xFF);
  170.             if (!WriteRaw(&temp, 1))
  171.                 return false;
  172.             shift += 8;
  173.         } while (shift < p_width_bits);
  174.         return true;
  175.     }
  176.     unsigned m_nch, m_bps, m_srate;
  177.     FILE *m_file;
  178.     unsigned m_data_bytes_written;
  179. };
  180. static void
  181. usage(const char *exename)
  182. {
  183.     printf
  184.         ("Usage: %s <infile.mpc> [<outfile.wav>]nIf <outfile.wav> is not specified, decoder will run in benchmark mode.n",
  185.          exename);
  186. }
  187. int
  188. main(int argc, char **argv)
  189. {
  190.     if (argc != 2 && argc != 3) {
  191.         if (argc > 0)
  192.             usage(argv[0]);
  193.         return 0;
  194.     }
  195.     FILE *input = fopen(argv[1], "rb");
  196.     FILE *output = 0;
  197.     if (input == 0) {
  198.         usage(argv[0]);
  199.         printf("Error opening input file: "%s"n", argv[1]);
  200.         return 1;
  201.     }
  202.     if (argc == 3) {
  203.         output = fopen(argv[2], "wb");
  204.         if (output == 0) {
  205.             fclose(input);
  206.             usage(argv[0]);
  207.             printf("Error opening output file: "%s"n", argv[2]);
  208.             return 1;
  209.         }
  210.     }
  211.     /* initialize our reader_data tag the reader will carry around with it */
  212.     reader_data data;
  213.     data.file = input;
  214.     data.seekable = true;
  215.     fseek(data.file, 0, SEEK_END);
  216.     data.size = ftell(data.file);
  217.     fseek(data.file, 0, SEEK_SET);
  218.     /* set up an mpc_reader linked to our function implementations */
  219.     mpc_decoder decoder;
  220.     mpc_reader reader;
  221.     reader.read = read_impl;
  222.     reader.seek = seek_impl;
  223.     reader.tell = tell_impl;
  224.     reader.get_size = get_size_impl;
  225.     reader.canseek = canseek_impl;
  226.     reader.data = &data;
  227.     /* read file's streaminfo data */
  228.     mpc_streaminfo info;
  229.     mpc_streaminfo_init(&info);
  230.     if (mpc_streaminfo_read(&info, &reader) != ERROR_CODE_OK) {
  231.         printf("Not a valid musepack file: "%s"n", argv[1]);
  232.         return 1;
  233.     }
  234.     /* instantiate a decoder with our file reader */
  235.     mpc_decoder_setup(&decoder, &reader);
  236.     if (!mpc_decoder_initialize(&decoder, &info)) {
  237.         printf("Error initializing decoder.n", argv[1]);
  238.         return 1;
  239.     }
  240.     /* decode the file */
  241.     printf("Decoding from:n%snTo:n%sn", argv[1],
  242.            output ? argv[2] : "N/A");
  243.     WavWriter *wavwriter =
  244.         output ? new WavWriter(output, 2, 16, info.sample_freq) : 0;
  245.     MPC_SAMPLE_FORMAT sample_buffer[MPC_DECODER_BUFFER_LENGTH];
  246.     clock_t begin, end;
  247.     begin = clock();
  248.     unsigned total_samples = 0;
  249.     mpc_bool_t successful = FALSE;
  250.     for (;;) {
  251.         unsigned status = mpc_decoder_decode(&decoder, sample_buffer, 0, 0);
  252.         if (status == (unsigned)(-1)) {
  253.             //decode error
  254.             printf("Error decoding file.n");
  255.             break;
  256.         }
  257.         else if (status == 0)   //EOF
  258.         {
  259.             successful = true;
  260.             break;
  261.         }
  262.         else                    //status>0
  263.         {
  264.             total_samples += status;
  265.             if (wavwriter) {
  266.                 if (!wavwriter->
  267.                     WriteSamples(sample_buffer, status * /* stereo */ 2)) {
  268.                     printf("Write error.n");
  269.                     break;
  270.                 }
  271.             }
  272.         }
  273.     }
  274.     end = clock();
  275.     if (wavwriter) {
  276.         delete wavwriter;
  277.     }
  278.     if (successful) {
  279.         printf("nFinished.nTotal samples decoded: %u.n", total_samples);
  280.         unsigned ms = (end - begin) * 1000 / CLOCKS_PER_SEC;
  281.         unsigned ratio =
  282.             (unsigned)((double)total_samples / (double)info.sample_freq /
  283.                        ((double)ms / 1000.0) * 100.0);
  284.         printf("Time: %u ms (%u.%02ux).n", ms, ratio / 100, ratio % 100);
  285.     }
  286.     return 0;
  287. }