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 "config.h" Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: Chris@0: #if (HAVE_SNDFILE) Chris@0: Chris@0: #include Chris@0: #include Chris@0: Chris@0: #define DEFAULT_CONVERTER SRC_SINC_MEDIUM_QUALITY Chris@0: Chris@0: #define BUFFER_LEN 4096 /*-(1<<16)-*/ Chris@0: Chris@0: static void usage_exit (const char *progname) ; Chris@0: static sf_count_t sample_rate_convert (SNDFILE *infile, SNDFILE *outfile, int converter, double src_ratio, int channels, double * gain) ; Chris@0: static double apply_gain (float * data, long frames, int channels, double max, double gain) ; Chris@0: Chris@0: int Chris@0: main (int argc, char *argv []) Chris@0: { SNDFILE *infile, *outfile = NULL ; Chris@0: SF_INFO sfinfo ; Chris@0: Chris@0: sf_count_t count ; Chris@0: double src_ratio = -1.0, gain = 1.0 ; Chris@0: int new_sample_rate = -1, k, converter, max_speed = SF_FALSE ; Chris@0: Chris@0: if (argc == 2 && strcmp (argv [1], "--version") == 0) Chris@0: { char buffer [64], *cptr ; Chris@0: Chris@0: if ((cptr = strrchr (argv [0], '/')) != NULL) Chris@0: argv [0] = cptr + 1 ; Chris@0: if ((cptr = strrchr (argv [0], '\\')) != NULL) Chris@0: argv [0] = cptr + 1 ; Chris@0: Chris@0: sf_command (NULL, SFC_GET_LIB_VERSION, buffer, sizeof (buffer)) ; Chris@0: Chris@0: printf ("%s (%s,%s)\n", argv [0], src_get_version (), buffer) ; Chris@0: exit (0) ; Chris@0: } ; Chris@0: Chris@0: if (argc != 5 && argc != 7 && argc != 8) Chris@0: usage_exit (argv [0]) ; Chris@0: Chris@0: /* Set default converter. */ Chris@0: converter = DEFAULT_CONVERTER ; Chris@0: Chris@0: for (k = 1 ; k < argc - 2 ; k++) Chris@0: { if (strcmp (argv [k], "--max-speed") == 0) Chris@0: max_speed = SF_TRUE ; Chris@0: else if (strcmp (argv [k], "-to") == 0) Chris@0: { k ++ ; Chris@0: new_sample_rate = atoi (argv [k]) ; Chris@0: } Chris@0: else if (strcmp (argv [k], "-by") == 0) Chris@0: { k ++ ; Chris@0: src_ratio = atof (argv [k]) ; Chris@0: } Chris@0: else if (strcmp (argv [k], "-c") == 0) Chris@0: { k ++ ; Chris@0: converter = atoi (argv [k]) ; Chris@0: } Chris@0: else Chris@0: usage_exit (argv [0]) ; Chris@0: } ; Chris@0: Chris@0: if (new_sample_rate <= 0 && src_ratio <= 0.0) Chris@0: usage_exit (argv [0]) ; Chris@0: Chris@0: if (src_get_name (converter) == NULL) Chris@0: { printf ("Error : bad converter number.\n") ; Chris@0: usage_exit (argv [0]) ; Chris@0: } ; Chris@0: Chris@0: if (strcmp (argv [argc - 2], argv [argc - 1]) == 0) Chris@0: { printf ("Error : input and output file names are the same.\n") ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if ((infile = sf_open (argv [argc - 2], SFM_READ, &sfinfo)) == NULL) Chris@0: { printf ("Error : Not able to open input file '%s'\n", argv [argc - 2]) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: printf ("Input File : %s\n", argv [argc - 2]) ; Chris@0: printf ("Sample Rate : %d\n", sfinfo.samplerate) ; Chris@0: printf ("Input Frames : %ld\n\n", (long) sfinfo.frames) ; Chris@0: Chris@0: if (new_sample_rate > 0) Chris@0: { src_ratio = (1.0 * new_sample_rate) / sfinfo.samplerate ; Chris@0: sfinfo.samplerate = new_sample_rate ; Chris@0: } Chris@0: else if (src_is_valid_ratio (src_ratio)) Chris@0: sfinfo.samplerate = (int) floor (sfinfo.samplerate * src_ratio) ; Chris@0: else Chris@0: { printf ("Not able to determine new sample rate. Exiting.\n") ; Chris@0: sf_close (infile) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (fabs (src_ratio - 1.0) < 1e-20) Chris@0: { printf ("Target samplerate and input samplerate are the same. Exiting.\n") ; Chris@0: sf_close (infile) ; Chris@0: exit (0) ; Chris@0: } ; Chris@0: Chris@0: printf ("SRC Ratio : %f\n", src_ratio) ; Chris@0: printf ("Converter : %s\n\n", src_get_name (converter)) ; Chris@0: Chris@0: if (src_is_valid_ratio (src_ratio) == 0) Chris@0: { printf ("Error : Sample rate change out of valid range.\n") ; Chris@0: sf_close (infile) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: /* Delete the output file length to zero if already exists. */ Chris@0: remove (argv [argc - 1]) ; Chris@0: Chris@0: printf ("Output file : %s\n", argv [argc - 1]) ; Chris@0: printf ("Sample Rate : %d\n", sfinfo.samplerate) ; Chris@0: Chris@0: do Chris@0: { sf_close (outfile) ; Chris@0: Chris@0: if ((outfile = sf_open (argv [argc - 1], SFM_WRITE, &sfinfo)) == NULL) Chris@0: { printf ("Error : Not able to open output file '%s'\n", argv [argc - 1]) ; Chris@0: sf_close (infile) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (max_speed) Chris@0: { /* This is mainly for the comparison program tests/src-evaluate.c */ Chris@0: sf_command (outfile, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ; Chris@0: } Chris@0: else Chris@0: { /* Update the file header after every write. */ Chris@0: sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ; Chris@0: } ; Chris@0: Chris@0: sf_command (outfile, SFC_SET_CLIPPING, NULL, SF_TRUE) ; Chris@0: Chris@0: count = sample_rate_convert (infile, outfile, converter, src_ratio, sfinfo.channels, &gain) ; Chris@0: } Chris@0: while (count < 0) ; Chris@0: Chris@0: printf ("Output Frames : %ld\n\n", (long) count) ; Chris@0: Chris@0: sf_close (infile) ; Chris@0: sf_close (outfile) ; Chris@0: Chris@0: return 0 ; Chris@0: } /* main */ Chris@0: Chris@0: /*============================================================================== Chris@0: */ Chris@0: Chris@0: static sf_count_t Chris@0: sample_rate_convert (SNDFILE *infile, SNDFILE *outfile, int converter, double src_ratio, int channels, double * gain) Chris@0: { static float input [BUFFER_LEN] ; Chris@0: static float output [BUFFER_LEN] ; Chris@0: Chris@0: SRC_STATE *src_state ; Chris@0: SRC_DATA src_data ; Chris@0: int error ; Chris@0: double max = 0.0 ; Chris@0: sf_count_t output_count = 0 ; Chris@0: Chris@0: sf_seek (infile, 0, SEEK_SET) ; Chris@0: sf_seek (outfile, 0, SEEK_SET) ; Chris@0: Chris@0: /* Initialize the sample rate converter. */ Chris@0: if ((src_state = src_new (converter, channels, &error)) == NULL) Chris@0: { printf ("\n\nError : src_new() failed : %s.\n\n", src_strerror (error)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: src_data.end_of_input = 0 ; /* Set this later. */ Chris@0: Chris@0: /* Start with zero to force load in while loop. */ Chris@0: src_data.input_frames = 0 ; Chris@0: src_data.data_in = input ; Chris@0: Chris@0: src_data.src_ratio = src_ratio ; Chris@0: Chris@0: src_data.data_out = output ; Chris@0: src_data.output_frames = BUFFER_LEN /channels ; Chris@0: Chris@0: while (1) Chris@0: { Chris@0: /* If the input buffer is empty, refill it. */ Chris@0: if (src_data.input_frames == 0) Chris@0: { src_data.input_frames = sf_readf_float (infile, input, BUFFER_LEN / channels) ; Chris@0: src_data.data_in = input ; Chris@0: Chris@0: /* The last read will not be a full buffer, so snd_of_input. */ Chris@0: if (src_data.input_frames < BUFFER_LEN / channels) Chris@0: src_data.end_of_input = SF_TRUE ; Chris@0: } ; Chris@0: Chris@0: if ((error = src_process (src_state, &src_data))) Chris@0: { printf ("\nError : %s\n", src_strerror (error)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: /* Terminate if done. */ Chris@0: if (src_data.end_of_input && src_data.output_frames_gen == 0) Chris@0: break ; Chris@0: Chris@0: max = apply_gain (src_data.data_out, src_data.output_frames_gen, channels, max, *gain) ; Chris@0: Chris@0: /* Write output. */ Chris@0: sf_writef_float (outfile, output, src_data.output_frames_gen) ; Chris@0: output_count += src_data.output_frames_gen ; Chris@0: Chris@0: src_data.data_in += src_data.input_frames_used * channels ; Chris@0: src_data.input_frames -= src_data.input_frames_used ; Chris@0: } ; Chris@0: Chris@0: src_state = src_delete (src_state) ; Chris@0: Chris@0: if (max > 1.0) Chris@0: { *gain = 1.0 / max ; Chris@0: printf ("\nOutput has clipped. Restarting conversion to prevent clipping.\n\n") ; Chris@0: return -1 ; Chris@0: } ; Chris@0: Chris@0: return output_count ; Chris@0: } /* sample_rate_convert */ Chris@0: Chris@0: static double Chris@0: apply_gain (float * data, long frames, int channels, double max, double gain) Chris@0: { Chris@0: long k ; Chris@0: Chris@0: for (k = 0 ; k < frames * channels ; k++) Chris@0: { data [k] *= gain ; Chris@0: Chris@0: if (fabs (data [k]) > max) Chris@0: max = fabs (data [k]) ; Chris@0: } ; Chris@0: Chris@0: return max ; Chris@0: } /* apply_gain */ Chris@0: Chris@0: static void Chris@0: usage_exit (const char *progname) Chris@0: { char lsf_ver [128] ; Chris@0: const char *cptr ; Chris@0: int k ; 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: Chris@0: sf_command (NULL, SFC_GET_LIB_VERSION, lsf_ver, sizeof (lsf_ver)) ; Chris@0: Chris@0: printf ("\n" Chris@0: " A Sample Rate Converter using libsndfile for file I/O and Secret \n" Chris@0: " Rabbit Code (aka libsamplerate) for performing the conversion.\n" Chris@0: " It works on any file format supported by libsndfile with any \n" Chris@0: " number of channels (limited only by host memory).\n" Chris@0: "\n" Chris@0: " %s\n" Chris@0: " %s\n" Chris@0: "\n" Chris@0: " Usage : \n" Chris@0: " %s -to [-c ] \n" Chris@0: " %s -by [-c ] \n" Chris@0: "\n", src_get_version (), lsf_ver, progname, progname) ; Chris@0: Chris@0: puts ( Chris@0: " The optional -c argument allows the converter type to be chosen from\n" Chris@0: " the following list :" Chris@0: "\n" Chris@0: ) ; Chris@0: Chris@0: for (k = 0 ; (cptr = src_get_name (k)) != NULL ; k++) Chris@0: printf (" %d : %s%s\n", k, cptr, k == DEFAULT_CONVERTER ? " (default)" : "") ; Chris@0: Chris@0: puts ("") ; Chris@0: Chris@0: exit (1) ; Chris@0: } /* usage_exit */ Chris@0: Chris@0: /*============================================================================== Chris@0: */ Chris@0: Chris@0: #else /* (HAVE_SNFILE == 0) */ Chris@0: Chris@0: /* Alternative main function when libsndfile is not available. */ Chris@0: Chris@0: int Chris@0: main (void) Chris@0: { puts ( Chris@0: "\n" Chris@0: "****************************************************************\n" Chris@0: " This example program was compiled without libsndfile \n" Chris@0: " (http://www.mega-nerd.com/libsndfile/).\n" Chris@0: " It is therefore completely broken and non-functional.\n" Chris@0: "****************************************************************\n" Chris@0: "\n" Chris@0: ) ; Chris@0: Chris@0: return 0 ; Chris@0: } /* main */ Chris@0: Chris@0: #endif Chris@0: