ftp.nice.ch/pub/next/unix/developer/_VoiceClass.s/VoiceClass/LPC/lpcresynth/lpcresynth.c

This is lpcresynth.c in view mode; [Download] [Up]

/*  lpcresynth routine by Perry R. Cook
			Stanford CCRMA 1991.
	This resynthesizes a sound from lpc
	data with optional modifications.  See
	lpcInstructions for details.
*/ 


#import <stdio.h>
#import <fcntl.h>
#import <string.h>
#import <math.h>
#import <sound/sound.h>

#define MAX_BLOCK 4096
#define MAX_ORDER 100
#define ONE_OVER_RANDLIMIT 9.313225e-10

main(ac,av) 
    int ac;
    char *av[]; 
{
    char *file_name_in,*source_file_in,*file_name_out;
    float coeffs[MAX_ORDER],Zs[MAX_ORDER],output,input;
    int fd,fd2,fdOut,n_read,n_read2,n_write;
    int i,j,k=0,hop_size,order,ticker;
    float pitch=100,last_pitch,pitchfactor=1.0,timemult=1.0;
    int sourceIn = 0;
    
    if (ac>=3)	{
	if (ac==3)	{
	    file_name_in = av[1];
             file_name_out = av[2];
         }
	if (ac>=5)	{
	    if (!strcmp(av[1],"-t")) {
	        timemult = atof(av[2]);
	    }
	    if (!strcmp(av[1],"-p")) {
	        pitchfactor = atof(av[2]);
	    }
	    if (!strcmp(av[1],"-s")) {
	        sourceIn = 1;
		source_file_in = av[2];
	    }
	    file_name_in = av[3];
             file_name_out = av[4];
         }
	if (ac>=7)	{
	    if (!strcmp(av[3],"-t")) {
	        timemult = atof(av[4]);
	    }
	    if (!strcmp(av[3],"-p")) {
	        pitchfactor = atof(av[4]);
	    }
	    if (!strcmp(av[3],"-s")) {
	        sourceIn = 1;
	        source_file_in = av[4];
	    }
	    file_name_in = av[5];
             file_name_out = av[6];
         }
	if (ac==9)	{
	    if (!strcmp(av[5],"-t")) {
	        timemult = atof(av[6]);
	    }
	    if (!strcmp(av[5],"-p")) {
	        pitchfactor = atof(av[6]);
	    }
	    if (!strcmp(av[5],"-s")) {
	        sourceIn = 1;
	        source_file_in = av[6];
	    }
	    file_name_in = av[7];
             file_name_out = av[8];
         }
	 fd = open(file_name_in,0);
	if (sourceIn) {
	    fd2 = open(source_file_in,0);
	    if (fd2==-1)	{
	        fprintf(stderr,"I can't find the sound input file!!\n");
		exit(0);
	    }
	}
	if (fd!=-1) 	{
	  if (!strcmp(file_name_in,file_name_out))	{
		fprintf(stderr,"Input and output files can't have same name!!\n");
		exit(0);
	   }
	   fdOut = creat(file_name_out, 0666);
	   n_read = read(fd,&order,4);
	   for (i=0;i<order;i++) Zs[i] = 0.0;
	   n_read = read(fd,&hop_size,4);
	   last_pitch = pitch;
	   n_read = read(fd,&pitch,4);
	   n_read = read(fd,&input,4);
	   if (pitchfactor>0) 	
		ticker = pitch / pitchfactor;
	   else
		ticker = 100.0/ fabs(pitchfactor);
	   hop_size *= timemult;
	   n_read = read(fd,coeffs,order*4);
	   while (n_read>0)	{
	       for (i=0;i<hop_size;i++)	{
		    output = 0.0;
		    if (!sourceIn) {
		        if ((pitch==0 && pitchfactor>0) || pitchfactor==0)
		            output = input  * 20.0 * (1.0 - random() * ONE_OVER_RANDLIMIT);
		        else {
		             ticker -= 1;
			    if (ticker <= 0) {
			        output = input * pitch * 3;
			        if (pitchfactor>0) 	
				    ticker = pitch / pitchfactor;
				else		{
				    output = input * last_pitch * 3;
				    ticker = last_pitch / fabs(pitchfactor);
				}
			    }
			}
		    }
		    else 	{
		        n_read2 = read(fd2,&output,4);
			if (!n_read2) {
			    fprintf(stderr,"I'm out of input samples!!!\n");
			    sourceIn = 0;
			}
		    }
        	    for (j=0;j<order;j++)  output += Zs[j]*coeffs[j];
    		    for (j=order-1;j>0;j--) Zs[j] = Zs[j-1];
    		    Zs[0] = output;
	             n_write = write(fdOut,&output,4);
	       }
	       k += hop_size;
	       printf ("%i \n",k);
	       if (pitch>0) last_pitch = pitch;
	       n_read = read(fd,&pitch,4);
	       n_read = read(fd,&input,4);
	       n_read = read(fd,coeffs,order*4);
	   }
	   close(fd);
	   close(fdOut);
         }
        else
    	    printf("I couldn't find your input file!!!\n");
    }
    else
    	printf("Format is lpcresynth [-t timemult] [-p pitchmult] [-s soundfile] infile.lpc outfile\n");
    return;
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.