Chris@41
|
1 /*
|
Chris@41
|
2 ** Copyright (c) 2002-2016, Erik de Castro Lopo <erikd@mega-nerd.com>
|
Chris@41
|
3 ** All rights reserved.
|
Chris@41
|
4 **
|
Chris@41
|
5 ** This code is released under 2-clause BSD license. Please see the
|
Chris@41
|
6 ** file at : https://github.com/erikd/libsamplerate/blob/master/COPYING
|
Chris@41
|
7 */
|
Chris@41
|
8
|
Chris@41
|
9 #include <stdio.h>
|
Chris@41
|
10 #include <stdlib.h>
|
Chris@41
|
11 #include <unistd.h>
|
Chris@41
|
12 #include <string.h>
|
Chris@41
|
13 #include <ctype.h>
|
Chris@41
|
14
|
Chris@41
|
15 #include "config.h"
|
Chris@41
|
16
|
Chris@41
|
17 #if (HAVE_FFTW3 && HAVE_SNDFILE && HAVE_SYS_TIMES_H)
|
Chris@41
|
18
|
Chris@41
|
19 #include <time.h>
|
Chris@41
|
20 #include <sys/times.h>
|
Chris@41
|
21
|
Chris@41
|
22 #include <sndfile.h>
|
Chris@41
|
23 #include <math.h>
|
Chris@41
|
24 #include <sys/utsname.h>
|
Chris@41
|
25
|
Chris@41
|
26 #include "util.h"
|
Chris@41
|
27
|
Chris@41
|
28 #define MAX_FREQS 4
|
Chris@41
|
29 #define BUFFER_LEN 80000
|
Chris@41
|
30
|
Chris@41
|
31 #define SAFE_STRNCAT(dest,src,len) \
|
Chris@41
|
32 { int safe_strncat_count ; \
|
Chris@41
|
33 safe_strncat_count = (len) - strlen (dest) - 1 ; \
|
Chris@41
|
34 strncat ((dest), (src), safe_strncat_count) ; \
|
Chris@41
|
35 (dest) [(len) - 1] = 0 ; \
|
Chris@41
|
36 } ;
|
Chris@41
|
37
|
Chris@41
|
38 typedef struct
|
Chris@41
|
39 { int freq_count ;
|
Chris@41
|
40 double freqs [MAX_FREQS] ;
|
Chris@41
|
41
|
Chris@41
|
42 int output_samplerate ;
|
Chris@41
|
43 int pass_band_peaks ;
|
Chris@41
|
44
|
Chris@41
|
45 double peak_value ;
|
Chris@41
|
46 } SNR_TEST ;
|
Chris@41
|
47
|
Chris@41
|
48 typedef struct
|
Chris@41
|
49 { const char *progname ;
|
Chris@41
|
50 const char *version_cmd ;
|
Chris@41
|
51 const char *version_start ;
|
Chris@41
|
52 const char *convert_cmd ;
|
Chris@41
|
53 int format ;
|
Chris@41
|
54 } RESAMPLE_PROG ;
|
Chris@41
|
55
|
Chris@41
|
56 static char *get_progname (char *) ;
|
Chris@41
|
57 static void usage_exit (const char *, const RESAMPLE_PROG *prog, int count) ;
|
Chris@41
|
58 static void measure_program (const RESAMPLE_PROG *prog, int verbose) ;
|
Chris@41
|
59 static void generate_source_wav (const char *filename, const double *freqs, int freq_count, int format) ;
|
Chris@41
|
60 static const char* get_machine_details (void) ;
|
Chris@41
|
61
|
Chris@41
|
62 static char version_string [512] ;
|
Chris@41
|
63
|
Chris@41
|
64 int
|
Chris@41
|
65 main (int argc, char *argv [])
|
Chris@41
|
66 { static RESAMPLE_PROG resample_progs [] =
|
Chris@41
|
67 { { "sndfile-resample",
|
Chris@41
|
68 "examples/sndfile-resample --version",
|
Chris@41
|
69 "libsamplerate",
|
Chris@41
|
70 "examples/sndfile-resample --max-speed -c 0 -to %d source.wav destination.wav",
|
Chris@41
|
71 SF_FORMAT_WAV | SF_FORMAT_PCM_32
|
Chris@41
|
72 },
|
Chris@41
|
73 { "sox",
|
Chris@41
|
74 "sox -h 2>&1",
|
Chris@41
|
75 "sox",
|
Chris@41
|
76 "sox source.wav -r %d destination.wav resample 0.835",
|
Chris@41
|
77 SF_FORMAT_WAV | SF_FORMAT_PCM_32
|
Chris@41
|
78 },
|
Chris@41
|
79 { "ResampAudio",
|
Chris@41
|
80 "ResampAudio --version",
|
Chris@41
|
81 "ResampAudio",
|
Chris@41
|
82 "ResampAudio -f cutoff=0.41,atten=100,ratio=128 -s %d source.wav destination.wav",
|
Chris@41
|
83 SF_FORMAT_WAV | SF_FORMAT_PCM_32
|
Chris@41
|
84 },
|
Chris@41
|
85
|
Chris@41
|
86 /*-
|
Chris@41
|
87 { /+*
|
Chris@41
|
88 ** The Shibatch converter doesn't work for all combinations of
|
Chris@41
|
89 ** source and destination sample rates. Therefore it can't be
|
Chris@41
|
90 ** included in this test.
|
Chris@41
|
91 *+/
|
Chris@41
|
92 "shibatch",
|
Chris@41
|
93 "ssrc",
|
Chris@41
|
94 "Shibatch",
|
Chris@41
|
95 "ssrc --rate %d source.wav destination.wav",
|
Chris@41
|
96 SF_FORMAT_WAV | SF_FORMAT_PCM_32
|
Chris@41
|
97 },-*/
|
Chris@41
|
98
|
Chris@41
|
99 /*-
|
Chris@41
|
100 { /+*
|
Chris@41
|
101 ** The resample program is not able to match the bandwidth and SNR
|
Chris@41
|
102 ** specs or sndfile-resample and hence will not be tested.
|
Chris@41
|
103 *+/
|
Chris@41
|
104 "resample",
|
Chris@41
|
105 "resample -version",
|
Chris@41
|
106 "resample",
|
Chris@41
|
107 "resample -to %d source.wav destination.wav",
|
Chris@41
|
108 SF_FORMAT_WAV | SF_FORMAT_FLOAT
|
Chris@41
|
109 },-*/
|
Chris@41
|
110
|
Chris@41
|
111 /*-
|
Chris@41
|
112 { "mplayer",
|
Chris@41
|
113 "mplayer -v 2>&1",
|
Chris@41
|
114 "MPlayer ",
|
Chris@41
|
115 "mplayer -ao pcm -srate %d source.wav >/dev/null 2>&1 && mv audiodump.wav destination.wav",
|
Chris@41
|
116 SF_FORMAT_WAV | SF_FORMAT_PCM_32
|
Chris@41
|
117 },-*/
|
Chris@41
|
118
|
Chris@41
|
119 } ; /* resample_progs */
|
Chris@41
|
120
|
Chris@41
|
121 char *progname ;
|
Chris@41
|
122 int prog = 0, verbose = 0 ;
|
Chris@41
|
123
|
Chris@41
|
124 progname = get_progname (argv [0]) ;
|
Chris@41
|
125
|
Chris@41
|
126 printf ("\n %s : evaluate a sample rate converter.\n", progname) ;
|
Chris@41
|
127
|
Chris@41
|
128 if (argc == 3 && strcmp ("--verbose", argv [1]) == 0)
|
Chris@41
|
129 { verbose = 1 ;
|
Chris@41
|
130 prog = atoi (argv [2]) ;
|
Chris@41
|
131 }
|
Chris@41
|
132 else if (argc == 2)
|
Chris@41
|
133 { verbose = 0 ;
|
Chris@41
|
134 prog = atoi (argv [1]) ;
|
Chris@41
|
135 }
|
Chris@41
|
136 else
|
Chris@41
|
137 usage_exit (progname, resample_progs, ARRAY_LEN (resample_progs)) ;
|
Chris@41
|
138
|
Chris@41
|
139 if (prog < 0 || prog >= ARRAY_LEN (resample_progs))
|
Chris@41
|
140 usage_exit (progname, resample_progs, ARRAY_LEN (resample_progs)) ;
|
Chris@41
|
141
|
Chris@41
|
142 measure_program (& (resample_progs [prog]), verbose) ;
|
Chris@41
|
143
|
Chris@41
|
144 puts ("") ;
|
Chris@41
|
145
|
Chris@41
|
146 return 0 ;
|
Chris@41
|
147 } /* main */
|
Chris@41
|
148
|
Chris@41
|
149 /*==============================================================================
|
Chris@41
|
150 */
|
Chris@41
|
151
|
Chris@41
|
152 static char *
|
Chris@41
|
153 get_progname (char *progname)
|
Chris@41
|
154 { char *cptr ;
|
Chris@41
|
155
|
Chris@41
|
156 if ((cptr = strrchr (progname, '/')) != NULL)
|
Chris@41
|
157 progname = cptr + 1 ;
|
Chris@41
|
158
|
Chris@41
|
159 if ((cptr = strrchr (progname, '\\')) != NULL)
|
Chris@41
|
160 progname = cptr + 1 ;
|
Chris@41
|
161
|
Chris@41
|
162 return progname ;
|
Chris@41
|
163 } /* get_progname */
|
Chris@41
|
164
|
Chris@41
|
165 static void
|
Chris@41
|
166 usage_exit (const char *progname, const RESAMPLE_PROG *prog, int count)
|
Chris@41
|
167 { int k ;
|
Chris@41
|
168
|
Chris@41
|
169 printf ("\n Usage : %s <number>\n\n", progname) ;
|
Chris@41
|
170
|
Chris@41
|
171 puts (" where <number> specifies the program to test:\n") ;
|
Chris@41
|
172
|
Chris@41
|
173 for (k = 0 ; k < count ; k++)
|
Chris@41
|
174 printf (" %d : %s\n", k, prog [k].progname) ;
|
Chris@41
|
175
|
Chris@41
|
176 puts ("\n"
|
Chris@41
|
177 " Obviously to test a given program you have to have it available on\n"
|
Chris@41
|
178 " your system. See http://www.mega-nerd.com/SRC/quality.html for\n"
|
Chris@41
|
179 " the download location of these programs.\n") ;
|
Chris@41
|
180
|
Chris@41
|
181 exit (1) ;
|
Chris@41
|
182 } /* usage_exit */
|
Chris@41
|
183
|
Chris@41
|
184 static const char*
|
Chris@41
|
185 get_machine_details (void)
|
Chris@41
|
186 { static char namestr [256] ;
|
Chris@41
|
187
|
Chris@41
|
188 struct utsname name ;
|
Chris@41
|
189
|
Chris@41
|
190 if (uname (&name) != 0)
|
Chris@41
|
191 { snprintf (namestr, sizeof (namestr), "Unknown") ;
|
Chris@41
|
192 return namestr ;
|
Chris@41
|
193 } ;
|
Chris@41
|
194
|
Chris@41
|
195 snprintf (namestr, sizeof (namestr), "%s (%s %s %s)", name.nodename,
|
Chris@41
|
196 name.machine, name.sysname, name.release) ;
|
Chris@41
|
197
|
Chris@41
|
198 return namestr ;
|
Chris@41
|
199 } /* get_machine_details */
|
Chris@41
|
200
|
Chris@41
|
201
|
Chris@41
|
202 /*==============================================================================
|
Chris@41
|
203 */
|
Chris@41
|
204
|
Chris@41
|
205 static void
|
Chris@41
|
206 get_version_string (const RESAMPLE_PROG *prog)
|
Chris@41
|
207 { FILE *file ;
|
Chris@41
|
208 char *cptr ;
|
Chris@41
|
209
|
Chris@41
|
210 /* Default. */
|
Chris@41
|
211 snprintf (version_string, sizeof (version_string), "no version") ;
|
Chris@41
|
212
|
Chris@41
|
213 if (prog->version_cmd == NULL)
|
Chris@41
|
214 return ;
|
Chris@41
|
215
|
Chris@41
|
216 if ((file = popen (prog->version_cmd, "r")) == NULL)
|
Chris@41
|
217 return ;
|
Chris@41
|
218
|
Chris@41
|
219 while ((cptr = fgets (version_string, sizeof (version_string), file)) != NULL)
|
Chris@41
|
220 {
|
Chris@41
|
221 if (strstr (cptr, prog->version_start) != NULL)
|
Chris@41
|
222 break ;
|
Chris@41
|
223
|
Chris@41
|
224 version_string [0] = 0 ;
|
Chris@41
|
225 } ;
|
Chris@41
|
226
|
Chris@41
|
227 pclose (file) ;
|
Chris@41
|
228
|
Chris@41
|
229 /* Remove trailing newline. */
|
Chris@41
|
230 if ((cptr = strchr (version_string, '\n')) != NULL)
|
Chris@41
|
231 cptr [0] = 0 ;
|
Chris@41
|
232
|
Chris@41
|
233 /* Remove leading whitespace from version string. */
|
Chris@41
|
234 cptr = version_string ;
|
Chris@41
|
235 while (cptr [0] != 0 && isspace (cptr [0]))
|
Chris@41
|
236 cptr ++ ;
|
Chris@41
|
237
|
Chris@41
|
238 if (cptr != version_string)
|
Chris@41
|
239 strncpy (version_string, cptr, sizeof (version_string)) ;
|
Chris@41
|
240
|
Chris@41
|
241 return ;
|
Chris@41
|
242 } /* get_version_string */
|
Chris@41
|
243
|
Chris@41
|
244 static void
|
Chris@41
|
245 generate_source_wav (const char *filename, const double *freqs, int freq_count, int format)
|
Chris@41
|
246 { static float buffer [BUFFER_LEN] ;
|
Chris@41
|
247
|
Chris@41
|
248 SNDFILE *sndfile ;
|
Chris@41
|
249 SF_INFO sfinfo ;
|
Chris@41
|
250
|
Chris@41
|
251 sfinfo.channels = 1 ;
|
Chris@41
|
252 sfinfo.samplerate = 44100 ;
|
Chris@41
|
253 sfinfo.format = format ;
|
Chris@41
|
254
|
Chris@41
|
255 if ((sndfile = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL)
|
Chris@41
|
256 { printf ("Line %d : cound not open '%s' : %s\n", __LINE__, filename, sf_strerror (NULL)) ;
|
Chris@41
|
257 exit (1) ;
|
Chris@41
|
258 } ;
|
Chris@41
|
259
|
Chris@41
|
260 sf_command (sndfile, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;
|
Chris@41
|
261
|
Chris@41
|
262 gen_windowed_sines (freq_count, freqs, 0.9, buffer, ARRAY_LEN (buffer)) ;
|
Chris@41
|
263
|
Chris@41
|
264 if (sf_write_float (sndfile, buffer, ARRAY_LEN (buffer)) != ARRAY_LEN (buffer))
|
Chris@41
|
265 { printf ("Line %d : sf_write_float short write.\n", __LINE__) ;
|
Chris@41
|
266 exit (1) ;
|
Chris@41
|
267 } ;
|
Chris@41
|
268
|
Chris@41
|
269 sf_close (sndfile) ;
|
Chris@41
|
270 } /* generate_source_wav */
|
Chris@41
|
271
|
Chris@41
|
272 static double
|
Chris@41
|
273 measure_destination_wav (char *filename, int *output_samples, int expected_peaks)
|
Chris@41
|
274 { static float buffer [250000] ;
|
Chris@41
|
275
|
Chris@41
|
276 SNDFILE *sndfile ;
|
Chris@41
|
277 SF_INFO sfinfo ;
|
Chris@41
|
278 double snr ;
|
Chris@41
|
279
|
Chris@41
|
280 if ((sndfile = sf_open (filename, SFM_READ, &sfinfo)) == NULL)
|
Chris@41
|
281 { printf ("Line %d : Cound not open '%s' : %s\n", __LINE__, filename, sf_strerror (NULL)) ;
|
Chris@41
|
282 exit (1) ;
|
Chris@41
|
283 } ;
|
Chris@41
|
284
|
Chris@41
|
285 if (sfinfo.channels != 1)
|
Chris@41
|
286 { printf ("Line %d : Bad channel count (%d). Should be 1.\n", __LINE__, sfinfo.channels) ;
|
Chris@41
|
287 exit (1) ;
|
Chris@41
|
288 } ;
|
Chris@41
|
289
|
Chris@41
|
290 if (sfinfo.frames > ARRAY_LEN (buffer))
|
Chris@41
|
291 { printf ("Line %d : Too many frames (%ld) of data in file.\n", __LINE__, (long) sfinfo.frames) ;
|
Chris@41
|
292 exit (1) ;
|
Chris@41
|
293 } ;
|
Chris@41
|
294
|
Chris@41
|
295 *output_samples = (int) sfinfo.frames ;
|
Chris@41
|
296
|
Chris@41
|
297 if (sf_read_float (sndfile, buffer, sfinfo.frames) != sfinfo.frames)
|
Chris@41
|
298 { printf ("Line %d : Bad read.\n", __LINE__) ;
|
Chris@41
|
299 exit (1) ;
|
Chris@41
|
300 } ;
|
Chris@41
|
301
|
Chris@41
|
302 sf_close (sndfile) ;
|
Chris@41
|
303
|
Chris@41
|
304 snr = calculate_snr (buffer, sfinfo.frames, expected_peaks) ;
|
Chris@41
|
305
|
Chris@41
|
306 return snr ;
|
Chris@41
|
307 } /* measure_desination_wav */
|
Chris@41
|
308
|
Chris@41
|
309 static double
|
Chris@41
|
310 measure_snr (const RESAMPLE_PROG *prog, int *output_samples, int verbose)
|
Chris@41
|
311 { static SNR_TEST snr_test [] =
|
Chris@41
|
312 {
|
Chris@41
|
313 { 1, { 0.211111111111 }, 48000, 1, 1.0 },
|
Chris@41
|
314 { 1, { 0.011111111111 }, 132301, 1, 1.0 },
|
Chris@41
|
315 { 1, { 0.111111111111 }, 92301, 1, 1.0 },
|
Chris@41
|
316 { 1, { 0.011111111111 }, 26461, 1, 1.0 },
|
Chris@41
|
317 { 1, { 0.011111111111 }, 13231, 1, 1.0 },
|
Chris@41
|
318 { 1, { 0.011111111111 }, 44101, 1, 1.0 },
|
Chris@41
|
319 { 2, { 0.311111, 0.49 }, 78199, 2, 1.0 },
|
Chris@41
|
320 { 2, { 0.011111, 0.49 }, 12345, 1, 0.5 },
|
Chris@41
|
321 { 2, { 0.0123456, 0.4 }, 20143, 1, 0.5 },
|
Chris@41
|
322 { 2, { 0.0111111, 0.4 }, 26461, 1, 0.5 },
|
Chris@41
|
323 { 1, { 0.381111111111 }, 58661, 1, 1.0 }
|
Chris@41
|
324 } ; /* snr_test */
|
Chris@41
|
325 static char command [256] ;
|
Chris@41
|
326
|
Chris@41
|
327 double snr, worst_snr = 500.0 ;
|
Chris@41
|
328 int k , retval, sample_count ;
|
Chris@41
|
329
|
Chris@41
|
330 *output_samples = 0 ;
|
Chris@41
|
331
|
Chris@41
|
332 for (k = 0 ; k < ARRAY_LEN (snr_test) ; k++)
|
Chris@41
|
333 { remove ("source.wav") ;
|
Chris@41
|
334 remove ("destination.wav") ;
|
Chris@41
|
335
|
Chris@41
|
336 if (verbose)
|
Chris@41
|
337 printf (" SNR test #%d : ", k) ;
|
Chris@41
|
338 fflush (stdout) ;
|
Chris@41
|
339 generate_source_wav ("source.wav", snr_test [k].freqs, snr_test [k].freq_count, prog->format) ;
|
Chris@41
|
340
|
Chris@41
|
341 snprintf (command, sizeof (command), prog->convert_cmd, snr_test [k].output_samplerate) ;
|
Chris@41
|
342 SAFE_STRNCAT (command, " >/dev/null 2>&1", sizeof (command)) ;
|
Chris@41
|
343 if ((retval = system (command)) != 0)
|
Chris@41
|
344 printf ("system returned %d\n", retval) ;
|
Chris@41
|
345
|
Chris@41
|
346 snr = measure_destination_wav ("destination.wav", &sample_count, snr_test->pass_band_peaks) ;
|
Chris@41
|
347
|
Chris@41
|
348 *output_samples += sample_count ;
|
Chris@41
|
349
|
Chris@41
|
350 if (fabs (snr) < fabs (worst_snr))
|
Chris@41
|
351 worst_snr = fabs (snr) ;
|
Chris@41
|
352
|
Chris@41
|
353 if (verbose)
|
Chris@41
|
354 printf ("%6.2f dB\n", snr) ;
|
Chris@41
|
355 } ;
|
Chris@41
|
356
|
Chris@41
|
357 return worst_snr ;
|
Chris@41
|
358 } /* measure_snr */
|
Chris@41
|
359
|
Chris@41
|
360 /*------------------------------------------------------------------------------
|
Chris@41
|
361 */
|
Chris@41
|
362
|
Chris@41
|
363 static double
|
Chris@41
|
364 measure_destination_peak (const char *filename)
|
Chris@41
|
365 { static float data [2 * BUFFER_LEN] ;
|
Chris@41
|
366 SNDFILE *sndfile ;
|
Chris@41
|
367 SF_INFO sfinfo ;
|
Chris@41
|
368 double peak = 0.0 ;
|
Chris@41
|
369 int k = 0 ;
|
Chris@41
|
370
|
Chris@41
|
371 if ((sndfile = sf_open (filename, SFM_READ, &sfinfo)) == NULL)
|
Chris@41
|
372 { printf ("Line %d : failed to open file %s\n", __LINE__, filename) ;
|
Chris@41
|
373 exit (1) ;
|
Chris@41
|
374 } ;
|
Chris@41
|
375
|
Chris@41
|
376 if (sfinfo.channels != 1)
|
Chris@41
|
377 { printf ("Line %d : bad channel count.\n", __LINE__) ;
|
Chris@41
|
378 exit (1) ;
|
Chris@41
|
379 } ;
|
Chris@41
|
380
|
Chris@41
|
381 if (sfinfo.frames > ARRAY_LEN (data) + 4 || sfinfo.frames < ARRAY_LEN (data) - 100)
|
Chris@41
|
382 { printf ("Line %d : bad frame count (got %d, expected %d).\n", __LINE__, (int) sfinfo.frames, ARRAY_LEN (data)) ;
|
Chris@41
|
383 exit (1) ;
|
Chris@41
|
384 } ;
|
Chris@41
|
385
|
Chris@41
|
386 if (sf_read_float (sndfile, data, sfinfo.frames) != sfinfo.frames)
|
Chris@41
|
387 { printf ("Line %d : bad read.\n", __LINE__) ;
|
Chris@41
|
388 exit (1) ;
|
Chris@41
|
389 } ;
|
Chris@41
|
390
|
Chris@41
|
391 sf_close (sndfile) ;
|
Chris@41
|
392
|
Chris@41
|
393 for (k = 0 ; k < (int) sfinfo.frames ; k++)
|
Chris@41
|
394 if (fabs (data [k]) > peak)
|
Chris@41
|
395 peak = fabs (data [k]) ;
|
Chris@41
|
396
|
Chris@41
|
397 return peak ;
|
Chris@41
|
398 } /* measure_destination_peak */
|
Chris@41
|
399
|
Chris@41
|
400 static double
|
Chris@41
|
401 find_attenuation (double freq, const RESAMPLE_PROG *prog, int verbose)
|
Chris@41
|
402 { static char command [256] ;
|
Chris@41
|
403 double output_peak ;
|
Chris@41
|
404 int retval ;
|
Chris@41
|
405 char *filename ;
|
Chris@41
|
406
|
Chris@41
|
407 filename = "destination.wav" ;
|
Chris@41
|
408
|
Chris@41
|
409 generate_source_wav ("source.wav", &freq, 1, prog->format) ;
|
Chris@41
|
410
|
Chris@41
|
411 remove (filename) ;
|
Chris@41
|
412
|
Chris@41
|
413 snprintf (command, sizeof (command), prog->convert_cmd, 88189) ;
|
Chris@41
|
414 SAFE_STRNCAT (command, " >/dev/null 2>&1", sizeof (command)) ;
|
Chris@41
|
415 if ((retval = system (command)) != 0)
|
Chris@41
|
416 printf ("system returned %d\n", retval) ;
|
Chris@41
|
417
|
Chris@41
|
418 output_peak = measure_destination_peak (filename) ;
|
Chris@41
|
419
|
Chris@41
|
420 if (verbose)
|
Chris@41
|
421 printf (" freq : %f peak : %f\n", freq, output_peak) ;
|
Chris@41
|
422
|
Chris@41
|
423 return fabs (20.0 * log10 (output_peak)) ;
|
Chris@41
|
424 } /* find_attenuation */
|
Chris@41
|
425
|
Chris@41
|
426 static double
|
Chris@41
|
427 bandwidth_test (const RESAMPLE_PROG *prog, int verbose)
|
Chris@41
|
428 { double f1, f2, a1, a2 ;
|
Chris@41
|
429 double freq, atten ;
|
Chris@41
|
430
|
Chris@41
|
431 f1 = 0.35 ;
|
Chris@41
|
432 a1 = find_attenuation (f1, prog, verbose) ;
|
Chris@41
|
433
|
Chris@41
|
434 f2 = 0.49999 ;
|
Chris@41
|
435 a2 = find_attenuation (f2, prog, verbose) ;
|
Chris@41
|
436
|
Chris@41
|
437
|
Chris@41
|
438 if (fabs (a1) < 1e-2 && a2 < 3.0)
|
Chris@41
|
439 return -1.0 ;
|
Chris@41
|
440
|
Chris@41
|
441 if (a1 > 3.0 || a2 < 3.0)
|
Chris@41
|
442 { printf ("\n\nLine %d : cannot bracket 3dB point.\n\n", __LINE__) ;
|
Chris@41
|
443 exit (1) ;
|
Chris@41
|
444 } ;
|
Chris@41
|
445
|
Chris@41
|
446 while (a2 - a1 > 1.0)
|
Chris@41
|
447 { freq = f1 + 0.5 * (f2 - f1) ;
|
Chris@41
|
448 atten = find_attenuation (freq, prog, verbose) ;
|
Chris@41
|
449
|
Chris@41
|
450 if (atten < 3.0)
|
Chris@41
|
451 { f1 = freq ;
|
Chris@41
|
452 a1 = atten ;
|
Chris@41
|
453 }
|
Chris@41
|
454 else
|
Chris@41
|
455 { f2 = freq ;
|
Chris@41
|
456 a2 = atten ;
|
Chris@41
|
457 } ;
|
Chris@41
|
458 } ;
|
Chris@41
|
459
|
Chris@41
|
460 freq = f1 + (3.0 - a1) * (f2 - f1) / (a2 - a1) ;
|
Chris@41
|
461
|
Chris@41
|
462 return 200.0 * freq ;
|
Chris@41
|
463 } /* bandwidth_test */
|
Chris@41
|
464
|
Chris@41
|
465 static void
|
Chris@41
|
466 measure_program (const RESAMPLE_PROG *prog, int verbose)
|
Chris@41
|
467 { double snr, bandwidth, conversion_rate ;
|
Chris@41
|
468 int output_samples ;
|
Chris@41
|
469 struct tms time_data ;
|
Chris@41
|
470 time_t time_now ;
|
Chris@41
|
471
|
Chris@41
|
472 printf ("\n Machine : %s\n", get_machine_details ()) ;
|
Chris@41
|
473 time_now = time (NULL) ;
|
Chris@41
|
474 printf (" Date : %s", ctime (&time_now)) ;
|
Chris@41
|
475
|
Chris@41
|
476 get_version_string (prog) ;
|
Chris@41
|
477 printf (" Program : %s\n", version_string) ;
|
Chris@41
|
478 printf (" Command : %s\n\n", prog->convert_cmd) ;
|
Chris@41
|
479
|
Chris@41
|
480 snr = measure_snr (prog, &output_samples, verbose) ;
|
Chris@41
|
481
|
Chris@41
|
482 printf (" Worst case SNR : %6.2f dB\n", snr) ;
|
Chris@41
|
483
|
Chris@41
|
484 times (&time_data) ;
|
Chris@41
|
485
|
Chris@41
|
486 conversion_rate = (1.0 * output_samples * sysconf (_SC_CLK_TCK)) / time_data.tms_cutime ;
|
Chris@41
|
487
|
Chris@41
|
488 printf (" Conversion rate : %5.0f samples/sec\n", conversion_rate) ;
|
Chris@41
|
489
|
Chris@41
|
490 bandwidth = bandwidth_test (prog, verbose) ;
|
Chris@41
|
491
|
Chris@41
|
492 if (bandwidth > 0.0)
|
Chris@41
|
493 printf (" Measured bandwidth : %5.2f %%\n", bandwidth) ;
|
Chris@41
|
494 else
|
Chris@41
|
495 printf (" Could not measure bandwidth (no -3dB point found).\n") ;
|
Chris@41
|
496
|
Chris@41
|
497 return ;
|
Chris@41
|
498 } /* measure_program */
|
Chris@41
|
499
|
Chris@41
|
500 /*##############################################################################
|
Chris@41
|
501 */
|
Chris@41
|
502
|
Chris@41
|
503 #else
|
Chris@41
|
504
|
Chris@41
|
505 int
|
Chris@41
|
506 main (void)
|
Chris@41
|
507 { puts ("\n"
|
Chris@41
|
508 "****************************************************************\n"
|
Chris@41
|
509 " This program has been compiled without :\n"
|
Chris@41
|
510 " 1) FFTW (http://www.fftw.org/).\n"
|
Chris@41
|
511 " 2) libsndfile (http://www.zip.com.au/~erikd/libsndfile/).\n"
|
Chris@41
|
512 " Without these two libraries there is not much it can do.\n"
|
Chris@41
|
513 "****************************************************************\n") ;
|
Chris@41
|
514
|
Chris@41
|
515 return 0 ;
|
Chris@41
|
516 } /* main */
|
Chris@41
|
517
|
Chris@41
|
518 #endif /* (HAVE_FFTW3 && HAVE_SNDFILE) */
|
Chris@41
|
519
|