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

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

/*
 *     Generated by the NeXT Interface Builder.
 */

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

struct headerform {
    char pref[4];
    int hdr_length;
    int file_length;
    int mode;
    int samp_rate;
    int num_channels;
    char comment[8];
};

main(ac,av) 
    int ac;
    char *av[]; 
{
    char *file_name_in,*file_name_out;
    char mode[4],file_name[100];
    short data[5000];
    unsigned char codec_data[5000];
    float float_data[5000],gain=1.0,max = 0.0;
    struct headerform hdr = {".snd",32,0,3,44100,1,"HDRHack"};

    int i,j,fd,fd2,n_read,n_write;
	
    if (ac>=3)	{
        if (ac==3)	{
	    file_name_in = av[1];
    	    file_name_out = av[2];
	}
	if (ac>=5)	{
    	    if (!strcmp(av[1],"-s"))	{
	        hdr.samp_rate = atoi(av[2]);
    	    }
	    if (!strcmp(av[1],"-g"))	{
	        gain = atof(av[2]);
    	    }
	    file_name_in = av[3];
    	    file_name_out = av[4];
    	}
	if (ac==7)	{
    	    if (!strcmp(av[3],"-s"))	{
	        hdr.samp_rate = atoi(av[4]);
    	    }
	    if (!strcmp(av[3],"-g"))	{
	        gain = atof(av[4]);
    	    }
    	    file_name_in = av[5];
    	    file_name_out = av[6];
    	}

        fd = open(file_name_in,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);
	}
	fd2 = creat(&file_name_out[0], 0644);
	i = 0;
	n_read = 10000;
	while (n_read>0)	{
		n_read = read(fd,&float_data[0],10000);
	    	if (gain==0.0)	for (j=0;j<n_read/4;j++) if (fabs((double)float_data[j])>max) max = fabs((double)float_data[j]);
		i += n_read/2;
	}
	if (gain==0.0) gain = 32000.0 / max;
	hdr.file_length = i;
	if (hdr.samp_rate==8012)	{
	    hdr.mode = 1; 
	    hdr.file_length /= 2;
	}
	close(fd);
         fd = open(file_name_in,0);
	n_write = write(fd2,&hdr,hdr.hdr_length);
	n_read = 10000;
	while (n_read>0)	{
	    if (hdr.samp_rate==8012)	{
	        n_read = read(fd,float_data,20000);
	        for (i=0;i<n_read/4;i++) codec_data[i] = SNDMulaw((short) (float_data[i] * gain));
	        n_write = write(fd2,&codec_data,n_read/4);
	    }
	    else	{
	        n_read = read(fd,float_data,20000);
	        for (i=0;i<n_read/4;i++) data[i] = (short) (float_data[i] * gain);
	        n_write = write(fd2,&data,n_read/2);
	    }
	}
	close(fd);
	close(fd2);
        }
        else
    	    printf("I couldn't find your input file!!!\n");
    }
    else
    	printf("Format is unfloat [-g gain] [-s samplingrate] infile.snd outfile.snd\n");
    return;
}

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