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