annotate src/libsndfile-1.0.25/programs/sndfile-convert.c @ 85:545efbb81310

Import initial set of sources
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 18 Mar 2013 14:12:14 +0000
parents
children
rev   line source
cannam@85 1 /*
cannam@85 2 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
cannam@85 3 **
cannam@85 4 ** All rights reserved.
cannam@85 5 **
cannam@85 6 ** Redistribution and use in source and binary forms, with or without
cannam@85 7 ** modification, are permitted provided that the following conditions are
cannam@85 8 ** met:
cannam@85 9 **
cannam@85 10 ** * Redistributions of source code must retain the above copyright
cannam@85 11 ** notice, this list of conditions and the following disclaimer.
cannam@85 12 ** * Redistributions in binary form must reproduce the above copyright
cannam@85 13 ** notice, this list of conditions and the following disclaimer in
cannam@85 14 ** the documentation and/or other materials provided with the
cannam@85 15 ** distribution.
cannam@85 16 ** * Neither the author nor the names of any contributors may be used
cannam@85 17 ** to endorse or promote products derived from this software without
cannam@85 18 ** specific prior written permission.
cannam@85 19 **
cannam@85 20 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
cannam@85 21 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
cannam@85 22 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
cannam@85 23 ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
cannam@85 24 ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
cannam@85 25 ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
cannam@85 26 ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
cannam@85 27 ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
cannam@85 28 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
cannam@85 29 ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
cannam@85 30 ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
cannam@85 31 */
cannam@85 32
cannam@85 33 #include <stdio.h>
cannam@85 34 #include <stdlib.h>
cannam@85 35 #include <string.h>
cannam@85 36 #include <ctype.h>
cannam@85 37
cannam@85 38 #include <sndfile.h>
cannam@85 39
cannam@85 40 #include "common.h"
cannam@85 41
cannam@85 42
cannam@85 43 typedef struct
cannam@85 44 { char *infilename, *outfilename ;
cannam@85 45 SF_INFO infileinfo, outfileinfo ;
cannam@85 46 } OptionData ;
cannam@85 47
cannam@85 48 static void copy_metadata (SNDFILE *outfile, SNDFILE *infile, int channels) ;
cannam@85 49
cannam@85 50 static void
cannam@85 51 usage_exit (const char *progname)
cannam@85 52 {
cannam@85 53 printf ("\nUsage : %s [options] [encoding] <input file> <output file>\n", progname) ;
cannam@85 54 puts ("\n"
cannam@85 55 " where [option] may be:\n\n"
cannam@85 56 " -override-sample-rate=X : force sample rate of input to X\n"
cannam@85 57 ) ;
cannam@85 58
cannam@85 59 puts (
cannam@85 60 " where [encoding] may be one of the following:\n\n"
cannam@85 61 " -pcms8 : force the output to signed 8 bit pcm\n"
cannam@85 62 " -pcmu8 : force the output to unsigned 8 bit pcm\n"
cannam@85 63 " -pcm16 : force the output to 16 bit pcm\n"
cannam@85 64 " -pcm24 : force the output to 24 bit pcm\n"
cannam@85 65 " -pcm32 : force the output to 32 bit pcm\n"
cannam@85 66 " -float32 : force the output to 32 bit floating point"
cannam@85 67 ) ;
cannam@85 68 puts (
cannam@85 69 " -ulaw : force the output ULAW\n"
cannam@85 70 " -alaw : force the output ALAW\n"
cannam@85 71 " -ima-adpcm : force the output to IMA ADPCM (WAV only)\n"
cannam@85 72 " -ms-adpcm : force the output to MS ADPCM (WAV only)\n"
cannam@85 73 " -gsm610 : force the GSM6.10 (WAV only)\n"
cannam@85 74 " -dwvw12 : force the output to 12 bit DWVW (AIFF only)\n"
cannam@85 75 " -dwvw16 : force the output to 16 bit DWVW (AIFF only)\n"
cannam@85 76 " -dwvw24 : force the output to 24 bit DWVW (AIFF only)\n"
cannam@85 77 " -vorbis : force the output to Vorbis (OGG only)\n"
cannam@85 78 ) ;
cannam@85 79
cannam@85 80 puts (
cannam@85 81 " The format of the output file is determined by the file extension of the\n"
cannam@85 82 " output file name. The following extensions are currently understood:\n"
cannam@85 83 ) ;
cannam@85 84
cannam@85 85 sfe_dump_format_map () ;
cannam@85 86
cannam@85 87 puts ("") ;
cannam@85 88 exit (0) ;
cannam@85 89 } /* usage_exit */
cannam@85 90
cannam@85 91 int
cannam@85 92 main (int argc, char * argv [])
cannam@85 93 { const char *progname, *infilename, *outfilename ;
cannam@85 94 SNDFILE *infile = NULL, *outfile = NULL ;
cannam@85 95 SF_INFO sfinfo ;
cannam@85 96 int k, outfilemajor, outfileminor = 0, infileminor ;
cannam@85 97 int override_sample_rate = 0 ; /* assume no sample rate override. */
cannam@85 98
cannam@85 99 progname = program_name (argv [0]) ;
cannam@85 100
cannam@85 101 if (argc < 3 || argc > 5)
cannam@85 102 { usage_exit (progname) ;
cannam@85 103 return 1 ;
cannam@85 104 } ;
cannam@85 105
cannam@85 106 infilename = argv [argc-2] ;
cannam@85 107 outfilename = argv [argc-1] ;
cannam@85 108
cannam@85 109 if (strcmp (infilename, outfilename) == 0)
cannam@85 110 { printf ("Error : Input and output filenames are the same.\n\n") ;
cannam@85 111 usage_exit (progname) ;
cannam@85 112 return 1 ;
cannam@85 113 } ;
cannam@85 114
cannam@85 115 if (strlen (infilename) > 1 && infilename [0] == '-')
cannam@85 116 { printf ("Error : Input filename (%s) looks like an option.\n\n", infilename) ;
cannam@85 117 usage_exit (progname) ;
cannam@85 118 return 1 ;
cannam@85 119 } ;
cannam@85 120
cannam@85 121 if (outfilename [0] == '-')
cannam@85 122 { printf ("Error : Output filename (%s) looks like an option.\n\n", outfilename) ;
cannam@85 123 usage_exit (progname) ;
cannam@85 124 return 1 ;
cannam@85 125 } ;
cannam@85 126
cannam@85 127 for (k = 1 ; k < argc - 2 ; k++)
cannam@85 128 { if (! strcmp (argv [k], "-pcms8"))
cannam@85 129 { outfileminor = SF_FORMAT_PCM_S8 ;
cannam@85 130 continue ;
cannam@85 131 } ;
cannam@85 132 if (! strcmp (argv [k], "-pcmu8"))
cannam@85 133 { outfileminor = SF_FORMAT_PCM_U8 ;
cannam@85 134 continue ;
cannam@85 135 } ;
cannam@85 136 if (! strcmp (argv [k], "-pcm16"))
cannam@85 137 { outfileminor = SF_FORMAT_PCM_16 ;
cannam@85 138 continue ;
cannam@85 139 } ;
cannam@85 140 if (! strcmp (argv [k], "-pcm24"))
cannam@85 141 { outfileminor = SF_FORMAT_PCM_24 ;
cannam@85 142 continue ;
cannam@85 143 } ;
cannam@85 144 if (! strcmp (argv [k], "-pcm32"))
cannam@85 145 { outfileminor = SF_FORMAT_PCM_32 ;
cannam@85 146 continue ;
cannam@85 147 } ;
cannam@85 148 if (! strcmp (argv [k], "-float32"))
cannam@85 149 { outfileminor = SF_FORMAT_FLOAT ;
cannam@85 150 continue ;
cannam@85 151 } ;
cannam@85 152 if (! strcmp (argv [k], "-ulaw"))
cannam@85 153 { outfileminor = SF_FORMAT_ULAW ;
cannam@85 154 continue ;
cannam@85 155 } ;
cannam@85 156 if (! strcmp (argv [k], "-alaw"))
cannam@85 157 { outfileminor = SF_FORMAT_ALAW ;
cannam@85 158 continue ;
cannam@85 159 } ;
cannam@85 160 if (! strcmp (argv [k], "-ima-adpcm"))
cannam@85 161 { outfileminor = SF_FORMAT_IMA_ADPCM ;
cannam@85 162 continue ;
cannam@85 163 } ;
cannam@85 164 if (! strcmp (argv [k], "-ms-adpcm"))
cannam@85 165 { outfileminor = SF_FORMAT_MS_ADPCM ;
cannam@85 166 continue ;
cannam@85 167 } ;
cannam@85 168 if (! strcmp (argv [k], "-gsm610"))
cannam@85 169 { outfileminor = SF_FORMAT_GSM610 ;
cannam@85 170 continue ;
cannam@85 171 } ;
cannam@85 172 if (! strcmp (argv [k], "-dwvw12"))
cannam@85 173 { outfileminor = SF_FORMAT_DWVW_12 ;
cannam@85 174 continue ;
cannam@85 175 } ;
cannam@85 176 if (! strcmp (argv [k], "-dwvw16"))
cannam@85 177 { outfileminor = SF_FORMAT_DWVW_16 ;
cannam@85 178 continue ;
cannam@85 179 } ;
cannam@85 180 if (! strcmp (argv [k], "-dwvw24"))
cannam@85 181 { outfileminor = SF_FORMAT_DWVW_24 ;
cannam@85 182 continue ;
cannam@85 183 } ;
cannam@85 184 if (! strcmp (argv [k], "-vorbis"))
cannam@85 185 { outfileminor = SF_FORMAT_VORBIS ;
cannam@85 186 continue ;
cannam@85 187 } ;
cannam@85 188
cannam@85 189 if (strstr (argv [k], "-override-sample-rate=") == argv [k])
cannam@85 190 { const char *ptr ;
cannam@85 191
cannam@85 192 ptr = argv [k] + strlen ("-override-sample-rate=") ;
cannam@85 193 override_sample_rate = atoi (ptr) ;
cannam@85 194 continue ;
cannam@85 195 } ;
cannam@85 196
cannam@85 197 printf ("Error : Not able to decode argunment '%s'.\n", argv [k]) ;
cannam@85 198 exit (1) ;
cannam@85 199 } ;
cannam@85 200
cannam@85 201 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@85 202
cannam@85 203 if ((infile = sf_open (infilename, SFM_READ, &sfinfo)) == NULL)
cannam@85 204 { printf ("Not able to open input file %s.\n", infilename) ;
cannam@85 205 puts (sf_strerror (NULL)) ;
cannam@85 206 return 1 ;
cannam@85 207 } ;
cannam@85 208
cannam@85 209 /* Update sample rate if forced to something else. */
cannam@85 210 if (override_sample_rate)
cannam@85 211 sfinfo.samplerate = override_sample_rate ;
cannam@85 212
cannam@85 213 infileminor = sfinfo.format & SF_FORMAT_SUBMASK ;
cannam@85 214
cannam@85 215 if ((sfinfo.format = sfe_file_type_of_ext (outfilename, sfinfo.format)) == 0)
cannam@85 216 { printf ("Error : Not able to determine output file type for %s.\n", outfilename) ;
cannam@85 217 return 1 ;
cannam@85 218 } ;
cannam@85 219
cannam@85 220 outfilemajor = sfinfo.format & (SF_FORMAT_TYPEMASK | SF_FORMAT_ENDMASK) ;
cannam@85 221
cannam@85 222 if (outfileminor == 0)
cannam@85 223 outfileminor = sfinfo.format & SF_FORMAT_SUBMASK ;
cannam@85 224
cannam@85 225 if (outfileminor != 0)
cannam@85 226 sfinfo.format = outfilemajor | outfileminor ;
cannam@85 227 else
cannam@85 228 sfinfo.format = outfilemajor | (sfinfo.format & SF_FORMAT_SUBMASK) ;
cannam@85 229
cannam@85 230 if ((sfinfo.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_XI)
cannam@85 231 switch (sfinfo.format & SF_FORMAT_SUBMASK)
cannam@85 232 { case SF_FORMAT_PCM_16 :
cannam@85 233 sfinfo.format = outfilemajor | SF_FORMAT_DPCM_16 ;
cannam@85 234 break ;
cannam@85 235
cannam@85 236 case SF_FORMAT_PCM_S8 :
cannam@85 237 case SF_FORMAT_PCM_U8 :
cannam@85 238 sfinfo.format = outfilemajor | SF_FORMAT_DPCM_8 ;
cannam@85 239 break ;
cannam@85 240 } ;
cannam@85 241
cannam@85 242 if (sf_format_check (&sfinfo) == 0)
cannam@85 243 { printf ("Error : output file format is invalid (0x%08X).\n", sfinfo.format) ;
cannam@85 244 return 1 ;
cannam@85 245 } ;
cannam@85 246
cannam@85 247 /* Open the output file. */
cannam@85 248 if ((outfile = sf_open (outfilename, SFM_WRITE, &sfinfo)) == NULL)
cannam@85 249 { printf ("Not able to open output file %s : %s\n", outfilename, sf_strerror (NULL)) ;
cannam@85 250 return 1 ;
cannam@85 251 } ;
cannam@85 252
cannam@85 253 /* Copy the metadata */
cannam@85 254 copy_metadata (outfile, infile, sfinfo.channels) ;
cannam@85 255
cannam@85 256 if ((outfileminor == SF_FORMAT_DOUBLE) || (outfileminor == SF_FORMAT_FLOAT)
cannam@85 257 || (infileminor == SF_FORMAT_DOUBLE) || (infileminor == SF_FORMAT_FLOAT)
cannam@85 258 || (infileminor == SF_FORMAT_VORBIS) || (outfileminor == SF_FORMAT_VORBIS))
cannam@85 259 sfe_copy_data_fp (outfile, infile, sfinfo.channels) ;
cannam@85 260 else
cannam@85 261 sfe_copy_data_int (outfile, infile, sfinfo.channels) ;
cannam@85 262
cannam@85 263 sf_close (infile) ;
cannam@85 264 sf_close (outfile) ;
cannam@85 265
cannam@85 266 return 0 ;
cannam@85 267 } /* main */
cannam@85 268
cannam@85 269 static void
cannam@85 270 copy_metadata (SNDFILE *outfile, SNDFILE *infile, int channels)
cannam@85 271 { SF_INSTRUMENT inst ;
cannam@85 272 SF_BROADCAST_INFO_2K binfo ;
cannam@85 273 const char *str ;
cannam@85 274 int k, chanmap [256] ;
cannam@85 275
cannam@85 276 for (k = SF_STR_FIRST ; k <= SF_STR_LAST ; k++)
cannam@85 277 { str = sf_get_string (infile, k) ;
cannam@85 278 if (str != NULL)
cannam@85 279 sf_set_string (outfile, k, str) ;
cannam@85 280 } ;
cannam@85 281
cannam@85 282 memset (&inst, 0, sizeof (inst)) ;
cannam@85 283 memset (&binfo, 0, sizeof (binfo)) ;
cannam@85 284
cannam@85 285 if (channels < ARRAY_LEN (chanmap))
cannam@85 286 { size_t size = channels * sizeof (chanmap [0]) ;
cannam@85 287
cannam@85 288 if (sf_command (infile, SFC_GET_CHANNEL_MAP_INFO, chanmap, size) == SF_TRUE)
cannam@85 289 sf_command (outfile, SFC_SET_CHANNEL_MAP_INFO, chanmap, size) ;
cannam@85 290 } ;
cannam@85 291
cannam@85 292 if (sf_command (infile, SFC_GET_INSTRUMENT, &inst, sizeof (inst)) == SF_TRUE)
cannam@85 293 sf_command (outfile, SFC_SET_INSTRUMENT, &inst, sizeof (inst)) ;
cannam@85 294
cannam@85 295 if (sf_command (infile, SFC_GET_BROADCAST_INFO, &binfo, sizeof (binfo)) == SF_TRUE)
cannam@85 296 sf_command (outfile, SFC_SET_BROADCAST_INFO, &binfo, sizeof (binfo)) ;
cannam@85 297
cannam@85 298 } /* copy_metadata */
cannam@85 299