Mercurial > hg > sv-dependency-builds
comparison src/libsndfile-1.0.27/programs/common.c @ 40:1df64224f5ac
Current libsndfile source
author | Chris Cannam |
---|---|
date | Tue, 18 Oct 2016 13:22:47 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
39:7ddb4fc30dac | 40:1df64224f5ac |
---|---|
1 /* | |
2 ** Copyright (C) 1999-2014 Erik de Castro Lopo <erikd@mega-nerd.com> | |
3 ** Copyright (C) 2008 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 <stdio.h> | |
35 #include <stdlib.h> | |
36 #include <string.h> | |
37 #include <ctype.h> | |
38 #include <stdint.h> | |
39 | |
40 #include <sndfile.h> | |
41 | |
42 #include "common.h" | |
43 | |
44 #define BUFFER_LEN 4096 | |
45 | |
46 #define MIN(x, y) ((x) < (y) ? (x) : (y)) | |
47 | |
48 void | |
49 sfe_copy_data_fp (SNDFILE *outfile, SNDFILE *infile, int channels, int normalize) | |
50 { static double data [BUFFER_LEN], max ; | |
51 int frames, readcount, k ; | |
52 | |
53 frames = BUFFER_LEN / channels ; | |
54 readcount = frames ; | |
55 | |
56 sf_command (infile, SFC_CALC_SIGNAL_MAX, &max, sizeof (max)) ; | |
57 | |
58 if (!normalize && max < 1.0) | |
59 { while (readcount > 0) | |
60 { readcount = sf_readf_double (infile, data, frames) ; | |
61 sf_writef_double (outfile, data, readcount) ; | |
62 } ; | |
63 } | |
64 else | |
65 { sf_command (infile, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ; | |
66 | |
67 while (readcount > 0) | |
68 { readcount = sf_readf_double (infile, data, frames) ; | |
69 for (k = 0 ; k < readcount * channels ; k++) | |
70 data [k] /= max ; | |
71 sf_writef_double (outfile, data, readcount) ; | |
72 } ; | |
73 } ; | |
74 | |
75 return ; | |
76 } /* sfe_copy_data_fp */ | |
77 | |
78 void | |
79 sfe_copy_data_int (SNDFILE *outfile, SNDFILE *infile, int channels) | |
80 { static int data [BUFFER_LEN] ; | |
81 int frames, readcount ; | |
82 | |
83 frames = BUFFER_LEN / channels ; | |
84 readcount = frames ; | |
85 | |
86 while (readcount > 0) | |
87 { readcount = sf_readf_int (infile, data, frames) ; | |
88 sf_writef_int (outfile, data, readcount) ; | |
89 } ; | |
90 | |
91 return ; | |
92 } /* sfe_copy_data_int */ | |
93 | |
94 /*============================================================================== | |
95 */ | |
96 | |
97 static int | |
98 merge_broadcast_info (SNDFILE * infile, SNDFILE * outfile, int format, const METADATA_INFO * info) | |
99 { SF_BROADCAST_INFO_2K binfo ; | |
100 int infileminor ; | |
101 | |
102 memset (&binfo, 0, sizeof (binfo)) ; | |
103 | |
104 if ((SF_FORMAT_TYPEMASK & format) != SF_FORMAT_WAV) | |
105 { printf ("Error : This is not a WAV file and hence broadcast info cannot be added to it.\n\n") ; | |
106 return 1 ; | |
107 } ; | |
108 | |
109 infileminor = SF_FORMAT_SUBMASK & format ; | |
110 | |
111 switch (infileminor) | |
112 { case SF_FORMAT_PCM_16 : | |
113 case SF_FORMAT_PCM_24 : | |
114 case SF_FORMAT_PCM_32 : | |
115 break ; | |
116 | |
117 default : | |
118 printf ( | |
119 "Warning : The EBU Technical Recommendation R68-2000 states that the only\n" | |
120 " allowed encodings are Linear PCM and MPEG3. This file is not in\n" | |
121 " the right format.\n\n" | |
122 ) ; | |
123 break ; | |
124 } ; | |
125 | |
126 if (sf_command (infile, SFC_GET_BROADCAST_INFO, &binfo, sizeof (binfo)) == 0) | |
127 { if (infile == outfile) | |
128 { printf ( | |
129 "Error : Attempting in-place broadcast info update, but file does not\n" | |
130 " have a 'bext' chunk to modify. The solution is to specify both\n" | |
131 " input and output files on the command line.\n\n" | |
132 ) ; | |
133 return 1 ; | |
134 } ; | |
135 } ; | |
136 | |
137 #define REPLACE_IF_NEW(x) \ | |
138 if (info->x != NULL) \ | |
139 { memset (binfo.x, 0, sizeof (binfo.x)) ; \ | |
140 memcpy (binfo.x, info->x, MIN (strlen (info->x), sizeof (binfo.x))) ; \ | |
141 } ; | |
142 | |
143 REPLACE_IF_NEW (description) ; | |
144 REPLACE_IF_NEW (originator) ; | |
145 REPLACE_IF_NEW (originator_reference) ; | |
146 REPLACE_IF_NEW (origination_date) ; | |
147 REPLACE_IF_NEW (origination_time) ; | |
148 REPLACE_IF_NEW (umid) ; | |
149 | |
150 /* Special case for Time Ref. */ | |
151 if (info->time_ref != NULL) | |
152 { uint64_t ts = atoll (info->time_ref) ; | |
153 | |
154 binfo.time_reference_high = (ts >> 32) ; | |
155 binfo.time_reference_low = (ts & 0xffffffff) ; | |
156 } ; | |
157 | |
158 /* Special case for coding_history because we may want to append. */ | |
159 if (info->coding_history != NULL) | |
160 { if (info->coding_hist_append) | |
161 { int slen = strlen (binfo.coding_history) ; | |
162 | |
163 while (slen > 1 && isspace (binfo.coding_history [slen - 1])) | |
164 slen -- ; | |
165 | |
166 memcpy (binfo.coding_history + slen, info->coding_history, sizeof (binfo.coding_history) - slen) ; | |
167 } | |
168 else | |
169 { size_t slen = MIN (strlen (info->coding_history), sizeof (binfo.coding_history)) ; | |
170 | |
171 memset (binfo.coding_history, 0, sizeof (binfo.coding_history)) ; | |
172 memcpy (binfo.coding_history, info->coding_history, slen) ; | |
173 binfo.coding_history_size = slen ; | |
174 } ; | |
175 } ; | |
176 | |
177 if (sf_command (outfile, SFC_SET_BROADCAST_INFO, &binfo, sizeof (binfo)) == 0) | |
178 { printf ("Error : Setting of broadcast info chunks failed.\n\n") ; | |
179 return 1 ; | |
180 } ; | |
181 | |
182 return 0 ; | |
183 } /* merge_broadcast_info*/ | |
184 | |
185 static void | |
186 update_strings (SNDFILE * outfile, const METADATA_INFO * info) | |
187 { | |
188 if (info->title != NULL) | |
189 sf_set_string (outfile, SF_STR_TITLE, info->title) ; | |
190 | |
191 if (info->copyright != NULL) | |
192 sf_set_string (outfile, SF_STR_COPYRIGHT, info->copyright) ; | |
193 | |
194 if (info->artist != NULL) | |
195 sf_set_string (outfile, SF_STR_ARTIST, info->artist) ; | |
196 | |
197 if (info->comment != NULL) | |
198 sf_set_string (outfile, SF_STR_COMMENT, info->comment) ; | |
199 | |
200 if (info->date != NULL) | |
201 sf_set_string (outfile, SF_STR_DATE, info->date) ; | |
202 | |
203 if (info->album != NULL) | |
204 sf_set_string (outfile, SF_STR_ALBUM, info->album) ; | |
205 | |
206 if (info->license != NULL) | |
207 sf_set_string (outfile, SF_STR_LICENSE, info->license) ; | |
208 | |
209 } /* update_strings */ | |
210 | |
211 | |
212 | |
213 void | |
214 sfe_apply_metadata_changes (const char * filenames [2], const METADATA_INFO * info) | |
215 { SNDFILE *infile = NULL, *outfile = NULL ; | |
216 SF_INFO sfinfo ; | |
217 METADATA_INFO tmpinfo ; | |
218 int error_code = 0 ; | |
219 | |
220 memset (&sfinfo, 0, sizeof (sfinfo)) ; | |
221 memset (&tmpinfo, 0, sizeof (tmpinfo)) ; | |
222 | |
223 if (filenames [1] == NULL) | |
224 infile = outfile = sf_open (filenames [0], SFM_RDWR, &sfinfo) ; | |
225 else | |
226 { infile = sf_open (filenames [0], SFM_READ, &sfinfo) ; | |
227 | |
228 /* Output must be WAV. */ | |
229 sfinfo.format = SF_FORMAT_WAV | (SF_FORMAT_SUBMASK & sfinfo.format) ; | |
230 outfile = sf_open (filenames [1], SFM_WRITE, &sfinfo) ; | |
231 } ; | |
232 | |
233 if (infile == NULL) | |
234 { printf ("Error : Not able to open input file '%s' : %s\n", filenames [0], sf_strerror (infile)) ; | |
235 error_code = 1 ; | |
236 goto cleanup_exit ; | |
237 } ; | |
238 | |
239 if (outfile == NULL) | |
240 { printf ("Error : Not able to open output file '%s' : %s\n", filenames [1], sf_strerror (outfile)) ; | |
241 error_code = 1 ; | |
242 goto cleanup_exit ; | |
243 } ; | |
244 | |
245 if (info->has_bext_fields && merge_broadcast_info (infile, outfile, sfinfo.format, info)) | |
246 { error_code = 1 ; | |
247 goto cleanup_exit ; | |
248 } ; | |
249 | |
250 if (infile != outfile) | |
251 { int infileminor = SF_FORMAT_SUBMASK & sfinfo.format ; | |
252 | |
253 /* If the input file is not the same as the output file, copy the data. */ | |
254 if ((infileminor == SF_FORMAT_DOUBLE) || (infileminor == SF_FORMAT_FLOAT)) | |
255 sfe_copy_data_fp (outfile, infile, sfinfo.channels, SF_FALSE) ; | |
256 else | |
257 sfe_copy_data_int (outfile, infile, sfinfo.channels) ; | |
258 } ; | |
259 | |
260 update_strings (outfile, info) ; | |
261 | |
262 cleanup_exit : | |
263 | |
264 if (outfile != NULL && outfile != infile) | |
265 sf_close (outfile) ; | |
266 | |
267 if (infile != NULL) | |
268 sf_close (infile) ; | |
269 | |
270 if (error_code) | |
271 exit (error_code) ; | |
272 | |
273 return ; | |
274 } /* sfe_apply_metadata_changes */ | |
275 | |
276 /*============================================================================== | |
277 */ | |
278 | |
279 typedef struct | |
280 { const char *ext ; | |
281 int len ; | |
282 int format ; | |
283 } OUTPUT_FORMAT_MAP ; | |
284 | |
285 static OUTPUT_FORMAT_MAP format_map [] = | |
286 { | |
287 { "wav", 0, SF_FORMAT_WAV }, | |
288 { "aif", 3, SF_FORMAT_AIFF }, | |
289 { "au", 0, SF_FORMAT_AU }, | |
290 { "snd", 0, SF_FORMAT_AU }, | |
291 { "raw", 0, SF_FORMAT_RAW }, | |
292 { "gsm", 0, SF_FORMAT_RAW }, | |
293 { "vox", 0, SF_FORMAT_RAW }, | |
294 { "paf", 0, SF_FORMAT_PAF | SF_ENDIAN_BIG }, | |
295 { "fap", 0, SF_FORMAT_PAF | SF_ENDIAN_LITTLE }, | |
296 { "svx", 0, SF_FORMAT_SVX }, | |
297 { "nist", 0, SF_FORMAT_NIST }, | |
298 { "sph", 0, SF_FORMAT_NIST }, | |
299 { "voc", 0, SF_FORMAT_VOC }, | |
300 { "ircam", 0, SF_FORMAT_IRCAM }, | |
301 { "sf", 0, SF_FORMAT_IRCAM }, | |
302 { "w64", 0, SF_FORMAT_W64 }, | |
303 { "mat", 0, SF_FORMAT_MAT4 }, | |
304 { "mat4", 0, SF_FORMAT_MAT4 }, | |
305 { "mat5", 0, SF_FORMAT_MAT5 }, | |
306 { "pvf", 0, SF_FORMAT_PVF }, | |
307 { "xi", 0, SF_FORMAT_XI }, | |
308 { "htk", 0, SF_FORMAT_HTK }, | |
309 { "sds", 0, SF_FORMAT_SDS }, | |
310 { "avr", 0, SF_FORMAT_AVR }, | |
311 { "wavex", 0, SF_FORMAT_WAVEX }, | |
312 { "sd2", 0, SF_FORMAT_SD2 }, | |
313 { "flac", 0, SF_FORMAT_FLAC }, | |
314 { "caf", 0, SF_FORMAT_CAF }, | |
315 { "wve", 0, SF_FORMAT_WVE }, | |
316 { "prc", 0, SF_FORMAT_WVE }, | |
317 { "ogg", 0, SF_FORMAT_OGG }, | |
318 { "oga", 0, SF_FORMAT_OGG }, | |
319 { "mpc", 0, SF_FORMAT_MPC2K }, | |
320 { "rf64", 0, SF_FORMAT_RF64 }, | |
321 } ; /* format_map */ | |
322 | |
323 int | |
324 sfe_file_type_of_ext (const char *str, int format) | |
325 { char buffer [16], *cptr ; | |
326 int k ; | |
327 | |
328 format &= SF_FORMAT_SUBMASK ; | |
329 | |
330 if ((cptr = strrchr (str, '.')) == NULL) | |
331 return 0 ; | |
332 | |
333 strncpy (buffer, cptr + 1, 15) ; | |
334 buffer [15] = 0 ; | |
335 | |
336 for (k = 0 ; buffer [k] ; k++) | |
337 buffer [k] = tolower ((buffer [k])) ; | |
338 | |
339 if (strcmp (buffer, "gsm") == 0) | |
340 return SF_FORMAT_RAW | SF_FORMAT_GSM610 ; | |
341 | |
342 if (strcmp (buffer, "vox") == 0) | |
343 return SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM ; | |
344 | |
345 for (k = 0 ; k < (int) (sizeof (format_map) / sizeof (format_map [0])) ; k++) | |
346 { if (format_map [k].len > 0 && strncmp (buffer, format_map [k].ext, format_map [k].len) == 0) | |
347 return format_map [k].format | format ; | |
348 else if (strcmp (buffer, format_map [k].ext) == 0) | |
349 return format_map [k].format | format ; | |
350 } ; | |
351 | |
352 /* Default if all the above fails. */ | |
353 return (SF_FORMAT_WAV | SF_FORMAT_PCM_24) ; | |
354 } /* sfe_file_type_of_ext */ | |
355 | |
356 void | |
357 sfe_dump_format_map (void) | |
358 { SF_FORMAT_INFO info ; | |
359 int k ; | |
360 | |
361 for (k = 0 ; k < ARRAY_LEN (format_map) ; k++) | |
362 { info.format = format_map [k].format ; | |
363 sf_command (NULL, SFC_GET_FORMAT_INFO, &info, sizeof (info)) ; | |
364 printf (" %-10s : %s\n", format_map [k].ext, info.name == NULL ? "????" : info.name) ; | |
365 } ; | |
366 | |
367 } /* sfe_dump_format_map */ | |
368 | |
369 const char * | |
370 program_name (const char * argv0) | |
371 { const char * tmp ; | |
372 | |
373 tmp = strrchr (argv0, '/') ; | |
374 argv0 = tmp ? tmp + 1 : argv0 ; | |
375 | |
376 /* Remove leading libtool name mangling. */ | |
377 if (strstr (argv0, "lt-") == argv0) | |
378 return argv0 + 3 ; | |
379 | |
380 return argv0 ; | |
381 } /* program_name */ | |
382 | |
383 const char * | |
384 sfe_endian_name (int format) | |
385 { | |
386 switch (format & SF_FORMAT_ENDMASK) | |
387 { case SF_ENDIAN_FILE : return "file" ; | |
388 case SF_ENDIAN_LITTLE : return "little" ; | |
389 case SF_ENDIAN_BIG : return "big" ; | |
390 case SF_ENDIAN_CPU : return "cpu" ; | |
391 default : break ; | |
392 } ; | |
393 | |
394 return "unknown" ; | |
395 } /* sfe_endian_name */ | |
396 | |
397 const char * | |
398 sfe_container_name (int format) | |
399 { | |
400 switch (format & SF_FORMAT_TYPEMASK) | |
401 { case SF_FORMAT_WAV : return "WAV" ; | |
402 case SF_FORMAT_AIFF : return "AIFF" ; | |
403 case SF_FORMAT_AU : return "AU" ; | |
404 case SF_FORMAT_RAW : return "RAW" ; | |
405 case SF_FORMAT_PAF : return "PAF" ; | |
406 case SF_FORMAT_SVX : return "SVX" ; | |
407 case SF_FORMAT_NIST : return "NIST" ; | |
408 case SF_FORMAT_VOC : return "VOC" ; | |
409 case SF_FORMAT_IRCAM : return "IRCAM" ; | |
410 case SF_FORMAT_W64 : return "W64" ; | |
411 case SF_FORMAT_MAT4 : return "MAT4" ; | |
412 case SF_FORMAT_MAT5 : return "MAT5" ; | |
413 case SF_FORMAT_PVF : return "PVF" ; | |
414 case SF_FORMAT_XI : return "XI" ; | |
415 case SF_FORMAT_HTK : return "HTK" ; | |
416 case SF_FORMAT_SDS : return "SDS" ; | |
417 case SF_FORMAT_AVR : return "AVR" ; | |
418 case SF_FORMAT_WAVEX : return "WAVEX" ; | |
419 case SF_FORMAT_SD2 : return "SD2" ; | |
420 case SF_FORMAT_FLAC : return "FLAC" ; | |
421 case SF_FORMAT_CAF : return "CAF" ; | |
422 case SF_FORMAT_WVE : return "WVE" ; | |
423 case SF_FORMAT_OGG : return "OGG" ; | |
424 case SF_FORMAT_MPC2K : return "MPC2K" ; | |
425 case SF_FORMAT_RF64 : return "RF64" ; | |
426 default : break ; | |
427 } ; | |
428 | |
429 return "unknown" ; | |
430 } /* sfe_container_name */ | |
431 | |
432 const char * | |
433 sfe_codec_name (int format) | |
434 { | |
435 switch (format & SF_FORMAT_SUBMASK) | |
436 { case SF_FORMAT_PCM_S8 : return "signed 8 bit PCM" ; | |
437 case SF_FORMAT_PCM_16 : return "16 bit PCM" ; | |
438 case SF_FORMAT_PCM_24 : return "24 bit PCM" ; | |
439 case SF_FORMAT_PCM_32 : return "32 bit PCM" ; | |
440 case SF_FORMAT_PCM_U8 : return "unsigned 8 bit PCM" ; | |
441 case SF_FORMAT_FLOAT : return "32 bit float" ; | |
442 case SF_FORMAT_DOUBLE : return "64 bit double" ; | |
443 case SF_FORMAT_ULAW : return "u-law" ; | |
444 case SF_FORMAT_ALAW : return "a-law" ; | |
445 case SF_FORMAT_IMA_ADPCM : return "IMA ADPCM" ; | |
446 case SF_FORMAT_MS_ADPCM : return "MS ADPCM" ; | |
447 case SF_FORMAT_GSM610 : return "gsm610" ; | |
448 case SF_FORMAT_VOX_ADPCM : return "Vox ADPCM" ; | |
449 case SF_FORMAT_G721_32 : return "g721 32kbps" ; | |
450 case SF_FORMAT_G723_24 : return "g723 24kbps" ; | |
451 case SF_FORMAT_G723_40 : return "g723 40kbps" ; | |
452 case SF_FORMAT_DWVW_12 : return "12 bit DWVW" ; | |
453 case SF_FORMAT_DWVW_16 : return "16 bit DWVW" ; | |
454 case SF_FORMAT_DWVW_24 : return "14 bit DWVW" ; | |
455 case SF_FORMAT_DWVW_N : return "DWVW" ; | |
456 case SF_FORMAT_DPCM_8 : return "8 bit DPCM" ; | |
457 case SF_FORMAT_DPCM_16 : return "16 bit DPCM" ; | |
458 case SF_FORMAT_VORBIS : return "Vorbis" ; | |
459 case SF_FORMAT_ALAC_16 : return "16 bit ALAC" ; | |
460 case SF_FORMAT_ALAC_20 : return "20 bit ALAC" ; | |
461 case SF_FORMAT_ALAC_24 : return "24 bit ALAC" ; | |
462 case SF_FORMAT_ALAC_32 : return "32 bit ALAC" ; | |
463 default : break ; | |
464 } ; | |
465 return "unknown" ; | |
466 } /* sfe_codec_name */ | |
467 |