Mercurial > hg > sv-dependency-builds
comparison src/libsamplerate-0.1.8/examples/sndfile-resample.c @ 0:c7265573341e
Import initial set of sources
author | Chris Cannam |
---|---|
date | Mon, 18 Mar 2013 14:12:14 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c7265573341e |
---|---|
1 /* | |
2 ** Copyright (C) 2002-2011 Erik de Castro Lopo <erikd@mega-nerd.com> | |
3 ** | |
4 ** This program is free software; you can redistribute it and/or modify | |
5 ** it under the terms of the GNU General Public License as published by | |
6 ** the Free Software Foundation; either version 2 of the License, or | |
7 ** (at your option) any later version. | |
8 ** | |
9 ** This program is distributed in the hope that it will be useful, | |
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 ** GNU General Public License for more details. | |
13 ** | |
14 ** You should have received a copy of the GNU General Public License | |
15 ** along with this program; if not, write to the Free Software | |
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. | |
17 */ | |
18 | |
19 #include "config.h" | |
20 | |
21 #include <stdio.h> | |
22 #include <stdlib.h> | |
23 #include <unistd.h> | |
24 #include <string.h> | |
25 #include <math.h> | |
26 | |
27 #if (HAVE_SNDFILE) | |
28 | |
29 #include <samplerate.h> | |
30 #include <sndfile.h> | |
31 | |
32 #define DEFAULT_CONVERTER SRC_SINC_MEDIUM_QUALITY | |
33 | |
34 #define BUFFER_LEN 4096 /*-(1<<16)-*/ | |
35 | |
36 static void usage_exit (const char *progname) ; | |
37 static sf_count_t sample_rate_convert (SNDFILE *infile, SNDFILE *outfile, int converter, double src_ratio, int channels, double * gain) ; | |
38 static double apply_gain (float * data, long frames, int channels, double max, double gain) ; | |
39 | |
40 int | |
41 main (int argc, char *argv []) | |
42 { SNDFILE *infile, *outfile = NULL ; | |
43 SF_INFO sfinfo ; | |
44 | |
45 sf_count_t count ; | |
46 double src_ratio = -1.0, gain = 1.0 ; | |
47 int new_sample_rate = -1, k, converter, max_speed = SF_FALSE ; | |
48 | |
49 if (argc == 2 && strcmp (argv [1], "--version") == 0) | |
50 { char buffer [64], *cptr ; | |
51 | |
52 if ((cptr = strrchr (argv [0], '/')) != NULL) | |
53 argv [0] = cptr + 1 ; | |
54 if ((cptr = strrchr (argv [0], '\\')) != NULL) | |
55 argv [0] = cptr + 1 ; | |
56 | |
57 sf_command (NULL, SFC_GET_LIB_VERSION, buffer, sizeof (buffer)) ; | |
58 | |
59 printf ("%s (%s,%s)\n", argv [0], src_get_version (), buffer) ; | |
60 exit (0) ; | |
61 } ; | |
62 | |
63 if (argc != 5 && argc != 7 && argc != 8) | |
64 usage_exit (argv [0]) ; | |
65 | |
66 /* Set default converter. */ | |
67 converter = DEFAULT_CONVERTER ; | |
68 | |
69 for (k = 1 ; k < argc - 2 ; k++) | |
70 { if (strcmp (argv [k], "--max-speed") == 0) | |
71 max_speed = SF_TRUE ; | |
72 else if (strcmp (argv [k], "-to") == 0) | |
73 { k ++ ; | |
74 new_sample_rate = atoi (argv [k]) ; | |
75 } | |
76 else if (strcmp (argv [k], "-by") == 0) | |
77 { k ++ ; | |
78 src_ratio = atof (argv [k]) ; | |
79 } | |
80 else if (strcmp (argv [k], "-c") == 0) | |
81 { k ++ ; | |
82 converter = atoi (argv [k]) ; | |
83 } | |
84 else | |
85 usage_exit (argv [0]) ; | |
86 } ; | |
87 | |
88 if (new_sample_rate <= 0 && src_ratio <= 0.0) | |
89 usage_exit (argv [0]) ; | |
90 | |
91 if (src_get_name (converter) == NULL) | |
92 { printf ("Error : bad converter number.\n") ; | |
93 usage_exit (argv [0]) ; | |
94 } ; | |
95 | |
96 if (strcmp (argv [argc - 2], argv [argc - 1]) == 0) | |
97 { printf ("Error : input and output file names are the same.\n") ; | |
98 exit (1) ; | |
99 } ; | |
100 | |
101 if ((infile = sf_open (argv [argc - 2], SFM_READ, &sfinfo)) == NULL) | |
102 { printf ("Error : Not able to open input file '%s'\n", argv [argc - 2]) ; | |
103 exit (1) ; | |
104 } ; | |
105 | |
106 printf ("Input File : %s\n", argv [argc - 2]) ; | |
107 printf ("Sample Rate : %d\n", sfinfo.samplerate) ; | |
108 printf ("Input Frames : %ld\n\n", (long) sfinfo.frames) ; | |
109 | |
110 if (new_sample_rate > 0) | |
111 { src_ratio = (1.0 * new_sample_rate) / sfinfo.samplerate ; | |
112 sfinfo.samplerate = new_sample_rate ; | |
113 } | |
114 else if (src_is_valid_ratio (src_ratio)) | |
115 sfinfo.samplerate = (int) floor (sfinfo.samplerate * src_ratio) ; | |
116 else | |
117 { printf ("Not able to determine new sample rate. Exiting.\n") ; | |
118 sf_close (infile) ; | |
119 exit (1) ; | |
120 } ; | |
121 | |
122 if (fabs (src_ratio - 1.0) < 1e-20) | |
123 { printf ("Target samplerate and input samplerate are the same. Exiting.\n") ; | |
124 sf_close (infile) ; | |
125 exit (0) ; | |
126 } ; | |
127 | |
128 printf ("SRC Ratio : %f\n", src_ratio) ; | |
129 printf ("Converter : %s\n\n", src_get_name (converter)) ; | |
130 | |
131 if (src_is_valid_ratio (src_ratio) == 0) | |
132 { printf ("Error : Sample rate change out of valid range.\n") ; | |
133 sf_close (infile) ; | |
134 exit (1) ; | |
135 } ; | |
136 | |
137 /* Delete the output file length to zero if already exists. */ | |
138 remove (argv [argc - 1]) ; | |
139 | |
140 printf ("Output file : %s\n", argv [argc - 1]) ; | |
141 printf ("Sample Rate : %d\n", sfinfo.samplerate) ; | |
142 | |
143 do | |
144 { sf_close (outfile) ; | |
145 | |
146 if ((outfile = sf_open (argv [argc - 1], SFM_WRITE, &sfinfo)) == NULL) | |
147 { printf ("Error : Not able to open output file '%s'\n", argv [argc - 1]) ; | |
148 sf_close (infile) ; | |
149 exit (1) ; | |
150 } ; | |
151 | |
152 if (max_speed) | |
153 { /* This is mainly for the comparison program tests/src-evaluate.c */ | |
154 sf_command (outfile, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ; | |
155 } | |
156 else | |
157 { /* Update the file header after every write. */ | |
158 sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ; | |
159 } ; | |
160 | |
161 sf_command (outfile, SFC_SET_CLIPPING, NULL, SF_TRUE) ; | |
162 | |
163 count = sample_rate_convert (infile, outfile, converter, src_ratio, sfinfo.channels, &gain) ; | |
164 } | |
165 while (count < 0) ; | |
166 | |
167 printf ("Output Frames : %ld\n\n", (long) count) ; | |
168 | |
169 sf_close (infile) ; | |
170 sf_close (outfile) ; | |
171 | |
172 return 0 ; | |
173 } /* main */ | |
174 | |
175 /*============================================================================== | |
176 */ | |
177 | |
178 static sf_count_t | |
179 sample_rate_convert (SNDFILE *infile, SNDFILE *outfile, int converter, double src_ratio, int channels, double * gain) | |
180 { static float input [BUFFER_LEN] ; | |
181 static float output [BUFFER_LEN] ; | |
182 | |
183 SRC_STATE *src_state ; | |
184 SRC_DATA src_data ; | |
185 int error ; | |
186 double max = 0.0 ; | |
187 sf_count_t output_count = 0 ; | |
188 | |
189 sf_seek (infile, 0, SEEK_SET) ; | |
190 sf_seek (outfile, 0, SEEK_SET) ; | |
191 | |
192 /* Initialize the sample rate converter. */ | |
193 if ((src_state = src_new (converter, channels, &error)) == NULL) | |
194 { printf ("\n\nError : src_new() failed : %s.\n\n", src_strerror (error)) ; | |
195 exit (1) ; | |
196 } ; | |
197 | |
198 src_data.end_of_input = 0 ; /* Set this later. */ | |
199 | |
200 /* Start with zero to force load in while loop. */ | |
201 src_data.input_frames = 0 ; | |
202 src_data.data_in = input ; | |
203 | |
204 src_data.src_ratio = src_ratio ; | |
205 | |
206 src_data.data_out = output ; | |
207 src_data.output_frames = BUFFER_LEN /channels ; | |
208 | |
209 while (1) | |
210 { | |
211 /* If the input buffer is empty, refill it. */ | |
212 if (src_data.input_frames == 0) | |
213 { src_data.input_frames = sf_readf_float (infile, input, BUFFER_LEN / channels) ; | |
214 src_data.data_in = input ; | |
215 | |
216 /* The last read will not be a full buffer, so snd_of_input. */ | |
217 if (src_data.input_frames < BUFFER_LEN / channels) | |
218 src_data.end_of_input = SF_TRUE ; | |
219 } ; | |
220 | |
221 if ((error = src_process (src_state, &src_data))) | |
222 { printf ("\nError : %s\n", src_strerror (error)) ; | |
223 exit (1) ; | |
224 } ; | |
225 | |
226 /* Terminate if done. */ | |
227 if (src_data.end_of_input && src_data.output_frames_gen == 0) | |
228 break ; | |
229 | |
230 max = apply_gain (src_data.data_out, src_data.output_frames_gen, channels, max, *gain) ; | |
231 | |
232 /* Write output. */ | |
233 sf_writef_float (outfile, output, src_data.output_frames_gen) ; | |
234 output_count += src_data.output_frames_gen ; | |
235 | |
236 src_data.data_in += src_data.input_frames_used * channels ; | |
237 src_data.input_frames -= src_data.input_frames_used ; | |
238 } ; | |
239 | |
240 src_state = src_delete (src_state) ; | |
241 | |
242 if (max > 1.0) | |
243 { *gain = 1.0 / max ; | |
244 printf ("\nOutput has clipped. Restarting conversion to prevent clipping.\n\n") ; | |
245 return -1 ; | |
246 } ; | |
247 | |
248 return output_count ; | |
249 } /* sample_rate_convert */ | |
250 | |
251 static double | |
252 apply_gain (float * data, long frames, int channels, double max, double gain) | |
253 { | |
254 long k ; | |
255 | |
256 for (k = 0 ; k < frames * channels ; k++) | |
257 { data [k] *= gain ; | |
258 | |
259 if (fabs (data [k]) > max) | |
260 max = fabs (data [k]) ; | |
261 } ; | |
262 | |
263 return max ; | |
264 } /* apply_gain */ | |
265 | |
266 static void | |
267 usage_exit (const char *progname) | |
268 { char lsf_ver [128] ; | |
269 const char *cptr ; | |
270 int k ; | |
271 | |
272 if ((cptr = strrchr (progname, '/')) != NULL) | |
273 progname = cptr + 1 ; | |
274 | |
275 if ((cptr = strrchr (progname, '\\')) != NULL) | |
276 progname = cptr + 1 ; | |
277 | |
278 | |
279 sf_command (NULL, SFC_GET_LIB_VERSION, lsf_ver, sizeof (lsf_ver)) ; | |
280 | |
281 printf ("\n" | |
282 " A Sample Rate Converter using libsndfile for file I/O and Secret \n" | |
283 " Rabbit Code (aka libsamplerate) for performing the conversion.\n" | |
284 " It works on any file format supported by libsndfile with any \n" | |
285 " number of channels (limited only by host memory).\n" | |
286 "\n" | |
287 " %s\n" | |
288 " %s\n" | |
289 "\n" | |
290 " Usage : \n" | |
291 " %s -to <new sample rate> [-c <number>] <input file> <output file>\n" | |
292 " %s -by <amount> [-c <number>] <input file> <output file>\n" | |
293 "\n", src_get_version (), lsf_ver, progname, progname) ; | |
294 | |
295 puts ( | |
296 " The optional -c argument allows the converter type to be chosen from\n" | |
297 " the following list :" | |
298 "\n" | |
299 ) ; | |
300 | |
301 for (k = 0 ; (cptr = src_get_name (k)) != NULL ; k++) | |
302 printf (" %d : %s%s\n", k, cptr, k == DEFAULT_CONVERTER ? " (default)" : "") ; | |
303 | |
304 puts ("") ; | |
305 | |
306 exit (1) ; | |
307 } /* usage_exit */ | |
308 | |
309 /*============================================================================== | |
310 */ | |
311 | |
312 #else /* (HAVE_SNFILE == 0) */ | |
313 | |
314 /* Alternative main function when libsndfile is not available. */ | |
315 | |
316 int | |
317 main (void) | |
318 { puts ( | |
319 "\n" | |
320 "****************************************************************\n" | |
321 " This example program was compiled without libsndfile \n" | |
322 " (http://www.mega-nerd.com/libsndfile/).\n" | |
323 " It is therefore completely broken and non-functional.\n" | |
324 "****************************************************************\n" | |
325 "\n" | |
326 ) ; | |
327 | |
328 return 0 ; | |
329 } /* main */ | |
330 | |
331 #endif | |
332 |