示例#1
0
文件: main.c 项目: great-scott/MCP
int main (int argc, const char **argv) 
{
    // Global Parameters
	struct parameters params;
    
    // MPC header struct
    MPC_header mpc;
    mpc.header[0] = 'c';
    mpc.header[1] = 'h';
    mpc.header[2] = 's';
    mpc.header[3] = 'h';
    
    // Apple Header struct
    Apple_header apple;
    
    Apple_transientHeader appleTrans;
    
    Filler fill;
    
    // Declare pointer to the markers
    EDWORD* markers;
    
    // Declare input properties for portsf
    PSF_PROPS inputProps, outputProps;
    psf_format outputFormat = PSF_AIFF;
    psf_format inputFormat = PSF_STDWAVE;
    int inFile = -1, outFile = -1;
    
    inputProps.format = inputFormat;
    outputProps.format = outputFormat;
    
    int chunkSize, numberFrames, numBeats;
    
    //  ----------------------------------------------------------------------
	
	if (init(argc, argv, &params))
	{
		printf("Initialization error\n");
		return 1;
	}
    
    markers = get_mpc_info(&mpc, &params);
    if (markers == 0)
    {
        // Just exit program
        printf("No Transients Found");
        return 1;
    }
    
    
    Apple_transient *transients = (Apple_transient *) malloc(sizeof(Apple_transient) * mpc.numberMarkers);
    
    for (int j = 0; j < mpc.numberMarkers; j++) 
    {
        int loc = j * 2;
        
        transients[j].unknown1 = REVDWBYTES(65536);
        transients[j].framePosition = REVDWBYTES(*(markers + loc + 1));
        //printf("Frame Position: %d\n", transients[j].framePosition);
    }
    
    chunkSize = (mpc.numberMarkers * sizeof(*transients)) + sizeof(appleTrans) - 8;
    
    printf("Apple Transients Size: %lu\n", sizeof(*transients));
    printf("MPC Transients: %d\n", mpc.numberMarkers);
    printf("Total Number of Transient Bytes: %lu\n", mpc.numberMarkers * sizeof(*transients));
    printf("Apple Transient Header Size: %lu\n", sizeof(appleTrans));
    
    inFile = psf_sndOpen(params.inputFileName, &inputProps, 0);
    numberFrames = psf_sndSize(inFile);
    
    params.duration = numberFrames / inputProps.srate;
    printf("Number of Frames: %d\n", numberFrames);
    //printf("Byte Rate: %d\n", psf_sndByteRate(inFile));
    printf("Sample Rate: %d\n", inputProps.srate);
    printf("Duration: %d\n", params.duration);
    
    // Manually copy values from input wav file to output aiff
    outputProps.srate = inputProps.srate;
    outputProps.chans = inputProps.chans;
    outputProps.samptype = inputProps.samptype;
    outputProps.chformat = inputProps.chformat;
    
    outFile = psf_sndCreate(params.outputFileName, &outputProps, 0, 0, PSF_CREATE_RDWR);
    
    // Actually create the aiff file
    create_aiff(&params, &inputProps, &outputProps, inFile, outFile);
    
    // Close both the input files and output files
    psf_sndClose(inFile);
    psf_sndClose(outFile);
    
    numBeats = get_number_beats(&params);
    create_apple_header(&apple, &appleTrans, &fill, chunkSize, mpc.numberMarkers, numBeats);
    process_apple_header(&params, &apple, &appleTrans, transients, &mpc, &fill);
	// We're all done	
	cleanup(&params, markers);
	
	return 0;
}
示例#2
0
int main(int argc, char *argv[])
{

	long outframesize,thissize;
	int i,ofd;
	int ifdlist[MAX_INFILES];
	//int force_stype = -1,out_stype;
    int force_wave = 0;
	int infilearg,inchans;
	int num_infiles = 0;
	int halfsec;
    MYLONG peaktime;
	float *outframe	= NULL,*inframe = NULL;
	PSF_PROPS firstinprops,inprops;
	PSF_CHPEAK *fpeaks = NULL;
    int wave_type = -1;
    char *create_msg = NULL;
    psf_format informat = PSF_STDWAVE;
	char* p_dot = NULL;		/* to find extension of outfile */
	
	for(i=0;i < MAX_INFILES;i++)
		ifdlist[i] = -1;

    /* CDP version number */
    if(argc==2 && (stricmp(argv[1],"--version")==0)){
        printf("2.0.1.\n");
        return 0;
    }
    
	if(argc < 4) {
		fprintf(stderr,"interlx: insufficient arguments\n");
		usage();
		exit(1);
	}

	while(argv[1][0] =='-'){
		
		switch(argv[1][1]){
        case('t'):
			if(argv[1][2]=='\0'){
				fprintf(stderr,"-t flag requires parameter\n");
				usage();
				exit(1);
			}
			wave_type = atoi(&(argv[1][2]));
			if((wave_type < 0) || wave_type >= OPT_MAXOPTS){
				fprintf(stderr,"wave type out of range\n");
				usage();
				exit(1);
			}
			force_wave = 1;
			break;
		default:
			fprintf(stderr,"\nabfpan: error: illegal flag option %s",argv[1]);
			exit(1);
		}

		argc--; argv++;
	}

	if(argc < 4){
		fprintf(stderr,"interlx error: at least two infiles required!\n");
		usage();
		exit(1);
	}

	if(psf_init()){
		printf("Startup failure.\n");
		return  1;
	}
	
	//open first infile and get properties
	ifdlist[0] = psf_sndOpen(argv[2],&firstinprops,0);
	if(ifdlist[0] < 0){
		fprintf(stderr,"unable to open infile %s\n",argv[2]);
		cleanup(ifdlist);
		return 1;
	}

/* we don't know how to deal with speaker positions yet, so disregard these
	if(firstinprops.chformat > MC_STD){
		printf(stderr,"Warning,interlx: ignoring source file speaker positions\n");
	}
*/
	outframesize = psf_sndSize(ifdlist[0]);
	if(outframesize < 0){
		fprintf(stderr,"unable to read size of infile %s\n",argv[2]);
		cleanup(ifdlist);
		return 1;
	}
	inchans = firstinprops.chans;
	/*we can always allow more channels if someone really needs it! */
	if(!(inchans==1 || inchans==2)){
		fprintf(stderr,"interlx: error: infile %s has %d channels\n"
				"\t(only mono and stereo files can be used)\n",argv[2],inchans);
		cleanup(ifdlist);
		return 1;
	}
	
	num_infiles = 1;
	printf("interleaving %d-channel files,sample rate = %d\n",inchans,firstinprops.srate);

	infilearg = 3;
	while(argv[infilearg] != NULL){
		if(strcmp(argv[infilearg],"0")==0){
			ifdlist[num_infiles] = -1;           // mark silent channel
		}
		else{

			if((ifdlist[num_infiles] = psf_sndOpen(argv[infilearg],&inprops,0)) < 0){
				fprintf(stderr,"cannot open infile %s\n",argv[infilearg]);
				cleanup(ifdlist);
				return 1;
			}
			if(inprops.chans != firstinprops.chans){
				fprintf(stderr,"interlx: error: channel mismatch from infile %s\n",argv[infilearg]);
				cleanup(ifdlist);
				return 1;
			}

			if(inprops.srate != firstinprops.srate){
			   fprintf(stderr,"interlx: error: sample rate mismatch from infile %s\n",argv[infilearg]);
				cleanup(ifdlist);
				return 1;
			}

			thissize = psf_sndSize(ifdlist[num_infiles]);
			if(thissize < 0){
				fprintf(stderr,"unable to read size of infile %s\n",argv[infilearg]);
				cleanup(ifdlist);
				return 1;
			}			
			outframesize = max(outframesize,thissize);
		}
		
		infilearg++;
		num_infiles++;
		if(num_infiles > MAX_INFILES){
			fprintf(stderr,"Sorry! too many infiles. Maximum accepted is %d.\n",MAX_INFILES);
			cleanup(ifdlist);
			exit(1);
		}

	}

	
	inframe = malloc(inchans * sizeof(float));
	if(inframe==NULL){
		puts("interlx: error: no memory for input buffer!\n");
		cleanup(ifdlist);
		return 1;
	}

	firstinprops.chans *= num_infiles;
	outframe = (float *) malloc(firstinprops.chans * sizeof(float));
	if(outframe==NULL){
		puts("\ninterlx: error: no memory for output buffer!\n");
		cleanup(ifdlist);
		return 1;
	}

	fpeaks = (PSF_CHPEAK *) calloc(firstinprops.chans,sizeof(PSF_CHPEAK));
	if(fpeaks==NULL){
		puts("interlx: error: no memory for internal PEAK buffer\n");
		cleanup(ifdlist);
		return 1;
	}
    if(force_wave){
		int i,matched;
		
		switch(wave_type){
        case(OPT_WAVEX_GENERIC):
			inprops.chformat = MC_STD;
			informat = PSF_WAVE_EX;
			create_msg = "creating STD WAVE_EX file";
			break;
		case(OPT_WAVEX):
			switch(firstinprops.chans){
			case(1):
				firstinprops.chformat = MC_MONO;
				informat = PSF_WAVE_EX;
				create_msg = "creating MONO WAVE_EX file";
				break;
			case(2):
				firstinprops.chformat = MC_STEREO;
				informat = PSF_WAVE_EX;
				create_msg = "creating STEREO WAVE_EX file";
				break;
			case(4):
				firstinprops.chformat = MC_QUAD;
				informat = PSF_WAVE_EX;
				create_msg = "creating QUAD WAVE_EX file";
				break;
			default:
				fprintf(stderr,"infile nchans incompatible with requested WAVE-EX format\n");
				usage();
				cleanup(ifdlist);
				return 1;				
			}
			break;
		case(OPT_WAVEX_LCRS):
			if(firstinprops.chans != 4){
				fprintf(stderr,"result must have four channels\n");
				usage();
				cleanup(ifdlist);
				return 1;
			}
			firstinprops.chformat = MC_LCRS;
			informat = PSF_WAVE_EX;
			create_msg = "creating LCRS-surround WAVE_EX file";
			break;
		case(OPT_WAVEX_SURROUND):
			if(firstinprops.chans != 6){
				fprintf(stderr,"result must have six channels\n");
				usage();
				cleanup(ifdlist);
				exit(1);
			}
			firstinprops.chformat = MC_DOLBY_5_1;
			informat = PSF_WAVE_EX;
			create_msg = "creating 5.1 surround WAVE_EX file";
			break;
            
        case(OPT_SURR_5_0):
			if(firstinprops.chans != 5){
				fprintf(stderr,"result must have five channels.\n");
				usage();
				cleanup(ifdlist);
				return 1;
			}
			firstinprops.chformat = MC_SURR_5_0;
			informat = PSF_WAVE_EX;
			create_msg = "creating 5.0 surround WAVE_EX file";
			break;

		case(OPT_WAVEX_BFORMAT):
			matched = 0;
			for(i=0;i < N_BFORMATS;i++)	{
				if(firstinprops.chans == bformats[i]){
					matched = 1;
					break;
				}
			}
			if(!matched){
				printf("WARNING: No Bformat definition for %d-channel file.\n",inprops.chans);
			}
			firstinprops.chformat = MC_BFMT;
			informat = PSF_WAVE_EX;
			create_msg = "creating AMBISONIC B-FORMAT WAVE_EX file";
			break;
        case OPT_WAVEX_7_1:
            if(firstinprops.chans != 8){
				fprintf(stderr,"result must have  channels\n");
				usage();
				cleanup(ifdlist);
				return 1;
			}
			firstinprops.chformat = MC_SURR_7_1;
			informat = PSF_WAVE_EX;
			create_msg = "creating 7.1 surround WAVE_EX file";
			break;
        case OPT_WAVEX_CUBE:
            if(firstinprops.chans != 8){
				fprintf(stderr,"result must have  channels\n");
				usage();
				cleanup(ifdlist);
				return 1;
			}
			firstinprops.chformat = MC_CUBE;
			informat = PSF_WAVE_EX;
			create_msg = "creating cube surround WAVE_EX file";
			break;
		case OPT_WAVEX_6_1:
            if(firstinprops.chans != 7){
				fprintf(stderr,"result must have  channels\n");
				usage();
				cleanup(ifdlist);
				return 1;
			}
			firstinprops.chformat = MC_SURR_6_1;
			informat = PSF_WAVE_EX;
			create_msg = "creating 6.1 surround WAVE_EX file";
			break;
		default:
            inprops.chformat = STDWAVE;
			informat = PSF_STDWAVE;
			create_msg = "creating plain sound file";
			break;
		}		
	}
	
	/*  want to avoid WAVE_EX if just plain WAVE possible */
	firstinprops.format = informat;
		/*firstinprops.chformat = MC_STD;*/		/* RWD April 2006 do this here? */
	
	p_dot = strrchr(argv[1],'.');
	if(stricmp(++p_dot,"amb")==0) {
		int i;
		int matched = 0;
		firstinprops.chformat = MC_BFMT;
		for(i=0;i < N_BFORMATS;i++)	{
			if(firstinprops.chans == bformats[i]){
				matched = 1;
				break;
			}
		}
		if(!matched)
			printf("\nWARNING: channel count %d unknown for BFormat.\n",firstinprops.chans);

	}
    if(!is_legalsize(outframesize,&firstinprops)){
        fprintf(stderr,"error: outfile size %ld exceeds capacity of format.\n",outframesize);
        return 1;
    }
    printf("\n%s: %d channels, %ld frames.\n",create_msg,firstinprops.chans,outframesize);
	ofd = psf_sndCreate(argv[1],&firstinprops,0,0,PSF_CREATE_RDWR);
	
	
	if(ofd < 0){
		fprintf(stderr,"interlx: error: unable to create outfile %s.\n",argv[1]);
		cleanup(ifdlist);
		return 1;
	}

	halfsec = firstinprops.srate / 2;
	for(i=0;i < outframesize; i++){		// frame loop
		float *p_framesamp,*p_filesamp;
		int j;
		
		p_framesamp = outframe;
		memset((char *)outframe,0,firstinprops.chans * sizeof(float));

		for(j=0;j < num_infiles;j++) {	//file loop
			int k,got;
            
			memset((char *)inframe,0,inchans * sizeof(float));
			if(ifdlist[j] < 0){
				got = inchans; // placeholder - write silent channel
			}
			else{
				if((got = psf_sndReadFloatFrames(ifdlist[j],inframe,1)) < 0){
					fprintf(stderr,"interlx: error reading from infile %s\n",argv[2+j]);
					psf_sndClose(ofd);					
					cleanup(ifdlist);
					return 1;
				}
			}
			if(got==1){
				p_filesamp = inframe;
				for(k=0;k < inchans;k++)	//channel loop
					*p_framesamp++	= *p_filesamp++;
			}
			else
				p_framesamp += inchans;
		}
		
		if(psf_sndWriteFloatFrames(ofd,outframe,1) < 0){
			fprintf(stderr,"interlx: error writing to outfile\n");
			psf_sndClose(ofd);            
            cleanup(ifdlist);
            return 1;
		}
        if(i % halfsec==0) {
			printf("%.2lf secs\r",(double)i / (double) firstinprops.srate);
            fflush(stdout);
        }
	}
	printf("%.4lf secs\nWritten %ld sample frames to %s\n",(double)outframesize / (double)firstinprops.srate,outframesize,argv[1]);
	if(psf_sndReadPeaks( ofd,fpeaks,&peaktime)){
        printf("PEAK data:\n");
        for(i = 0; i < firstinprops.chans; i++) {
            double val, dbval;
            val = (double) fpeaks[i].val;
            dbval = 20.0 * log10(val);
            printf("CH %d: %.6f (%.2lfdB) at frame %u:\t%.4f secs\n",i,
                   val,dbval,(unsigned int) fpeaks[i].pos,(double)fpeaks[i].pos / (double) firstinprops.srate);
        }
    }
	
	printf("\n");
	psf_sndClose(ofd);
	free(inframe);
	free(outframe);
    if(fpeaks)
        free(fpeaks);
	cleanup(ifdlist);
    
	return 0;
}
示例#3
0
int main(int argc, char**argv)
{
	PSF_PROPS props;
	long framesread, totalread; 
	DWORD nFrames;	
	int size; 
	int limit;
	int N; // copy infile N times
	/* init all resource vals to default states */ 
	int ifd=-1, ofd=-1;
	int error=0;
	psf_format outformat = PSF_FMT_UNKNOWN; 
	PSF_CHPEAK* peaks = NULL;
	float* buffer = NULL;

	printf ("SF2FLOAT: convert soundfile to floats format.\n");

	if (argc!=ARG_NARGS)
	{
		printf("insufficient arguments.\n"
		       "USAGE:\tsf2float infile outfile buffer limit N\n");
		return 1;
	}
	
	/* startup portsf */
	if(psf_init())
	{
		printf("ERROR: unable to start portsf\n");
		return 1;
	}

	/* initialize buffer */
	nFrames = (DWORD)atoi(argv[ARG_BUFF]);
	if (nFrames < 1)
	{
		printf("ERROR: buffer size must be at least 1\n");
		return 1;
	}

	/* initialize limit */
	limit = atoi(argv[ARG_LIMIT]);
	if (limit<1)
	{
		printf("ERROR: size limit must be positive.\n");
		return 1;
	}

	/* initialize N */ 
	N = atoi(argv[ARG_N]);
	if (N<1)
	{
		printf("ERROR: N must be at least one.\n");
		return 1;
	}

	/* open infile */ 
	ifd = psf_sndOpen(argv[ARG_INFILE], &props, 0);
	if (ifd<0)
	{
		printf("ERROR: unable to open infile \"%s\"\n",argv[ARG_INFILE]);
		return 1;
	}

	/* we now have a resource, so we use goto hereafter
	   on hitting any error */
	/* get number of frames from infile */
	size = psf_sndSize(ifd);
	if(size<0)
	{
		printf("ERROR: unable to obtain the size of \"%s\"\n",argv[ARG_INFILE]);
		error++;
		goto exit;
	}
	/* check if copy limit is less than size */
	if(size<limit)
	{
		printf("ERROR: infile size is less than the copy limit.\n"
		       "infile:\t%s\n"
		       "infile size:\t%d frames\n"
		       "copy limit:\t%d frames\n",
		        argv[ARG_INFILE], size, limit);
		error++;
		goto exit;
	}
	
	/* tell user if source file is already floats */
	if (props.samptype==PSF_SAMP_IEEE_FLOAT)
		printf("Info: infile is already in floats format.\n");

	/* check if infile uses 8-bit samples*/ 
	if (props.samptype==PSF_SAMP_8)
	{
		printf("ERROR: sf2float does not support 8-bit format.\n");
		error++;
		goto exit;
	}

	if(!psf_sndInfileProperties(argv[ARG_INFILE],ifd,&props))
	{
		error++;
		goto exit;
	}	
	props.samptype=PSF_SAMP_IEEE_FLOAT; 

	/* check if outfile extension is one we know about */	
	outformat = psf_getFormatExt(argv[ARG_OUTFILE]);
	if (outformat == PSF_FMT_UNKNOWN)
	{
		printf("Outfile name \"%s\" has unknown format.\n"
		       "Use any of .wav .aiff .aif .afc .aifc\n",
		        argv[ARG_OUTFILE]);
		error++;
		goto exit;
	}
	props.format = outformat;

	/* create outfile */
	ofd = psf_sndCreate(argv[ARG_OUTFILE], &props, 0, 0, PSF_CREATE_RDWR);
	if (ofd<0)
	{
		printf("ERROR: unable to create outfile \"%s\"\n",argv[ARG_OUTFILE]);
		error++;
		goto exit;
	}
	/* allocate space for sample frames */
	if (limit<nFrames)
		nFrames = (DWORD)limit;
	buffer= (float*)malloc(props.chans*sizeof(float)*nFrames);
	if (buffer==NULL)
	{
		puts("No memory!\n");
		error++;
		goto exit;
	}

	/* and allocate space for PEAK info */
	peaks = (PSF_CHPEAK*)malloc(props.chans*sizeof(PSF_CHPEAK));
	if (peaks==NULL)
	{
		puts("No memory!\n");
		error++;
		goto exit;
	}

	printf("copying...\n");

	int i=0;
	int j;
	/* copy the infile N times */
	for (j=0; j<N; j++)
	{

		/* make sure to set nFrames to the correct value
		   every time you pass through the for loop */ 
		if (limit < atoi(argv[ARG_BUFF]))
			nFrames = (DWORD)limit;
		else
			nFrames = (DWORD)atoi(argv[ARG_BUFF]);

		totalread = 0; /* running count of sample frames */
		if(psf_sndSeek(ifd,0,PSF_SEEK_SET))
		{
			printf("ERROR: cannot reset infile\n");
			error++;
			goto exit;
		}
		/* nFrames loop to do copy, report any errors */
		framesread = psf_sndReadFloatFrames(ifd,buffer,nFrames);
		while (framesread>0&&totalread<limit)
		{
			i++;
			
			/* update copy status after refreshing the buffer every 100 times */
			if (i%100==0)
				printf("%ld samples copied...  %ld%%\r",totalread,100*totalread/size);

			totalread+=framesread;

			if(psf_sndWriteFloatFrames(ofd,buffer,nFrames)<0)
			{
				printf("Error writing to outfile.\n");
				error++;
				break;
			}
			/* make sure not to copy frames past the limit */ 
			if (nFrames+totalread > limit)
				nFrames = (DWORD)(limit-totalread); 

			framesread = psf_sndReadFloatFrames(ifd,buffer,nFrames);

		}
	}	
	totalread *= N; /* total number of frames copied */ 

	if(framesread<0)
	{
		printf("Error reading infile. Outfile is incomplete.\n");
		error++;
	}
	else
		printf("Done. %ld sample frames copied to %s\n",
		        totalread, argv[ARG_OUTFILE]);

	/* report PEAKS to user */
	if (psf_sndReadPeaks(ofd,peaks,NULL)>0)
	{
		long i;
		double peaktime;
		double peakDB;
		printf("Peak information:\n");
		for (i=0; i<props.chans; i++)
		{
			peaktime = (double)peaks[i].pos/props.srate; 
			if (peaks[i].val == 0.0)
				peaks[i].val = 1.0e-4;
			peakDB = log10(peaks[i].val);
			printf("CH %ld:\t%.4f\t(%.4f dB) at %.4f secs\n",
			        i+1, peaks[i].val, peakDB, peaktime);	
		}
	}
	
	/* do all the cleanup */
	exit:
	if (ifd>=0)
		psf_sndClose(ifd);
	if (ofd>=0)
		psf_sndClose(ofd);
	if (buffer)
		free(buffer);
	if (peaks)
		free(peaks);
	psf_finish();
	 	
	return error;
}
示例#4
0
int main(int argc, char**argv)  {
    long inframes,framesread=0,total_framesread;
    double maxsamp = 0.0,startpos = 0.0,endpos;
    long startframe = 0,endframe;
    long halfsecframes = 0;
    int i,j,ifd = -1;
    int chans;
    int do_norm = 0;
	PSF_PROPS inprops;
    CH_RMSINFO* rmsinfo = NULL;
    double* rmsfac  = 0;
    double* ampsum  = 0;
    double* ampsumb = 0;
    double* inbuf   = 0;
    double* nrmsfac = 0;
    double* nampsum = 0;
    
    
    /* CDP version number */
    if(argc==2 && (stricmp(argv[1],"--version")==0)){
        printf("1.0.1\n");
        return 0;
    }
#ifdef WIN32    
# if _MSC_VER && _MSC_VER <= 1200
     _set_new_handler( newhandler );
# endif
#endif
    if(psf_init()){
		puts("unable to start portsf\n");
		return 1;
	}
    if(argc < 2){		 
		usage();
		return(1);
	}
    while(argv[1][0]=='-'){
		switch(argv[1][1]){
        case 'n':
            do_norm = 1;
            break;
        default:
            fprintf(stderr, "Unrecognised flag option %s\n",argv[1]);
            return 1;
        }
        argc--; argv++;
    }
    if(argc < 2){		 
		usage();
		return(1);
	}
    if((ifd = psf_sndOpen(argv[ARG_INFILE],&inprops, 0)) < 0){
		fprintf(stderr,"\nUnable to open input soundfile %s",argv[ARG_INFILE]);
		return(1);
	}
	inframes = psf_sndSize(ifd);  // m/c frames
    endframe = inframes;
    
	if(inframes <= 0)
        return 0;
	if(argc >= 3) {
        long lpos;
        startpos = atof(argv[ARG_INFILE+1]);
        if(startpos < 0.0){
            fprintf(stderr,"Error: startpos must be positive\n");
            return 1;
        }
        lpos = (long)( startpos * inprops.srate);
        if(lpos > inframes){
            fprintf(stderr,"Error: startpos value beyond end of file.\n");
            return 1;
        }
        startframe = lpos;
    }
    if(argc >= 4) {
        long lpos;
        endpos = atof(argv[ARG_INFILE+2]);
        
        lpos = (long)(endpos * inprops.srate);
        if(lpos > inframes){
            fprintf(stderr,"Warning: endpos value too large - reset to end of file.\n");
            return 1;
        }
        endframe = lpos;
        if(!(endframe > startframe)){
            fprintf(stderr,"Error: endpos must be beyond startpos.\n");
            return 1;
        }
    }
    if(startframe)
        printf("Starting at frame %ld, ending at frame %ld\n",startframe, endframe);
    
    chans   = inprops.chans;
    try {
        inbuf   = new double[BUFLEN * chans];
        rmsinfo = new CH_RMSINFO[chans];
        rmsfac  = new double[chans];
        ampsum  = new double[chans];
        ampsumb = new double[chans];
        nrmsfac = new double[chans];
        nampsum = new double[chans];
    }
    catch(...){
        fputs("no memory!\n",stderr);
        return 1;
    }
    for(i=0; i < chans; i++){
        rmsfac[i]  = 0.0;
        ampsum[i]  = 0.0;
        ampsumb[i] = 0.0;
        nrmsfac[i] = 0.0;
        nampsum[i] = 0.0;
    }
    halfsecframes = inprops.srate / 2; 
    signal(SIGINT,runhandler);
    long wanted = endframe - startframe;
    printf("Scanning %ld frames (%.3lf secs):\n",wanted, (double)wanted / inprops.srate);
    total_framesread = 0;
    if(startframe) {
        if(psf_sndSeek(ifd,startframe,PSF_SEEK_SET)){
            fprintf(stderr,"File Seek error.\n");
            return 1;
        }
    }
        
	while((framesread = psf_sndReadDoubleFrames(ifd,inbuf,BUFLEN)) > 0){
        double fval;
        for(i = 0;i < framesread;i++) {
            for(j = 0; j < chans; j++){
                double val = inbuf[i*chans + j];
                fval = fabs(val);
                maxsamp = fval >  maxsamp ? fval : maxsamp;
                ampsum[j]   += fval;
                rmsfac[j]   += val*val; 
                ampsumb[j]  += val;
            }
            total_framesread++;
            if(scanning==0)
                break;
            if(total_framesread == wanted) 
                break;
            if((total_framesread % halfsecframes) == 0){
                printf("%.2lf\r",total_framesread / (double) inprops.srate);
                fflush(stdout);
            }
        }
        if(total_framesread == wanted) {
            break;
        }
    }
    if(framesread < 0){
        fprintf(stderr,"Error reading file.\n");
        return 1;
    }
    for(i=0;i < chans;i++){
        rmsfac[i]  /= total_framesread;
        rmsfac[i]   = sqrt(rmsfac[i]);
        ampsum[i]  /= total_framesread;
        ampsumb[i] /= total_framesread;
    }

    double normfac = 1.0 / maxsamp;
    if(scanning==0)
        printf("\nScan stopped.\n");
    if(total_framesread < inframes){
        printf("Scanned %ld frames (%.2lf secs).\n",total_framesread,total_framesread / (double)inprops.srate);
    }
    
    printf("Maximum sample = %lf (%.2lfdB)\n",maxsamp,20.0 * log10(maxsamp));
    printf("Maximum normalisation factor = %.4f\n",normfac);
    
    for(i=0;i < chans;i++){
        rmsinfo[i].rmspower    = rmsfac[i];
        rmsinfo[i].abssum      = ampsum[i];
        rmsinfo[i].bisum       = ampsumb[i];
        rmsinfo[i].norm_rms    = normfac * rmsfac[i];
        rmsinfo[i].norm_abssum = normfac * ampsum[i];
    }
    if(do_norm){
        printf("\t   RMS LEVEL\t    AVG  \t   NET DC\t   NORM RMS\t  NORM AVG\n");
        printf("CH\t AMP\t DB\t AMP\t DB\t AMP\t DB\t AMP\t DB\t AMP\t DB     \n");
    }
    else{
        printf("\t   RMS LEVEL\t    AVG  \t   NET DC\n");
        printf("CH\t AMP\t DB\t AMP\t DB\t AMP\t DB\n"); 
    }
    for(i=0;i < chans;i++){
        double d1,d2,d3,d4,d5;
        
        d1 = 20*log10(rmsfac[i]);
        d2 = 20*log10(ampsum[i]);
        d3 = 20*log10(fabs(ampsumb[i]));
        d4 = 20*log10(normfac * rmsfac[i]);
        d5 = 20*log10(normfac * ampsum[i]);
        if(do_norm){
            printf("%d\t%.5lf\t%.2lf\t%.5lf\t%.2lf\t%+.4lf\t%.2lf\t%.5lf\t%.2lf\t%.5lf\t%.2lf\n",i+1,
               rmsinfo[i].rmspower,d1,
               rmsinfo[i].abssum,d2,
               rmsinfo[i].bisum,d3,
               rmsinfo[i].norm_rms,d4,
               rmsinfo[i].norm_abssum,d5
            );
        }
        else {
            printf("%d\t%.5lf\t%.2lf\t%.5lf\t%.2lf\t%+.4lf\t%.2lf\n",i+1,
                   rmsinfo[i].rmspower,d1,
                   rmsinfo[i].abssum,d2,
                   rmsinfo[i].bisum,d3
            );
        }
    }
        
    delete [] inbuf;
    delete [] rmsfac;
    delete [] ampsum;
    delete [] ampsumb;
    delete [] rmsinfo;
    psf_sndClose(ifd);
    psf_finish();

	return 0;
}
示例#5
0
int main(int argc, char**argv)
{
	PSF_PROPS props;
	long framesread, totalread; 
	DWORD nFrames;	
	int size; 
	int limit;
	int N; // copy infile N times
	float ampfac; 
	float dbval;

	/* init all resource vals to default states */ 
	int ifd=-1, ofd=-1;
	int error=0;
	psf_format outformat = PSF_FMT_UNKNOWN; 
	PSF_CHPEAK* peaks = NULL;
	float* buffer = NULL;

	/* init flags for command-line options */ 
	int isamp=0; // default scale factor is in dBs 

	printf ("SFGAIN: change level of soundfile.\n");

	if ((argc<ARG_NOPS)||(argc>ARG_OPS))
	{
		printf("insufficient arguments.\n"
		       "USAGE:\tsfgain infile outfile buffer limit N [dBval | -a ampfac]\n"
		       "dBval must be <= 0 or ampfac must be > 0\n"); 
		return 1;
	}

	/* check for command-line options */
	if (argc==ARG_OPS)
	{
		if (argv[ARG_OP][0]=='-')	
		{
			if (argv[ARG_OP][1]=='a')
				isamp=1;
			else
			{
				printf("ERROR: %s is not a valid command-line option.\n"
				       "USAGE:\tsfgain infile outfile buffer limit N [dBval | -a ampfac]\n"
				       "dBval must be <= 0 or ampfac must be > 0\n", argv[ARG_OP]); 
				return 1;
			}
		}
	}
	
	/* startup portsf */
	if(psf_init())
	{
		printf("ERROR: unable to start portsf\n");
		return 1;
	}

	/* initialize buffer */
	nFrames = (DWORD)atoi(argv[ARG_BUFF]);
	if (nFrames < 1)
	{
		printf("ERROR: buffer size must be at least 1\n");
		return 1;
	}

	/* initialize limit */
	limit = atoi(argv[ARG_LIMIT]);
	if (limit<1)
	{
		printf("ERROR: size limit must be positive.\n");
		return 1;
	}

	/* initialize N */ 
	N = atoi(argv[ARG_N]);
	if (N<1)
	{
		printf("ERROR: N must be at least one.\n");
		return 1;
	}

	/* initialize dBval or ampfac */
	if (isamp)
	{	
		ampfac = atof(argv[ARG_AMP]);
		if (ampfac <= 0.0)
		{
			printf("ERROR: ampfac must be positive.\n");
			return 1;
		}		
		if (ampfac == 1.0)
		{
			printf("ERROR: an ampfac of 1 creates an outfile "
			       "       identicle to the infile\n");
			return 1;
		}
	}	
	else
	{
		dbval = atof(argv[ARG_DB]);
		if (dbval > 0.0)
		{
			printf("ERROR: dBval cannot be positive.\n");
			return 1;
		}
		if (dbval==0.0)
		{
			printf("ERROR: dBval of 0 creates an outfile "
						 "identicle to the infile\n");  
			return 1;
		}
		/* convert dB to amps */
		ampfac = pow(10.0, dbval/20.0);
	}

	/* open infile */ 
	ifd = psf_sndOpen(argv[ARG_INFILE], &props, 0);
	if (ifd<0)
	{
		printf("ERROR: unable to open infile \"%s\"\n",argv[ARG_INFILE]);
		return 1;
	}

	/* we now have a resource, so we use goto hereafter
		 on hitting any error */
	/* get number of frames from infile */
	size = psf_sndSize(ifd);
	if(size<0)
	{
		printf("ERROR: unable to obtain the size of \"%s\"\n",argv[ARG_INFILE]);
		error++;
		goto exit;
	}
	/* check if copy limit is less than size */
	if(size<limit)
	{
		printf("ERROR: infile size is less than the copy limit.\n"
		       "infile:\t%s\n"
		       "infile size:\t%d frames\n"
		       "copy limit:\t%d frames\n",
		        argv[ARG_INFILE], size, limit);
		error++;
		goto exit;
	}
	
	/* check if infile uses 8-bit samples*/ 
	if (props.samptype==PSF_SAMP_8)
	{
		printf("ERROR: sfgain does not support 8-bit format.\n");
		error++;
		goto exit;
	}

	/* display infile properties */
	if(!psf_sndInfileProperties(argv[ARG_INFILE],ifd,&props))
	{
		error++;
		goto exit;
	}	

	/* check if outfile extension is one we know about */	
	outformat = psf_getFormatExt(argv[ARG_OUTFILE]);
	if (outformat == PSF_FMT_UNKNOWN)
	{
		printf("Outfile name \"%s\" has unknown format.\n"
		       "Use any of .wav .aiff .aif .afc .aifc\n",
		        argv[ARG_OUTFILE]);
		error++;
		goto exit;
	}
	props.format = outformat;

	/* create outfile */
	ofd = psf_sndCreate(argv[ARG_OUTFILE], &props, 0, 0, PSF_CREATE_RDWR);
	if (ofd<0)
	{
		printf("ERROR: unable to create outfile \"%s\"\n",argv[ARG_OUTFILE]);
		error++;
		goto exit;
	}
	/* allocate space for sample frames */
	if (limit<nFrames)
		nFrames = (DWORD)limit;
	buffer= (float*)malloc(props.chans*sizeof(float)*nFrames);
	if (buffer==NULL)
	{
		puts("No memory!\n");
		error++;
		goto exit;
	}

	/* and allocate space for PEAK info */
	peaks = (PSF_CHPEAK*)malloc(props.chans*sizeof(PSF_CHPEAK));
	if (peaks==NULL)
	{
		puts("No memory!\n");
		error++;
		goto exit;
	}

	printf("copying...\n");

	int update=0; 
	int loop;
	int i;
	int j;

	/* copy the infile N times */
	for (loop=0; loop<N; loop++)
	{

		/* make sure to set nFrames to the correct value
			 every time you pass through the for loop */ 
		if (limit < atoi(argv[ARG_BUFF]))
			nFrames = (DWORD)limit;
		else
			nFrames = (DWORD)atoi(argv[ARG_BUFF]);

		totalread = 0; /* running count of sample frames */
		if(psf_sndSeek(ifd,0,PSF_SEEK_SET))
		{
			printf("ERROR: cannot reset infile\n");
			error++;
			goto exit;
		}
		/* nFrames loop to do copy, report any errors */
		framesread = psf_sndReadFloatFrames(ifd,buffer,nFrames);
		while (framesread>0&&totalread<limit)
		{
			update++;
			
			/* update copy status after refreshing the buffer every 100 times */
			if (update%100==0)
				printf("%ld samples copied...  %ld%%\r",totalread,100*totalread/size);

			totalread+=framesread;
		
			/* change sample values by amp factor */
			for (j=0; j<nFrames; j++)
				for (i=0; i<props.chans; i++)	
					buffer[props.chans*j+i] *= ampfac; 
			if(psf_sndWriteFloatFrames(ofd,buffer,nFrames)<0)
			{
				printf("Error writing to outfile.\n");
				error++;
				break;
			}
			/* make sure not to copy frames past the limit */ 
			if (nFrames+totalread > limit)
				nFrames = (DWORD)(limit-totalread); 

			framesread = psf_sndReadFloatFrames(ifd,buffer,nFrames);

		}
	}	
	totalread *= N; /* total number of frames copied */ 

	if(framesread<0)
	{
		printf("Error reading infile. Outfile is incomplete.\n");
		error++;
	}
	else
		printf("Done. %ld sample frames copied to %s\n",
						totalread, argv[ARG_OUTFILE]);

	/* report PEAKS to user */
	if (psf_sndReadPeaks(ofd,peaks,NULL)>0)
	{
		long i;
		double peaktime;
		double peakDB;
		printf("Peak information:\n");
		for (i=0; i<props.chans; i++)
		{
			peaktime = (double)peaks[i].pos/props.srate; 
			if (peaks[i].val == 0.0)
				peaks[i].val = 1.0e-4;
			peakDB = log10(peaks[i].val);
			printf("CH %ld:\t%.4f\t(%.4f dB) at %.4f secs\n",
			        i+1, peaks[i].val, peakDB, peaktime);	
		}
	}
	
	/* do all the cleanup */
	exit:
	if (ifd>=0)
		psf_sndClose(ifd);
	if (ofd>=0)
		psf_sndClose(ofd);
	if (buffer)
		free(buffer);
	if (peaks)
		free(peaks);
	psf_finish();
	 	
	return error;
}