sndfile-info.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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 <inttypes.h>
  36. #include <ctype.h>
  37. #include <math.h>
  38. #include <sndfile.h>
  39. #include "common.h"
  40. #define BUFFER_LEN (1 << 16)
  41. #if (defined (WIN32) || defined (_WIN32))
  42. #include <windows.h>
  43. #endif
  44. static void print_version (void) ;
  45. static void usage_exit (const char *progname) ;
  46. static void info_dump (const char *filename) ;
  47. static int instrument_dump (const char *filename) ;
  48. static int broadcast_dump (const char *filename) ;
  49. static int chanmap_dump (const char *filename) ;
  50. static int cart_dump (const char *filename) ;
  51. static void total_dump (void) ;
  52. static double total_seconds = 0.0 ;
  53. int
  54. main (int argc, char *argv [])
  55. { int k ;
  56. print_version () ;
  57. if (argc < 2 || strcmp (argv [1], "--help") == 0 || strcmp (argv [1], "-h") == 0)
  58. { usage_exit (program_name (argv [0])) ;
  59. return 1 ;
  60. } ;
  61. if (strcmp (argv [1], "--instrument") == 0)
  62. { int error = 0 ;
  63. for (k = 2 ; k < argc ; k++)
  64. error += instrument_dump (argv [k]) ;
  65. return error ;
  66. } ;
  67. if (strcmp (argv [1], "--broadcast") == 0)
  68. { int error = 0 ;
  69. for (k = 2 ; k < argc ; k++)
  70. error += broadcast_dump (argv [k]) ;
  71. return error ;
  72. } ;
  73. if (strcmp (argv [1], "--channel-map") == 0)
  74. { int error = 0 ;
  75. for (k = 2 ; k < argc ; k++)
  76. error += chanmap_dump (argv [k]) ;
  77. return error ;
  78. } ;
  79. if (strcmp (argv [1], "--cart") == 0)
  80. { int error = 0 ;
  81. for (k = 2 ; k < argc ; k++)
  82. error += cart_dump (argv [k]) ;
  83. return error ;
  84. } ;
  85. for (k = 1 ; k < argc ; k++)
  86. info_dump (argv [k]) ;
  87. if (argc > 2)
  88. total_dump () ;
  89. return 0 ;
  90. } /* main */
  91. /*==============================================================================
  92. ** Print version and usage.
  93. */
  94. static double data [BUFFER_LEN] ;
  95. static void
  96. print_version (void)
  97. { char buffer [256] ;
  98. sf_command (NULL, SFC_GET_LIB_VERSION, buffer, sizeof (buffer)) ;
  99. printf ("\nVersion : %s\n\n", buffer) ;
  100. } /* print_version */
  101. static void
  102. usage_exit (const char *progname)
  103. { printf ("Usage :\n %s <file> ...\n", progname) ;
  104. printf (" Prints out information about one or more sound files.\n\n") ;
  105. printf (" %s --instrument <file>\n", progname) ;
  106. printf (" Prints out the instrument data for the given file.\n\n") ;
  107. printf (" %s --broadcast <file>\n", progname) ;
  108. printf (" Prints out the broadcast WAV info for the given file.\n\n") ;
  109. printf (" %s --channel-map <file>\n", progname) ;
  110. printf (" Prints out the channel map for the given file.\n\n") ;
  111. printf (" %s --cart <file>\n", progname) ;
  112. printf (" Prints out the cart chunk WAV info for the given file.\n\n") ;
  113. #if (defined (_WIN32) || defined (WIN32))
  114. printf ("This is a Unix style command line application which\n"
  115. "should be run in a MSDOS box or Command Shell window.\n\n") ;
  116. printf ("Sleeping for 5 seconds before exiting.\n\n") ;
  117. fflush (stdout) ;
  118. /* This is the officially blessed by microsoft way but I can't get
  119. ** it to link.
  120. ** Sleep (15) ;
  121. ** Instead, use this:
  122. */
  123. Sleep (5 * 1000) ;
  124. #endif
  125. exit (0) ;
  126. } /* usage_exit */
  127. /*==============================================================================
  128. ** Dumping of sndfile info.
  129. */
  130. static double data [BUFFER_LEN] ;
  131. static double
  132. calc_decibels (SF_INFO * sfinfo, double max)
  133. { double decibels ;
  134. switch (sfinfo->format & SF_FORMAT_SUBMASK)
  135. { case SF_FORMAT_PCM_U8 :
  136. case SF_FORMAT_PCM_S8 :
  137. decibels = max / 0x80 ;
  138. break ;
  139. case SF_FORMAT_PCM_16 :
  140. decibels = max / 0x8000 ;
  141. break ;
  142. case SF_FORMAT_PCM_24 :
  143. decibels = max / 0x800000 ;
  144. break ;
  145. case SF_FORMAT_PCM_32 :
  146. decibels = max / 0x80000000 ;
  147. break ;
  148. case SF_FORMAT_FLOAT :
  149. case SF_FORMAT_DOUBLE :
  150. decibels = max / 1.0 ;
  151. break ;
  152. default :
  153. decibels = max / 0x8000 ;
  154. break ;
  155. } ;
  156. return 20.0 * log10 (decibels) ;
  157. } /* calc_decibels */
  158. static const char *
  159. format_duration_str (double seconds)
  160. { static char str [128] ;
  161. int hrs, min ;
  162. double sec ;
  163. memset (str, 0, sizeof (str)) ;
  164. hrs = (int) (seconds / 3600.0) ;
  165. min = (int) ((seconds - (hrs * 3600.0)) / 60.0) ;
  166. sec = seconds - (hrs * 3600.0) - (min * 60.0) ;
  167. snprintf (str, sizeof (str) - 1, "%02d:%02d:%06.3f", hrs, min, sec) ;
  168. return str ;
  169. } /* format_duration_str */
  170. static const char *
  171. generate_duration_str (SF_INFO *sfinfo)
  172. {
  173. double seconds ;
  174. if (sfinfo->samplerate < 1)
  175. return NULL ;
  176. if (sfinfo->frames / sfinfo->samplerate > 0x7FFFFFFF)
  177. return "unknown" ;
  178. seconds = (1.0 * sfinfo->frames) / sfinfo->samplerate ;
  179. /* Accumulate the total of all known file durations */
  180. total_seconds += seconds ;
  181. return format_duration_str (seconds) ;
  182. } /* generate_duration_str */
  183. static void
  184. info_dump (const char *filename)
  185. { static char strbuffer [BUFFER_LEN] ;
  186. SNDFILE *file ;
  187. SF_INFO sfinfo ;
  188. double signal_max, decibels ;
  189. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  190. if ((file = sf_open (filename, SFM_READ, &sfinfo)) == NULL)
  191. { printf ("Error : Not able to open input file %s.\n", filename) ;
  192. fflush (stdout) ;
  193. memset (data, 0, sizeof (data)) ;
  194. sf_command (file, SFC_GET_LOG_INFO, strbuffer, BUFFER_LEN) ;
  195. puts (strbuffer) ;
  196. puts (sf_strerror (NULL)) ;
  197. return ;
  198. } ;
  199. printf ("========================================\n") ;
  200. sf_command (file, SFC_GET_LOG_INFO, strbuffer, BUFFER_LEN) ;
  201. puts (strbuffer) ;
  202. printf ("----------------------------------------\n") ;
  203. printf ("Sample Rate : %d\n", sfinfo.samplerate) ;
  204. if (sfinfo.frames == SF_COUNT_MAX)
  205. printf ("Frames : unknown\n") ;
  206. else
  207. printf ("Frames : %" PRId64 "\n", sfinfo.frames) ;
  208. printf ("Channels : %d\n", sfinfo.channels) ;
  209. printf ("Format : 0x%08X\n", sfinfo.format) ;
  210. printf ("Sections : %d\n", sfinfo.sections) ;
  211. printf ("Seekable : %s\n", (sfinfo.seekable ? "TRUE" : "FALSE")) ;
  212. printf ("Duration : %s\n", generate_duration_str (&sfinfo)) ;
  213. if (sfinfo.frames < 100 * 1024 * 1024)
  214. { /* Do not use sf_signal_max because it doesn't work for non-seekable files . */
  215. sf_command (file, SFC_CALC_SIGNAL_MAX, &signal_max, sizeof (signal_max)) ;
  216. decibels = calc_decibels (&sfinfo, signal_max) ;
  217. printf ("Signal Max : %g (%4.2f dB)\n", signal_max, decibels) ;
  218. } ;
  219. putchar ('\n') ;
  220. sf_close (file) ;
  221. } /* info_dump */
  222. /*==============================================================================
  223. ** Dumping of SF_INSTRUMENT data.
  224. */
  225. static const char *
  226. str_of_type (int mode)
  227. { switch (mode)
  228. { case SF_LOOP_NONE : return "none" ;
  229. case SF_LOOP_FORWARD : return "fwd " ;
  230. case SF_LOOP_BACKWARD : return "back" ;
  231. case SF_LOOP_ALTERNATING : return "alt " ;
  232. default : break ;
  233. } ;
  234. return "????" ;
  235. } /* str_of_mode */
  236. static int
  237. instrument_dump (const char *filename)
  238. { SNDFILE *file ;
  239. SF_INFO sfinfo ;
  240. SF_INSTRUMENT inst ;
  241. int got_inst, k ;
  242. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  243. if ((file = sf_open (filename, SFM_READ, &sfinfo)) == NULL)
  244. { printf ("Error : Not able to open input file %s.\n", filename) ;
  245. fflush (stdout) ;
  246. memset (data, 0, sizeof (data)) ;
  247. puts (sf_strerror (NULL)) ;
  248. return 1 ;
  249. } ;
  250. got_inst = sf_command (file, SFC_GET_INSTRUMENT, &inst, sizeof (inst)) ;
  251. sf_close (file) ;
  252. if (got_inst == SF_FALSE)
  253. { printf ("Error : File '%s' does not contain instrument data.\n\n", filename) ;
  254. return 1 ;
  255. } ;
  256. printf ("Instrument : %s\n\n", filename) ;
  257. printf (" Gain : %d\n", inst.gain) ;
  258. printf (" Base note : %d\n", inst.basenote) ;
  259. printf (" Velocity : %d - %d\n", (int) inst.velocity_lo, (int) inst.velocity_hi) ;
  260. printf (" Key : %d - %d\n", (int) inst.key_lo, (int) inst.key_hi) ;
  261. printf (" Loop points : %d\n", inst.loop_count) ;
  262. for (k = 0 ; k < inst.loop_count ; k++)
  263. printf (" %-2d Mode : %s Start : %6d End : %6d Count : %6d\n", k, str_of_type (inst.loops [k].mode), inst.loops [k].start, inst.loops [k].end, inst.loops [k].count) ;
  264. putchar ('\n') ;
  265. return 0 ;
  266. } /* instrument_dump */
  267. static int
  268. broadcast_dump (const char *filename)
  269. { SNDFILE *file ;
  270. SF_INFO sfinfo ;
  271. SF_BROADCAST_INFO_2K bext ;
  272. double time_ref_sec ;
  273. int got_bext ;
  274. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  275. if ((file = sf_open (filename, SFM_READ, &sfinfo)) == NULL)
  276. { printf ("Error : Not able to open input file %s.\n", filename) ;
  277. fflush (stdout) ;
  278. memset (data, 0, sizeof (data)) ;
  279. puts (sf_strerror (NULL)) ;
  280. return 1 ;
  281. } ;
  282. memset (&bext, 0, sizeof (SF_BROADCAST_INFO_2K)) ;
  283. got_bext = sf_command (file, SFC_GET_BROADCAST_INFO, &bext, sizeof (bext)) ;
  284. sf_close (file) ;
  285. if (got_bext == SF_FALSE)
  286. { printf ("Error : File '%s' does not contain broadcast information.\n\n", filename) ;
  287. return 1 ;
  288. } ;
  289. /*
  290. ** From : http://www.ebu.ch/en/technical/publications/userguides/bwf_user_guide.php
  291. **
  292. ** Time Reference:
  293. ** This field is a count from midnight in samples to the first sample
  294. ** of the audio sequence.
  295. */
  296. time_ref_sec = ((pow (2.0, 32) * bext.time_reference_high) + (1.0 * bext.time_reference_low)) / sfinfo.samplerate ;
  297. printf ("Description : %.*s\n", (int) sizeof (bext.description), bext.description) ;
  298. printf ("Originator : %.*s\n", (int) sizeof (bext.originator), bext.originator) ;
  299. printf ("Origination ref : %.*s\n", (int) sizeof (bext.originator_reference), bext.originator_reference) ;
  300. printf ("Origination date : %.*s\n", (int) sizeof (bext.origination_date), bext.origination_date) ;
  301. printf ("Origination time : %.*s\n", (int) sizeof (bext.origination_time), bext.origination_time) ;
  302. if (bext.time_reference_high == 0 && bext.time_reference_low == 0)
  303. printf ("Time ref : 0\n") ;
  304. else
  305. printf ("Time ref : 0x%x%08x (%.6f seconds)\n", bext.time_reference_high, bext.time_reference_low, time_ref_sec) ;
  306. printf ("BWF version : %d\n", bext.version) ;
  307. printf ("UMID : %.*s\n", (int) sizeof (bext.umid), bext.umid) ;
  308. printf ("Coding history : %.*s\n", bext.coding_history_size, bext.coding_history) ;
  309. return 0 ;
  310. } /* broadcast_dump */
  311. static int
  312. chanmap_dump (const char *filename)
  313. { SNDFILE *file ;
  314. SF_INFO sfinfo ;
  315. int * channel_map ;
  316. int got_chanmap, k ;
  317. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  318. if ((file = sf_open (filename, SFM_READ, &sfinfo)) == NULL)
  319. { printf ("Error : Not able to open input file %s.\n", filename) ;
  320. fflush (stdout) ;
  321. memset (data, 0, sizeof (data)) ;
  322. puts (sf_strerror (NULL)) ;
  323. return 1 ;
  324. } ;
  325. if ((channel_map = calloc (sfinfo.channels, sizeof (int))) == NULL)
  326. { printf ("Error : malloc failed.\n\n") ;
  327. return 1 ;
  328. } ;
  329. got_chanmap = sf_command (file, SFC_GET_CHANNEL_MAP_INFO, channel_map, sfinfo.channels * sizeof (int)) ;
  330. sf_close (file) ;
  331. if (got_chanmap == SF_FALSE)
  332. { printf ("Error : File '%s' does not contain channel map information.\n\n", filename) ;
  333. free (channel_map) ;
  334. return 1 ;
  335. } ;
  336. printf ("File : %s\n\n", filename) ;
  337. puts (" Chan Position") ;
  338. for (k = 0 ; k < sfinfo.channels ; k ++)
  339. { const char * name ;
  340. #define CASE_NAME(x) case x : name = #x ; break ;
  341. switch (channel_map [k])
  342. { CASE_NAME (SF_CHANNEL_MAP_INVALID) ;
  343. CASE_NAME (SF_CHANNEL_MAP_MONO) ;
  344. CASE_NAME (SF_CHANNEL_MAP_LEFT) ;
  345. CASE_NAME (SF_CHANNEL_MAP_RIGHT) ;
  346. CASE_NAME (SF_CHANNEL_MAP_CENTER) ;
  347. CASE_NAME (SF_CHANNEL_MAP_FRONT_LEFT) ;
  348. CASE_NAME (SF_CHANNEL_MAP_FRONT_RIGHT) ;
  349. CASE_NAME (SF_CHANNEL_MAP_FRONT_CENTER) ;
  350. CASE_NAME (SF_CHANNEL_MAP_REAR_CENTER) ;
  351. CASE_NAME (SF_CHANNEL_MAP_REAR_LEFT) ;
  352. CASE_NAME (SF_CHANNEL_MAP_REAR_RIGHT) ;
  353. CASE_NAME (SF_CHANNEL_MAP_LFE) ;
  354. CASE_NAME (SF_CHANNEL_MAP_FRONT_LEFT_OF_CENTER) ;
  355. CASE_NAME (SF_CHANNEL_MAP_FRONT_RIGHT_OF_CENTER) ;
  356. CASE_NAME (SF_CHANNEL_MAP_SIDE_LEFT) ;
  357. CASE_NAME (SF_CHANNEL_MAP_SIDE_RIGHT) ;
  358. CASE_NAME (SF_CHANNEL_MAP_TOP_CENTER) ;
  359. CASE_NAME (SF_CHANNEL_MAP_TOP_FRONT_LEFT) ;
  360. CASE_NAME (SF_CHANNEL_MAP_TOP_FRONT_RIGHT) ;
  361. CASE_NAME (SF_CHANNEL_MAP_TOP_FRONT_CENTER) ;
  362. CASE_NAME (SF_CHANNEL_MAP_TOP_REAR_LEFT) ;
  363. CASE_NAME (SF_CHANNEL_MAP_TOP_REAR_RIGHT) ;
  364. CASE_NAME (SF_CHANNEL_MAP_TOP_REAR_CENTER) ;
  365. CASE_NAME (SF_CHANNEL_MAP_MAX) ;
  366. default : name = "default" ;
  367. break ;
  368. } ;
  369. printf (" %3d %s\n", k, name) ;
  370. } ;
  371. putchar ('\n') ;
  372. free (channel_map) ;
  373. return 0 ;
  374. } /* chanmap_dump */
  375. static int
  376. cart_dump (const char *filename)
  377. { SNDFILE *file ;
  378. SF_INFO sfinfo ;
  379. SF_CART_INFO_VAR (1024) cart ;
  380. int got_cart, k ;
  381. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  382. memset (&cart, 0, sizeof (cart)) ;
  383. if ((file = sf_open (filename, SFM_READ, &sfinfo)) == NULL)
  384. { printf ("Error : Not able to open input file %s.\n", filename) ;
  385. fflush (stdout) ;
  386. memset (data, 0, sizeof (data)) ;
  387. puts (sf_strerror (NULL)) ;
  388. return 1 ;
  389. } ;
  390. got_cart = sf_command (file, SFC_GET_CART_INFO, &cart, sizeof (cart)) ;
  391. sf_close (file) ;
  392. if (got_cart == SF_FALSE)
  393. { printf ("Error : File '%s' does not contain cart information.\n\n", filename) ;
  394. return 1 ;
  395. } ;
  396. printf ("Version : %.*s\n", (int) sizeof (cart.version), cart.version) ;
  397. printf ("Title : %.*s\n", (int) sizeof (cart.title), cart.title) ;
  398. printf ("Artist : %.*s\n", (int) sizeof (cart.artist), cart.artist) ;
  399. printf ("Cut id : %.*s\n", (int) sizeof (cart.cut_id), cart.cut_id) ;
  400. printf ("Category : %.*s\n", (int) sizeof (cart.category), cart.category) ;
  401. printf ("Classification : %.*s\n", (int) sizeof (cart.classification), cart.classification) ;
  402. printf ("Out cue : %.*s\n", (int) sizeof (cart.out_cue), cart.out_cue) ;
  403. printf ("Start date : %.*s\n", (int) sizeof (cart.start_date), cart.start_date) ;
  404. printf ("Start time : %.*s\n", (int) sizeof (cart.start_time), cart.start_time) ;
  405. printf ("End date : %.*s\n", (int) sizeof (cart.end_date), cart.end_date) ;
  406. printf ("End time : %.*s\n", (int) sizeof (cart.end_time), cart.end_time) ;
  407. printf ("App id : %.*s\n", (int) sizeof (cart.producer_app_id), cart.producer_app_id) ;
  408. printf ("App version : %.*s\n", (int) sizeof (cart.producer_app_version), cart.producer_app_version) ;
  409. printf ("User defined : %.*s\n", (int) sizeof (cart.user_def), cart.user_def) ;
  410. printf ("Level ref. : %d\n", cart.level_reference) ;
  411. printf ("Post timers :\n") ;
  412. for (k = 0 ; k < ARRAY_LEN (cart.post_timers) ; k++)
  413. if (cart.post_timers [k].usage [0])
  414. printf (" %d %.*s %d\n", k, (int) sizeof (cart.post_timers [k].usage), cart.post_timers [k].usage, cart.post_timers [k].value) ;
  415. printf ("Reserved : %.*s\n", (int) sizeof (cart.reserved), cart.reserved) ;
  416. printf ("Url : %.*s\n", (int) sizeof (cart.url), cart.url) ;
  417. printf ("Tag text : %.*s\n", cart.tag_text_size, cart.tag_text) ;
  418. return 0 ;
  419. } /* cart_dump */
  420. static void
  421. total_dump (void)
  422. { printf ("========================================\n") ;
  423. printf ("Total Duration : %s\n", format_duration_str (total_seconds)) ;
  424. } /* total_dump */