annotate src/libsndfile-1.0.25/programs/sndfile-convert.c @ 83:ae30d91d2ffe

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