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