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