sndfile-convert.c 11 KB

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