User Manual first draft.
[Faustine.git] / interpreter / lib / src / libsndfile-1.0.25 / programs / sndfile-convert.c
1 /*
2 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
3 **
4 ** All rights reserved.
5 **
6 ** Redistribution and use in source and binary forms, with or without
7 ** modification, are permitted provided that the following conditions are
8 ** met:
9 **
10 ** * Redistributions of source code must retain the above copyright
11 ** notice, this list of conditions and the following disclaimer.
12 ** * Redistributions in binary form must reproduce the above copyright
13 ** notice, this list of conditions and the following disclaimer in
14 ** the documentation and/or other materials provided with the
15 ** distribution.
16 ** * Neither the author nor the names of any contributors may be used
17 ** to endorse or promote products derived from this software without
18 ** specific prior written permission.
19 **
20 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30 ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <ctype.h>
37
38 #include <sndfile.h>
39
40 #include "common.h"
41
42
43 typedef struct
44 { char *infilename, *outfilename ;
45 SF_INFO infileinfo, outfileinfo ;
46 } OptionData ;
47
48 static void copy_metadata (SNDFILE *outfile, SNDFILE *infile, int channels) ;
49
50 static void
51 usage_exit (const char *progname)
52 {
53 printf ("\nUsage : %s [options] [encoding] <input file> <output file>\n", progname) ;
54 puts ("\n"
55 " where [option] may be:\n\n"
56 " -override-sample-rate=X : force sample rate of input to X\n"
57 ) ;
58
59 puts (
60 " where [encoding] may be one of the following:\n\n"
61 " -pcms8 : force the output to signed 8 bit pcm\n"
62 " -pcmu8 : force the output to unsigned 8 bit pcm\n"
63 " -pcm16 : force the output to 16 bit pcm\n"
64 " -pcm24 : force the output to 24 bit pcm\n"
65 " -pcm32 : force the output to 32 bit pcm\n"
66 " -float32 : force the output to 32 bit floating point"
67 ) ;
68 puts (
69 " -ulaw : force the output ULAW\n"
70 " -alaw : force the output ALAW\n"
71 " -ima-adpcm : force the output to IMA ADPCM (WAV only)\n"
72 " -ms-adpcm : force the output to MS ADPCM (WAV only)\n"
73 " -gsm610 : force the GSM6.10 (WAV only)\n"
74 " -dwvw12 : force the output to 12 bit DWVW (AIFF only)\n"
75 " -dwvw16 : force the output to 16 bit DWVW (AIFF only)\n"
76 " -dwvw24 : force the output to 24 bit DWVW (AIFF only)\n"
77 " -vorbis : force the output to Vorbis (OGG only)\n"
78 ) ;
79
80 puts (
81 " The format of the output file is determined by the file extension of the\n"
82 " output file name. The following extensions are currently understood:\n"
83 ) ;
84
85 sfe_dump_format_map () ;
86
87 puts ("") ;
88 exit (0) ;
89 } /* usage_exit */
90
91 int
92 main (int argc, char * argv [])
93 { const char *progname, *infilename, *outfilename ;
94 SNDFILE *infile = NULL, *outfile = NULL ;
95 SF_INFO sfinfo ;
96 int k, outfilemajor, outfileminor = 0, infileminor ;
97 int override_sample_rate = 0 ; /* assume no sample rate override. */
98
99 progname = program_name (argv [0]) ;
100
101 if (argc < 3 || argc > 5)
102 { usage_exit (progname) ;
103 return 1 ;
104 } ;
105
106 infilename = argv [argc-2] ;
107 outfilename = argv [argc-1] ;
108
109 if (strcmp (infilename, outfilename) == 0)
110 { printf ("Error : Input and output filenames are the same.\n\n") ;
111 usage_exit (progname) ;
112 return 1 ;
113 } ;
114
115 if (strlen (infilename) > 1 && infilename [0] == '-')
116 { printf ("Error : Input filename (%s) looks like an option.\n\n", infilename) ;
117 usage_exit (progname) ;
118 return 1 ;
119 } ;
120
121 if (outfilename [0] == '-')
122 { printf ("Error : Output filename (%s) looks like an option.\n\n", outfilename) ;
123 usage_exit (progname) ;
124 return 1 ;
125 } ;
126
127 for (k = 1 ; k < argc - 2 ; k++)
128 { if (! strcmp (argv [k], "-pcms8"))
129 { outfileminor = SF_FORMAT_PCM_S8 ;
130 continue ;
131 } ;
132 if (! strcmp (argv [k], "-pcmu8"))
133 { outfileminor = SF_FORMAT_PCM_U8 ;
134 continue ;
135 } ;
136 if (! strcmp (argv [k], "-pcm16"))
137 { outfileminor = SF_FORMAT_PCM_16 ;
138 continue ;
139 } ;
140 if (! strcmp (argv [k], "-pcm24"))
141 { outfileminor = SF_FORMAT_PCM_24 ;
142 continue ;
143 } ;
144 if (! strcmp (argv [k], "-pcm32"))
145 { outfileminor = SF_FORMAT_PCM_32 ;
146 continue ;
147 } ;
148 if (! strcmp (argv [k], "-float32"))
149 { outfileminor = SF_FORMAT_FLOAT ;
150 continue ;
151 } ;
152 if (! strcmp (argv [k], "-ulaw"))
153 { outfileminor = SF_FORMAT_ULAW ;
154 continue ;
155 } ;
156 if (! strcmp (argv [k], "-alaw"))
157 { outfileminor = SF_FORMAT_ALAW ;
158 continue ;
159 } ;
160 if (! strcmp (argv [k], "-ima-adpcm"))
161 { outfileminor = SF_FORMAT_IMA_ADPCM ;
162 continue ;
163 } ;
164 if (! strcmp (argv [k], "-ms-adpcm"))
165 { outfileminor = SF_FORMAT_MS_ADPCM ;
166 continue ;
167 } ;
168 if (! strcmp (argv [k], "-gsm610"))
169 { outfileminor = SF_FORMAT_GSM610 ;
170 continue ;
171 } ;
172 if (! strcmp (argv [k], "-dwvw12"))
173 { outfileminor = SF_FORMAT_DWVW_12 ;
174 continue ;
175 } ;
176 if (! strcmp (argv [k], "-dwvw16"))
177 { outfileminor = SF_FORMAT_DWVW_16 ;
178 continue ;
179 } ;
180 if (! strcmp (argv [k], "-dwvw24"))
181 { outfileminor = SF_FORMAT_DWVW_24 ;
182 continue ;
183 } ;
184 if (! strcmp (argv [k], "-vorbis"))
185 { outfileminor = SF_FORMAT_VORBIS ;
186 continue ;
187 } ;
188
189 if (strstr (argv [k], "-override-sample-rate=") == argv [k])
190 { const char *ptr ;
191
192 ptr = argv [k] + strlen ("-override-sample-rate=") ;
193 override_sample_rate = atoi (ptr) ;
194 continue ;
195 } ;
196
197 printf ("Error : Not able to decode argunment '%s'.\n", argv [k]) ;
198 exit (1) ;
199 } ;
200
201 memset (&sfinfo, 0, sizeof (sfinfo)) ;
202
203 if ((infile = sf_open (infilename, SFM_READ, &sfinfo)) == NULL)
204 { printf ("Not able to open input file %s.\n", infilename) ;
205 puts (sf_strerror (NULL)) ;
206 return 1 ;
207 } ;
208
209 /* Update sample rate if forced to something else. */
210 if (override_sample_rate)
211 sfinfo.samplerate = override_sample_rate ;
212
213 infileminor = sfinfo.format & SF_FORMAT_SUBMASK ;
214
215 if ((sfinfo.format = sfe_file_type_of_ext (outfilename, sfinfo.format)) == 0)
216 { printf ("Error : Not able to determine output file type for %s.\n", outfilename) ;
217 return 1 ;
218 } ;
219
220 outfilemajor = sfinfo.format & (SF_FORMAT_TYPEMASK | SF_FORMAT_ENDMASK) ;
221
222 if (outfileminor == 0)
223 outfileminor = sfinfo.format & SF_FORMAT_SUBMASK ;
224
225 if (outfileminor != 0)
226 sfinfo.format = outfilemajor | outfileminor ;
227 else
228 sfinfo.format = outfilemajor | (sfinfo.format & SF_FORMAT_SUBMASK) ;
229
230 if ((sfinfo.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_XI)
231 switch (sfinfo.format & SF_FORMAT_SUBMASK)
232 { case SF_FORMAT_PCM_16 :
233 sfinfo.format = outfilemajor | SF_FORMAT_DPCM_16 ;
234 break ;
235
236 case SF_FORMAT_PCM_S8 :
237 case SF_FORMAT_PCM_U8 :
238 sfinfo.format = outfilemajor | SF_FORMAT_DPCM_8 ;
239 break ;
240 } ;
241
242 if (sf_format_check (&sfinfo) == 0)
243 { printf ("Error : output file format is invalid (0x%08X).\n", sfinfo.format) ;
244 return 1 ;
245 } ;
246
247 /* Open the output file. */
248 if ((outfile = sf_open (outfilename, SFM_WRITE, &sfinfo)) == NULL)
249 { printf ("Not able to open output file %s : %s\n", outfilename, sf_strerror (NULL)) ;
250 return 1 ;
251 } ;
252
253 /* Copy the metadata */
254 copy_metadata (outfile, infile, sfinfo.channels) ;
255
256 if ((outfileminor == SF_FORMAT_DOUBLE) || (outfileminor == SF_FORMAT_FLOAT)
257 || (infileminor == SF_FORMAT_DOUBLE) || (infileminor == SF_FORMAT_FLOAT)
258 || (infileminor == SF_FORMAT_VORBIS) || (outfileminor == SF_FORMAT_VORBIS))
259 sfe_copy_data_fp (outfile, infile, sfinfo.channels) ;
260 else
261 sfe_copy_data_int (outfile, infile, sfinfo.channels) ;
262
263 sf_close (infile) ;
264 sf_close (outfile) ;
265
266 return 0 ;
267 } /* main */
268
269 static void
270 copy_metadata (SNDFILE *outfile, SNDFILE *infile, int channels)
271 { SF_INSTRUMENT inst ;
272 SF_BROADCAST_INFO_2K binfo ;
273 const char *str ;
274 int k, chanmap [256] ;
275
276 for (k = SF_STR_FIRST ; k <= SF_STR_LAST ; k++)
277 { str = sf_get_string (infile, k) ;
278 if (str != NULL)
279 sf_set_string (outfile, k, str) ;
280 } ;
281
282 memset (&inst, 0, sizeof (inst)) ;
283 memset (&binfo, 0, sizeof (binfo)) ;
284
285 if (channels < ARRAY_LEN (chanmap))
286 { size_t size = channels * sizeof (chanmap [0]) ;
287
288 if (sf_command (infile, SFC_GET_CHANNEL_MAP_INFO, chanmap, size) == SF_TRUE)
289 sf_command (outfile, SFC_SET_CHANNEL_MAP_INFO, chanmap, size) ;
290 } ;
291
292 if (sf_command (infile, SFC_GET_INSTRUMENT, &inst, sizeof (inst)) == SF_TRUE)
293 sf_command (outfile, SFC_SET_INSTRUMENT, &inst, sizeof (inst)) ;
294
295 if (sf_command (infile, SFC_GET_BROADCAST_INFO, &binfo, sizeof (binfo)) == SF_TRUE)
296 sf_command (outfile, SFC_SET_BROADCAST_INFO, &binfo, sizeof (binfo)) ;
297
298 } /* copy_metadata */
299