コード例 #1
0
ファイル: Resample.cpp プロジェクト: tuanmasterit/audacity
   VarRateResample::VarRateResample(const bool useBestMethod, const double dMinFactor, const double dMaxFactor)
      : Resample()
   {
      this->SetMethod(useBestMethod);
      if (!src_is_valid_ratio (dMinFactor) || !src_is_valid_ratio (dMaxFactor)) {
         fprintf(stderr, "libsamplerate supports only resampling factors between 1/SRC_MAX_RATIO and SRC_MAX_RATIO.\n");
         // FIX-ME: Audacity will hang after this if branch.
         mHandle = NULL;
         return;
      }

      int err;
      SRC_STATE *state = src_new(mMethod, 1, &err);
      mHandle = (void *)state;
      mShouldReset = false;
      mSamplesLeft = 0;
   }
コード例 #2
0
ファイル: misc_test.c プロジェクト: CoolOppo/audacity
static void
src_ratio_test (void)
{	int k ;

	puts ("    src_ratio_test (SRC ratio must be in range [1/12, 12]):" ) ;


	for (k = 0 ; k < ARRAY_LEN (ratio_test) ; k++)
	{	if (ratio_test [k].should_pass && src_is_valid_ratio (ratio_test [k].ratio) == 0)
		{	printf ("\n\nLine %d : SRC ratio %f should have passed.\n\n", __LINE__, ratio_test [k].ratio) ;
			exit (1) ;
			} ;
		if (! ratio_test [k].should_pass && src_is_valid_ratio (ratio_test [k].ratio) != 0)
		{	printf ("\n\nLine %d : SRC ratio %f should not have passed.\n\n", __LINE__, ratio_test [k].ratio) ;
			exit (1) ;
			} ;
		printf ("\t SRC ratio (%8.5f) : %s .................... ok\n", ratio_test [k].ratio,
			(ratio_test [k].should_pass ? "pass" : "fail")) ;
		} ;

	puts ("") ;

	return ;
} /* src_ratio_test */
コード例 #3
0
ファイル: effects.c プロジェクト: akshaal/demovibes
void* fx_resample_init(int channels, int sr_from, int sr_to)
{
    assert(channels >= 1 && channels <= MAX_CHANNELS);
    int err = 0;
    struct fx_resampler* r = util_malloc(sizeof(struct fx_resampler));
    r->channels = channels;
    r->ratio    = (double)sr_to / sr_from;
    if (!src_is_valid_ratio(r->ratio))
        goto error;
    for (int ch = 0; ch < channels; ch++) {
        r->state[ch] = src_new(SRC_SINC_FASTEST, 1, &err);
        if (err)
            goto error;
    }
    LOG_DEBUG("[resample] init, %d channels, %f ratio", channels, r->ratio);
    return r;

error:
    LOG_ERROR("[resample] init failed (%s)", src_strerror(err));
    fx_resample_free(r);
    return NULL;
}
コード例 #4
0
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 */
コード例 #5
0
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;
}
コード例 #6
0
ファイル: audiodata.c プロジェクト: 1Programmer/splayer
AUDIODATA_EXPORT
float* readaudio(const char *filename, const int sr, float *sigbuf, unsigned int *buflen,\
                 const float nbsecs, AudioMetaData *mdata, int *error)
{
  long orig_sr;
  unsigned int orig_length = 0;
  float *inbuffer = NULL;
  *error = PHERR_SUCCESS;

  if (filename == NULL || buflen == NULL) {
    *error = PHERR_NULLARG;
    return NULL;
  }
  if (mdata) init_mdata(mdata);

  const char *suffix = strrchr(filename, '.');

  if (*suffix != '\0' && !strncasecmp(suffix+1, "mp3",3)) {
#ifdef HAVE_MPG123
    inbuffer = readaudio_mp3(filename, &orig_sr, &orig_length, nbsecs, mdata, error);
#endif /* HAVE_MPG123 */
  } else {
    inbuffer = readaudio_snd(filename, &orig_sr, &orig_length, nbsecs, mdata, error);
  }  

  if (inbuffer == NULL){
    return NULL;
  }

  /* if no data extracted for title, use the file name */ 
  if (mdata && mdata->title2 == NULL){
      char *name = strrchr(filename, '/');
      if (name == NULL) name = strchr(filename, '\\');
      if (name) mdata->title2 = strdup(name+1);
  }

  /* resample float array */ 
  /* set desired sr ratio */ 
  double sr_ratio = (double)(sr)/(double)orig_sr;
  if (src_is_valid_ratio(sr_ratio) == 0){
    *error = PHERR_BADSR;
    free(inbuffer);
    return NULL;
  }

  /* allocate output buffer for conversion */ 
  unsigned int outbufferlength = sr_ratio*(orig_length);

  float *outbuffer = NULL;
  if (sigbuf && outbufferlength < *buflen){
    outbuffer = sigbuf;
  } else {
    outbuffer = (float*)malloc(outbufferlength*sizeof(float));
  }

  if (!outbuffer){
    free(inbuffer);
    *error = PHERR_NOBUFALLOCD;
    return NULL;
  }

  SRC_STATE *src_state = src_new(SRC_LINEAR, 1, error);
  if (!src_state){
    *error = PHERR_SRCCONTXT;
    free(inbuffer);
    if (outbuffer != sigbuf) free(outbuffer);
     return NULL;
  }

  SRC_DATA src_data;
  src_data.data_in = inbuffer;
  src_data.data_out = outbuffer;
  src_data.input_frames = orig_length;
  src_data.output_frames = outbufferlength;
  src_data.end_of_input = SF_TRUE;
  src_data.src_ratio = sr_ratio;

  /* sample rate conversion */ 
  *error = src_process(src_state, &src_data);
  if (*error){
    *error = PHERR_SRCPROC;
    free(inbuffer);
    if (outbuffer != sigbuf) free(outbuffer);
    src_delete(src_state);
    return NULL;
  }

  *buflen = outbufferlength;

  src_delete(src_state);
  free(inbuffer);

  return outbuffer;
} 
コード例 #7
0
ファイル: audiophash.cpp プロジェクト: HeeJaeYo/pHash
float* ph_readaudio2(const char *filename, int sr, float *sigbuf, int &buflen, const float nbsecs) {

    long orig_sr;
    float *inbuffer = NULL;
    unsigned int inbufferlength;
    buflen = 0;

    const char *suffix = strrchr(filename, '.');
    if (suffix == NULL) return NULL;
    if (!strcasecmp(suffix+1, "mp3")) {
#ifdef HAVE_LIBMPG123
        inbuffer = readaudio_mp3(filename, &orig_sr, nbsecs, &inbufferlength);
#endif /* HAVE_LIBMPG123 */
    } else {
        inbuffer = readaudio_snd(filename, &orig_sr, nbsecs, &inbufferlength);
    }

    if (inbuffer == NULL) {
        return NULL;
    }

    /* resample float array */
    /* set desired sr ratio */
    double sr_ratio = (double)(sr)/(double)orig_sr;
    if (src_is_valid_ratio(sr_ratio) == 0) {
        free(inbuffer);
        return NULL;
    }

    /* allocate output buffer for conversion */
    unsigned int outbufferlength = sr_ratio*inbufferlength;
    float *outbuffer = (float*)malloc(outbufferlength*sizeof(float));
    if (!outbuffer) {
        free(inbuffer);
        return NULL;
    }

    int error;
    SRC_STATE *src_state = src_new(SRC_LINEAR, 1, &error);
    if (!src_state) {
        free(inbuffer);
        free(outbuffer);
        return NULL;
    }

    SRC_DATA src_data;
    src_data.data_in = inbuffer;
    src_data.data_out = outbuffer;
    src_data.input_frames = inbufferlength;
    src_data.output_frames = outbufferlength;
    src_data.end_of_input = SF_TRUE;
    src_data.src_ratio = sr_ratio;

    /* sample rate conversion */
    if (error = src_process(src_state, &src_data)) {
        free(inbuffer);
        free(outbuffer);
        src_delete(src_state);
        return NULL;
    }

    buflen = src_data.output_frames;

    src_delete(src_state);
    free(inbuffer);

    return outbuffer;
}