Asterisk - The Open Source Telephony Project  18.5.0
pitsyn.c
Go to the documentation of this file.
1 /*
2 
3 $Log$
4 Revision 1.16 2004/06/26 03:50:14 markster
5 Merge source cleanups (bug #1911)
6 
7 Revision 1.15 2003/11/23 22:14:32 markster
8 Various warning cleanups
9 
10 Revision 1.14 2003/02/12 13:59:15 matteo
11 mer feb 12 14:56:57 CET 2003
12 
13 Revision 1.1.1.1 2003/02/12 13:59:15 matteo
14 mer feb 12 14:56:57 CET 2003
15 
16 Revision 1.2 2000/01/05 08:20:39 markster
17 Some OSS fixes and a few lpc changes to make it actually work
18 
19  * Revision 1.2 1996/08/20 20:40:12 jaf
20  * Removed all static local variables that were SAVE'd in the Fortran
21  * code, and put them in struct lpc10_decoder_state that is passed as an
22  * argument.
23  *
24  * Removed init function, since all initialization is now done in
25  * init_lpc10_decoder_state().
26  *
27  * Revision 1.1 1996/08/19 22:31:12 jaf
28  * Initial revision
29  *
30 
31 */
32 
33 /* -- translated by f2c (version 19951025).
34  You must link the resulting object file with the libraries:
35  -lf2c -lm (in that order)
36 */
37 
38 #include "f2c.h"
39 
40 #ifdef P_R_O_T_O_T_Y_P_E_S
41 extern int pitsyn_(integer *order, integer *voice, integer *pitch, real *rms, real *rc, integer *lframe, integer *ivuv, integer *ipiti, real *rmsi, real *rci, integer *nout, real *ratio, struct lpc10_decoder_state *st);
42 #endif
43 
44 /* ***************************************************************** */
45 
46 /* PITSYN Version 53 */
47 
48 /* $Log$
49  * Revision 1.16 2004/06/26 03:50:14 markster
50  * Merge source cleanups (bug #1911)
51  *
52  * Revision 1.15 2003/11/23 22:14:32 markster
53  * Various warning cleanups
54  *
55  * Revision 1.14 2003/02/12 13:59:15 matteo
56  * mer feb 12 14:56:57 CET 2003
57  *
58  * Revision 1.1.1.1 2003/02/12 13:59:15 matteo
59  * mer feb 12 14:56:57 CET 2003
60  *
61  * Revision 1.2 2000/01/05 08:20:39 markster
62  * Some OSS fixes and a few lpc changes to make it actually work
63  *
64  * Revision 1.2 1996/08/20 20:40:12 jaf
65  * Removed all static local variables that were SAVE'd in the Fortran
66  * code, and put them in struct lpc10_decoder_state that is passed as an
67  * argument.
68  *
69  * Removed init function, since all initialization is now done in
70  * init_lpc10_decoder_state().
71  *
72  * Revision 1.1 1996/08/19 22:31:12 jaf
73  * Initial revision
74  * */
75 /* Revision 1.2 1996/03/25 18:49:07 jaf */
76 /* Added comments about which indices of array arguments are read or */
77 /* written. */
78 
79 /* Rearranged local variable declarations to indicate which need to be */
80 /* saved from one invocation to the next. Added entry INITPITSYN to */
81 /* reinitialize local state variables, if desired. */
82 
83 /* Added lots of comments about proving that the maximum number of pitch */
84 /* periods (NOUT) that can be returned is 16. The call to STOP that */
85 /* could happen if NOUT got too large was removed as a result. */
86 
87 /* Also proved that the total number of samples returned from N calls, */
88 /* each with identical values of LFRAME, will always be in the range */
89 /* N*LFRAME-MAXPIT+1 to N*LFRAME. */
90 
91 /* Revision 1.1 1996/02/07 14:48:18 jaf */
92 /* Initial revision */
93 
94 
95 /* ***************************************************************** */
96 
97 /* Synthesize a single pitch epoch */
98 
99 /* Input: */
100 /* ORDER - Synthesis order (number of RC's) */
101 /* VOICE - Half frame voicing decisions */
102 /* Indices 1 through 2 read. */
103 /* LFRAME - Length of speech buffer */
104 /* Input/Output: */
105 /* PITCH - Pitch */
106 /* This value should be in the range MINPIT (20) to MAXPIT */
107 /* (156), inclusive. */
108 /* PITCH can be modified under some conditions. */
109 /* RMS - Energy (can be modified) */
110 /* RMS is changed to 1 if the value passed in is less than 1. */
111 /* RC - Reflection coefficients */
112 /* Indices 1 through ORDER can be temporarily overwritten with */
113 /* RCO, and then replaced with original values, under some */
114 /* conditions. */
115 /* Output: */
116 /* IVUV - Pitch epoch voicing decisions */
117 /* Indices (I) of IVUV, IPITI, and RMSI are written, */
118 /* and indices (J,I) of RCI are written, */
119 /* where I ranges from 1 to NOUT, and J ranges from 1 to ORDER. */
120 /* IPITI - Pitch epoch length */
121 /* RMSI - Pitch epoch energy */
122 /* RCI - Pitch epoch RC's */
123 /* NOUT - Number of pitch periods in this frame */
124 /* This is at least 0, at least 1 if MAXPIT .LT. LFRAME (this */
125 /* is currently true on every call), and can never be more than */
126 /* (LFRAME+MAXPIT-1)/PITCH, which is currently 16 with */
127 /* LFRAME=180, MAXPIT=156, and PITCH .GE. 20, as SYNTHS */
128 /* guarantees when it calls this subroutine. */
129 /* RATIO - Previous to present energy ratio */
130 /* Always assigned a value. */
131 
132 /* Subroutine */ int pitsyn_(integer *order, integer *voice,
133  integer *pitch, real *rms, real *rc, integer *lframe, integer *ivuv,
134  integer *ipiti, real *rmsi, real *rci, integer *nout, real *ratio,
135  struct lpc10_decoder_state *st)
136 {
137  /* Initialized data */
138 
139  real *rmso;
140  logical *first;
141 
142  /* System generated locals */
143  integer rci_dim1 = 0, rci_offset, i__1, i__2;
144  real r__1;
145 
146  /* Builtin functions */
147  double log(doublereal), exp(doublereal);
148 
149  /* Local variables */
150  real alrn, alro, yarc[10], prop;
151  integer i__, j, vflag, jused, lsamp;
152  integer *jsamp;
153  real slope;
154  integer *ipito;
155  real uvpit;
156  integer ip, nl, ivoice;
157  integer *ivoico;
158  integer istart;
159  real *rco;
160  real xxy;
161 
162 /* Arguments */
163 /* $Log$
164  * Revision 1.16 2004/06/26 03:50:14 markster
165  * Merge source cleanups (bug #1911)
166  *
167  * Revision 1.15 2003/11/23 22:14:32 markster
168  * Various warning cleanups
169  *
170  * Revision 1.14 2003/02/12 13:59:15 matteo
171  * mer feb 12 14:56:57 CET 2003
172  *
173  * Revision 1.1.1.1 2003/02/12 13:59:15 matteo
174  * mer feb 12 14:56:57 CET 2003
175  *
176  * Revision 1.2 2000/01/05 08:20:39 markster
177  * Some OSS fixes and a few lpc changes to make it actually work
178  *
179  * Revision 1.2 1996/08/20 20:40:12 jaf
180  * Removed all static local variables that were SAVE'd in the Fortran
181  * code, and put them in struct lpc10_decoder_state that is passed as an
182  * argument.
183  *
184  * Removed init function, since all initialization is now done in
185  * init_lpc10_decoder_state().
186  *
187  * Revision 1.1 1996/08/19 22:31:12 jaf
188  * Initial revision
189  * */
190 /* Revision 1.3 1996/03/29 22:03:47 jaf */
191 /* Removed definitions for any constants that were no longer used. */
192 
193 /* Revision 1.2 1996/03/26 19:34:33 jaf */
194 /* Added comments indicating which constants are not needed in an */
195 /* application that uses the LPC-10 coder. */
196 
197 /* Revision 1.1 1996/02/07 14:43:51 jaf */
198 /* Initial revision */
199 
200 /* LPC Configuration parameters: */
201 /* Frame size, Prediction order, Pitch period */
202 /* Local variables that need not be saved */
203 /* LSAMP is initialized in the IF (FIRST) THEN clause, but it is */
204 /* not used the first time through, and it is given a value before
205 */
206 /* use whenever FIRST is .FALSE., so it appears unnecessary to */
207 /* assign it a value when FIRST is .TRUE. */
208 /* Local state */
209 /* FIRST - .TRUE. only on first call to PITSYN. */
210 /* IVOICO - Previous VOICE(2) value. */
211 /* IPITO - Previous PITCH value. */
212 /* RMSO - Previous RMS value. */
213 /* RCO - Previous RC values. */
214 
215 /* JSAMP - If this routine is called N times with identical values of */
216 /* LFRAME, then the total length of all pitch periods returned */
217 /* is always N*LFRAME-JSAMP, and JSAMP is always in the range 0
218 */
219 /* to MAXPIT-1 (see below for why this is so). Thus JSAMP is */
220 /* the number of samples "left over" from the previous call to */
221 /* PITSYN, that haven't been "used" in a pitch period returned */
222 /* from this subroutine. Every time this subroutine is called,
223 */
224 /* it returns pitch periods with a total length of at most */
225 /* LFRAME+JSAMP. */
226 
227 /* IVOICO, IPITO, RCO, and JSAMP need not be assigned an initial value */
228 /* with a DATA statement, because they are always initialized on the */
229 /* first call to PITSYN. */
230 
231 /* FIRST and RMSO should be initialized with DATA statements, because */
232 /* even on the first call, they are used before being initialized. */
233  /* Parameter adjustments */
234  if (rc) {
235  --rc;
236  }
237  if (rci) {
238  rci_dim1 = *order;
239  rci_offset = rci_dim1 + 1;
240  rci -= rci_offset;
241  }
242  if (voice) {
243  --voice;
244  }
245  if (ivuv) {
246  --ivuv;
247  }
248  if (ipiti) {
249  --ipiti;
250  }
251  if (rmsi) {
252  --rmsi;
253  }
254 
255  /* Function Body */
256  ivoico = &(st->ivoico);
257  ipito = &(st->ipito);
258  rmso = &(st->rmso);
259  rco = &(st->rco[0]);
260  jsamp = &(st->jsamp);
261  first = &(st->first_pitsyn);
262 
263  if (*rms < 1.f) {
264  *rms = 1.f;
265  }
266  if (*rmso < 1.f) {
267  *rmso = 1.f;
268  }
269  uvpit = 0.f;
270  *ratio = *rms / (*rmso + 8.f);
271  if (*first) {
272  lsamp = 0;
273  ivoice = voice[2];
274  if (ivoice == 0) {
275  *pitch = *lframe / 4;
276  }
277  *nout = *lframe / *pitch;
278  *jsamp = *lframe - *nout * *pitch;
279 
280 /* SYNTHS only calls this subroutine with PITCH in the range
281 20 */
282 /* to 156. LFRAME = MAXFRM = 180, so NOUT is somewhere in th
283 e */
284 /* range 1 to 9. */
285 
286 /* JSAMP is "LFRAME mod PITCH", so it is in the range 0 to */
287 /* (PITCH-1), or 0 to MAXPIT-1=155, after the first call. */
288 
289  i__1 = *nout;
290  for (i__ = 1; i__ <= i__1; ++i__) {
291  i__2 = *order;
292  for (j = 1; j <= i__2; ++j) {
293  rci[j + i__ * rci_dim1] = rc[j];
294  }
295  ivuv[i__] = ivoice;
296  ipiti[i__] = *pitch;
297  rmsi[i__] = *rms;
298  }
299  *first = FALSE_;
300  } else {
301  vflag = 0;
302  lsamp = *lframe + *jsamp;
303  slope = (*pitch - *ipito) / (real) lsamp;
304  *nout = 0;
305  jused = 0;
306  istart = 1;
307  if (voice[1] == *ivoico && voice[2] == voice[1]) {
308  if (voice[2] == 0) {
309 /* SSUV - - 0 , 0 , 0 */
310  *pitch = *lframe / 4;
311  *ipito = *pitch;
312  if (*ratio > 8.f) {
313  *rmso = *rms;
314  }
315  }
316 /* SSVC - - 1 , 1 , 1 */
317  slope = (*pitch - *ipito) / (real) lsamp;
318  ivoice = voice[2];
319  } else {
320  if (*ivoico != 1) {
321  if (*ivoico == voice[1]) {
322 /* UV2VC2 - - 0 , 0 , 1 */
323  nl = lsamp - *lframe / 4;
324  } else {
325 /* UV2VC1 - - 0 , 1 , 1 */
326  nl = lsamp - *lframe * 3 / 4;
327  }
328  ipiti[1] = nl / 2;
329  ipiti[2] = nl - ipiti[1];
330  ivuv[1] = 0;
331  ivuv[2] = 0;
332  rmsi[1] = *rmso;
333  rmsi[2] = *rmso;
334  i__1 = *order;
335  for (i__ = 1; i__ <= i__1; ++i__) {
336  rci[i__ + rci_dim1] = rco[i__ - 1];
337  rci[i__ + (rci_dim1 << 1)] = rco[i__ - 1];
338  rco[i__ - 1] = rc[i__];
339  }
340  slope = 0.f;
341  *nout = 2;
342  *ipito = *pitch;
343  jused = nl;
344  istart = nl + 1;
345  ivoice = 1;
346  } else {
347  if (*ivoico != voice[1]) {
348 /* VC2UV1 - - 1 , 0 , 0 */
349  lsamp = *lframe / 4 + *jsamp;
350  } else {
351 /* VC2UV2 - - 1 , 1 , 0 */
352  lsamp = *lframe * 3 / 4 + *jsamp;
353  }
354  i__1 = *order;
355  for (i__ = 1; i__ <= i__1; ++i__) {
356  yarc[i__ - 1] = rc[i__];
357  rc[i__] = rco[i__ - 1];
358  }
359  ivoice = 1;
360  slope = 0.f;
361  vflag = 1;
362  }
363  }
364 /* Here is the value of most variables that are used below, depending
365 on */
366 /* the values of IVOICO, VOICE(1), and VOICE(2). VOICE(1) and VOICE(2
367 ) */
368 /* are input arguments, and IVOICO is the value of VOICE(2) on the */
369 /* previous call (see notes for the IF (NOUT .NE. 0) statement near th
370 e */
371 /* end). Each of these three values is either 0 or 1. These three */
372 /* values below are given as 3-bit long strings, in the order IVOICO,
373 */
374 /* VOICE(1), and VOICE(2). It appears that the code above assumes tha
375 t */
376 /* the bit sequences 010 and 101 never occur, but I wonder whether a
377 */
378 /* large enough number of bit errors in the channel could cause such a
379  */
380 /* thing to happen, and if so, could that cause NOUT to ever go over 1
381 1? */
382 
383 /* Note that all of the 180 values in the table are really LFRAME, but
384  */
385 /* 180 has fewer characters, and it makes the table a little more */
386 /* concrete. If LFRAME is ever changed, keep this in mind. Similarly
387 , */
388 /* 135's are 3*LFRAME/4, and 45's are LFRAME/4. If LFRAME is not a */
389 /* multiple of 4, then the 135 for NL-JSAMP is actually LFRAME-LFRAME/
390 4, */
391 /* and the 45 for NL-JSAMP is actually LFRAME-3*LFRAME/4. */
392 
393 /* Note that LSAMP-JSAMP is given as the variable. This was just for
394 */
395 /* brevity, to avoid adding "+JSAMP" to all of the column entries. */
396 /* Similarly for NL-JSAMP. */
397 
398 /* Variable | 000 001 011,010 111 110 100,101 */
399 /* ------------+-------------------------------------------------- */
400 /* ISTART | 1 NL+1 NL+1 1 1 1 */
401 /* LSAMP-JSAMP | 180 180 180 180 135 45 */
402 /* IPITO | 45 PITCH PITCH oldPITCH oldPITCH oldPITCH */
403 /* SLOPE | 0 0 0 seebelow 0 0 */
404 /* JUSED | 0 NL NL 0 0 0 */
405 /* PITCH | 45 PITCH PITCH PITCH PITCH PITCH */
406 /* NL-JSAMP | -- 135 45 -- -- -- */
407 /* VFLAG | 0 0 0 0 1 1 */
408 /* NOUT | 0 2 2 0 0 0 */
409 /* IVOICE | 0 1 1 1 1 1 */
410 
411 /* while_loop | once once once once twice twice */
412 
413 /* ISTART | -- -- -- -- JUSED+1 JUSED+1 */
414 /* LSAMP-JSAMP | -- -- -- -- 180 180 */
415 /* IPITO | -- -- -- -- oldPITCH oldPITCH */
416 /* SLOPE | -- -- -- -- 0 0 */
417 /* JUSED | -- -- -- -- ?? ?? */
418 /* PITCH | -- -- -- -- PITCH PITCH */
419 /* NL-JSAMP | -- -- -- -- -- -- */
420 /* VFLAG | -- -- -- -- 0 0 */
421 /* NOUT | -- -- -- -- ?? ?? */
422 /* IVOICE | -- -- -- -- 0 0 */
423 
424 
425 /* UVPIT is always 0.0 on the first pass through the DO WHILE (.TRUE.)
426  */
427 /* loop below. */
428 
429 /* The only possible non-0 value of SLOPE (in column 111) is */
430 /* (PITCH-IPITO)/FLOAT(LSAMP) */
431 
432 /* Column 101 is identical to 100. Any good properties we can prove
433 */
434 /* for 100 will also hold for 101. Similarly for 010 and 011. */
435 
436 /* SYNTHS calls this subroutine with PITCH restricted to the range 20
437 to */
438 /* 156. IPITO is similarly restricted to this range, after the first
439 */
440 /* call. IP below is also restricted to this range, given the */
441 /* definitions of IPITO, SLOPE, UVPIT, and that I is in the range ISTA
442 RT */
443 /* to LSAMP. */
444 
445  while(TRUE_) {
446 
447 /* JUSED is the total length of all pitch periods curr
448 ently */
449 /* in the output arrays, in samples. */
450 
451 /* An invariant of the DO I = ISTART,LSAMP loop below,
452  under */
453 /* the condition that IP is always in the range 1 thro
454 ugh */
455 /* MAXPIT, is: */
456 
457 /* (I - MAXPIT) .LE. JUSED .LE. (I-1) */
458 
459 /* Note that the final value of I is LSAMP+1, so that
460 after */
461 /* the DO loop is complete, we know: */
462 
463 /* (LSAMP - MAXPIT + 1) .LE. JUSED .LE. LSAMP */
464 
465  i__1 = lsamp;
466  for (i__ = istart; i__ <= i__1; ++i__) {
467  r__1 = *ipito + slope * i__;
468  ip = (integer)(r__1 + .5f);
469  if (uvpit != 0.f) {
470  ip = (integer)uvpit;
471  }
472  if (ip <= i__ - jused) {
473  ++(*nout);
474 
475 /* The following check is no longer nece
476 ssary, now that */
477 /* we can prove that NOUT will never go
478 over 16. */
479 
480 /* IF (NOUT .GT. 16) STOP 'PITSYN: too many epochs'
481 */
482 
483  ipiti[*nout] = ip;
484  *pitch = ip;
485  ivuv[*nout] = ivoice;
486  jused += ip;
487  prop = (jused - ip / 2) / (real) lsamp;
488  i__2 = *order;
489  for (j = 1; j <= i__2; ++j) {
490  alro = (real)log((rco[j - 1] + 1) / (1 - rco[j - 1]));
491  alrn = (real)log((rc[j] + 1) / (1 - rc[j]));
492  xxy = alro + prop * (alrn - alro);
493  xxy = (real)exp(xxy);
494  rci[j + *nout * rci_dim1] = (xxy - 1) / (xxy + 1);
495  }
496  rmsi[*nout] = (real)(log(*rmso) + prop * (log(*rms) - log(*rmso)));
497  rmsi[*nout] = (real)exp(rmsi[*nout]);
498  }
499  }
500  if (vflag != 1) {
501  goto L100;
502  }
503 
504 /* I want to prove what range UVPIT must lie in after
505 the */
506 /* assignments to it below. To do this, I must determ
507 ine */
508 /* what range (LSAMP-ISTART) must lie in, after the */
509 /* assignments to ISTART and LSAMP below. */
510 
511 /* Let oldLSAMP be the value of LSAMP at this point in
512  the */
513 /* execution. This is 135+JSAMP in state 110, or 45+J
514 SAMP in */
515 /* states 100 or 101. */
516 
517 /* Given the loop invariant on JUSED above, we know th
518 at: */
519 
520 /* (oldLSAMP - MAXPIT + 1) .LE. JUSED .LE. oldLSAMP */
521 
522 /* ISTART is one more than this. */
523 
524 /* Let newLSAMP be the value assigned to LSAMP below.
525  This */
526 /* is 180+JSAMP. Thus (newLSAMP-oldLSAMP) is either 4
527 5 or */
528 /* 135, depending on the state. */
529 
530 /* Thus, the range of newLSAMP-ISTART is: */
531 
532 /* (newLSAMP-(oldLSAMP+1)) .LE. newLSAMP-ISTART */
533 /* .LE. (newLSAMP-(oldLSAMP - MAXPIT + 2)) */
534 
535 /* or: */
536 
537 /* 46 .LE. newLSAMP-ISTART .LE. 133+MAXPIT .EQ. 289 */
538 
539 /* Therefore, UVPIT is in the range 23 to 144 after th
540 e first */
541 /* assignment to UVPIT below, and after the conditiona
542 l */
543 /* assignment, it is in the range 23 to 90. */
544 
545 /* The important thing is that it is in the range 20 t
546 o 156, */
547 /* so that in the loop above, IP is always in this ran
548 ge. */
549 
550  vflag = 0;
551  istart = jused + 1;
552  lsamp = *lframe + *jsamp;
553  slope = 0.f;
554  ivoice = 0;
555  uvpit = (real) ((lsamp - istart) / 2);
556  if (uvpit > 90.f) {
557  uvpit /= 2;
558  }
559  *rmso = *rms;
560  i__1 = *order;
561  for (i__ = 1; i__ <= i__1; ++i__) {
562  rc[i__] = yarc[i__ - 1];
563  rco[i__ - 1] = yarc[i__ - 1];
564  }
565  }
566 L100:
567  *jsamp = lsamp - jused;
568  }
569 /* Given that the maximum pitch period MAXPIT .LT. LFRAME (this is
570 */
571 /* currently true on every call, since SYNTHS always sets */
572 /* LFRAME=180), NOUT will always be .GE. 1 at this point. */
573  if (*nout != 0) {
574  *ivoico = voice[2];
575  *ipito = *pitch;
576  *rmso = *rms;
577  i__1 = *order;
578  for (i__ = 1; i__ <= i__1; ++i__) {
579  rco[i__ - 1] = rc[i__];
580  }
581  }
582  return 0;
583 } /* pitsyn_ */
#define TRUE_
Definition: f2c.h:67
real rco[10]
Definition: lpc10.h:170
int pitsyn_(integer *order, integer *voice, integer *pitch, real *rms, real *rc, integer *lframe, integer *ivuv, integer *ipiti, real *rmsi, real *rci, integer *nout, real *ratio, struct lpc10_decoder_state *st)
Definition: pitsyn.c:132
integer order
Definition: analys.c:66
double doublereal
Definition: f2c.h:50
float real
Definition: lpc10.h:79
struct sla_ringing_trunk * first
Definition: app_meetme.c:1092
integer ipito
Definition: lpc10.h:168
integer lframe
Definition: analys.c:66
logical first_pitsyn
Definition: lpc10.h:172
integer jsamp
Definition: lpc10.h:171
INT32 logical
Definition: lpc10.h:81
integer ivoico
Definition: lpc10.h:167
INT32 integer
Definition: lpc10.h:80
#define FALSE_
Definition: f2c.h:68