www.pudn.com > lpc.zip > pitsyn_i.c


/*******************************************************************
*
*	PITSYN Version 53
*
*******************************************************************
*
*   Synthesize a single pitch epoch
*
* Inputs:
*  ORDER  - Synthesis order (number of RC's)
*  VOICE  - Half frame voicing decisions
*  PITCH  - Pitch
*  RMS    - Energy
*  RC     - Reflection coefficients
*  LFRAME - Length of speech buffer
* Outputs:
*  IVUV   - Pitch epoch voicing decisions
*  IPITI  - Pitch epoch length
*  RMSI   - Pitch epoch energy
*  RCI    - Pitch epoch RC's
*  NOUT   - Number of pitch periods in this frame
*  RATIO  - Previous to present energy ratio
*/

#include "ourstuff.h"
#include "config.ch"
#include "lpcdefs.h"
#include 

pitsyn_i(voice, pitch, rms, rc, ivuv, ipiti, rmsi, rci, nout, ratio )
int_type voice[], *pitch;
int_type ivuv[], ipiti[], *nout;
int_type *rms, rc[], rmsi[], rci[ORDER][11], *ratio;
{
static int_type rco[MAXORD], yarc[MAXORD];
int_type i, j, lsamp, ip, istart, ivoice;
static int_type ivoico=0, ipito=0;
int_type jused, nl;
static int_type jsamp;
int_type alrn, alro, prop;
static int_type rmso=0;
int_type slope, uvpit, vflag, xxy;
static short first=1;

if (*rms<1) *rms = 1;
if (rmso<1) rmso = 1;
uvpit = 0;
*ratio = (*rms << 6)/(rmso+8);
if (first) {	/*1*/
  lsamp = 0;
  ivoice = voice[2];
  if (ivoice==0) {
    *pitch = LFRAME>>2;
  }
  *nout = LFRAME / *pitch;
  jsamp = LFRAME - *nout * *pitch;
  for(i=1;i<=*nout;i++)	{
    for(j=1;j<=ORDER;j++) {
      rci[j-1][i-1] = rc[j];
    }
    ivuv[i] = ivoice;
    ipiti[i] = *pitch;
    rmsi[i] = *rms;
  }
  /*
  printf("(i) ivoice = %d *pitch = %d *nout = %d jsamp = %d\n",
	 ivoice, *pitch, *nout, jsamp);
	 */
  first = 0;
} /*1*/
else	{ /*1*/
  vflag = 0;
  lsamp = LFRAME + jsamp;
  slope = ((*pitch-ipito) << 14)/lsamp;
  /*
  printf("(i) slope = %f, %d %d %d\n",slope/16384.,*pitch,ipito,lsamp);
  */
  *nout = 0;
  jused = 0;
  istart = 1;
  if ((voice[1]==ivoico)&&(voice[2]==voice[1])) { /*2*/
    if (voice[2]==0) {
/* SSUV - -   0  ,  0  ,  0	*/
      *pitch = LFRAME>>2;
      ipito = *pitch;
      if( *ratio>256 ) rmso = *rms;
    }
/* SSVC - -   1  ,  1  ,  1	*/
    slope = ((*pitch-ipito) << 14)/lsamp;
    /*
    printf("Changed slope to %f\n",slope/16384.);
    */
    ivoice = voice[2];
  } /*2*/
  else	{ /*2*/
    if (ivoico!=1) {
      if (ivoico==voice[1])
/* UV2VC2 - -  0  ,  0  ,  1	*/
	nl = lsamp - (LFRAME>>2);
      else
/* UV2VC1 - -  0  ,  1  ,  1	*/
	nl = lsamp - 3*(LFRAME>>2);
      
      ipiti[1] = nl>>1;
      ipiti[2] = nl - ipiti[1];
      ivuv[1] = 0;
      ivuv[2] = 0;
      rmsi[1] = rmso;
      rmsi[2] = rmso;
      for(i=1;i<=ORDER;i++)	{
	rci[i-1][0] = rco[i-1];
	rci[i-1][1] = rco[i-1];
	rco[i-1]   = rc[i];
      }
      slope = 0;
      /*
      printf("Changed slope to 0\n");
      */
      *nout = 2;
      ipito = *pitch;
      jused = nl;
      istart = nl + 1;
      ivoice = 1;
    }
    else	{
      if (ivoico!=voice[1]) 
/* VC2UV1 - -   1  ,  0  ,  0	*/
	lsamp = (LFRAME>>2) + jsamp;
      else
/* VC2UV2 - -   1  ,  1  ,  0	*/
	lsamp = 3*(LFRAME>>2) + jsamp;
      
      for(i=1;i<=ORDER;i++)	{
	yarc[i-1] = rc[i];
	rc[i] = rco[i-1];
      }
      ivoice = 1;
      slope = 0;
      /*
      printf("Changed slope to 0 (b)\n");
      */
      vflag = 1;
    }
  } /*2*/
  /*
  printf("(i) top of while %d, uvpit = %d, ipito = %d, slope = %f, istart = %d\n",
	 lsamp,uvpit,ipito,slope/16384.,istart);
	 */
  while (1)	{	/*3*/
    for(i=istart;i<=lsamp;i++)	{ /*4*/
      if (uvpit!=0) {
	/*
	printf("uvpit = %d\n",uvpit);
	*/
	ip = uvpit;
      } else {
	/*
	printf("i> %d %d\n",ipito, slope);
	*/
/*
	printf(">>>> ipito = %d, slope = %f\n",ipito,slope/16384.);
	*/
	ip = ((ipito*2) + ((((long_type)slope*(long_type)i)>>14)*2) + 1)/2;
      }
      if (ip<=i-jused) { /*5*/
	/*
	printf("(i) ip = %d i = %d jused = %d nout = %d\n",ip,i,jused,*nout);
	*/
	(*nout)++;
	if (*nout > 11) {
#ifdef TURBO
	  printf("PITSYN: too many epochs\n");
#endif
	  exit(1);
	}
	ipiti[*nout] = ip;
	*pitch = ip;
	ivuv[*nout] = ivoice;
	jused += ip;
	prop = ((jused<<14)-(ip<<13))/lsamp;
	for(j=1;j<=ORDER;j++)	{
	  alro = (int_type)
	    4096. * log((16384+rco[j-1])/(double)(16384-rco[j-1]));
	  alrn = (int_type)
	    4096. * log((16384+rc[j])/(double)(16384-rc[j]));
	  xxy = alro + (((long_type)(prop>>2)*(long_type)(alrn-alro)) >> 12);
	  xxy = (int_type) 256. * exp(xxy/(double)4096.);
	  rci[j-1][*nout-1] = ((xxy-256)<<14)/(xxy+256);
	}
	rmsi[*nout]=(int_type)(4096.*log(rmso/(double)32.)) +
	  ((prop>>2)*( (int_type)(4096.0*log(*rms/(double)32.)) -
		      (int_type)(4096.0*log(rmso/(double)32.)) ) >> 12);
	/*
	printf("(i) rmsi[*nout (%d) ] = %f (%f %f)  -> ",
	       *nout,rmsi[*nout]/4096.,rmso/32.,*rms/32.);
	       */
	rmsi[*nout] = (int_type)(exp(rmsi[*nout]/(double)4096.)*32.);
	/*
	printf("%f\n",rmsi[*nout]/32.);
	*/
      }	/*5*/
    } /*4*/
    if (vflag!=1) break;
    vflag = 0;
    istart = jused + 1;
    lsamp = LFRAME + jsamp;
    slope = 0;
    ivoice = 0;
    uvpit = (lsamp-istart)>>1;
    if(uvpit>90) uvpit = uvpit>>1;
    rmso = *rms;
    for(i=1;i<=ORDER;i++)	{
      rc[i] = yarc[i-1];
      rco[i-1] = yarc[i-1];
    }
  }/*3*/
  jsamp = lsamp - jused;
} /*1*/
if (*nout!=0) {
  ivoico = voice[2];
  ipito = *pitch;
  rmso = *rms;
  /*DO I = 1,ORDER */
  for(i=1;i<=ORDER;i++) {
    rco[i-1] = rc[i];
  }
}

}