2 ** Copyright (C) 2008-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
3 ** Copyright (C) 2008-2010 George Blood Audio
5 ** All rights reserved.
7 ** Redistribution and use in source and binary forms, with or without
8 ** modification, are permitted provided that the following conditions are
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
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.
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.
46 #define BUFFER_LEN (1 << 16)
49 static void usage_exit (const char *progname
, int exit_code
) ;
50 static void missing_param (const char * option
) ;
51 static void read_localtime (struct tm
* timedata
) ;
52 static int has_bext_fields_set (const METADATA_INFO
* info
) ;
55 main (int argc
, char *argv
[])
56 { METADATA_INFO info
;
58 const char *progname
;
59 const char * filenames
[2] = { NULL
, NULL
} ;
62 /* Store the program name. */
63 progname
= program_name (argv
[0]) ;
65 /* Check if we've been asked for help. */
66 if (argc
< 3 || strcmp (argv
[1], "--help") == 0 || strcmp (argv
[1], "-h") == 0)
67 usage_exit (progname
, 0) ;
69 /* Clear set all fields of the struct to zero bytes. */
70 memset (&info
, 0, sizeof (info
)) ;
72 /* Get the time in case we need it later. */
73 read_localtime (&timedata
) ;
75 for (k
= 1 ; k
< argc
; k
++)
78 if (argv
[k
][0] != '-')
79 { if (filenames
[0] == NULL
)
80 filenames
[0] = argv
[k
] ;
81 else if (filenames
[1] == NULL
)
82 filenames
[1] = argv
[k
] ;
84 { printf ("Error : Already have two file names on the command line and then found '%s'.\n\n", argv
[k
]) ;
85 usage_exit (progname
, 1) ;
90 #define HANDLE_BEXT_ARG(cmd,field) \
91 if (strcmp (argv [k], cmd) == 0) \
93 if (k == argc) missing_param (argv [k - 1]) ; \
94 info.field = argv [k] ; \
98 HANDLE_BEXT_ARG ("--bext-description", description
) ;
99 HANDLE_BEXT_ARG ("--bext-originator", originator
) ;
100 HANDLE_BEXT_ARG ("--bext-orig-ref", originator_reference
) ;
101 HANDLE_BEXT_ARG ("--bext-umid", umid
) ;
102 HANDLE_BEXT_ARG ("--bext-orig-date", origination_date
) ;
103 HANDLE_BEXT_ARG ("--bext-orig-time", origination_time
) ;
104 HANDLE_BEXT_ARG ("--bext-coding-hist", coding_history
) ;
105 HANDLE_BEXT_ARG ("--bext-time-ref", time_ref
) ;
107 #define HANDLE_STR_ARG(cmd,field) \
108 if (strcmp (argv [k], cmd) == 0) \
110 if (k == argc) missing_param (argv [k - 1]) ; \
111 info.field = argv [k] ; \
115 HANDLE_STR_ARG ("--str-comment", comment
) ;
116 HANDLE_STR_ARG ("--str-title", title
) ;
117 HANDLE_STR_ARG ("--str-copyright", copyright
) ;
118 HANDLE_STR_ARG ("--str-artist", artist
) ;
119 HANDLE_STR_ARG ("--str-date", date
) ;
120 HANDLE_STR_ARG ("--str-album", album
) ;
121 HANDLE_STR_ARG ("--str-license", license
) ;
123 /* Following options do not take an argument. */
124 if (strcmp (argv
[k
], "--bext-auto-time-date") == 0)
125 { snprintf (tmp
, sizeof (tmp
), "%02d:%02d:%02d", timedata
.tm_hour
, timedata
.tm_min
, timedata
.tm_sec
) ;
126 info
.origination_time
= strdup (tmp
) ;
128 snprintf (tmp
, sizeof (tmp
), "%04d-%02d-%02d", timedata
.tm_year
+ 1900, timedata
.tm_mon
+ 1, timedata
.tm_mday
) ;
129 info
.origination_date
= strdup (tmp
) ;
133 if (strcmp (argv
[k
], "--bext-auto-time") == 0)
134 { snprintf (tmp
, sizeof (tmp
), "%02d:%02d:%02d", timedata
.tm_hour
, timedata
.tm_min
, timedata
.tm_sec
) ;
135 info
.origination_time
= strdup (tmp
) ;
139 if (strcmp (argv
[k
], "--bext-auto-date") == 0)
140 { snprintf (tmp
, sizeof (tmp
), "%04d-%02d-%02d", timedata
.tm_year
+ 1900, timedata
.tm_mon
+ 1, timedata
.tm_mday
) ;
141 info
.origination_date
= strdup (tmp
) ;
145 if (strcmp (argv
[k
], "--str-auto-date") == 0)
146 { snprintf (tmp
, sizeof (tmp
), "%04d-%02d-%02d", timedata
.tm_year
+ 1900, timedata
.tm_mon
+ 1, timedata
.tm_mday
) ;
148 info
.date
= strdup (tmp
) ;
152 printf ("Error : Don't know what to do with command line arg '%s'.\n\n", argv
[k
]) ;
153 usage_exit (progname
, 1) ;
156 /* Find out if any of the 'bext' fields are set. */
157 info
.has_bext_fields
= has_bext_fields_set (&info
) ;
159 if (filenames
[0] == NULL
)
160 { printf ("Error : No input file specificed.\n\n") ;
164 if (filenames
[1] != NULL
&& strcmp (filenames
[0], filenames
[1]) == 0)
165 { printf ("Error : Input and output files are the same.\n\n") ;
169 if (info
.coding_history
!= NULL
&& filenames
[1] == NULL
)
171 "Error : Trying to update coding history of an existing file which unfortunately\n"
172 " is not supported. Instead, create a new file using :\n"
174 " %s --bext-coding-hist \"Coding history\" old_file.wav new_file.wav\n"
180 sfe_apply_metadata_changes (filenames
, &info
) ;
185 /*==============================================================================
186 ** Print version and usage.
190 usage_exit (const char *progname
, int exit_code
)
191 { printf ("\nUsage :\n\n"
192 " %s [options] <file>\n"
193 " %s [options] <input file> <output file>\n"
195 progname
, progname
) ;
198 "Where an option is made up of a pair of a field to set (one of\n"
199 "the 'bext' or metadata fields below) and a string. Fields are\n"
204 " --bext-description Set the 'bext' description.\n"
205 " --bext-originator Set the 'bext' originator.\n"
206 " --bext-orig-ref Set the 'bext' originator reference.\n"
207 " --bext-umid Set the 'bext' UMID.\n"
208 " --bext-orig-date Set the 'bext' origination date.\n"
209 " --bext-orig-time Set the 'bext' origination time.\n"
210 " --bext-coding-hist Set the 'bext' coding history.\n"
211 " --bext-time-raf Set the 'bext' Time ref.\n"
213 " --str-comment Set the metadata comment.\n"
214 " --str-title Set the metadata title.\n"
215 " --str-copyright Set the metadata copyright.\n"
216 " --str-artist Set the metadata artist.\n"
217 " --str-date Set the metadata date.\n"
218 " --str-album Set the metadata album.\n"
219 " --str-license Set the metadata license.\n"
223 "There are also the following arguments which do not take a\n"
225 " --bext-auto-time-date Set the 'bext' time and date to current time/date.\n"
226 " --bext-auto-time Set the 'bext' time to current time.\n"
227 " --bext-auto-date Set the 'bext' date to current date.\n"
228 " --str-auto-date Set the metadata date to current date.\n"
232 "Most of the above operations can be done in-place on an existing\n"
233 "file. If any operation cannot be performed, the application will\n"
234 "exit with an appropriate error message.\n"
237 printf ("Using %s.\n\n", sf_version_string ()) ;
242 missing_param (const char * option
)
244 printf ("Error : Option '%s' needs a parameter but doesn't seem to have one.\n\n", option
) ;
246 } /* missing_param */
248 /*==============================================================================
252 has_bext_fields_set (const METADATA_INFO
* info
)
254 if (info
->description
|| info
->originator
|| info
->originator_reference
)
257 if (info
->origination_date
|| info
->origination_time
|| info
->umid
|| info
->coding_history
|| info
->time_ref
)
261 } /* has_bext_fields_set */
264 read_localtime (struct tm
* timedata
)
268 memset (timedata
, 0, sizeof (struct tm
)) ;
270 #if defined (HAVE_LOCALTIME_R)
271 /* If the re-entrant version is available, use it. */
272 localtime_r (¤t
, timedata
) ;
273 #elif defined (HAVE_LOCALTIME)
276 /* Otherwise use the standard one and copy the data to local storage. */
277 if ((tmptr
= localtime (¤t
)) != NULL
)
278 memcpy (timedata
, tmptr
, sizeof (struct tm
)) ;
283 } /* read_localtime */