static void
usage_exit (const char *progname)
{	char lsf_ver [128] ;
	const char	*cptr ;
	int		k ;

	if ((cptr = strrchr (progname, '/')) != NULL)
		progname = cptr + 1 ;

	if ((cptr = strrchr (progname, '\\')) != NULL)
		progname = cptr + 1 ;


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

	printf ("\n"
		"  A Sample Rate Converter using libsndfile for file I/O and Secret \n"
		"  Rabbit Code (aka libsamplerate) for performing the conversion.\n"
		"  It works on any file format supported by libsndfile with any \n"
		"  number of channels (limited only by host memory).\n"
		"\n"
		"       %s\n"
		"       %s\n"
		"\n"
		"  Usage : \n"
		"       %s -to <new sample rate> [-c <number>] <input file> <output file>\n"
		"       %s -by <amount> [-c <number>] <input file> <output file>\n"
		"\n", src_get_version (), lsf_ver, progname, progname) ;

	puts (
		"  The optional -c argument allows the converter type to be chosen from\n"
		"  the following list :"
		"\n"
		) ;

	for (k = 0 ; (cptr = src_get_name (k)) != NULL ; k++)
		printf ("       %d : %s%s\n", k, cptr, k == DEFAULT_CONVERTER ? " (default)" : "") ;

	puts ("\n"
		"  The --no-normalize option disables clipping check and normalization.") ;

	puts ("") ;

	exit (1) ;
} /* usage_exit */
Example #2
0
int
main (void)
{
	/* Force output of the Electric Fence banner message. */
	force_efence_banner () ;

	puts ("") ;

	printf ("    version : %s\n\n", src_get_version ()) ;

	/* Current max converter is SRC_LINEAR. */
	name_test (SRC_LINEAR) ;

	error_test () ;

	src_ratio_test () ;

	return 0 ;
} /* main */
Example #3
0
int
main (void)
{
	puts ("") ;

	printf ("    version : %s\n\n", src_get_version ()) ;

	/* Current max converter is SRC_LINEAR. */
	name_test () ;

	error_test () ;

	src_ratio_test () ;

	zero_input_test (SRC_ZERO_ORDER_HOLD) ;
	zero_input_test (SRC_LINEAR) ;
	zero_input_test (SRC_SINC_FASTEST) ;

	puts ("") ;
	return 0 ;
} /* main */
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 */
Example #5
0
int migrate_collection(void * config, void * sconfig)
{
    char old_coll_path[PATH_MAX];
    char version[256];
    int ret;

    struct filesystem_configuration_s * fs_config = 
        (struct filesystem_configuration_s *) config;
    struct server_configuration_s * server_config =
        (struct server_configuration_s *) sconfig;

    memset(version, 0, 256);
    /* find version of source storage space */
    ret = src_get_version(
        server_config->storage_path, 
        fs_config->coll_id, 
        fs_config->file_system_name,
        version, 254);
    if(ret < 0)
    {
        fprintf(stderr, 
                "Error: failed to read version of src storage space\n"
                "       for filesystem: %s (%08x)\n",
                fs_config->file_system_name, fs_config->coll_id);
        return ret;
    }
        
    /* call the appropriate translation routine based on the version */
    if(strncmp(version, "0.0.1", 5) == 0)
    {
        sprintf(old_coll_path, "%s/%08x-old-%s",
                server_config->storage_path, 
                fs_config->coll_id, version);

        ret = access(old_coll_path, F_OK);
        if(ret == 0)
        {
            if(opts.cleanup_set)
            {
                /* user asked to remove this old collection instead
                 * of creating it
                 */
                if(verbose) printf("VERBOSE Removing old collection at: %s\n",
                                   old_coll_path);
                ret = recursive_rmdir(old_coll_path);
                if(ret < 0)
                {
                    fprintf(stderr, 
                            "Error: failed to remove %s\n", 
                            old_coll_path);
                    return -1;
                }
                return 0;
            }

            if(verbose) printf("VERBOSE %s already exists.\n", 
                               old_coll_path);
            fprintf(stderr, 
                    "Error: unable to confirm availability of backup subdirectory (%s)\n"
                    "       for migration of fs: %s (%08x).\n", 
                    old_coll_path,
                    fs_config->file_system_name,
                    fs_config->coll_id);
            fprintf(stderr, 
                    "Error: please make sure that the migration "
                    "has not already been performed.\n");
            return -1;
        }

        ret = translate_0_0_1(
            server_config->storage_path, old_coll_path, 
            fs_config->file_system_name, 
            fs_config->coll_id);
        if(ret < 0)
        {
            fprintf(stderr, 
                    "Error: failed to translate from %s collection\n"
                    "       for fs: %s (%08x).\n",
                    version, fs_config->file_system_name, fs_config->coll_id);
            return -1;
        }

        if(opts.cleanup_set)
        {
            /* user asked to remove this old collection instead
             * of creating it
             */
            if(verbose) printf("VERBOSE Removing old collection at: %s\n",
                               old_coll_path);
            ret = unlink(old_coll_path);
            if(ret < 0)
            {
                perror("unlink");
                return -1;
            }
        }
    }
    else
    {
        if(opts.cleanup_set)
        {
            /* user asked to remove this old collection instead
             * of creating it, but we don't know what the version
             * is anymore
             */
            DIR * storage_dir;
            struct dirent * next_dirent;
            char collname[PATH_MAX];
            int collname_length;
            int removed_olddirs = 0;

            collname_length = sprintf(collname, "%08x-old", fs_config->coll_id);

            storage_dir = opendir(server_config->storage_path);
            if(!storage_dir)
            {
                fprintf(stderr, "Error: failed to open directory: %s\n",
                        server_config->storage_path);
                return -1;
            }

            while((next_dirent = readdir(storage_dir)) != NULL)
            {
                int d_namelen = strlen(next_dirent->d_name);
                if(collname_length < d_namelen &&
                   strncmp(next_dirent->d_name, collname, collname_length) == 0)
                {
                    char old_coll_path[PATH_MAX];

                    sprintf(old_coll_path, "%s/%s",
                            server_config->storage_path, next_dirent->d_name);

                    /* found an old version, delete it */
                    if(verbose) 
                        printf("VERBOSE Removing old collection at: %s\n",
                               old_coll_path);
                    ret = recursive_rmdir(old_coll_path);
                    if(ret < 0)
                    {
                        fprintf(
                            stderr, 
                            "Error: failed to remove old collection at: %s\n",
                            old_coll_path);
                        closedir(storage_dir);
                        return -1;
                    }
                    removed_olddirs = 1;
                }
            }

            if(removed_olddirs == 0)
            {
                printf("\nWARNING: No old collections with name \"%s\" "
                       "were found to cleanup.\n",
                       fs_config->file_system_name);
            }

            closedir(storage_dir);
        }
        else
        {
            /* complain if we don't recognize the version */
            fprintf(stderr, 
                    "Error: unknown collection version: %s\n"
                    "       for fs: %s (%08x).\n", 
                    version, fs_config->file_system_name, fs_config->coll_id);
            return -1;
        }
    }

    return 0;
}
PJ_DEF(pj_status_t) pjmedia_resample_create( pj_pool_t *pool,
					     pj_bool_t high_quality,
					     pj_bool_t large_filter,
					     unsigned channel_count,
					     unsigned rate_in,
					     unsigned rate_out,
					     unsigned samples_per_frame,
					     pjmedia_resample **p_resample)
{
    pjmedia_resample *resample;
    int type, err;

    PJ_ASSERT_RETURN(pool && p_resample && rate_in &&
		     rate_out && samples_per_frame, PJ_EINVAL);

    resample = PJ_POOL_ZALLOC_T(pool, pjmedia_resample);
    PJ_ASSERT_RETURN(resample, PJ_ENOMEM);

    /* Select conversion type */
    if (high_quality) {
	type = large_filter ? SRC_SINC_BEST_QUALITY : SRC_SINC_MEDIUM_QUALITY;
    } else {
	type = large_filter ? SRC_SINC_FASTEST : SRC_LINEAR;
    }

    /* Create converter */
    resample->state = src_new(type, channel_count, &err);
    if (resample->state == NULL) {
	PJ_LOG(4,(THIS_FILE, "Error creating resample: %s", 
		  src_strerror(err)));
	return PJMEDIA_ERROR;
    }

    /* Calculate ratio */
    resample->ratio = rate_out * 1.0 / rate_in;

    /* Calculate number of samples for input and output */
    resample->in_samples = samples_per_frame;
    resample->out_samples = rate_out / (rate_in / samples_per_frame);

    resample->frame_in = (float*) 
			 pj_pool_calloc(pool, 
					resample->in_samples + 8, 
					sizeof(float));

    resample->frame_out = (float*) 
			  pj_pool_calloc(pool, 
					 resample->out_samples + 8, 
					 sizeof(float));

    /* Set the converter ratio */
    err = src_set_ratio(resample->state, resample->ratio);
    if (err != 0) {
	PJ_LOG(4,(THIS_FILE, "Error creating resample: %s", 
		  src_strerror(err)));
	return PJMEDIA_ERROR;
    }

    /* Done */

    PJ_LOG(5,(THIS_FILE, 
	      "Resample using libsamplerate %s, type=%s (%s), "
	      "ch=%d, in/out rate=%d/%d", 
	      src_get_version(),
	      src_get_name(type), src_get_description(type),
	      channel_count, rate_in, rate_out));

    *p_resample = resample;

    return PJ_SUCCESS;
}