All pastes #907248 Raw Edit

Something

public text v1 · immutable
·published 2008-02-17 12:35 UTC
rendered paste body
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <samplerate.h>
#include <sndfile.h>
#define _USE_MATH_DEFINES
#include <math.h>

#define REDFRAMES 256
int main(int argc, char *argv[])
{
	int freq,samprate,origsamp;
	float *buf,*buf2;
	double freq2pi,src_ratio=-1.0;
	SNDFILE	*infile,*outfile;
	SRC_DATA data;
	SF_INFO sfinfo;
	if((argc!=3)&&(argc!=5))
	{
		printf("Usage: inverter [-f freq] inputfile outputfile\n");
		printf("default freq=2500 Hz\n");
		printf("Example: inverter -f 3000 1.wav 2.wav\n");
		return 1;
	}
	freq=2500;
	if(strcmp(argv[1],"-f")==0)
		freq=atoi(argv[2]);
	samprate=freq<<1;
	if (strcmp(argv[argc-2],argv[argc-1])==0)
	{
		printf("Error: input and output file names are the same\n");
		exit(1);
	}
	memset(&sfinfo,0,sizeof(sfinfo));
	if (!(infile=sf_open(argv[argc-2],SFM_READ,&sfinfo)))
	{
		printf("Error: Not able to open input file '%s'\n",argv[argc-2]);
		exit(1);
	}
	origsamp=sfinfo.samplerate;
	printf("Input File   : %s\n",argv[argc-2]);
	printf("Sample Rate  : %d\n",origsamp);
	printf("Input Frames : %I64d\n\n",sfinfo.frames);
	printf("malloc %I64d bytes... ",(sfinfo.frames+REDFRAMES)*sfinfo.channels*4);
	src_ratio=(1.0*samprate)/sfinfo.samplerate;
	if(src_is_valid_ratio(src_ratio)==0)
	{
		printf("Error: Sample rate change out of valid range.\n");
		sf_close(infile);
		exit(1);
	}

	buf=new float[(size_t)((sfinfo.frames+REDFRAMES)*sfinfo.channels)];
	if(buf==NULL)
	{
		printf("Error\n");
		exit(1);
	}
	printf("Done\n");
	printf("Reading data... ");
    sf_count_t readcount=sf_readf_float(infile,buf,sfinfo.frames);
	if(readcount!=sfinfo.frames)
	{
		printf("Problem reading file, only %I64d frames will be converted",readcount);
	}
	printf("Done\n");
	sf_close(infile);
	printf("File 1 closed\nPrefiltering...\n");
	data.data_in=buf;
	data.input_frames=(long)readcount;
	data.src_ratio=src_ratio;
	data.output_frames=(long)(readcount*samprate/origsamp+REDFRAMES);
	printf("malloc %ld bytes... ",data.output_frames*sfinfo.channels*4);
	buf2=new float[data.output_frames*sfinfo.channels];
	if(buf2==NULL)
	{
		printf("Error\n");
		exit(1);
	}
	printf("Done\n");
	data.data_out=buf2;
	printf("Stage 1 input_frames=%ld output_frames=%ld src_ratio=%g\n",data.input_frames,data.output_frames,data.src_ratio);
	src_simple(&data,SRC_SINC_BEST_QUALITY,sfinfo.channels);
	printf("Used %ld frames, output %ld frames\n",data.input_frames_used,data.output_frames_gen);
	data.data_in=buf2;
	data.data_out=buf;
	data.input_frames=data.output_frames_gen;
	data.output_frames=(long)(sfinfo.frames+REDFRAMES);
	data.src_ratio=1.0/data.src_ratio;
	printf("Stage 2 input_frames=%ld output_frames=%ld src_ratio=%g\n",data.input_frames,data.output_frames,data.src_ratio);
	src_simple(&data,SRC_SINC_BEST_QUALITY,sfinfo.channels);
	printf("Used %ld frames, output %ld frames\n",data.input_frames_used,data.output_frames_gen);
	printf("Processing sine... ");
	freq2pi=M_PI*samprate;
	for(sf_count_t i=0;i<data.output_frames_gen;i++)
	{
		double modulator=sin(freq2pi*i/origsamp);
		for(int k=0;k<sfinfo.channels;k++)
			buf[i*sfinfo.channels+k]*=modulator;
	}
	printf("Done\nPostfiltering\n");
	data.data_in=buf;
	data.input_frames=data.output_frames_gen;
	data.src_ratio=src_ratio;
	data.output_frames=(long)(readcount*samprate/origsamp+REDFRAMES);
	data.data_out=buf2;
	printf("Stage 1 input_frames=%ld output_frames=%ld src_ratio=%g\n",data.input_frames,data.output_frames,data.src_ratio);
	src_simple(&data,SRC_SINC_BEST_QUALITY,sfinfo.channels);
	printf("Used %ld frames, output %ld frames\n",data.input_frames_used,data.output_frames_gen);
	data.data_in=buf2;
	data.data_out=buf;
	data.input_frames=data.output_frames_gen;
	data.output_frames=(long)(sfinfo.frames+REDFRAMES);
	data.src_ratio=1.0/data.src_ratio;
	printf("Stage 2 input_frames=%ld output_frames=%ld src_ratio=%g\n",data.input_frames,data.output_frames,data.src_ratio);
	src_simple(&data,SRC_SINC_BEST_QUALITY,sfinfo.channels);
	printf("Used %ld frames, output %ld frames\n",data.input_frames_used,data.output_frames_gen);
	delete [] buf2;
	remove(argv[argc-1]);
	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);
	}
	printf("Writing... ");
	sf_writef_float(outfile,buf,data.output_frames_gen);
	sf_close(outfile);
	printf("Done\n");
	delete [] buf;
	return 0;
}