Chris@0: /* Chris@0: ** Copyright (C) 2002-2011 Erik de Castro Lopo Chris@0: ** Chris@0: ** This program is free software; you can redistribute it and/or modify Chris@0: ** it under the terms of the GNU General Public License as published by Chris@0: ** the Free Software Foundation; either version 2 of the License, or Chris@0: ** (at your option) any later version. Chris@0: ** Chris@0: ** This program is distributed in the hope that it will be useful, Chris@0: ** but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: ** GNU General Public License for more details. Chris@0: ** Chris@0: ** You should have received a copy of the GNU General Public License Chris@0: ** along with this program; if not, write to the Free Software Chris@0: ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. Chris@0: */ Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: Chris@0: #include "config.h" Chris@0: Chris@0: #if (HAVE_FFTW3 && HAVE_SNDFILE && HAVE_SYS_TIMES_H) Chris@0: Chris@0: #include Chris@0: #include Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: Chris@0: #include "util.h" Chris@0: Chris@0: #define MAX_FREQS 4 Chris@0: #define BUFFER_LEN 80000 Chris@0: Chris@0: #define SAFE_STRNCAT(dest,src,len) \ Chris@0: { int safe_strncat_count ; \ Chris@0: safe_strncat_count = (len) - strlen (dest) - 1 ; \ Chris@0: strncat ((dest), (src), safe_strncat_count) ; \ Chris@0: (dest) [(len) - 1] = 0 ; \ Chris@0: } ; Chris@0: Chris@0: typedef struct Chris@0: { int freq_count ; Chris@0: double freqs [MAX_FREQS] ; Chris@0: Chris@0: int output_samplerate ; Chris@0: int pass_band_peaks ; Chris@0: Chris@0: double peak_value ; Chris@0: } SNR_TEST ; Chris@0: Chris@0: typedef struct Chris@0: { const char *progname ; Chris@0: const char *version_cmd ; Chris@0: const char *version_start ; Chris@0: const char *convert_cmd ; Chris@0: int format ; Chris@0: } RESAMPLE_PROG ; Chris@0: Chris@0: static char *get_progname (char *) ; Chris@0: static void usage_exit (const char *, const RESAMPLE_PROG *prog, int count) ; Chris@0: static void measure_program (const RESAMPLE_PROG *prog, int verbose) ; Chris@0: static void generate_source_wav (const char *filename, const double *freqs, int freq_count, int format) ; Chris@0: static const char* get_machine_details (void) ; Chris@0: Chris@0: static char version_string [512] ; Chris@0: Chris@0: int Chris@0: main (int argc, char *argv []) Chris@0: { static RESAMPLE_PROG resample_progs [] = Chris@0: { { "sndfile-resample", Chris@0: "examples/sndfile-resample --version", Chris@0: "libsamplerate", Chris@0: "examples/sndfile-resample --max-speed -c 0 -to %d source.wav destination.wav", Chris@0: SF_FORMAT_WAV | SF_FORMAT_PCM_32 Chris@0: }, Chris@0: { "sox", Chris@0: "sox -h 2>&1", Chris@0: "sox", Chris@0: "sox source.wav -r %d destination.wav resample 0.835", Chris@0: SF_FORMAT_WAV | SF_FORMAT_PCM_32 Chris@0: }, Chris@0: { "ResampAudio", Chris@0: "ResampAudio --version", Chris@0: "ResampAudio", Chris@0: "ResampAudio -f cutoff=0.41,atten=100,ratio=128 -s %d source.wav destination.wav", Chris@0: SF_FORMAT_WAV | SF_FORMAT_PCM_32 Chris@0: }, Chris@0: Chris@0: /*- Chris@0: { /+* Chris@0: ** The Shibatch converter doesn't work for all combinations of Chris@0: ** source and destination sample rates. Therefore it can't be Chris@0: ** included in this test. Chris@0: *+/ Chris@0: "shibatch", Chris@0: "ssrc", Chris@0: "Shibatch", Chris@0: "ssrc --rate %d source.wav destination.wav", Chris@0: SF_FORMAT_WAV | SF_FORMAT_PCM_32 Chris@0: },-*/ Chris@0: Chris@0: /*- Chris@0: { /+* Chris@0: ** The resample program is not able to match the bandwidth and SNR Chris@0: ** specs or sndfile-resample and hence will not be tested. Chris@0: *+/ Chris@0: "resample", Chris@0: "resample -version", Chris@0: "resample", Chris@0: "resample -to %d source.wav destination.wav", Chris@0: SF_FORMAT_WAV | SF_FORMAT_FLOAT Chris@0: },-*/ Chris@0: Chris@0: /*- Chris@0: { "mplayer", Chris@0: "mplayer -v 2>&1", Chris@0: "MPlayer ", Chris@0: "mplayer -ao pcm -srate %d source.wav >/dev/null 2>&1 && mv audiodump.wav destination.wav", Chris@0: SF_FORMAT_WAV | SF_FORMAT_PCM_32 Chris@0: },-*/ Chris@0: Chris@0: } ; /* resample_progs */ Chris@0: Chris@0: char *progname ; Chris@0: int prog = 0, verbose = 0 ; Chris@0: Chris@0: progname = get_progname (argv [0]) ; Chris@0: Chris@0: printf ("\n %s : evaluate a sample rate converter.\n", progname) ; Chris@0: Chris@0: if (argc == 3 && strcmp ("--verbose", argv [1]) == 0) Chris@0: { verbose = 1 ; Chris@0: prog = atoi (argv [2]) ; Chris@0: } Chris@0: else if (argc == 2) Chris@0: { verbose = 0 ; Chris@0: prog = atoi (argv [1]) ; Chris@0: } Chris@0: else Chris@0: usage_exit (progname, resample_progs, ARRAY_LEN (resample_progs)) ; Chris@0: Chris@0: if (prog < 0 || prog >= ARRAY_LEN (resample_progs)) Chris@0: usage_exit (progname, resample_progs, ARRAY_LEN (resample_progs)) ; Chris@0: Chris@0: measure_program (& (resample_progs [prog]), verbose) ; Chris@0: Chris@0: puts ("") ; Chris@0: Chris@0: return 0 ; Chris@0: } /* main */ Chris@0: Chris@0: /*============================================================================== Chris@0: */ Chris@0: Chris@0: static char * Chris@0: get_progname (char *progname) Chris@0: { char *cptr ; Chris@0: Chris@0: if ((cptr = strrchr (progname, '/')) != NULL) Chris@0: progname = cptr + 1 ; Chris@0: Chris@0: if ((cptr = strrchr (progname, '\\')) != NULL) Chris@0: progname = cptr + 1 ; Chris@0: Chris@0: return progname ; Chris@0: } /* get_progname */ Chris@0: Chris@0: static void Chris@0: usage_exit (const char *progname, const RESAMPLE_PROG *prog, int count) Chris@0: { int k ; Chris@0: Chris@0: printf ("\n Usage : %s \n\n", progname) ; Chris@0: Chris@0: puts (" where specifies the program to test:\n") ; Chris@0: Chris@0: for (k = 0 ; k < count ; k++) Chris@0: printf (" %d : %s\n", k, prog [k].progname) ; Chris@0: Chris@0: puts ("\n" Chris@0: " Obviously to test a given program you have to have it available on\n" Chris@0: " your system. See http://www.mega-nerd.com/SRC/quality.html for\n" Chris@0: " the download location of these programs.\n") ; Chris@0: Chris@0: exit (1) ; Chris@0: } /* usage_exit */ Chris@0: Chris@0: static const char* Chris@0: get_machine_details (void) Chris@0: { static char namestr [256] ; Chris@0: Chris@0: struct utsname name ; Chris@0: Chris@0: if (uname (&name) != 0) Chris@0: { snprintf (namestr, sizeof (namestr), "Unknown") ; Chris@0: return namestr ; Chris@0: } ; Chris@0: Chris@0: snprintf (namestr, sizeof (namestr), "%s (%s %s %s)", name.nodename, Chris@0: name.machine, name.sysname, name.release) ; Chris@0: Chris@0: return namestr ; Chris@0: } /* get_machine_details */ Chris@0: Chris@0: Chris@0: /*============================================================================== Chris@0: */ Chris@0: Chris@0: static void Chris@0: get_version_string (const RESAMPLE_PROG *prog) Chris@0: { FILE *file ; Chris@0: char *cptr ; Chris@0: Chris@0: /* Default. */ Chris@0: snprintf (version_string, sizeof (version_string), "no version") ; Chris@0: Chris@0: if (prog->version_cmd == NULL) Chris@0: return ; Chris@0: Chris@0: if ((file = popen (prog->version_cmd, "r")) == NULL) Chris@0: return ; Chris@0: Chris@0: while ((cptr = fgets (version_string, sizeof (version_string), file)) != NULL) Chris@0: { Chris@0: if (strstr (cptr, prog->version_start) != NULL) Chris@0: break ; Chris@0: Chris@0: version_string [0] = 0 ; Chris@0: } ; Chris@0: Chris@0: pclose (file) ; Chris@0: Chris@0: /* Remove trailing newline. */ Chris@0: if ((cptr = strchr (version_string, '\n')) != NULL) Chris@0: cptr [0] = 0 ; Chris@0: Chris@0: /* Remove leading whitespace from version string. */ Chris@0: cptr = version_string ; Chris@0: while (cptr [0] != 0 && isspace (cptr [0])) Chris@0: cptr ++ ; Chris@0: Chris@0: if (cptr != version_string) Chris@0: strncpy (version_string, cptr, sizeof (version_string)) ; Chris@0: Chris@0: return ; Chris@0: } /* get_version_string */ Chris@0: Chris@0: static void Chris@0: generate_source_wav (const char *filename, const double *freqs, int freq_count, int format) Chris@0: { static float buffer [BUFFER_LEN] ; Chris@0: Chris@0: SNDFILE *sndfile ; Chris@0: SF_INFO sfinfo ; Chris@0: Chris@0: sfinfo.channels = 1 ; Chris@0: sfinfo.samplerate = 44100 ; Chris@0: sfinfo.format = format ; Chris@0: Chris@0: if ((sndfile = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL) Chris@0: { printf ("Line %d : cound not open '%s' : %s\n", __LINE__, filename, sf_strerror (NULL)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: sf_command (sndfile, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ; Chris@0: Chris@0: gen_windowed_sines (freq_count, freqs, 0.9, buffer, ARRAY_LEN (buffer)) ; Chris@0: Chris@0: if (sf_write_float (sndfile, buffer, ARRAY_LEN (buffer)) != ARRAY_LEN (buffer)) Chris@0: { printf ("Line %d : sf_write_float short write.\n", __LINE__) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: sf_close (sndfile) ; Chris@0: } /* generate_source_wav */ Chris@0: Chris@0: static double Chris@0: measure_destination_wav (char *filename, int *output_samples, int expected_peaks) Chris@0: { static float buffer [250000] ; Chris@0: Chris@0: SNDFILE *sndfile ; Chris@0: SF_INFO sfinfo ; Chris@0: double snr ; Chris@0: Chris@0: if ((sndfile = sf_open (filename, SFM_READ, &sfinfo)) == NULL) Chris@0: { printf ("Line %d : Cound not open '%s' : %s\n", __LINE__, filename, sf_strerror (NULL)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (sfinfo.channels != 1) Chris@0: { printf ("Line %d : Bad channel count (%d). Should be 1.\n", __LINE__, sfinfo.channels) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (sfinfo.frames > ARRAY_LEN (buffer)) Chris@0: { printf ("Line %d : Too many frames (%ld) of data in file.\n", __LINE__, (long) sfinfo.frames) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: *output_samples = (int) sfinfo.frames ; Chris@0: Chris@0: if (sf_read_float (sndfile, buffer, sfinfo.frames) != sfinfo.frames) Chris@0: { printf ("Line %d : Bad read.\n", __LINE__) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: sf_close (sndfile) ; Chris@0: Chris@0: snr = calculate_snr (buffer, sfinfo.frames, expected_peaks) ; Chris@0: Chris@0: return snr ; Chris@0: } /* measure_desination_wav */ Chris@0: Chris@0: static double Chris@0: measure_snr (const RESAMPLE_PROG *prog, int *output_samples, int verbose) Chris@0: { static SNR_TEST snr_test [] = Chris@0: { Chris@0: { 1, { 0.211111111111 }, 48000, 1, 1.0 }, Chris@0: { 1, { 0.011111111111 }, 132301, 1, 1.0 }, Chris@0: { 1, { 0.111111111111 }, 92301, 1, 1.0 }, Chris@0: { 1, { 0.011111111111 }, 26461, 1, 1.0 }, Chris@0: { 1, { 0.011111111111 }, 13231, 1, 1.0 }, Chris@0: { 1, { 0.011111111111 }, 44101, 1, 1.0 }, Chris@0: { 2, { 0.311111, 0.49 }, 78199, 2, 1.0 }, Chris@0: { 2, { 0.011111, 0.49 }, 12345, 1, 0.5 }, Chris@0: { 2, { 0.0123456, 0.4 }, 20143, 1, 0.5 }, Chris@0: { 2, { 0.0111111, 0.4 }, 26461, 1, 0.5 }, Chris@0: { 1, { 0.381111111111 }, 58661, 1, 1.0 } Chris@0: } ; /* snr_test */ Chris@0: static char command [256] ; Chris@0: Chris@0: double snr, worst_snr = 500.0 ; Chris@0: int k , retval, sample_count ; Chris@0: Chris@0: *output_samples = 0 ; Chris@0: Chris@0: for (k = 0 ; k < ARRAY_LEN (snr_test) ; k++) Chris@0: { remove ("source.wav") ; Chris@0: remove ("destination.wav") ; Chris@0: Chris@0: if (verbose) Chris@0: printf (" SNR test #%d : ", k) ; Chris@0: fflush (stdout) ; Chris@0: generate_source_wav ("source.wav", snr_test [k].freqs, snr_test [k].freq_count, prog->format) ; Chris@0: Chris@0: snprintf (command, sizeof (command), prog->convert_cmd, snr_test [k].output_samplerate) ; Chris@0: SAFE_STRNCAT (command, " >/dev/null 2>&1", sizeof (command)) ; Chris@0: if ((retval = system (command)) != 0) Chris@0: printf ("system returned %d\n", retval) ; Chris@0: Chris@0: snr = measure_destination_wav ("destination.wav", &sample_count, snr_test->pass_band_peaks) ; Chris@0: Chris@0: *output_samples += sample_count ; Chris@0: Chris@0: if (fabs (snr) < fabs (worst_snr)) Chris@0: worst_snr = fabs (snr) ; Chris@0: Chris@0: if (verbose) Chris@0: printf ("%6.2f dB\n", snr) ; Chris@0: } ; Chris@0: Chris@0: return worst_snr ; Chris@0: } /* measure_snr */ Chris@0: Chris@0: /*------------------------------------------------------------------------------ Chris@0: */ Chris@0: Chris@0: static double Chris@0: measure_destination_peak (const char *filename) Chris@0: { static float data [2 * BUFFER_LEN] ; Chris@0: SNDFILE *sndfile ; Chris@0: SF_INFO sfinfo ; Chris@0: double peak = 0.0 ; Chris@0: int k = 0 ; Chris@0: Chris@0: if ((sndfile = sf_open (filename, SFM_READ, &sfinfo)) == NULL) Chris@0: { printf ("Line %d : failed to open file %s\n", __LINE__, filename) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (sfinfo.channels != 1) Chris@0: { printf ("Line %d : bad channel count.\n", __LINE__) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (sfinfo.frames > ARRAY_LEN (data) + 4 || sfinfo.frames < ARRAY_LEN (data) - 100) Chris@0: { printf ("Line %d : bad frame count (got %d, expected %d).\n", __LINE__, (int) sfinfo.frames, ARRAY_LEN (data)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (sf_read_float (sndfile, data, sfinfo.frames) != sfinfo.frames) Chris@0: { printf ("Line %d : bad read.\n", __LINE__) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: sf_close (sndfile) ; Chris@0: Chris@0: for (k = 0 ; k < (int) sfinfo.frames ; k++) Chris@0: if (fabs (data [k]) > peak) Chris@0: peak = fabs (data [k]) ; Chris@0: Chris@0: return peak ; Chris@0: } /* measure_destination_peak */ Chris@0: Chris@0: static double Chris@0: find_attenuation (double freq, const RESAMPLE_PROG *prog, int verbose) Chris@0: { static char command [256] ; Chris@0: double output_peak ; Chris@0: int retval ; Chris@0: char *filename ; Chris@0: Chris@0: filename = "destination.wav" ; Chris@0: Chris@0: generate_source_wav ("source.wav", &freq, 1, prog->format) ; Chris@0: Chris@0: remove (filename) ; Chris@0: Chris@0: snprintf (command, sizeof (command), prog->convert_cmd, 88189) ; Chris@0: SAFE_STRNCAT (command, " >/dev/null 2>&1", sizeof (command)) ; Chris@0: if ((retval = system (command)) != 0) Chris@0: printf ("system returned %d\n", retval) ; Chris@0: Chris@0: output_peak = measure_destination_peak (filename) ; Chris@0: Chris@0: if (verbose) Chris@0: printf (" freq : %f peak : %f\n", freq, output_peak) ; Chris@0: Chris@0: return fabs (20.0 * log10 (output_peak)) ; Chris@0: } /* find_attenuation */ Chris@0: Chris@0: static double Chris@0: bandwidth_test (const RESAMPLE_PROG *prog, int verbose) Chris@0: { double f1, f2, a1, a2 ; Chris@0: double freq, atten ; Chris@0: Chris@0: f1 = 0.35 ; Chris@0: a1 = find_attenuation (f1, prog, verbose) ; Chris@0: Chris@0: f2 = 0.49999 ; Chris@0: a2 = find_attenuation (f2, prog, verbose) ; Chris@0: Chris@0: Chris@0: if (fabs (a1) < 1e-2 && a2 < 3.0) Chris@0: return -1.0 ; Chris@0: Chris@0: if (a1 > 3.0 || a2 < 3.0) Chris@0: { printf ("\n\nLine %d : cannot bracket 3dB point.\n\n", __LINE__) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: while (a2 - a1 > 1.0) Chris@0: { freq = f1 + 0.5 * (f2 - f1) ; Chris@0: atten = find_attenuation (freq, prog, verbose) ; Chris@0: Chris@0: if (atten < 3.0) Chris@0: { f1 = freq ; Chris@0: a1 = atten ; Chris@0: } Chris@0: else Chris@0: { f2 = freq ; Chris@0: a2 = atten ; Chris@0: } ; Chris@0: } ; Chris@0: Chris@0: freq = f1 + (3.0 - a1) * (f2 - f1) / (a2 - a1) ; Chris@0: Chris@0: return 200.0 * freq ; Chris@0: } /* bandwidth_test */ Chris@0: Chris@0: static void Chris@0: measure_program (const RESAMPLE_PROG *prog, int verbose) Chris@0: { double snr, bandwidth, conversion_rate ; Chris@0: int output_samples ; Chris@0: struct tms time_data ; Chris@0: time_t time_now ; Chris@0: Chris@0: printf ("\n Machine : %s\n", get_machine_details ()) ; Chris@0: time_now = time (NULL) ; Chris@0: printf (" Date : %s", ctime (&time_now)) ; Chris@0: Chris@0: get_version_string (prog) ; Chris@0: printf (" Program : %s\n", version_string) ; Chris@0: printf (" Command : %s\n\n", prog->convert_cmd) ; Chris@0: Chris@0: snr = measure_snr (prog, &output_samples, verbose) ; Chris@0: Chris@0: printf (" Worst case SNR : %6.2f dB\n", snr) ; Chris@0: Chris@0: times (&time_data) ; Chris@0: Chris@0: conversion_rate = (1.0 * output_samples * sysconf (_SC_CLK_TCK)) / time_data.tms_cutime ; Chris@0: Chris@0: printf (" Conversion rate : %5.0f samples/sec\n", conversion_rate) ; Chris@0: Chris@0: bandwidth = bandwidth_test (prog, verbose) ; Chris@0: Chris@0: if (bandwidth > 0.0) Chris@0: printf (" Measured bandwidth : %5.2f %%\n", bandwidth) ; Chris@0: else Chris@0: printf (" Could not measure bandwidth (no -3dB point found).\n") ; Chris@0: Chris@0: return ; Chris@0: } /* measure_program */ Chris@0: Chris@0: /*############################################################################## Chris@0: */ Chris@0: Chris@0: #else Chris@0: Chris@0: int Chris@0: main (void) Chris@0: { puts ("\n" Chris@0: "****************************************************************\n" Chris@0: " This program has been compiled without :\n" Chris@0: " 1) FFTW (http://www.fftw.org/).\n" Chris@0: " 2) libsndfile (http://www.zip.com.au/~erikd/libsndfile/).\n" Chris@0: " Without these two libraries there is not much it can do.\n" Chris@0: "****************************************************************\n") ; Chris@0: Chris@0: return 0 ; Chris@0: } /* main */ Chris@0: Chris@0: #endif /* (HAVE_FFTW3 && HAVE_SNDFILE) */ Chris@0: