libsndfile source files.
[Faustine.git] / interpretor / libsndfile-1.0.25 / programs / sndfile-metadata-set.c
1 /*
2 ** Copyright (C) 2008-2011 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
34 #include <config.h>
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <ctype.h>
40 #include <time.h>
41
42 #include <sndfile.h>
43
44 #include "common.h"
45
46 #define BUFFER_LEN (1 << 16)
47
48
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) ;
53
54 int
55 main (int argc, char *argv [])
56 { METADATA_INFO info ;
57 struct tm timedata ;
58 const char *progname ;
59 const char * filenames [2] = { NULL, NULL } ;
60 int k ;
61
62 /* Store the program name. */
63 progname = program_name (argv [0]) ;
64
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) ;
68
69 /* Clear set all fields of the struct to zero bytes. */
70 memset (&info, 0, sizeof (info)) ;
71
72 /* Get the time in case we need it later. */
73 read_localtime (&timedata) ;
74
75 for (k = 1 ; k < argc ; k++)
76 { char tmp [20] ;
77
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] ;
83 else
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) ;
86 } ;
87 continue ;
88 } ;
89
90 #define HANDLE_BEXT_ARG(cmd,field) \
91 if (strcmp (argv [k], cmd) == 0) \
92 { k ++ ; \
93 if (k == argc) missing_param (argv [k - 1]) ; \
94 info.field = argv [k] ; \
95 continue ; \
96 } ;
97
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) ;
106
107 #define HANDLE_STR_ARG(cmd,field) \
108 if (strcmp (argv [k], cmd) == 0) \
109 { k ++ ; \
110 if (k == argc) missing_param (argv [k - 1]) ; \
111 info.field = argv [k] ; \
112 continue ; \
113 } ;
114
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) ;
122
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) ;
127
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) ;
130 continue ;
131 } ;
132
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) ;
136 continue ;
137 } ;
138
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) ;
142 continue ;
143 } ;
144
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) ;
147
148 info.date = strdup (tmp) ;
149 continue ;
150 } ;
151
152 printf ("Error : Don't know what to do with command line arg '%s'.\n\n", argv [k]) ;
153 usage_exit (progname, 1) ;
154 } ;
155
156 /* Find out if any of the 'bext' fields are set. */
157 info.has_bext_fields = has_bext_fields_set (&info) ;
158
159 if (filenames [0] == NULL)
160 { printf ("Error : No input file specificed.\n\n") ;
161 exit (1) ;
162 } ;
163
164 if (filenames [1] != NULL && strcmp (filenames [0], filenames [1]) == 0)
165 { printf ("Error : Input and output files are the same.\n\n") ;
166 exit (1) ;
167 } ;
168
169 if (info.coding_history != NULL && filenames [1] == NULL)
170 { printf ("\n"
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"
173 "\n"
174 " %s --bext-coding-hist \"Coding history\" old_file.wav new_file.wav\n"
175 "\n",
176 progname) ;
177 exit (1) ;
178 } ;
179
180 sfe_apply_metadata_changes (filenames, &info) ;
181
182 return 0 ;
183 } /* main */
184
185 /*==============================================================================
186 ** Print version and usage.
187 */
188
189 static void
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"
194 "\n",
195 progname, progname) ;
196
197 puts (
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"
200 "as follows :\n"
201 ) ;
202
203 puts (
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"
212 "\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"
220 ) ;
221
222 puts (
223 "There are also the following arguments which do not take a\n"
224 "parameter :\n\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"
229 ) ;
230
231 puts (
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"
235 ) ;
236
237 printf ("Using %s.\n\n", sf_version_string ()) ;
238 exit (exit_code) ;
239 } /* usage_exit */
240
241 static void
242 missing_param (const char * option)
243 {
244 printf ("Error : Option '%s' needs a parameter but doesn't seem to have one.\n\n", option) ;
245 exit (1) ;
246 } /* missing_param */
247
248 /*==============================================================================
249 */
250
251 static int
252 has_bext_fields_set (const METADATA_INFO * info)
253 {
254 if (info->description || info->originator || info->originator_reference)
255 return 1 ;
256
257 if (info->origination_date || info->origination_time || info->umid || info->coding_history || info->time_ref)
258 return 1 ;
259
260 return 0 ;
261 } /* has_bext_fields_set */
262
263 static void
264 read_localtime (struct tm * timedata)
265 { time_t current ;
266
267 time (&current) ;
268 memset (timedata, 0, sizeof (struct tm)) ;
269
270 #if defined (HAVE_LOCALTIME_R)
271 /* If the re-entrant version is available, use it. */
272 localtime_r (&current, timedata) ;
273 #elif defined (HAVE_LOCALTIME)
274 {
275 struct tm *tmptr ;
276 /* Otherwise use the standard one and copy the data to local storage. */
277 if ((tmptr = localtime (&current)) != NULL)
278 memcpy (timedata, tmptr, sizeof (struct tm)) ;
279 }
280 #endif
281
282 return ;
283 } /* read_localtime */
284