Chris@0: /* Chris@0: ** Copyright (C) 1999-2011 Erik de Castro Lopo Chris@0: ** Chris@0: ** All rights reserved. Chris@0: ** Chris@0: ** Redistribution and use in source and binary forms, with or without Chris@0: ** modification, are permitted provided that the following conditions are Chris@0: ** met: Chris@0: ** Chris@0: ** * Redistributions of source code must retain the above copyright Chris@0: ** notice, this list of conditions and the following disclaimer. Chris@0: ** * Redistributions in binary form must reproduce the above copyright Chris@0: ** notice, this list of conditions and the following disclaimer in Chris@0: ** the documentation and/or other materials provided with the Chris@0: ** distribution. Chris@0: ** * Neither the author nor the names of any contributors may be used Chris@0: ** to endorse or promote products derived from this software without Chris@0: ** specific prior written permission. Chris@0: ** Chris@0: ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS Chris@0: ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED Chris@0: ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR Chris@0: ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR Chris@0: ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, Chris@0: ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, Chris@0: ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; Chris@0: ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, Chris@0: ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR Chris@0: ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF Chris@0: ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Chris@0: */ Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: Chris@0: #include Chris@0: Chris@0: #include "common.h" Chris@0: Chris@0: Chris@0: typedef struct Chris@0: { char *infilename, *outfilename ; Chris@0: SF_INFO infileinfo, outfileinfo ; Chris@0: } OptionData ; Chris@0: Chris@0: static void copy_metadata (SNDFILE *outfile, SNDFILE *infile, int channels) ; Chris@0: Chris@0: static void Chris@0: usage_exit (const char *progname) Chris@0: { Chris@0: printf ("\nUsage : %s [options] [encoding] \n", progname) ; Chris@0: puts ("\n" Chris@0: " where [option] may be:\n\n" Chris@0: " -override-sample-rate=X : force sample rate of input to X\n" Chris@0: ) ; Chris@0: Chris@0: puts ( Chris@0: " where [encoding] may be one of the following:\n\n" Chris@0: " -pcms8 : force the output to signed 8 bit pcm\n" Chris@0: " -pcmu8 : force the output to unsigned 8 bit pcm\n" Chris@0: " -pcm16 : force the output to 16 bit pcm\n" Chris@0: " -pcm24 : force the output to 24 bit pcm\n" Chris@0: " -pcm32 : force the output to 32 bit pcm\n" Chris@0: " -float32 : force the output to 32 bit floating point" Chris@0: ) ; Chris@0: puts ( Chris@0: " -ulaw : force the output ULAW\n" Chris@0: " -alaw : force the output ALAW\n" Chris@0: " -ima-adpcm : force the output to IMA ADPCM (WAV only)\n" Chris@0: " -ms-adpcm : force the output to MS ADPCM (WAV only)\n" Chris@0: " -gsm610 : force the GSM6.10 (WAV only)\n" Chris@0: " -dwvw12 : force the output to 12 bit DWVW (AIFF only)\n" Chris@0: " -dwvw16 : force the output to 16 bit DWVW (AIFF only)\n" Chris@0: " -dwvw24 : force the output to 24 bit DWVW (AIFF only)\n" Chris@0: " -vorbis : force the output to Vorbis (OGG only)\n" Chris@0: ) ; Chris@0: Chris@0: puts ( Chris@0: " The format of the output file is determined by the file extension of the\n" Chris@0: " output file name. The following extensions are currently understood:\n" Chris@0: ) ; Chris@0: Chris@0: sfe_dump_format_map () ; Chris@0: Chris@0: puts ("") ; Chris@0: exit (0) ; Chris@0: } /* usage_exit */ Chris@0: Chris@0: int Chris@0: main (int argc, char * argv []) Chris@0: { const char *progname, *infilename, *outfilename ; Chris@0: SNDFILE *infile = NULL, *outfile = NULL ; Chris@0: SF_INFO sfinfo ; Chris@0: int k, outfilemajor, outfileminor = 0, infileminor ; Chris@0: int override_sample_rate = 0 ; /* assume no sample rate override. */ Chris@0: Chris@0: progname = program_name (argv [0]) ; Chris@0: Chris@0: if (argc < 3 || argc > 5) Chris@0: { usage_exit (progname) ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: infilename = argv [argc-2] ; Chris@0: outfilename = argv [argc-1] ; Chris@0: Chris@0: if (strcmp (infilename, outfilename) == 0) Chris@0: { printf ("Error : Input and output filenames are the same.\n\n") ; Chris@0: usage_exit (progname) ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: if (strlen (infilename) > 1 && infilename [0] == '-') Chris@0: { printf ("Error : Input filename (%s) looks like an option.\n\n", infilename) ; Chris@0: usage_exit (progname) ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: if (outfilename [0] == '-') Chris@0: { printf ("Error : Output filename (%s) looks like an option.\n\n", outfilename) ; Chris@0: usage_exit (progname) ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: for (k = 1 ; k < argc - 2 ; k++) Chris@0: { if (! strcmp (argv [k], "-pcms8")) Chris@0: { outfileminor = SF_FORMAT_PCM_S8 ; Chris@0: continue ; Chris@0: } ; Chris@0: if (! strcmp (argv [k], "-pcmu8")) Chris@0: { outfileminor = SF_FORMAT_PCM_U8 ; Chris@0: continue ; Chris@0: } ; Chris@0: if (! strcmp (argv [k], "-pcm16")) Chris@0: { outfileminor = SF_FORMAT_PCM_16 ; Chris@0: continue ; Chris@0: } ; Chris@0: if (! strcmp (argv [k], "-pcm24")) Chris@0: { outfileminor = SF_FORMAT_PCM_24 ; Chris@0: continue ; Chris@0: } ; Chris@0: if (! strcmp (argv [k], "-pcm32")) Chris@0: { outfileminor = SF_FORMAT_PCM_32 ; Chris@0: continue ; Chris@0: } ; Chris@0: if (! strcmp (argv [k], "-float32")) Chris@0: { outfileminor = SF_FORMAT_FLOAT ; Chris@0: continue ; Chris@0: } ; Chris@0: if (! strcmp (argv [k], "-ulaw")) Chris@0: { outfileminor = SF_FORMAT_ULAW ; Chris@0: continue ; Chris@0: } ; Chris@0: if (! strcmp (argv [k], "-alaw")) Chris@0: { outfileminor = SF_FORMAT_ALAW ; Chris@0: continue ; Chris@0: } ; Chris@0: if (! strcmp (argv [k], "-ima-adpcm")) Chris@0: { outfileminor = SF_FORMAT_IMA_ADPCM ; Chris@0: continue ; Chris@0: } ; Chris@0: if (! strcmp (argv [k], "-ms-adpcm")) Chris@0: { outfileminor = SF_FORMAT_MS_ADPCM ; Chris@0: continue ; Chris@0: } ; Chris@0: if (! strcmp (argv [k], "-gsm610")) Chris@0: { outfileminor = SF_FORMAT_GSM610 ; Chris@0: continue ; Chris@0: } ; Chris@0: if (! strcmp (argv [k], "-dwvw12")) Chris@0: { outfileminor = SF_FORMAT_DWVW_12 ; Chris@0: continue ; Chris@0: } ; Chris@0: if (! strcmp (argv [k], "-dwvw16")) Chris@0: { outfileminor = SF_FORMAT_DWVW_16 ; Chris@0: continue ; Chris@0: } ; Chris@0: if (! strcmp (argv [k], "-dwvw24")) Chris@0: { outfileminor = SF_FORMAT_DWVW_24 ; Chris@0: continue ; Chris@0: } ; Chris@0: if (! strcmp (argv [k], "-vorbis")) Chris@0: { outfileminor = SF_FORMAT_VORBIS ; Chris@0: continue ; Chris@0: } ; Chris@0: Chris@0: if (strstr (argv [k], "-override-sample-rate=") == argv [k]) Chris@0: { const char *ptr ; Chris@0: Chris@0: ptr = argv [k] + strlen ("-override-sample-rate=") ; Chris@0: override_sample_rate = atoi (ptr) ; Chris@0: continue ; Chris@0: } ; Chris@0: Chris@0: printf ("Error : Not able to decode argunment '%s'.\n", argv [k]) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: memset (&sfinfo, 0, sizeof (sfinfo)) ; Chris@0: Chris@0: if ((infile = sf_open (infilename, SFM_READ, &sfinfo)) == NULL) Chris@0: { printf ("Not able to open input file %s.\n", infilename) ; Chris@0: puts (sf_strerror (NULL)) ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: /* Update sample rate if forced to something else. */ Chris@0: if (override_sample_rate) Chris@0: sfinfo.samplerate = override_sample_rate ; Chris@0: Chris@0: infileminor = sfinfo.format & SF_FORMAT_SUBMASK ; Chris@0: Chris@0: if ((sfinfo.format = sfe_file_type_of_ext (outfilename, sfinfo.format)) == 0) Chris@0: { printf ("Error : Not able to determine output file type for %s.\n", outfilename) ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: outfilemajor = sfinfo.format & (SF_FORMAT_TYPEMASK | SF_FORMAT_ENDMASK) ; Chris@0: Chris@0: if (outfileminor == 0) Chris@0: outfileminor = sfinfo.format & SF_FORMAT_SUBMASK ; Chris@0: Chris@0: if (outfileminor != 0) Chris@0: sfinfo.format = outfilemajor | outfileminor ; Chris@0: else Chris@0: sfinfo.format = outfilemajor | (sfinfo.format & SF_FORMAT_SUBMASK) ; Chris@0: Chris@0: if ((sfinfo.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_XI) Chris@0: switch (sfinfo.format & SF_FORMAT_SUBMASK) Chris@0: { case SF_FORMAT_PCM_16 : Chris@0: sfinfo.format = outfilemajor | SF_FORMAT_DPCM_16 ; Chris@0: break ; Chris@0: Chris@0: case SF_FORMAT_PCM_S8 : Chris@0: case SF_FORMAT_PCM_U8 : Chris@0: sfinfo.format = outfilemajor | SF_FORMAT_DPCM_8 ; Chris@0: break ; Chris@0: } ; Chris@0: Chris@0: if (sf_format_check (&sfinfo) == 0) Chris@0: { printf ("Error : output file format is invalid (0x%08X).\n", sfinfo.format) ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: /* Open the output file. */ Chris@0: if ((outfile = sf_open (outfilename, SFM_WRITE, &sfinfo)) == NULL) Chris@0: { printf ("Not able to open output file %s : %s\n", outfilename, sf_strerror (NULL)) ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: /* Copy the metadata */ Chris@0: copy_metadata (outfile, infile, sfinfo.channels) ; Chris@0: Chris@0: if ((outfileminor == SF_FORMAT_DOUBLE) || (outfileminor == SF_FORMAT_FLOAT) Chris@0: || (infileminor == SF_FORMAT_DOUBLE) || (infileminor == SF_FORMAT_FLOAT) Chris@0: || (infileminor == SF_FORMAT_VORBIS) || (outfileminor == SF_FORMAT_VORBIS)) Chris@0: sfe_copy_data_fp (outfile, infile, sfinfo.channels) ; Chris@0: else Chris@0: sfe_copy_data_int (outfile, infile, sfinfo.channels) ; Chris@0: Chris@0: sf_close (infile) ; Chris@0: sf_close (outfile) ; Chris@0: Chris@0: return 0 ; Chris@0: } /* main */ Chris@0: Chris@0: static void Chris@0: copy_metadata (SNDFILE *outfile, SNDFILE *infile, int channels) Chris@0: { SF_INSTRUMENT inst ; Chris@0: SF_BROADCAST_INFO_2K binfo ; Chris@0: const char *str ; Chris@0: int k, chanmap [256] ; Chris@0: Chris@0: for (k = SF_STR_FIRST ; k <= SF_STR_LAST ; k++) Chris@0: { str = sf_get_string (infile, k) ; Chris@0: if (str != NULL) Chris@0: sf_set_string (outfile, k, str) ; Chris@0: } ; Chris@0: Chris@0: memset (&inst, 0, sizeof (inst)) ; Chris@0: memset (&binfo, 0, sizeof (binfo)) ; Chris@0: Chris@0: if (channels < ARRAY_LEN (chanmap)) Chris@0: { size_t size = channels * sizeof (chanmap [0]) ; Chris@0: Chris@0: if (sf_command (infile, SFC_GET_CHANNEL_MAP_INFO, chanmap, size) == SF_TRUE) Chris@0: sf_command (outfile, SFC_SET_CHANNEL_MAP_INFO, chanmap, size) ; Chris@0: } ; Chris@0: Chris@0: if (sf_command (infile, SFC_GET_INSTRUMENT, &inst, sizeof (inst)) == SF_TRUE) Chris@0: sf_command (outfile, SFC_SET_INSTRUMENT, &inst, sizeof (inst)) ; Chris@0: Chris@0: if (sf_command (infile, SFC_GET_BROADCAST_INFO, &binfo, sizeof (binfo)) == SF_TRUE) Chris@0: sf_command (outfile, SFC_SET_BROADCAST_INFO, &binfo, sizeof (binfo)) ; Chris@0: Chris@0: } /* copy_metadata */ Chris@0: