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