2 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
3 ** Copyright (C) 2008 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.
44 #define BUFFER_LEN 4096
46 #define MIN(x,y) ((x) < (y) ? (x) : (y))
49 sfe_copy_data_fp (SNDFILE
*outfile
, SNDFILE
*infile
, int channels
)
50 { static double data
[BUFFER_LEN
], max
;
51 int frames
, readcount
, k
;
53 frames
= BUFFER_LEN
/ channels
;
56 sf_command (infile
, SFC_CALC_SIGNAL_MAX
, &max
, sizeof (max
)) ;
59 { while (readcount
> 0)
60 { readcount
= sf_readf_double (infile
, data
, frames
) ;
61 sf_writef_double (outfile
, data
, readcount
) ;
65 { sf_command (infile
, SFC_SET_NORM_DOUBLE
, NULL
, SF_FALSE
) ;
68 { readcount
= sf_readf_double (infile
, data
, frames
) ;
69 for (k
= 0 ; k
< readcount
* channels
; k
++)
71 sf_writef_double (outfile
, data
, readcount
) ;
76 } /* sfe_copy_data_fp */
79 sfe_copy_data_int (SNDFILE
*outfile
, SNDFILE
*infile
, int channels
)
80 { static int data
[BUFFER_LEN
] ;
81 int frames
, readcount
;
83 frames
= BUFFER_LEN
/ channels
;
87 { readcount
= sf_readf_int (infile
, data
, frames
) ;
88 sf_writef_int (outfile
, data
, readcount
) ;
92 } /* sfe_copy_data_int */
94 /*==============================================================================
98 merge_broadcast_info (SNDFILE
* infile
, SNDFILE
* outfile
, int format
, const METADATA_INFO
* info
)
99 { SF_BROADCAST_INFO_2K binfo
;
102 memset (&binfo
, 0, sizeof (binfo
)) ;
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") ;
109 infileminor
= SF_FORMAT_SUBMASK
& format
;
112 { case SF_FORMAT_PCM_16
:
113 case SF_FORMAT_PCM_24
:
114 case SF_FORMAT_PCM_32
:
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"
126 if (sf_command (infile
, SFC_GET_BROADCAST_INFO
, &binfo
, sizeof (binfo
)) == 0)
127 { if (infile
== outfile
)
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"
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))) ; \
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
) ;
150 /* Special case for Time Ref. */
151 if (info
->time_ref
!= NULL
)
152 { uint64_t ts
= atoll (info
->time_ref
) ;
154 binfo
.time_reference_high
= (ts
>> 32) ;
155 binfo
.time_reference_low
= (ts
& 0xffffffff) ;
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
) ;
163 while (slen
> 1 && isspace (binfo
.coding_history
[slen
- 1]))
166 memcpy (binfo
.coding_history
+ slen
, info
->coding_history
, sizeof (binfo
.coding_history
) - slen
) ;
169 { size_t slen
= MIN (strlen (info
->coding_history
), sizeof (binfo
.coding_history
)) ;
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
;
177 if (sf_command (outfile
, SFC_SET_BROADCAST_INFO
, &binfo
, sizeof (binfo
)) == 0)
178 { printf ("Error : Setting of broadcast info chunks failed.\n\n") ;
183 } /* merge_broadcast_info*/
186 update_strings (SNDFILE
* outfile
, const METADATA_INFO
* info
)
188 if (info
->title
!= NULL
)
189 sf_set_string (outfile
, SF_STR_TITLE
, info
->title
) ;
191 if (info
->copyright
!= NULL
)
192 sf_set_string (outfile
, SF_STR_COPYRIGHT
, info
->copyright
) ;
194 if (info
->artist
!= NULL
)
195 sf_set_string (outfile
, SF_STR_ARTIST
, info
->artist
) ;
197 if (info
->comment
!= NULL
)
198 sf_set_string (outfile
, SF_STR_COMMENT
, info
->comment
) ;
200 if (info
->date
!= NULL
)
201 sf_set_string (outfile
, SF_STR_DATE
, info
->date
) ;
203 if (info
->album
!= NULL
)
204 sf_set_string (outfile
, SF_STR_ALBUM
, info
->album
) ;
206 if (info
->license
!= NULL
)
207 sf_set_string (outfile
, SF_STR_LICENSE
, info
->license
) ;
209 } /* update_strings */
214 sfe_apply_metadata_changes (const char * filenames
[2], const METADATA_INFO
* info
)
215 { SNDFILE
*infile
= NULL
, *outfile
= NULL
;
217 METADATA_INFO tmpinfo
;
220 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
221 memset (&tmpinfo
, 0, sizeof (tmpinfo
)) ;
223 if (filenames
[1] == NULL
)
224 infile
= outfile
= sf_open (filenames
[0], SFM_RDWR
, &sfinfo
) ;
226 { infile
= sf_open (filenames
[0], SFM_READ
, &sfinfo
) ;
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
) ;
234 { printf ("Error : Not able to open input file '%s' : %s\n", filenames
[0], sf_strerror (infile
)) ;
240 { printf ("Error : Not able to open output file '%s' : %s\n", filenames
[1], sf_strerror (outfile
)) ;
245 if (info
->has_bext_fields
&& merge_broadcast_info (infile
, outfile
, sfinfo
.format
, info
))
250 if (infile
!= outfile
)
251 { int infileminor
= SF_FORMAT_SUBMASK
& sfinfo
.format
;
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
) ;
257 sfe_copy_data_int (outfile
, infile
, sfinfo
.channels
) ;
260 update_strings (outfile
, info
) ;
264 if (outfile
!= NULL
&& outfile
!= infile
)
274 } /* sfe_apply_metadata_changes */
276 /*==============================================================================
283 } OUTPUT_FORMAT_MAP
;
285 static OUTPUT_FORMAT_MAP format_map
[] =
287 { "aif", 3, SF_FORMAT_AIFF
},
288 { "wav", 0, SF_FORMAT_WAV
},
289 { "au", 0, SF_FORMAT_AU
},
290 { "caf", 0, SF_FORMAT_CAF
},
291 { "flac", 0, SF_FORMAT_FLAC
},
292 { "snd", 0, SF_FORMAT_AU
},
293 { "svx", 0, SF_FORMAT_SVX
},
294 { "paf", 0, SF_ENDIAN_BIG
| SF_FORMAT_PAF
},
295 { "fap", 0, SF_ENDIAN_LITTLE
| SF_FORMAT_PAF
},
296 { "gsm", 0, SF_FORMAT_RAW
},
297 { "nist", 0, SF_FORMAT_NIST
},
298 { "htk", 0, SF_FORMAT_HTK
},
299 { "ircam", 0, SF_FORMAT_IRCAM
},
300 { "sf", 0, SF_FORMAT_IRCAM
},
301 { "voc", 0, SF_FORMAT_VOC
},
302 { "w64", 0, SF_FORMAT_W64
},
303 { "raw", 0, SF_FORMAT_RAW
},
304 { "mat4", 0, SF_FORMAT_MAT4
},
305 { "mat5", 0, SF_FORMAT_MAT5
},
306 { "mat", 0, SF_FORMAT_MAT4
},
307 { "pvf", 0, SF_FORMAT_PVF
},
308 { "sds", 0, SF_FORMAT_SDS
},
309 { "sd2", 0, SF_FORMAT_SD2
},
310 { "vox", 0, SF_FORMAT_RAW
},
311 { "xi", 0, SF_FORMAT_XI
},
312 { "wve", 0, SF_FORMAT_WVE
},
313 { "oga", 0, SF_FORMAT_OGG
},
314 { "mpc", 0, SF_FORMAT_MPC2K
},
315 { "rf64", 0, SF_FORMAT_RF64
},
319 sfe_file_type_of_ext (const char *str
, int format
)
320 { char buffer
[16], *cptr
;
323 format
&= SF_FORMAT_SUBMASK
;
325 if ((cptr
= strrchr (str
, '.')) == NULL
)
328 strncpy (buffer
, cptr
+ 1, 15) ;
331 for (k
= 0 ; buffer
[k
] ; k
++)
332 buffer
[k
] = tolower ((buffer
[k
])) ;
334 if (strcmp (buffer
, "gsm") == 0)
335 return SF_FORMAT_RAW
| SF_FORMAT_GSM610
;
337 if (strcmp (buffer
, "vox") == 0)
338 return SF_FORMAT_RAW
| SF_FORMAT_VOX_ADPCM
;
340 for (k
= 0 ; k
< (int) (sizeof (format_map
) / sizeof (format_map
[0])) ; k
++)
341 { if (format_map
[k
].len
> 0 && strncmp (buffer
, format_map
[k
].ext
, format_map
[k
].len
) == 0)
342 return format_map
[k
].format
| format
;
343 else if (strcmp (buffer
, format_map
[k
].ext
) == 0)
344 return format_map
[k
].format
| format
;
347 /* Default if all the above fails. */
348 return (SF_FORMAT_WAV
| SF_FORMAT_PCM_24
) ;
349 } /* sfe_file_type_of_ext */
352 sfe_dump_format_map (void)
353 { SF_FORMAT_INFO info
;
356 for (k
= 0 ; k
< ARRAY_LEN (format_map
) ; k
++)
357 { info
.format
= format_map
[k
].format
;
358 sf_command (NULL
, SFC_GET_FORMAT_INFO
, &info
, sizeof (info
)) ;
359 printf (" %-10s : %s\n", format_map
[k
].ext
, info
.name
== NULL
? "????" : info
.name
) ;
362 } /* sfe_dump_format_map */
365 program_name (const char * argv0
)
368 tmp
= strrchr (argv0
, '/') ;
369 argv0
= tmp
? tmp
+ 1 : argv0
;
371 tmp
= strrchr (argv0
, '/') ;
372 argv0
= tmp
? tmp
+ 1 : argv0
;
374 /* Remove leading libtool name mangling. */
375 if (strstr (argv0
, "lt-") == argv0
)