Chris@0
|
1 /*
|
Chris@0
|
2 ** Copyright (C) 2008-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
|
Chris@0
|
3 ** Copyright (C) 2008-2010 George Blood Audio
|
Chris@0
|
4 **
|
Chris@0
|
5 ** All rights reserved.
|
Chris@0
|
6 **
|
Chris@0
|
7 ** Redistribution and use in source and binary forms, with or without
|
Chris@0
|
8 ** modification, are permitted provided that the following conditions are
|
Chris@0
|
9 ** met:
|
Chris@0
|
10 **
|
Chris@0
|
11 ** * Redistributions of source code must retain the above copyright
|
Chris@0
|
12 ** notice, this list of conditions and the following disclaimer.
|
Chris@0
|
13 ** * Redistributions in binary form must reproduce the above copyright
|
Chris@0
|
14 ** notice, this list of conditions and the following disclaimer in
|
Chris@0
|
15 ** the documentation and/or other materials provided with the
|
Chris@0
|
16 ** distribution.
|
Chris@0
|
17 ** * Neither the author nor the names of any contributors may be used
|
Chris@0
|
18 ** to endorse or promote products derived from this software without
|
Chris@0
|
19 ** specific prior written permission.
|
Chris@0
|
20 **
|
Chris@0
|
21 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
Chris@0
|
22 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
Chris@0
|
23 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
Chris@0
|
24 ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
Chris@0
|
25 ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
Chris@0
|
26 ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
Chris@0
|
27 ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
Chris@0
|
28 ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
Chris@0
|
29 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
Chris@0
|
30 ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
Chris@0
|
31 ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
Chris@0
|
32 */
|
Chris@0
|
33
|
Chris@0
|
34 #include <config.h>
|
Chris@0
|
35
|
Chris@0
|
36 #include <stdio.h>
|
Chris@0
|
37 #include <stdlib.h>
|
Chris@0
|
38 #include <string.h>
|
Chris@0
|
39 #include <ctype.h>
|
Chris@0
|
40 #include <time.h>
|
Chris@0
|
41
|
Chris@0
|
42 #include <sndfile.h>
|
Chris@0
|
43
|
Chris@0
|
44 #include "common.h"
|
Chris@0
|
45
|
Chris@0
|
46 #define BUFFER_LEN (1 << 16)
|
Chris@0
|
47
|
Chris@0
|
48
|
Chris@0
|
49 static void usage_exit (const char *progname, int exit_code) ;
|
Chris@0
|
50 static void missing_param (const char * option) ;
|
Chris@0
|
51 static void read_localtime (struct tm * timedata) ;
|
Chris@0
|
52 static int has_bext_fields_set (const METADATA_INFO * info) ;
|
Chris@0
|
53
|
Chris@0
|
54 int
|
Chris@0
|
55 main (int argc, char *argv [])
|
Chris@0
|
56 { METADATA_INFO info ;
|
Chris@0
|
57 struct tm timedata ;
|
Chris@0
|
58 const char *progname ;
|
Chris@0
|
59 const char * filenames [2] = { NULL, NULL } ;
|
Chris@0
|
60 int k ;
|
Chris@0
|
61
|
Chris@0
|
62 /* Store the program name. */
|
Chris@0
|
63 progname = program_name (argv [0]) ;
|
Chris@0
|
64
|
Chris@0
|
65 /* Check if we've been asked for help. */
|
Chris@0
|
66 if (argc < 3 || strcmp (argv [1], "--help") == 0 || strcmp (argv [1], "-h") == 0)
|
Chris@0
|
67 usage_exit (progname, 0) ;
|
Chris@0
|
68
|
Chris@0
|
69 /* Clear set all fields of the struct to zero bytes. */
|
Chris@0
|
70 memset (&info, 0, sizeof (info)) ;
|
Chris@0
|
71
|
Chris@0
|
72 /* Get the time in case we need it later. */
|
Chris@0
|
73 read_localtime (&timedata) ;
|
Chris@0
|
74
|
Chris@0
|
75 for (k = 1 ; k < argc ; k++)
|
Chris@0
|
76 { char tmp [20] ;
|
Chris@0
|
77
|
Chris@0
|
78 if (argv [k][0] != '-')
|
Chris@0
|
79 { if (filenames [0] == NULL)
|
Chris@0
|
80 filenames [0] = argv [k] ;
|
Chris@0
|
81 else if (filenames [1] == NULL)
|
Chris@0
|
82 filenames [1] = argv [k] ;
|
Chris@0
|
83 else
|
Chris@0
|
84 { printf ("Error : Already have two file names on the command line and then found '%s'.\n\n", argv [k]) ;
|
Chris@0
|
85 usage_exit (progname, 1) ;
|
Chris@0
|
86 } ;
|
Chris@0
|
87 continue ;
|
Chris@0
|
88 } ;
|
Chris@0
|
89
|
Chris@0
|
90 #define HANDLE_BEXT_ARG(cmd,field) \
|
Chris@0
|
91 if (strcmp (argv [k], cmd) == 0) \
|
Chris@0
|
92 { k ++ ; \
|
Chris@0
|
93 if (k == argc) missing_param (argv [k - 1]) ; \
|
Chris@0
|
94 info.field = argv [k] ; \
|
Chris@0
|
95 continue ; \
|
Chris@0
|
96 } ;
|
Chris@0
|
97
|
Chris@0
|
98 HANDLE_BEXT_ARG ("--bext-description", description) ;
|
Chris@0
|
99 HANDLE_BEXT_ARG ("--bext-originator", originator) ;
|
Chris@0
|
100 HANDLE_BEXT_ARG ("--bext-orig-ref", originator_reference) ;
|
Chris@0
|
101 HANDLE_BEXT_ARG ("--bext-umid", umid) ;
|
Chris@0
|
102 HANDLE_BEXT_ARG ("--bext-orig-date", origination_date) ;
|
Chris@0
|
103 HANDLE_BEXT_ARG ("--bext-orig-time", origination_time) ;
|
Chris@0
|
104 HANDLE_BEXT_ARG ("--bext-coding-hist", coding_history) ;
|
Chris@0
|
105 HANDLE_BEXT_ARG ("--bext-time-ref", time_ref) ;
|
Chris@0
|
106
|
Chris@0
|
107 #define HANDLE_STR_ARG(cmd,field) \
|
Chris@0
|
108 if (strcmp (argv [k], cmd) == 0) \
|
Chris@0
|
109 { k ++ ; \
|
Chris@0
|
110 if (k == argc) missing_param (argv [k - 1]) ; \
|
Chris@0
|
111 info.field = argv [k] ; \
|
Chris@0
|
112 continue ; \
|
Chris@0
|
113 } ;
|
Chris@0
|
114
|
Chris@0
|
115 HANDLE_STR_ARG ("--str-comment", comment) ;
|
Chris@0
|
116 HANDLE_STR_ARG ("--str-title", title) ;
|
Chris@0
|
117 HANDLE_STR_ARG ("--str-copyright", copyright) ;
|
Chris@0
|
118 HANDLE_STR_ARG ("--str-artist", artist) ;
|
Chris@0
|
119 HANDLE_STR_ARG ("--str-date", date) ;
|
Chris@0
|
120 HANDLE_STR_ARG ("--str-album", album) ;
|
Chris@0
|
121 HANDLE_STR_ARG ("--str-license", license) ;
|
Chris@0
|
122
|
Chris@0
|
123 /* Following options do not take an argument. */
|
Chris@0
|
124 if (strcmp (argv [k], "--bext-auto-time-date") == 0)
|
Chris@0
|
125 { snprintf (tmp, sizeof (tmp), "%02d:%02d:%02d", timedata.tm_hour, timedata.tm_min, timedata.tm_sec) ;
|
Chris@0
|
126 info.origination_time = strdup (tmp) ;
|
Chris@0
|
127
|
Chris@0
|
128 snprintf (tmp, sizeof (tmp), "%04d-%02d-%02d", timedata.tm_year + 1900, timedata.tm_mon + 1, timedata.tm_mday) ;
|
Chris@0
|
129 info.origination_date = strdup (tmp) ;
|
Chris@0
|
130 continue ;
|
Chris@0
|
131 } ;
|
Chris@0
|
132
|
Chris@0
|
133 if (strcmp (argv [k], "--bext-auto-time") == 0)
|
Chris@0
|
134 { snprintf (tmp, sizeof (tmp), "%02d:%02d:%02d", timedata.tm_hour, timedata.tm_min, timedata.tm_sec) ;
|
Chris@0
|
135 info.origination_time = strdup (tmp) ;
|
Chris@0
|
136 continue ;
|
Chris@0
|
137 } ;
|
Chris@0
|
138
|
Chris@0
|
139 if (strcmp (argv [k], "--bext-auto-date") == 0)
|
Chris@0
|
140 { snprintf (tmp, sizeof (tmp), "%04d-%02d-%02d", timedata.tm_year + 1900, timedata.tm_mon + 1, timedata.tm_mday) ;
|
Chris@0
|
141 info.origination_date = strdup (tmp) ;
|
Chris@0
|
142 continue ;
|
Chris@0
|
143 } ;
|
Chris@0
|
144
|
Chris@0
|
145 if (strcmp (argv [k], "--str-auto-date") == 0)
|
Chris@0
|
146 { snprintf (tmp, sizeof (tmp), "%04d-%02d-%02d", timedata.tm_year + 1900, timedata.tm_mon + 1, timedata.tm_mday) ;
|
Chris@0
|
147
|
Chris@0
|
148 info.date = strdup (tmp) ;
|
Chris@0
|
149 continue ;
|
Chris@0
|
150 } ;
|
Chris@0
|
151
|
Chris@0
|
152 printf ("Error : Don't know what to do with command line arg '%s'.\n\n", argv [k]) ;
|
Chris@0
|
153 usage_exit (progname, 1) ;
|
Chris@0
|
154 } ;
|
Chris@0
|
155
|
Chris@0
|
156 /* Find out if any of the 'bext' fields are set. */
|
Chris@0
|
157 info.has_bext_fields = has_bext_fields_set (&info) ;
|
Chris@0
|
158
|
Chris@0
|
159 if (filenames [0] == NULL)
|
Chris@0
|
160 { printf ("Error : No input file specificed.\n\n") ;
|
Chris@0
|
161 exit (1) ;
|
Chris@0
|
162 } ;
|
Chris@0
|
163
|
Chris@0
|
164 if (filenames [1] != NULL && strcmp (filenames [0], filenames [1]) == 0)
|
Chris@0
|
165 { printf ("Error : Input and output files are the same.\n\n") ;
|
Chris@0
|
166 exit (1) ;
|
Chris@0
|
167 } ;
|
Chris@0
|
168
|
Chris@0
|
169 if (info.coding_history != NULL && filenames [1] == NULL)
|
Chris@0
|
170 { printf ("\n"
|
Chris@0
|
171 "Error : Trying to update coding history of an existing file which unfortunately\n"
|
Chris@0
|
172 " is not supported. Instead, create a new file using :\n"
|
Chris@0
|
173 "\n"
|
Chris@0
|
174 " %s --bext-coding-hist \"Coding history\" old_file.wav new_file.wav\n"
|
Chris@0
|
175 "\n",
|
Chris@0
|
176 progname) ;
|
Chris@0
|
177 exit (1) ;
|
Chris@0
|
178 } ;
|
Chris@0
|
179
|
Chris@0
|
180 sfe_apply_metadata_changes (filenames, &info) ;
|
Chris@0
|
181
|
Chris@0
|
182 return 0 ;
|
Chris@0
|
183 } /* main */
|
Chris@0
|
184
|
Chris@0
|
185 /*==============================================================================
|
Chris@0
|
186 ** Print version and usage.
|
Chris@0
|
187 */
|
Chris@0
|
188
|
Chris@0
|
189 static void
|
Chris@0
|
190 usage_exit (const char *progname, int exit_code)
|
Chris@0
|
191 { printf ("\nUsage :\n\n"
|
Chris@0
|
192 " %s [options] <file>\n"
|
Chris@0
|
193 " %s [options] <input file> <output file>\n"
|
Chris@0
|
194 "\n",
|
Chris@0
|
195 progname, progname) ;
|
Chris@0
|
196
|
Chris@0
|
197 puts (
|
Chris@0
|
198 "Where an option is made up of a pair of a field to set (one of\n"
|
Chris@0
|
199 "the 'bext' or metadata fields below) and a string. Fields are\n"
|
Chris@0
|
200 "as follows :\n"
|
Chris@0
|
201 ) ;
|
Chris@0
|
202
|
Chris@0
|
203 puts (
|
Chris@0
|
204 " --bext-description Set the 'bext' description.\n"
|
Chris@0
|
205 " --bext-originator Set the 'bext' originator.\n"
|
Chris@0
|
206 " --bext-orig-ref Set the 'bext' originator reference.\n"
|
Chris@0
|
207 " --bext-umid Set the 'bext' UMID.\n"
|
Chris@0
|
208 " --bext-orig-date Set the 'bext' origination date.\n"
|
Chris@0
|
209 " --bext-orig-time Set the 'bext' origination time.\n"
|
Chris@0
|
210 " --bext-coding-hist Set the 'bext' coding history.\n"
|
Chris@0
|
211 " --bext-time-raf Set the 'bext' Time ref.\n"
|
Chris@0
|
212 "\n"
|
Chris@0
|
213 " --str-comment Set the metadata comment.\n"
|
Chris@0
|
214 " --str-title Set the metadata title.\n"
|
Chris@0
|
215 " --str-copyright Set the metadata copyright.\n"
|
Chris@0
|
216 " --str-artist Set the metadata artist.\n"
|
Chris@0
|
217 " --str-date Set the metadata date.\n"
|
Chris@0
|
218 " --str-album Set the metadata album.\n"
|
Chris@0
|
219 " --str-license Set the metadata license.\n"
|
Chris@0
|
220 ) ;
|
Chris@0
|
221
|
Chris@0
|
222 puts (
|
Chris@0
|
223 "There are also the following arguments which do not take a\n"
|
Chris@0
|
224 "parameter :\n\n"
|
Chris@0
|
225 " --bext-auto-time-date Set the 'bext' time and date to current time/date.\n"
|
Chris@0
|
226 " --bext-auto-time Set the 'bext' time to current time.\n"
|
Chris@0
|
227 " --bext-auto-date Set the 'bext' date to current date.\n"
|
Chris@0
|
228 " --str-auto-date Set the metadata date to current date.\n"
|
Chris@0
|
229 ) ;
|
Chris@0
|
230
|
Chris@0
|
231 puts (
|
Chris@0
|
232 "Most of the above operations can be done in-place on an existing\n"
|
Chris@0
|
233 "file. If any operation cannot be performed, the application will\n"
|
Chris@0
|
234 "exit with an appropriate error message.\n"
|
Chris@0
|
235 ) ;
|
Chris@0
|
236
|
Chris@0
|
237 printf ("Using %s.\n\n", sf_version_string ()) ;
|
Chris@0
|
238 exit (exit_code) ;
|
Chris@0
|
239 } /* usage_exit */
|
Chris@0
|
240
|
Chris@0
|
241 static void
|
Chris@0
|
242 missing_param (const char * option)
|
Chris@0
|
243 {
|
Chris@0
|
244 printf ("Error : Option '%s' needs a parameter but doesn't seem to have one.\n\n", option) ;
|
Chris@0
|
245 exit (1) ;
|
Chris@0
|
246 } /* missing_param */
|
Chris@0
|
247
|
Chris@0
|
248 /*==============================================================================
|
Chris@0
|
249 */
|
Chris@0
|
250
|
Chris@0
|
251 static int
|
Chris@0
|
252 has_bext_fields_set (const METADATA_INFO * info)
|
Chris@0
|
253 {
|
Chris@0
|
254 if (info->description || info->originator || info->originator_reference)
|
Chris@0
|
255 return 1 ;
|
Chris@0
|
256
|
Chris@0
|
257 if (info->origination_date || info->origination_time || info->umid || info->coding_history || info->time_ref)
|
Chris@0
|
258 return 1 ;
|
Chris@0
|
259
|
Chris@0
|
260 return 0 ;
|
Chris@0
|
261 } /* has_bext_fields_set */
|
Chris@0
|
262
|
Chris@0
|
263 static void
|
Chris@0
|
264 read_localtime (struct tm * timedata)
|
Chris@0
|
265 { time_t current ;
|
Chris@0
|
266
|
Chris@0
|
267 time (¤t) ;
|
Chris@0
|
268 memset (timedata, 0, sizeof (struct tm)) ;
|
Chris@0
|
269
|
Chris@0
|
270 #if defined (HAVE_LOCALTIME_R)
|
Chris@0
|
271 /* If the re-entrant version is available, use it. */
|
Chris@0
|
272 localtime_r (¤t, timedata) ;
|
Chris@0
|
273 #elif defined (HAVE_LOCALTIME)
|
Chris@0
|
274 {
|
Chris@0
|
275 struct tm *tmptr ;
|
Chris@0
|
276 /* Otherwise use the standard one and copy the data to local storage. */
|
Chris@0
|
277 if ((tmptr = localtime (¤t)) != NULL)
|
Chris@0
|
278 memcpy (timedata, tmptr, sizeof (struct tm)) ;
|
Chris@0
|
279 }
|
Chris@0
|
280 #endif
|
Chris@0
|
281
|
Chris@0
|
282 return ;
|
Chris@0
|
283 } /* read_localtime */
|
Chris@0
|
284
|