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