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