common.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. ** Copyright (C) 1999-2019 Erik de Castro Lopo <[email protected]>
  3. ** Copyright (C) 2008 George Blood Audio
  4. **
  5. ** All rights reserved.
  6. **
  7. ** Redistribution and use in source and binary forms, with or without
  8. ** modification, are permitted provided that the following conditions are
  9. ** met:
  10. **
  11. ** * Redistributions of source code must retain the above copyright
  12. ** notice, this list of conditions and the following disclaimer.
  13. ** * Redistributions in binary form must reproduce the above copyright
  14. ** notice, this list of conditions and the following disclaimer in
  15. ** the documentation and/or other materials provided with the
  16. ** distribution.
  17. ** * Neither the author nor the names of any contributors may be used
  18. ** to endorse or promote products derived from this software without
  19. ** specific prior written permission.
  20. **
  21. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  23. ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24. ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  25. ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26. ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  28. ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  30. ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  31. ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <ctype.h>
  37. #include <stdint.h>
  38. #include <math.h>
  39. #include <sndfile.h>
  40. #include "common.h"
  41. #define BUFFER_LEN 4096
  42. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  43. int
  44. sfe_copy_data_fp (SNDFILE *outfile, SNDFILE *infile, int channels, int normalize)
  45. { static double data [BUFFER_LEN], max ;
  46. sf_count_t frames, readcount, k ;
  47. frames = BUFFER_LEN / channels ;
  48. readcount = frames ;
  49. sf_command (infile, SFC_CALC_SIGNAL_MAX, &max, sizeof (max)) ;
  50. if (!isnormal (max)) /* neither zero, subnormal, infinite, nor NaN */
  51. return 1 ;
  52. if (!normalize && max < 1.0)
  53. { while (readcount > 0)
  54. { readcount = sf_readf_double (infile, data, frames) ;
  55. sf_writef_double (outfile, data, readcount) ;
  56. } ;
  57. }
  58. else
  59. { sf_command (infile, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
  60. while (readcount > 0)
  61. { readcount = sf_readf_double (infile, data, frames) ;
  62. for (k = 0 ; k < readcount * channels ; k++)
  63. { data [k] /= max ;
  64. if (!isfinite (data [k])) /* infinite or NaN */
  65. return 1 ;
  66. }
  67. sf_writef_double (outfile, data, readcount) ;
  68. } ;
  69. } ;
  70. return 0 ;
  71. } /* sfe_copy_data_fp */
  72. void
  73. sfe_copy_data_int (SNDFILE *outfile, SNDFILE *infile, int channels)
  74. { static int data [BUFFER_LEN] ;
  75. int frames, readcount ;
  76. frames = BUFFER_LEN / channels ;
  77. readcount = frames ;
  78. while (readcount > 0)
  79. { readcount = (int) sf_readf_int (infile, data, frames) ;
  80. sf_writef_int (outfile, data, readcount) ;
  81. } ;
  82. return ;
  83. } /* sfe_copy_data_int */
  84. /*==============================================================================
  85. */
  86. static int
  87. merge_broadcast_info (SNDFILE * infile, SNDFILE * outfile, int format, const METADATA_INFO * info)
  88. { SF_BROADCAST_INFO_2K binfo ;
  89. int infileminor ;
  90. memset (&binfo, 0, sizeof (binfo)) ;
  91. if ((SF_FORMAT_TYPEMASK & format) != SF_FORMAT_WAV)
  92. { printf ("Error : This is not a WAV file and hence broadcast info cannot be added to it.\n\n") ;
  93. return 1 ;
  94. } ;
  95. infileminor = SF_FORMAT_SUBMASK & format ;
  96. switch (infileminor)
  97. { case SF_FORMAT_PCM_16 :
  98. case SF_FORMAT_PCM_24 :
  99. case SF_FORMAT_PCM_32 :
  100. case SF_FORMAT_MPEG_LAYER_III :
  101. break ;
  102. default :
  103. printf (
  104. "Warning : The EBU Technical Recommendation R68-2000 states that the only\n"
  105. " allowed encodings are Linear PCM and MPEG3. This file is not in\n"
  106. " the right format.\n\n"
  107. ) ;
  108. break ;
  109. } ;
  110. if (sf_command (infile, SFC_GET_BROADCAST_INFO, &binfo, sizeof (binfo)) == 0)
  111. { if (infile == outfile)
  112. { printf (
  113. "Error : Attempting in-place broadcast info update, but file does not\n"
  114. " have a 'bext' chunk to modify. The solution is to specify both\n"
  115. " input and output files on the command line.\n\n"
  116. ) ;
  117. return 1 ;
  118. } ;
  119. } ;
  120. #define REPLACE_IF_NEW(x) \
  121. if (info->x != NULL) \
  122. { memset (binfo.x, 0, sizeof (binfo.x)) ; \
  123. memcpy (binfo.x, info->x, MIN (strlen (info->x), sizeof (binfo.x))) ; \
  124. } ;
  125. REPLACE_IF_NEW (description) ;
  126. REPLACE_IF_NEW (originator) ;
  127. REPLACE_IF_NEW (originator_reference) ;
  128. REPLACE_IF_NEW (origination_date) ;
  129. REPLACE_IF_NEW (origination_time) ;
  130. REPLACE_IF_NEW (umid) ;
  131. /* Special case loudness values */
  132. #define REPLACE_IF_NEW_INT(x) \
  133. if (info->x != NULL) \
  134. { binfo.x = round (atof (info->x) * 100.0) ; \
  135. } ;
  136. REPLACE_IF_NEW_INT (loudness_value) ;
  137. REPLACE_IF_NEW_INT (loudness_range) ;
  138. REPLACE_IF_NEW_INT (max_true_peak_level) ;
  139. REPLACE_IF_NEW_INT (max_momentary_loudness) ;
  140. REPLACE_IF_NEW_INT (max_shortterm_loudness) ;
  141. /* Special case for Time Ref. */
  142. if (info->time_ref != NULL)
  143. { uint64_t ts = atoll (info->time_ref) ;
  144. binfo.time_reference_high = (ts >> 32) ;
  145. binfo.time_reference_low = (ts & 0xffffffff) ;
  146. } ;
  147. /* Special case for coding_history because we may want to append. */
  148. if (info->coding_history != NULL)
  149. { if (info->coding_hist_append)
  150. { int slen = (int) strlen (binfo.coding_history) ;
  151. while (slen > 1 && isspace (binfo.coding_history [slen - 1]))
  152. slen -- ;
  153. memcpy (binfo.coding_history + slen, info->coding_history, sizeof (binfo.coding_history) - slen) ;
  154. }
  155. else
  156. { size_t slen = MIN (strlen (info->coding_history), sizeof (binfo.coding_history)) ;
  157. memset (binfo.coding_history, 0, sizeof (binfo.coding_history)) ;
  158. memcpy (binfo.coding_history, info->coding_history, slen) ;
  159. binfo.coding_history_size = (uint32_t) slen ;
  160. } ;
  161. } ;
  162. if (sf_command (outfile, SFC_SET_BROADCAST_INFO, &binfo, sizeof (binfo)) == 0)
  163. { printf ("Error : Setting of broadcast info chunks failed.\n\n") ;
  164. return 1 ;
  165. } ;
  166. return 0 ;
  167. } /* merge_broadcast_info*/
  168. static void
  169. update_strings (SNDFILE * outfile, const METADATA_INFO * info)
  170. {
  171. if (info->title != NULL)
  172. sf_set_string (outfile, SF_STR_TITLE, info->title) ;
  173. if (info->copyright != NULL)
  174. sf_set_string (outfile, SF_STR_COPYRIGHT, info->copyright) ;
  175. if (info->artist != NULL)
  176. sf_set_string (outfile, SF_STR_ARTIST, info->artist) ;
  177. if (info->comment != NULL)
  178. sf_set_string (outfile, SF_STR_COMMENT, info->comment) ;
  179. if (info->date != NULL)
  180. sf_set_string (outfile, SF_STR_DATE, info->date) ;
  181. if (info->album != NULL)
  182. sf_set_string (outfile, SF_STR_ALBUM, info->album) ;
  183. if (info->license != NULL)
  184. sf_set_string (outfile, SF_STR_LICENSE, info->license) ;
  185. } /* update_strings */
  186. void
  187. sfe_apply_metadata_changes (const char * filenames [2], const METADATA_INFO * info)
  188. { SNDFILE *infile = NULL, *outfile = NULL ;
  189. SF_INFO sfinfo ;
  190. METADATA_INFO tmpinfo ;
  191. int error_code = 0 ;
  192. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  193. memset (&tmpinfo, 0, sizeof (tmpinfo)) ;
  194. if (filenames [1] == NULL)
  195. infile = outfile = sf_open (filenames [0], SFM_RDWR, &sfinfo) ;
  196. else
  197. { infile = sf_open (filenames [0], SFM_READ, &sfinfo) ;
  198. /* Output must be WAV. */
  199. sfinfo.format = SF_FORMAT_WAV | (SF_FORMAT_SUBMASK & sfinfo.format) ;
  200. outfile = sf_open (filenames [1], SFM_WRITE, &sfinfo) ;
  201. } ;
  202. if (infile == NULL)
  203. { printf ("Error : Not able to open input file '%s' : %s\n", filenames [0], sf_strerror (infile)) ;
  204. error_code = 1 ;
  205. goto cleanup_exit ;
  206. } ;
  207. if (outfile == NULL)
  208. { printf ("Error : Not able to open output file '%s' : %s\n", filenames [1], sf_strerror (outfile)) ;
  209. error_code = 1 ;
  210. goto cleanup_exit ;
  211. } ;
  212. if (info->has_bext_fields && merge_broadcast_info (infile, outfile, sfinfo.format, info))
  213. { error_code = 1 ;
  214. goto cleanup_exit ;
  215. } ;
  216. if (infile != outfile)
  217. { int infileminor = SF_FORMAT_SUBMASK & sfinfo.format ;
  218. /* If the input file is not the same as the output file, copy the data. */
  219. if ((infileminor == SF_FORMAT_DOUBLE) || (infileminor == SF_FORMAT_FLOAT))
  220. { if (sfe_copy_data_fp (outfile, infile, sfinfo.channels, SF_FALSE) != 0)
  221. { printf ("Error : Not able to decode input file '%s'\n", filenames [0]) ;
  222. error_code = 1 ;
  223. goto cleanup_exit ;
  224. } ;
  225. }
  226. else
  227. sfe_copy_data_int (outfile, infile, sfinfo.channels) ;
  228. } ;
  229. update_strings (outfile, info) ;
  230. cleanup_exit :
  231. if (outfile != NULL && outfile != infile)
  232. sf_close (outfile) ;
  233. if (infile != NULL)
  234. sf_close (infile) ;
  235. if (error_code)
  236. exit (error_code) ;
  237. return ;
  238. } /* sfe_apply_metadata_changes */
  239. /*==============================================================================
  240. */
  241. typedef struct
  242. { const char *ext ;
  243. int len ;
  244. int format ;
  245. } OUTPUT_FORMAT_MAP ;
  246. /* Map a file name extension to a container format. */
  247. static OUTPUT_FORMAT_MAP format_map [] =
  248. {
  249. { "wav", 0, SF_FORMAT_WAV },
  250. { "aif", 3, SF_FORMAT_AIFF },
  251. { "au", 0, SF_FORMAT_AU },
  252. { "snd", 0, SF_FORMAT_AU },
  253. { "raw", 0, SF_FORMAT_RAW },
  254. { "gsm", 0, SF_FORMAT_RAW | SF_FORMAT_GSM610 },
  255. { "vox", 0, SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM },
  256. { "paf", 0, SF_FORMAT_PAF | SF_ENDIAN_BIG },
  257. { "fap", 0, SF_FORMAT_PAF | SF_ENDIAN_LITTLE },
  258. { "svx", 0, SF_FORMAT_SVX },
  259. { "nist", 0, SF_FORMAT_NIST },
  260. { "sph", 0, SF_FORMAT_NIST },
  261. { "voc", 0, SF_FORMAT_VOC },
  262. { "ircam", 0, SF_FORMAT_IRCAM },
  263. { "sf", 0, SF_FORMAT_IRCAM },
  264. { "w64", 0, SF_FORMAT_W64 },
  265. { "mat", 0, SF_FORMAT_MAT4 },
  266. { "mat4", 0, SF_FORMAT_MAT4 },
  267. { "mat5", 0, SF_FORMAT_MAT5 },
  268. { "pvf", 0, SF_FORMAT_PVF },
  269. { "xi", 0, SF_FORMAT_XI },
  270. { "htk", 0, SF_FORMAT_HTK },
  271. { "sds", 0, SF_FORMAT_SDS },
  272. { "avr", 0, SF_FORMAT_AVR },
  273. { "wavex", 0, SF_FORMAT_WAVEX },
  274. { "sd2", 0, SF_FORMAT_SD2 },
  275. { "flac", 0, SF_FORMAT_FLAC },
  276. { "caf", 0, SF_FORMAT_CAF },
  277. { "wve", 0, SF_FORMAT_WVE },
  278. { "prc", 0, SF_FORMAT_WVE },
  279. { "oga", 0, SF_FORMAT_OGG },
  280. { "ogg", 0, SF_FORMAT_OGG | SF_FORMAT_VORBIS },
  281. { "opus", 0, SF_FORMAT_OGG | SF_FORMAT_OPUS },
  282. { "mpc", 0, SF_FORMAT_MPC2K },
  283. { "rf64", 0, SF_FORMAT_RF64 },
  284. { "mp3", 0, SF_FORMAT_MPEG | SF_FORMAT_MPEG_LAYER_III },
  285. } ; /* format_map */
  286. int
  287. sfe_file_type_of_ext (const char *str, int format)
  288. { char buffer [16], *cptr ;
  289. int k ;
  290. format &= SF_FORMAT_SUBMASK ;
  291. if ((cptr = strrchr (str, '.')) == NULL)
  292. return 0 ;
  293. strncpy (buffer, cptr + 1, 15) ;
  294. buffer [15] = 0 ;
  295. for (k = 0 ; buffer [k] ; k++)
  296. buffer [k] = tolower ((buffer [k])) ;
  297. for (k = 0 ; k < (int) (sizeof (format_map) / sizeof (format_map [0])) ; k++)
  298. { if ((format_map [k].len > 0 && strncmp (buffer, format_map [k].ext, format_map [k].len) == 0) ||
  299. (strcmp (buffer, format_map [k].ext) == 0))
  300. { if (format_map [k].format & SF_FORMAT_SUBMASK)
  301. return format_map [k].format ;
  302. else
  303. return format_map [k].format | format ;
  304. } ;
  305. } ;
  306. /* Default if all the above fails. */
  307. return (SF_FORMAT_WAV | SF_FORMAT_PCM_24) ;
  308. } /* sfe_file_type_of_ext */
  309. void
  310. sfe_dump_format_map (void)
  311. { SF_FORMAT_INFO info ;
  312. int k ;
  313. for (k = 0 ; k < ARRAY_LEN (format_map) ; k++)
  314. { info.format = format_map [k].format ;
  315. sf_command (NULL, SFC_GET_FORMAT_INFO, &info, sizeof (info)) ;
  316. printf (" %-10s : %s", format_map [k].ext, info.name == NULL ? "????" : info.name) ;
  317. if (format_map [k].format & SF_FORMAT_SUBMASK)
  318. { info.format = format_map [k].format & SF_FORMAT_SUBMASK ;
  319. sf_command (NULL, SFC_GET_FORMAT_INFO, &info, sizeof (info)) ;
  320. printf (" %s", info.name == NULL ? "????" : info.name) ;
  321. } ;
  322. putchar ('\n') ;
  323. } ;
  324. } /* sfe_dump_format_map */
  325. const char *
  326. program_name (const char * argv0)
  327. { const char * tmp ;
  328. tmp = strrchr (argv0, '/') ;
  329. argv0 = tmp ? tmp + 1 : argv0 ;
  330. /* Remove leading libtool name mangling. */
  331. if (strstr (argv0, "lt-") == argv0)
  332. return argv0 + 3 ;
  333. return argv0 ;
  334. } /* program_name */
  335. const char *
  336. sfe_endian_name (int format)
  337. {
  338. switch (format & SF_FORMAT_ENDMASK)
  339. { case SF_ENDIAN_FILE : return "file" ;
  340. case SF_ENDIAN_LITTLE : return "little" ;
  341. case SF_ENDIAN_BIG : return "big" ;
  342. case SF_ENDIAN_CPU : return "cpu" ;
  343. default : break ;
  344. } ;
  345. return "unknown" ;
  346. } /* sfe_endian_name */
  347. const char *
  348. sfe_container_name (int format)
  349. {
  350. switch (format & SF_FORMAT_TYPEMASK)
  351. { case SF_FORMAT_WAV : return "WAV" ;
  352. case SF_FORMAT_AIFF : return "AIFF" ;
  353. case SF_FORMAT_AU : return "AU" ;
  354. case SF_FORMAT_RAW : return "RAW" ;
  355. case SF_FORMAT_PAF : return "PAF" ;
  356. case SF_FORMAT_SVX : return "SVX" ;
  357. case SF_FORMAT_NIST : return "NIST" ;
  358. case SF_FORMAT_VOC : return "VOC" ;
  359. case SF_FORMAT_IRCAM : return "IRCAM" ;
  360. case SF_FORMAT_W64 : return "W64" ;
  361. case SF_FORMAT_MAT4 : return "MAT4" ;
  362. case SF_FORMAT_MAT5 : return "MAT5" ;
  363. case SF_FORMAT_PVF : return "PVF" ;
  364. case SF_FORMAT_XI : return "XI" ;
  365. case SF_FORMAT_HTK : return "HTK" ;
  366. case SF_FORMAT_SDS : return "SDS" ;
  367. case SF_FORMAT_AVR : return "AVR" ;
  368. case SF_FORMAT_WAVEX : return "WAVEX" ;
  369. case SF_FORMAT_SD2 : return "SD2" ;
  370. case SF_FORMAT_FLAC : return "FLAC" ;
  371. case SF_FORMAT_CAF : return "CAF" ;
  372. case SF_FORMAT_WVE : return "WVE" ;
  373. case SF_FORMAT_OGG : return "OGG" ;
  374. case SF_FORMAT_MPC2K : return "MPC2K" ;
  375. case SF_FORMAT_RF64 : return "RF64" ;
  376. case SF_FORMAT_MPEG : return "MPEG" ;
  377. default : break ;
  378. } ;
  379. return "unknown" ;
  380. } /* sfe_container_name */
  381. const char *
  382. sfe_codec_name (int format)
  383. {
  384. switch (format & SF_FORMAT_SUBMASK)
  385. { case SF_FORMAT_PCM_S8 : return "signed 8 bit PCM" ;
  386. case SF_FORMAT_PCM_16 : return "16 bit PCM" ;
  387. case SF_FORMAT_PCM_24 : return "24 bit PCM" ;
  388. case SF_FORMAT_PCM_32 : return "32 bit PCM" ;
  389. case SF_FORMAT_PCM_U8 : return "unsigned 8 bit PCM" ;
  390. case SF_FORMAT_FLOAT : return "32 bit float" ;
  391. case SF_FORMAT_DOUBLE : return "64 bit double" ;
  392. case SF_FORMAT_ULAW : return "u-law" ;
  393. case SF_FORMAT_ALAW : return "a-law" ;
  394. case SF_FORMAT_IMA_ADPCM : return "IMA ADPCM" ;
  395. case SF_FORMAT_MS_ADPCM : return "MS ADPCM" ;
  396. case SF_FORMAT_GSM610 : return "gsm610" ;
  397. case SF_FORMAT_VOX_ADPCM : return "Vox ADPCM" ;
  398. case SF_FORMAT_G721_32 : return "g721 32kbps" ;
  399. case SF_FORMAT_G723_24 : return "g723 24kbps" ;
  400. case SF_FORMAT_G723_40 : return "g723 40kbps" ;
  401. case SF_FORMAT_DWVW_12 : return "12 bit DWVW" ;
  402. case SF_FORMAT_DWVW_16 : return "16 bit DWVW" ;
  403. case SF_FORMAT_DWVW_24 : return "14 bit DWVW" ;
  404. case SF_FORMAT_DWVW_N : return "DWVW" ;
  405. case SF_FORMAT_DPCM_8 : return "8 bit DPCM" ;
  406. case SF_FORMAT_DPCM_16 : return "16 bit DPCM" ;
  407. case SF_FORMAT_VORBIS : return "Vorbis" ;
  408. case SF_FORMAT_ALAC_16 : return "16 bit ALAC" ;
  409. case SF_FORMAT_ALAC_20 : return "20 bit ALAC" ;
  410. case SF_FORMAT_ALAC_24 : return "24 bit ALAC" ;
  411. case SF_FORMAT_ALAC_32 : return "32 bit ALAC" ;
  412. case SF_FORMAT_OPUS : return "Opus" ;
  413. case SF_FORMAT_MPEG_LAYER_I : return "MPEG layer 1" ;
  414. case SF_FORMAT_MPEG_LAYER_II : return "MPEG layer 2" ;
  415. case SF_FORMAT_MPEG_LAYER_III : return "MPEG layer 3" ;
  416. default : break ;
  417. } ;
  418. return "unknown" ;
  419. } /* sfe_codec_name */