int
main (int argc, char *argv [])
{	SNDFILE	*infile, *outfile = NULL ;
	SF_INFO sfinfo ;

	int normalize = 1 ;
	sf_count_t	count ;
	double		src_ratio = -1.0, gain = 1.0 ;
	int			new_sample_rate = -1, k, converter, max_speed = SF_FALSE ;

	if (argc == 2 && strcmp (argv [1], "--version") == 0)
	{	char buffer [64], *cptr ;

		if ((cptr = strrchr (argv [0], '/')) != NULL)
			argv [0] = cptr + 1 ;
		if ((cptr = strrchr (argv [0], '\\')) != NULL)
			argv [0] = cptr + 1 ;

		sf_command (NULL, SFC_GET_LIB_VERSION, buffer, sizeof (buffer)) ;

		printf ("%s (%s,%s)\n", argv [0], src_get_version (), buffer) ;
		exit (0) ;
		} ;

	if (argc != 5 && argc != 7 && argc != 8)
		usage_exit (argv [0]) ;

	/* Set default converter. */
	converter = DEFAULT_CONVERTER ;

	for (k = 1 ; k < argc - 2 ; k++)
	{	if (strcmp (argv [k], "--max-speed") == 0)
			max_speed = SF_TRUE ;
		else if (strcmp (argv [k], "--no-normalize") == 0)
			normalize = 0 ;
		else if (strcmp (argv [k], "-to") == 0)
		{	k ++ ;
			new_sample_rate = atoi (argv [k]) ;
			}
		else if (strcmp (argv [k], "-by") == 0)
		{	k ++ ;
			src_ratio = atof (argv [k]) ;
			}
		else if (strcmp (argv [k], "-c") == 0)
		{	k ++ ;
			converter = atoi (argv [k]) ;
			}
		else
			usage_exit (argv [0]) ;
		} ;

	if (new_sample_rate <= 0 && src_ratio <= 0.0)
		usage_exit (argv [0]) ;

	if (src_get_name (converter) == NULL)
	{	printf ("Error : bad converter number.\n") ;
		usage_exit (argv [0]) ;
		} ;

	if (strcmp (argv [argc - 2], argv [argc - 1]) == 0)
	{	printf ("Error : input and output file names are the same.\n") ;
		exit (1) ;
		} ;

	if ((infile = sf_open (argv [argc - 2], SFM_READ, &sfinfo)) == NULL)
	{	printf ("Error : Not able to open input file '%s'\n", argv [argc - 2]) ;
		exit (1) ;
		} ;

	printf ("Input File    : %s\n", argv [argc - 2]) ;
	printf ("Sample Rate   : %d\n", sfinfo.samplerate) ;
	printf ("Input Frames  : %ld\n\n", (long) sfinfo.frames) ;

	if (new_sample_rate > 0)
	{	src_ratio = (1.0 * new_sample_rate) / sfinfo.samplerate ;
		sfinfo.samplerate = new_sample_rate ;
		}
	else if (src_is_valid_ratio (src_ratio))
		sfinfo.samplerate = (int) floor (sfinfo.samplerate * src_ratio) ;
	else
	{	printf ("Not able to determine new sample rate. Exiting.\n") ;
		sf_close (infile) ;
		exit (1) ;
		} ;

	if (fabs (src_ratio - 1.0) < 1e-20)
	{	printf ("Target samplerate and input samplerate are the same. Exiting.\n") ;
		sf_close (infile) ;
		exit (0) ;
		} ;

	printf ("SRC Ratio     : %f\n", src_ratio) ;
	printf ("Converter     : %s\n\n", src_get_name (converter)) ;

	if (src_is_valid_ratio (src_ratio) == 0)
	{	printf ("Error : Sample rate change out of valid range.\n") ;
		sf_close (infile) ;
		exit (1) ;
		} ;

	/* Delete the output file length to zero if already exists. */
	remove (argv [argc - 1]) ;

	printf ("Output file   : %s\n", argv [argc - 1]) ;
	printf ("Sample Rate   : %d\n", sfinfo.samplerate) ;

	do
	{	sf_close (outfile) ;

		if ((outfile = sf_open (argv [argc - 1], SFM_WRITE, &sfinfo)) == NULL)
		{	printf ("Error : Not able to open output file '%s'\n", argv [argc - 1]) ;
			sf_close (infile) ;
			exit (1) ;
			} ;

		if (max_speed)
		{	/* This is mainly for the comparison program tests/src-evaluate.c */
			sf_command (outfile, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;
			}
		else
		{	/* Update the file header after every write. */
			sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
			} ;

		sf_command (outfile, SFC_SET_CLIPPING, NULL, SF_TRUE) ;

		count = sample_rate_convert (infile, outfile, converter, src_ratio, sfinfo.channels, &gain, normalize) ;
		}
	while (count < 0) ;

	printf ("Output Frames : %ld\n\n", (long) count) ;

	sf_close (infile) ;
	sf_close (outfile) ;

	return 0 ;
} /* main */
string Audio_file_reader::resample(int new_sample_rate) {
    sf_count_t count;
    string rname;
    SNDFILE *sf_rs = NULL;

    double gain = 1.0, src_ratio = -1.0;
    int samplerate = sf_info.samplerate;

    if (samplerate <= new_sample_rate) {
        printf("WARNING: The original file has sampling frequency %d less than the resampled frequency %d\n", samplerate, new_sample_rate);
    }

    int converter = DEFAULT_CONVERTER ;

#ifdef DEBUG
    printf ("Input File    : %s\n", name) ;
    printf ("Sample Rate   : %d\n", sf_info.samplerate) ;
    printf ("Input Frames  : %ld\n\n", (long) sf_info.frames) ;
#endif

    src_ratio = (1.0 * new_sample_rate) / samplerate;
    sf_info.samplerate = new_sample_rate ;


    if (fabs (src_ratio - 1.0) < 1e-20)
    {
        printf ("Target samplerate and input samplerate are the same. Exiting.\n") ;
        return "";
    }

#ifdef DEBUG
    printf ("SRC Ratio     : %f\n", src_ratio) ;
    printf ("Converter     : %s\n\n", src_get_name (converter)) ;
#endif

    if (src_is_valid_ratio (src_ratio) == 0)
    {
        printf ("Error : Sample rate change out of valid range.\n");
        return "";
    }

    rname = "resample_" + string(name);
    remove (rname.c_str()) ;

#ifdef DEBUG
    printf ("Output file   : %s\n", rname.c_str());
    printf ("Sample Rate   : %d\n", sf_info.samplerate) ;
#endif


    if (sf_rs != NULL) {
        sf_close (sf_rs);
    }
    if ((sf_rs = sf_open(rname.c_str(), SFM_WRITE, &sf_info)) == NULL) {
        printf ("Error : Not able to open output file '%s'\n", rname.c_str());
        sf_close (sf);
        return "";
    };
    /* Update the file header after every write. */
    sf_command (sf_rs, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE);

    sf_command (sf_rs, SFC_SET_CLIPPING, NULL, SF_TRUE) ;

    count = sample_rate_convert (sf_rs, converter, src_ratio, sf_info.channels, &gain) ;

    sf_close(sf_rs);


#ifdef DEBUG
    printf ("Output Frames : %ld\n\n", (long) count) ;
#endif
    return rname;
}