sndfile-metadata-set.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. ** Copyright (C) 2008-2016 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 missing_param (const char * option) ;
  44. static void read_localtime (struct tm * timedata) ;
  45. static int has_bext_fields_set (const METADATA_INFO * info) ;
  46. int
  47. main (int argc, char *argv [])
  48. { METADATA_INFO info ;
  49. struct tm timedata ;
  50. const char *progname ;
  51. const char * filenames [2] = { NULL, NULL } ;
  52. char date [128], time [128] ;
  53. int k ;
  54. /* Store the program name. */
  55. progname = program_name (argv [0]) ;
  56. /* Check if we've been asked for help. */
  57. if (argc < 3 || strcmp (argv [1], "--help") == 0 || strcmp (argv [1], "-h") == 0)
  58. usage_exit (progname, 0) ;
  59. /* Set all fields of the struct to zero bytes. */
  60. memset (&info, 0, sizeof (info)) ;
  61. /* Get the time in case we need it later. */
  62. read_localtime (&timedata) ;
  63. for (k = 1 ; k < argc ; k++)
  64. { if (argv [k][0] != '-')
  65. { if (filenames [0] == NULL)
  66. filenames [0] = argv [k] ;
  67. else if (filenames [1] == NULL)
  68. filenames [1] = argv [k] ;
  69. else
  70. { printf ("Error : Already have two file names on the command line and then found '%s'.\n\n", argv [k]) ;
  71. usage_exit (progname, 1) ;
  72. } ;
  73. continue ;
  74. } ;
  75. #define HANDLE_BEXT_ARG(cmd, field) \
  76. if (strcmp (argv [k], cmd) == 0) \
  77. { k ++ ; \
  78. if (k == argc) missing_param (argv [k - 1]) ; \
  79. info.field = argv [k] ; \
  80. continue ; \
  81. } ;
  82. HANDLE_BEXT_ARG ("--bext-description", description) ;
  83. HANDLE_BEXT_ARG ("--bext-originator", originator) ;
  84. HANDLE_BEXT_ARG ("--bext-orig-ref", originator_reference) ;
  85. HANDLE_BEXT_ARG ("--bext-umid", umid) ;
  86. HANDLE_BEXT_ARG ("--bext-orig-date", origination_date) ;
  87. HANDLE_BEXT_ARG ("--bext-orig-time", origination_time) ;
  88. HANDLE_BEXT_ARG ("--bext-loudness-value", loudness_value) ;
  89. HANDLE_BEXT_ARG ("--bext-loudness-range", loudness_range) ;
  90. HANDLE_BEXT_ARG ("--bext-max-truepeak", max_true_peak_level) ;
  91. HANDLE_BEXT_ARG ("--bext-max-momentary", max_momentary_loudness) ;
  92. HANDLE_BEXT_ARG ("--bext-max-shortterm", max_shortterm_loudness) ;
  93. HANDLE_BEXT_ARG ("--bext-coding-hist", coding_history) ;
  94. HANDLE_BEXT_ARG ("--bext-time-ref", time_ref) ;
  95. #define HANDLE_STR_ARG(cmd, field) \
  96. if (strcmp (argv [k], cmd) == 0) \
  97. { k ++ ; \
  98. if (k == argc) missing_param (argv [k - 1]) ; \
  99. info.field = argv [k] ; \
  100. continue ; \
  101. } ;
  102. HANDLE_STR_ARG ("--str-comment", comment) ;
  103. HANDLE_STR_ARG ("--str-title", title) ;
  104. HANDLE_STR_ARG ("--str-copyright", copyright) ;
  105. HANDLE_STR_ARG ("--str-artist", artist) ;
  106. HANDLE_STR_ARG ("--str-date", date) ;
  107. HANDLE_STR_ARG ("--str-album", album) ;
  108. HANDLE_STR_ARG ("--str-license", license) ;
  109. /* Following options do not take an argument. */
  110. if (strcmp (argv [k], "--bext-auto-time-date") == 0)
  111. { snprintf (time, sizeof (time), "%02d:%02d:%02d", timedata.tm_hour, timedata.tm_min, timedata.tm_sec) ;
  112. info.origination_time = time ;
  113. snprintf (date, sizeof (date), "%04d-%02d-%02d", timedata.tm_year + 1900, timedata.tm_mon + 1, timedata.tm_mday) ;
  114. info.origination_date = date ;
  115. continue ;
  116. } ;
  117. if (strcmp (argv [k], "--bext-auto-time") == 0)
  118. { snprintf (time, sizeof (time), "%02d:%02d:%02d", timedata.tm_hour, timedata.tm_min, timedata.tm_sec) ;
  119. info.origination_time = time ;
  120. continue ;
  121. } ;
  122. if (strcmp (argv [k], "--bext-auto-date") == 0)
  123. { snprintf (date, sizeof (date), "%04d-%02d-%02d", timedata.tm_year + 1900, timedata.tm_mon + 1, timedata.tm_mday) ;
  124. info.origination_date = strdup (date) ;
  125. continue ;
  126. } ;
  127. if (strcmp (argv [k], "--str-auto-date") == 0)
  128. { snprintf (date, sizeof (date), "%04d-%02d-%02d", timedata.tm_year + 1900, timedata.tm_mon + 1, timedata.tm_mday) ;
  129. info.date = strdup (date) ;
  130. continue ;
  131. } ;
  132. printf ("Error : Don't know what to do with command line arg '%s'.\n\n", argv [k]) ;
  133. usage_exit (progname, 1) ;
  134. } ;
  135. /* Find out if any of the 'bext' fields are set. */
  136. info.has_bext_fields = has_bext_fields_set (&info) ;
  137. if (filenames [0] == NULL)
  138. { printf ("Error : No input file specificed.\n\n") ;
  139. exit (1) ;
  140. } ;
  141. if (filenames [1] != NULL && strcmp (filenames [0], filenames [1]) == 0)
  142. { printf ("Error : Input and output files are the same.\n\n") ;
  143. exit (1) ;
  144. } ;
  145. if (info.coding_history != NULL && filenames [1] == NULL)
  146. { printf ("\n"
  147. "Error : Trying to update coding history of an existing file which unfortunately\n"
  148. " is not supported. Instead, create a new file using :\n"
  149. "\n"
  150. " %s --bext-coding-hist \"Coding history\" old_file.wav new_file.wav\n"
  151. "\n",
  152. progname) ;
  153. exit (1) ;
  154. } ;
  155. sfe_apply_metadata_changes (filenames, &info) ;
  156. return 0 ;
  157. } /* main */
  158. /*==============================================================================
  159. ** Print version and usage.
  160. */
  161. static void
  162. usage_exit (const char *progname, int exit_code)
  163. { printf ("\nUsage :\n\n"
  164. " %s [options] <file>\n"
  165. " %s [options] <input file> <output file>\n"
  166. "\n",
  167. progname, progname) ;
  168. puts (
  169. "Where an option is made up of a pair of a field to set (one of\n"
  170. "the 'bext' or metadata fields below) and a string. Fields are\n"
  171. "as follows :\n"
  172. ) ;
  173. puts (
  174. " --bext-description Set the 'bext' description.\n"
  175. " --bext-originator Set the 'bext' originator.\n"
  176. " --bext-orig-ref Set the 'bext' originator reference.\n"
  177. " --bext-umid Set the 'bext' UMID.\n"
  178. " --bext-orig-date Set the 'bext' origination date.\n"
  179. " --bext-orig-time Set the 'bext' origination time.\n"
  180. " --bext-loudness-value Set the 'bext' loudness value.\n"
  181. " --bext-loudness-range Set the 'bext' loudness range.\n"
  182. " --bext-max-truepeak Set the 'bext' max. true peak level\n"
  183. " --bext-max-momentary Set the 'bext' max. momentary loudness\n"
  184. " --bext-max-shortterm Set the 'bext' max. short term loudness\n"
  185. " --bext-coding-hist Set the 'bext' coding history.\n"
  186. " --bext-time-ref Set the 'bext' Time ref.\n"
  187. "\n"
  188. " --str-comment Set the metadata comment.\n"
  189. " --str-title Set the metadata title.\n"
  190. " --str-copyright Set the metadata copyright.\n"
  191. " --str-artist Set the metadata artist.\n"
  192. " --str-date Set the metadata date.\n"
  193. " --str-album Set the metadata album.\n"
  194. " --str-license Set the metadata license.\n"
  195. ) ;
  196. puts (
  197. "There are also the following arguments which do not take a\n"
  198. "parameter :\n\n"
  199. " --bext-auto-time-date Set the 'bext' time and date to current time/date.\n"
  200. " --bext-auto-time Set the 'bext' time to current time.\n"
  201. " --bext-auto-date Set the 'bext' date to current date.\n"
  202. " --str-auto-date Set the metadata date to current date.\n"
  203. ) ;
  204. puts (
  205. "Most of the above operations can be done in-place on an existing\n"
  206. "file. If any operation cannot be performed, the application will\n"
  207. "exit with an appropriate error message.\n"
  208. ) ;
  209. printf ("Using %s.\n\n", sf_version_string ()) ;
  210. exit (exit_code) ;
  211. } /* usage_exit */
  212. static void
  213. missing_param (const char * option)
  214. {
  215. printf ("Error : Option '%s' needs a parameter but doesn't seem to have one.\n\n", option) ;
  216. exit (1) ;
  217. } /* missing_param */
  218. /*==============================================================================
  219. */
  220. static int
  221. has_bext_fields_set (const METADATA_INFO * info)
  222. {
  223. if (info->description || info->originator || info->originator_reference)
  224. return 1 ;
  225. if (info->origination_date || info->origination_time || info->umid || info->coding_history || info->time_ref)
  226. return 1 ;
  227. if (info->loudness_value || info->loudness_range || info->max_true_peak_level || info->max_momentary_loudness || info->max_shortterm_loudness)
  228. return 1 ;
  229. return 0 ;
  230. } /* has_bext_fields_set */
  231. static void
  232. read_localtime (struct tm * timedata)
  233. { time_t current ;
  234. time (&current) ;
  235. memset (timedata, 0, sizeof (struct tm)) ;
  236. #if defined (_WIN32)
  237. /* If the re-entrant version is available, use it. */
  238. localtime_s (timedata, &current) ;
  239. #elif defined (HAVE_LOCALTIME_R)
  240. /* If the re-entrant version is available, use it. */
  241. localtime_r (&current, timedata) ;
  242. #elif defined (HAVE_LOCALTIME)
  243. {
  244. struct tm *tmptr ;
  245. /* Otherwise use the standard one and copy the data to local storage. */
  246. if ((tmptr = localtime (&current)) != NULL)
  247. memcpy (timedata, tmptr, sizeof (struct tm)) ;
  248. }
  249. #endif
  250. return ;
  251. } /* read_localtime */