sndfile-metadata-get.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. ** Copyright (C) 2008-2014 Erik de Castro Lopo <[email protected]>
  3. ** Copyright (C) 2008-2010 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 <config.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <ctype.h>
  38. #include <time.h>
  39. #include <sndfile.h>
  40. #include "common.h"
  41. #define BUFFER_LEN (1 << 16)
  42. static void usage_exit (const char *progname, int exit_code) ;
  43. static void process_args (SNDFILE * file, const SF_BROADCAST_INFO_2K * binfo, int argc, char * argv []) ;
  44. int
  45. main (int argc, char *argv [])
  46. { SNDFILE *file ;
  47. SF_INFO sfinfo ;
  48. SF_BROADCAST_INFO_2K binfo ;
  49. const char *progname ;
  50. const char * filename = NULL ;
  51. int start ;
  52. /* Store the program name. */
  53. progname = program_name (argv [0]) ;
  54. /* Check if we've been asked for help. */
  55. if (argc < 2 || strcmp (argv [1], "--help") == 0 || strcmp (argv [1], "-h") == 0)
  56. usage_exit (progname, 0) ;
  57. if (argv [argc - 1][0] != '-')
  58. { filename = argv [argc - 1] ;
  59. start = 1 ;
  60. }
  61. else if (argv [1][0] != '-')
  62. { filename = argv [1] ;
  63. start = 2 ;
  64. }
  65. else
  66. { printf ("Error : Either the first or the last command line parameter should be a filename.\n\n") ;
  67. exit (1) ;
  68. } ;
  69. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  70. if ((file = sf_open (filename, SFM_READ, &sfinfo)) == NULL)
  71. { printf ("Error : Open of file '%s' failed : %s\n\n", filename, sf_strerror (file)) ;
  72. exit (1) ;
  73. } ;
  74. memset (&binfo, 0, sizeof (binfo)) ;
  75. if (sf_command (file, SFC_GET_BROADCAST_INFO, &binfo, sizeof (binfo)) == 0)
  76. memset (&binfo, 0, sizeof (binfo)) ;
  77. process_args (file, &binfo, argc - 2, argv + start) ;
  78. sf_close (file) ;
  79. return 0 ;
  80. } /* main */
  81. /*==============================================================================
  82. ** Print version and usage.
  83. */
  84. static void
  85. usage_exit (const char *progname, int exit_code)
  86. { printf ("\nUsage :\n %s [options] <file>\n\nOptions:\n", progname) ;
  87. puts (
  88. " --bext-description Print the 'bext' description.\n"
  89. " --bext-originator Print the 'bext' originator info.\n"
  90. " --bext-orig-ref Print the 'bext' origination reference.\n"
  91. " --bext-umid Print the 'bext' UMID.\n"
  92. " --bext-orig-date Print the 'bext' origination date.\n"
  93. " --bext-orig-time Print the 'bext' origination time.\n"
  94. " --bext-loudness-value Print the 'bext' loudness value.\n"
  95. " --bext-loudness-range Print the 'bext' loudness range.\n"
  96. " --bext-max-truepeak Print the 'bext' max. true peak level\n"
  97. " --bext-max-momentary Print the 'bext' max. momentary loudness\n"
  98. " --bext-max-shortterm Print the 'bext' max. short term loudness\n"
  99. " --bext-coding-hist Print the 'bext' coding history.\n"
  100. ) ;
  101. puts (
  102. " --str-title Print the title metadata.\n"
  103. " --str-copyright Print the copyright metadata.\n"
  104. " --str-artist Print the artist metadata.\n"
  105. " --str-comment Print the comment metadata.\n"
  106. " --str-date Print the creation date metadata.\n"
  107. " --str-album Print the album metadata.\n"
  108. " --str-license Print the license metadata.\n"
  109. ) ;
  110. printf ("Using %s.\n\n", sf_version_string ()) ;
  111. exit (exit_code) ;
  112. } /* usage_exit */
  113. static void
  114. process_args (SNDFILE * file, const SF_BROADCAST_INFO_2K * binfo, int argc, char * argv [])
  115. { const char * str ;
  116. int k, do_all = 0 ;
  117. #define HANDLE_BEXT_ARG(cmd, name, field) \
  118. if (do_all || strcmp (argv [k], cmd) == 0) \
  119. { printf ("%-22s : %.*s\n", name, (int) sizeof (binfo->field), binfo->field) ; \
  120. if (! do_all) \
  121. continue ; \
  122. } ;
  123. #define HANDLE_BEXT_ARG_INT(cmd, name, field) \
  124. if (do_all || strcmp (argv [k], cmd) == 0) \
  125. { printf ("%-22s : %6.2f\n", name, binfo->field / 100.0) ; \
  126. if (! do_all) \
  127. continue ; \
  128. } ;
  129. #define HANDLE_STR_ARG(cmd, name, id) \
  130. if (do_all || strcmp (argv [k], cmd) == 0) \
  131. { str = sf_get_string (file, id) ; \
  132. printf ("%-22s : %s\n", name, str ? str : "") ; \
  133. if (! do_all) continue ; \
  134. } ;
  135. if (argc == 0)
  136. { do_all = 1 ;
  137. argc = 1 ;
  138. } ;
  139. for (k = 0 ; k < argc ; k++)
  140. { if (do_all || strcmp (argv [k], "--all") == 0)
  141. do_all = 1 ;
  142. HANDLE_BEXT_ARG ("--bext-description", "Description", description) ;
  143. HANDLE_BEXT_ARG ("--bext-originator", "Originator", originator) ;
  144. HANDLE_BEXT_ARG ("--bext-orig-ref", "Origination ref", originator_reference) ;
  145. HANDLE_BEXT_ARG ("--bext-umid", "UMID", umid) ;
  146. HANDLE_BEXT_ARG ("--bext-orig-date", "Origination date", origination_date) ;
  147. HANDLE_BEXT_ARG ("--bext-orig-time", "Origination time", origination_time) ;
  148. HANDLE_BEXT_ARG_INT ("--bext-loudness-value", "Loudness value", loudness_value) ;
  149. HANDLE_BEXT_ARG_INT ("--bext-loudness-range", "Loudness range", loudness_range) ;
  150. HANDLE_BEXT_ARG_INT ("--bext-max-truepeak", "Max. true peak level", max_true_peak_level) ;
  151. HANDLE_BEXT_ARG_INT ("--bext-max-momentary", "Max. momentary level", max_momentary_loudness) ;
  152. HANDLE_BEXT_ARG_INT ("--bext-max-shortterm", "Max. short term level", max_shortterm_loudness) ;
  153. HANDLE_BEXT_ARG ("--bext-coding-hist", "Coding history", coding_history) ;
  154. HANDLE_STR_ARG ("--str-title", "Name", SF_STR_TITLE) ;
  155. HANDLE_STR_ARG ("--str-copyright", "Copyright", SF_STR_COPYRIGHT) ;
  156. HANDLE_STR_ARG ("--str-artist", "Artist", SF_STR_ARTIST) ;
  157. HANDLE_STR_ARG ("--str-comment", "Comment", SF_STR_COMMENT) ;
  158. HANDLE_STR_ARG ("--str-date", "Create date", SF_STR_DATE) ;
  159. HANDLE_STR_ARG ("--str-album", "Album", SF_STR_ALBUM) ;
  160. HANDLE_STR_ARG ("--str-license", "License", SF_STR_LICENSE) ;
  161. if (! do_all)
  162. { printf ("Error : Don't know what to do with command line arg '%s'.\n\n", argv [k]) ;
  163. exit (1) ;
  164. } ;
  165. break ;
  166. } ;
  167. return ;
  168. } /* process_args */