sndfile-metadata-get.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. ** Copyright (C) 2008-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
  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. /* Get the time in case we need it later. */
  70. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  71. if ((file = sf_open (filename, SFM_READ, &sfinfo)) == NULL)
  72. { printf ("Error : Open of file '%s' failed : %s\n\n", filename, sf_strerror (file)) ;
  73. exit (1) ;
  74. } ;
  75. memset (&binfo, 0, sizeof (binfo)) ;
  76. if (sf_command (file, SFC_GET_BROADCAST_INFO, &binfo, sizeof (binfo)) == 0)
  77. memset (&binfo, 0, sizeof (binfo)) ;
  78. process_args (file, &binfo, argc - 2, argv + start) ;
  79. sf_close (file) ;
  80. return 0 ;
  81. } /* main */
  82. /*==============================================================================
  83. ** Print version and usage.
  84. */
  85. static void
  86. usage_exit (const char *progname, int exit_code)
  87. { printf ("\nUsage :\n %s [options] <file>\n\nOptions:\n", progname) ;
  88. puts (
  89. " --bext-description Print the 'bext' description.\n"
  90. " --bext-originator Print the 'bext; originator info.\n"
  91. " --bext-orig-ref Print the 'bext' origination reference.\n"
  92. " --bext-umid Print the 'bext' UMID.\n"
  93. " --bext-orig-date Print the 'bext' origination date.\n"
  94. " --bext-orig-time Print the 'bext' origination time.\n"
  95. " --bext-coding-hist Print the 'bext' coding history.\n"
  96. ) ;
  97. puts (
  98. " --str-title Print the title metadata.\n"
  99. " --str-copyright Print the copyright metadata.\n"
  100. " --str-artist Print the artist metadata.\n"
  101. " --str-comment Print the comment metadata.\n"
  102. " --str-date Print the creation date metadata.\n"
  103. " --str-album Print the album metadata.\n"
  104. " --str-license Print the license metadata.\n"
  105. ) ;
  106. printf ("Using %s.\n\n", sf_version_string ()) ;
  107. exit (exit_code) ;
  108. } /* usage_exit */
  109. static void
  110. process_args (SNDFILE * file, const SF_BROADCAST_INFO_2K * binfo, int argc, char * argv [])
  111. { const char * str ;
  112. int k, do_all = 0 ;
  113. #define HANDLE_BEXT_ARG(cmd, name, field) \
  114. if (do_all || strcmp (argv [k], cmd) == 0) \
  115. { printf ("%-20s : %.*s\n", name, (int) sizeof (binfo->field), binfo->field) ; \
  116. if (! do_all) \
  117. continue ; \
  118. } ;
  119. #define HANDLE_STR_ARG(cmd, name, id) \
  120. if (do_all || strcmp (argv [k], cmd) == 0) \
  121. { str = sf_get_string (file, id) ; \
  122. printf ("%-20s : %s\n", name, str ? str : "") ; \
  123. if (! do_all) continue ; \
  124. } ;
  125. for (k = 0 ; k < argc ; k++)
  126. { if (do_all || strcmp (argv [k], "--all") == 0)
  127. do_all = 1 ;
  128. HANDLE_BEXT_ARG ("--bext-description", "Description", description) ;
  129. HANDLE_BEXT_ARG ("--bext-originator", "Originator", originator) ;
  130. HANDLE_BEXT_ARG ("--bext-orig-ref", "Origination ref", originator_reference) ;
  131. HANDLE_BEXT_ARG ("--bext-umid", "UMID", umid) ;
  132. HANDLE_BEXT_ARG ("--bext-orig-date", "Origination date", origination_date) ;
  133. HANDLE_BEXT_ARG ("--bext-orig-time", "Origination time", origination_time) ;
  134. HANDLE_BEXT_ARG ("--bext-coding-hist", "Coding history", coding_history) ;
  135. HANDLE_STR_ARG ("--str-title", "Name", SF_STR_TITLE) ;
  136. HANDLE_STR_ARG ("--str-copyright", "Copyright", SF_STR_COPYRIGHT) ;
  137. HANDLE_STR_ARG ("--str-artist", "Artist", SF_STR_ARTIST) ;
  138. HANDLE_STR_ARG ("--str-comment", "Comment", SF_STR_COMMENT) ;
  139. HANDLE_STR_ARG ("--str-date", "Create date", SF_STR_DATE) ;
  140. HANDLE_STR_ARG ("--str-album", "Album", SF_STR_ALBUM) ;
  141. HANDLE_STR_ARG ("--str-license", "License", SF_STR_LICENSE) ;
  142. if (! do_all)
  143. { printf ("Error : Don't know what to do with command line arg '%s'.\n\n", argv [k]) ;
  144. exit (1) ;
  145. } ;
  146. break ;
  147. } ;
  148. return ;
  149. } /* process_args */