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

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

#include <stdio.h>
#include <sound/sound.h>
#include <math.h>


/*  	********************TIME SHIFT**************************************************/

main(int argc,char *argv[])	/* convolve two files */
{
    short *data_in,*data_out;
    unsigned char *codec_data_in,*codec_data_out;
    char *file_in,*file_out;
    SNDSoundStruct *sound_in, *sound_out, *sound_out2;
    int num_samples,i,j,hopIn,hopOut,frame,num_frames;
    int timeIn,timeInMax,timeOut,timeOutMax;
    float factor;
    double gain,delta;

    if (argc<4) {
        fprintf(stderr,"usage: timeshift factor [hopsize] filein fileout\n");
	exit(0);
    }
    factor = atof(argv[1]);

    if (argc==4) {
        file_in = argv[2];
	file_out = argv[3];
    }
    else {
        file_in = argv[3];
	file_out = argv[4];
    }
    
    if (SNDReadSoundfile(file_in,&sound_in)) 	{
        fprintf(stderr,"Can't open first input file!!\n");
	exit(0);
    }
    hopOut = 0.08 * sound_in->samplingRate;
    if (argc>4) hopOut = atoi(argv[2]);
    hopIn = hopOut / factor;
    frame = 0.75 * hopOut;
    delta = 0.5 / (double) frame / sound_in->channelCount;
   SNDAlloc(&sound_out, (sound_in->dataSize + (hopOut + hopIn) * 2)* factor, sound_in->dataFormat,
   		 sound_in->samplingRate, sound_in->channelCount, 12) ;

    
    printf("Time shifting %s by %f to output file %s\n",file_in,factor,file_out);

    if (sound_in->dataFormat==3) 	{
        num_samples = sound_in->dataSize / 2 / sound_in->channelCount;
        data_in = (short *) sound_in + sound_in->dataLocation;
        data_out = (short *) sound_out + sound_out->dataLocation;
    }
    else	{
        SNDAlloc(&sound_out2, (sound_in->dataSize + (hopOut + hopIn)) * factor * 2, 3,
   		 22050, 1, 12) ;
        num_samples = sound_in->dataSize;
        data_out = (short*) sound_out2 + sound_out2->dataLocation;
	codec_data_in =  (unsigned char*) sound_in + sound_in->dataLocation;
        codec_data_out =  (unsigned char*) sound_out + sound_out->dataLocation;
    }
    
    num_frames = (num_samples - hopOut) / hopIn;
    printf("Number of Frames to do: %i\n",num_frames);

    if (sound_in->dataFormat==3) 	{
      for (i=0;i<num_samples * factor;i++) data_out[i] = 0;
      if (sound_in->channelCount == 1)	{
          for (i=0;i<num_frames;i++)	{
	    timeIn = hopIn * i;
	    timeInMax = timeIn + 3 * frame;
	    timeOut = hopOut * i;
	    timeOutMax = timeOut + 3 * frame;
	    gain = 0.0;
	    for (j=1;j<frame;j++)   {
	        gain += delta;
	        data_out[j+timeOut] += gain * data_in[j + timeIn];
	        data_out[timeOutMax-j] += gain * data_in[timeInMax-j];
	    }
	    for (j=frame;j<=frame*2;j++)  {
	        data_out[j+timeOut] += gain * data_in[j + timeIn];
	    }
    	  printf("%i     ",i);
          }
      }
      else 	{
          for (i=0;i<num_frames;i++)	{
	    timeIn = hopIn * i;
	    timeInMax = timeIn + 3 * frame;
	    timeOut = hopOut * i;
	    timeOutMax = timeOut + 3 * frame;
	    gain = 0.0;
	    for (j=1;j<frame;j++)   {
	        gain += delta;
	        data_out[2*(j+timeOut)] += gain * data_in[2*(j + timeIn)];
	        data_out[2*(timeOutMax-j)] += gain * data_in[2*(timeInMax-j)];
	        data_out[2*(j+timeOut)+1] += gain * data_in[2*(j + timeIn)+1];
	        data_out[2*(timeOutMax-j)+1] += gain * data_in[2*(timeInMax-j)+1];
	    }
	    for (j=frame;j<=frame*2;j++)  {
	        data_out[2*(j+timeOut)] += gain * data_in[2*(j + timeIn)];
	        data_out[2*(j+timeOut)+1] += gain * data_in[2*(j + timeIn)];
	    }
    	  printf("%i     ",i);
          }
      }
    }
    else	{
      for (i=0;i<num_samples * factor;i++) codec_data_out[i] = SNDMulaw(0);
      for (i=0;i<num_frames;i++)	{
	timeIn = hopIn * i;
	timeInMax = timeIn + 3 * frame * sound_in->channelCount;
	timeOut = hopOut * i;
	timeOutMax = timeOut + 3 * frame * sound_in->channelCount;
	gain = 0.0;
	for (j=1;j<frame * sound_in->channelCount;j++)   {
	    gain += delta;
	    data_out[j+timeOut] += gain * SNDiMulaw(codec_data_in[j + timeIn]);
	    data_out[timeOutMax-j] += gain * SNDiMulaw(codec_data_in[timeInMax-j]);
	}
	for (j=frame * sound_in->channelCount;j<=frame*2*sound_in->channelCount;j++)  {
	    data_out[j+timeOut] += gain * SNDiMulaw(codec_data_in[j + timeIn]);
	}
    	  printf("%i     ",i);
      }
      for (i=0;i<num_samples*factor;i++) codec_data_out[i] = SNDMulaw(data_out[i]);
      SNDFree(sound_out2);
    }
    
    SNDWait(SNDWriteSoundfile(file_out,sound_out));
    SNDFree(sound_in);
    SNDFree(sound_out);
    	  printf("Finished!!! \n\n");
    return;
}

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