sndfile-convert.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. ** Copyright (C) 1999-2019 Erik de Castro Lopo <[email protected]>
  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 : signed 8 bit pcm\n"
  58. " -pcmu8 : unsigned 8 bit pcm\n"
  59. " -pcm16 : 16 bit pcm\n"
  60. " -pcm24 : 24 bit pcm\n"
  61. " -pcm32 : 32 bit pcm\n"
  62. " -float32 : 32 bit floating point\n"
  63. " -float64 : 64 bit floating point\n"
  64. " -ulaw : ULAW\n"
  65. " -alaw : ALAW\n"
  66. " -alac16 : 16 bit ALAC (CAF only)\n"
  67. " -alac20 : 20 bit ALAC (CAF only)\n"
  68. " -alac24 : 24 bit ALAC (CAF only)\n"
  69. " -alac32 : 32 bit ALAC (CAF only)\n"
  70. " -ima-adpcm : IMA ADPCM (WAV only)\n"
  71. " -ms-adpcm : MS ADPCM (WAV only)\n"
  72. " -gsm610 : GSM6.10 (WAV only)\n"
  73. " -dwvw12 : 12 bit DWVW (AIFF only)\n"
  74. " -dwvw16 : 16 bit DWVW (AIFF only)\n"
  75. " -dwvw24 : 24 bit DWVW (AIFF only)\n"
  76. " -vorbis : Vorbis (OGG only)\n"
  77. " -opus : Opus (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 (1) ;
  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. int channels = sfinfo->channels ;
  98. sfinfo->format = old_format & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
  99. if (endian && sf_format_check (sfinfo))
  100. { printf ("Error : output file format does not support %s endian-ness.\n", sfe_endian_name (endian)) ;
  101. exit (1) ;
  102. } ;
  103. sfinfo->channels = 1 ;
  104. if (sf_format_check (sfinfo))
  105. { printf ("Error : output file format does not support %d channels.\n", channels) ;
  106. exit (1) ;
  107. } ;
  108. printf ("\n"
  109. "Error : output file format is invalid.\n"
  110. "The '%s' container does not support '%s' codec data.\n"
  111. "Run '%s --help' for clues.\n\n",
  112. sfe_container_name (sfinfo->format), sfe_codec_name (sfinfo->format), program_name (argv0)) ;
  113. exit (1) ;
  114. } /* report_format_error_exit */
  115. int
  116. main (int argc, char * argv [])
  117. { const char *progname, *infilename, *outfilename ;
  118. SNDFILE *infile = NULL, *outfile = NULL ;
  119. SF_INFO sfinfo ;
  120. int k, outfilemajor, outfileminor = 0, infileminor ;
  121. int override_sample_rate = 0 ; /* assume no sample rate override. */
  122. int endian = SF_ENDIAN_FILE, normalize = SF_FALSE ;
  123. progname = program_name (argv [0]) ;
  124. if (argc < 3 || argc > 5)
  125. usage_exit (progname) ;
  126. infilename = argv [argc-2] ;
  127. outfilename = argv [argc-1] ;
  128. if (strcmp (infilename, outfilename) == 0)
  129. { printf ("Error : Input and output filenames are the same.\n\n") ;
  130. usage_exit (progname) ;
  131. } ;
  132. if (strlen (infilename) > 1 && infilename [0] == '-')
  133. { printf ("Error : Input filename (%s) looks like an option.\n\n", infilename) ;
  134. usage_exit (progname) ;
  135. } ;
  136. if (outfilename [0] == '-')
  137. { printf ("Error : Output filename (%s) looks like an option.\n\n", outfilename) ;
  138. usage_exit (progname) ;
  139. } ;
  140. for (k = 1 ; k < argc - 2 ; k++)
  141. { if (! strcmp (argv [k], "-pcms8"))
  142. { outfileminor = SF_FORMAT_PCM_S8 ;
  143. continue ;
  144. } ;
  145. if (! strcmp (argv [k], "-pcmu8"))
  146. { outfileminor = SF_FORMAT_PCM_U8 ;
  147. continue ;
  148. } ;
  149. if (! strcmp (argv [k], "-pcm16"))
  150. { outfileminor = SF_FORMAT_PCM_16 ;
  151. continue ;
  152. } ;
  153. if (! strcmp (argv [k], "-pcm24"))
  154. { outfileminor = SF_FORMAT_PCM_24 ;
  155. continue ;
  156. } ;
  157. if (! strcmp (argv [k], "-pcm32"))
  158. { outfileminor = SF_FORMAT_PCM_32 ;
  159. continue ;
  160. } ;
  161. if (! strcmp (argv [k], "-float32"))
  162. { outfileminor = SF_FORMAT_FLOAT ;
  163. continue ;
  164. } ;
  165. if (! strcmp (argv [k], "-float64"))
  166. { outfileminor = SF_FORMAT_DOUBLE ;
  167. continue ;
  168. } ;
  169. if (! strcmp (argv [k], "-ulaw"))
  170. { outfileminor = SF_FORMAT_ULAW ;
  171. continue ;
  172. } ;
  173. if (! strcmp (argv [k], "-alaw"))
  174. { outfileminor = SF_FORMAT_ALAW ;
  175. continue ;
  176. } ;
  177. if (! strcmp (argv [k], "-alac16"))
  178. { outfileminor = SF_FORMAT_ALAC_16 ;
  179. continue ;
  180. } ;
  181. if (! strcmp (argv [k], "-alac20"))
  182. { outfileminor = SF_FORMAT_ALAC_20 ;
  183. continue ;
  184. } ;
  185. if (! strcmp (argv [k], "-alac24"))
  186. { outfileminor = SF_FORMAT_ALAC_24 ;
  187. continue ;
  188. } ;
  189. if (! strcmp (argv [k], "-alac32"))
  190. { outfileminor = SF_FORMAT_ALAC_32 ;
  191. continue ;
  192. } ;
  193. if (! strcmp (argv [k], "-ima-adpcm"))
  194. { outfileminor = SF_FORMAT_IMA_ADPCM ;
  195. continue ;
  196. } ;
  197. if (! strcmp (argv [k], "-ms-adpcm"))
  198. { outfileminor = SF_FORMAT_MS_ADPCM ;
  199. continue ;
  200. } ;
  201. if (! strcmp (argv [k], "-gsm610"))
  202. { outfileminor = SF_FORMAT_GSM610 ;
  203. continue ;
  204. } ;
  205. if (! strcmp (argv [k], "-dwvw12"))
  206. { outfileminor = SF_FORMAT_DWVW_12 ;
  207. continue ;
  208. } ;
  209. if (! strcmp (argv [k], "-dwvw16"))
  210. { outfileminor = SF_FORMAT_DWVW_16 ;
  211. continue ;
  212. } ;
  213. if (! strcmp (argv [k], "-dwvw24"))
  214. { outfileminor = SF_FORMAT_DWVW_24 ;
  215. continue ;
  216. } ;
  217. if (! strcmp (argv [k], "-vorbis"))
  218. { outfileminor = SF_FORMAT_VORBIS ;
  219. continue ;
  220. } ;
  221. if (! strcmp (argv [k], "-opus"))
  222. { outfileminor = SF_FORMAT_OPUS ;
  223. continue ;
  224. } ;
  225. if (strstr (argv [k], "-override-sample-rate=") == argv [k])
  226. { const char *ptr ;
  227. ptr = argv [k] + strlen ("-override-sample-rate=") ;
  228. override_sample_rate = atoi (ptr) ;
  229. continue ;
  230. } ;
  231. if (! strcmp (argv [k], "-endian=little"))
  232. { endian = SF_ENDIAN_LITTLE ;
  233. continue ;
  234. } ;
  235. if (! strcmp (argv [k], "-endian=big"))
  236. { endian = SF_ENDIAN_BIG ;
  237. continue ;
  238. } ;
  239. if (! strcmp (argv [k], "-endian=cpu"))
  240. { endian = SF_ENDIAN_CPU ;
  241. continue ;
  242. } ;
  243. if (! strcmp (argv [k], "-endian=file"))
  244. { endian = SF_ENDIAN_FILE ;
  245. continue ;
  246. } ;
  247. if (! strcmp (argv [k], "-normalize"))
  248. { normalize = SF_TRUE ;
  249. continue ;
  250. } ;
  251. printf ("Error : Not able to decode argument '%s'.\n", argv [k]) ;
  252. exit (1) ;
  253. } ;
  254. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  255. if ((infile = sf_open (infilename, SFM_READ, &sfinfo)) == NULL)
  256. { printf ("Not able to open input file %s.\n", infilename) ;
  257. puts (sf_strerror (NULL)) ;
  258. return 1 ;
  259. } ;
  260. /* Update sample rate if forced to something else. */
  261. if (override_sample_rate)
  262. sfinfo.samplerate = override_sample_rate ;
  263. infileminor = sfinfo.format & SF_FORMAT_SUBMASK ;
  264. if ((sfinfo.format = sfe_file_type_of_ext (outfilename, sfinfo.format)) == 0)
  265. { printf ("Error : Not able to determine output file type for %s.\n", outfilename) ;
  266. return 1 ;
  267. } ;
  268. outfilemajor = sfinfo.format & (SF_FORMAT_TYPEMASK | SF_FORMAT_ENDMASK) ;
  269. if (outfileminor == 0)
  270. outfileminor = sfinfo.format & SF_FORMAT_SUBMASK ;
  271. if (outfileminor != 0)
  272. sfinfo.format = outfilemajor | outfileminor ;
  273. else
  274. sfinfo.format = outfilemajor | (sfinfo.format & SF_FORMAT_SUBMASK) ;
  275. sfinfo.format |= endian ;
  276. if ((sfinfo.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_XI)
  277. switch (sfinfo.format & SF_FORMAT_SUBMASK)
  278. { case SF_FORMAT_PCM_16 :
  279. sfinfo.format = outfilemajor | SF_FORMAT_DPCM_16 ;
  280. break ;
  281. case SF_FORMAT_PCM_S8 :
  282. case SF_FORMAT_PCM_U8 :
  283. sfinfo.format = outfilemajor | SF_FORMAT_DPCM_8 ;
  284. break ;
  285. } ;
  286. if (sf_format_check (&sfinfo) == 0)
  287. { sf_close (infile) ;
  288. report_format_error_exit (argv [0], &sfinfo) ;
  289. } ;
  290. if ((sfinfo.format & SF_FORMAT_SUBMASK) == SF_FORMAT_GSM610 && sfinfo.samplerate != 8000)
  291. { printf (
  292. "WARNING: GSM 6.10 data format only supports 8kHz sample rate. The converted\n"
  293. "ouput file will contain the input data converted to the GSM 6.10 data format\n"
  294. "but not re-sampled.\n"
  295. ) ;
  296. } ;
  297. /* Open the output file. */
  298. if ((outfile = sf_open (outfilename, SFM_WRITE, &sfinfo)) == NULL)
  299. { printf ("Not able to open output file %s : %s\n", outfilename, sf_strerror (NULL)) ;
  300. return 1 ;
  301. } ;
  302. /* Copy the metadata */
  303. copy_metadata (outfile, infile, sfinfo.channels) ;
  304. if (normalize
  305. || (outfileminor == SF_FORMAT_DOUBLE) || (outfileminor == SF_FORMAT_FLOAT)
  306. || (infileminor == SF_FORMAT_DOUBLE) || (infileminor == SF_FORMAT_FLOAT)
  307. || (infileminor == SF_FORMAT_OPUS) || (outfileminor == SF_FORMAT_OPUS)
  308. || (infileminor == SF_FORMAT_VORBIS) || (outfileminor == SF_FORMAT_VORBIS)
  309. || (infileminor == SF_FORMAT_MPEG_LAYER_I)
  310. || (infileminor == SF_FORMAT_MPEG_LAYER_II)
  311. || (infileminor == SF_FORMAT_MPEG_LAYER_III) || (outfileminor == SF_FORMAT_MPEG_LAYER_III))
  312. { if (sfe_copy_data_fp (outfile, infile, sfinfo.channels, normalize) != 0)
  313. { printf ("Error : Not able to decode input file %s.\n", infilename) ;
  314. return 1 ;
  315. } ;
  316. }
  317. else
  318. sfe_copy_data_int (outfile, infile, sfinfo.channels) ;
  319. sf_close (infile) ;
  320. sf_close (outfile) ;
  321. return 0 ;
  322. } /* main */
  323. static void
  324. copy_metadata (SNDFILE *outfile, SNDFILE *infile, int channels)
  325. { SF_INSTRUMENT inst ;
  326. SF_CUES cues ;
  327. SF_BROADCAST_INFO_2K binfo ;
  328. const char *str ;
  329. int k, chanmap [256] ;
  330. for (k = SF_STR_FIRST ; k <= SF_STR_LAST ; k++)
  331. { str = sf_get_string (infile, k) ;
  332. if (str != NULL)
  333. sf_set_string (outfile, k, str) ;
  334. } ;
  335. memset (&inst, 0, sizeof (inst)) ;
  336. memset (&cues, 0, sizeof (cues)) ;
  337. memset (&binfo, 0, sizeof (binfo)) ;
  338. if (channels < ARRAY_LEN (chanmap))
  339. { int size = channels * sizeof (chanmap [0]) ;
  340. if (sf_command (infile, SFC_GET_CHANNEL_MAP_INFO, chanmap, size) == SF_TRUE)
  341. sf_command (outfile, SFC_SET_CHANNEL_MAP_INFO, chanmap, size) ;
  342. } ;
  343. if (sf_command (infile, SFC_GET_CUE, &cues, sizeof (cues)) == SF_TRUE)
  344. sf_command (outfile, SFC_SET_CUE, &cues, sizeof (cues)) ;
  345. if (sf_command (infile, SFC_GET_INSTRUMENT, &inst, sizeof (inst)) == SF_TRUE)
  346. sf_command (outfile, SFC_SET_INSTRUMENT, &inst, sizeof (inst)) ;
  347. if (sf_command (infile, SFC_GET_BROADCAST_INFO, &binfo, sizeof (binfo)) == SF_TRUE)
  348. sf_command (outfile, SFC_SET_BROADCAST_INFO, &binfo, sizeof (binfo)) ;
  349. } /* copy_metadata */