Properly play a holdtime message if the announce-holdtime option is
[asterisk-bristuff.git] / codecs / lpc10 / pitsyn.c
blobea8177e223d8d5ea92f9e22776685172136525a2
1 /*
3 $Log$
4 Revision 1.16 2004/06/26 03:50:14 markster
5 Merge source cleanups (bug #1911)
7 Revision 1.15 2003/11/23 22:14:32 markster
8 Various warning cleanups
10 Revision 1.14 2003/02/12 13:59:15 matteo
11 mer feb 12 14:56:57 CET 2003
13 Revision 1.1.1.1 2003/02/12 13:59:15 matteo
14 mer feb 12 14:56:57 CET 2003
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
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.
24 * Removed init function, since all initialization is now done in
25 * init_lpc10_decoder_state().
27 * Revision 1.1 1996/08/19 22:31:12 jaf
28 * Initial revision
33 /* -- translated by f2c (version 19951025).
34 You must link the resulting object file with the libraries:
35 -lf2c -lm (in that order)
38 #include "f2c.h"
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
44 /* ***************************************************************** */
46 /* PITSYN Version 53 */
48 /* $Log$
49 * Revision 1.16 2004/06/26 03:50:14 markster
50 * Merge source cleanups (bug #1911)
52 * Revision 1.15 2003/11/23 22:14:32 markster
53 * Various warning cleanups
55 * Revision 1.14 2003/02/12 13:59:15 matteo
56 * mer feb 12 14:56:57 CET 2003
58 * Revision 1.1.1.1 2003/02/12 13:59:15 matteo
59 * mer feb 12 14:56:57 CET 2003
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
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.
69 * Removed init function, since all initialization is now done in
70 * init_lpc10_decoder_state().
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 commments about which indices of array arguments are read or */
77 /* written. */
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. */
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. */
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. */
91 /* Revision 1.1 1996/02/07 14:48:18 jaf */
92 /* Initial revision */
95 /* ***************************************************************** */
97 /* Synthesize a single pitch epoch */
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. */
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)
137 /* Initialized data */
139 real *rmso;
140 logical *first;
142 /* System generated locals */
143 integer rci_dim1 = 0, rci_offset, i__1, i__2;
144 real r__1;
146 /* Builtin functions */
147 double log(doublereal), exp(doublereal);
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;
162 /* Arguments */
163 /* $Log$
164 * Revision 1.16 2004/06/26 03:50:14 markster
165 * Merge source cleanups (bug #1911)
167 * Revision 1.15 2003/11/23 22:14:32 markster
168 * Various warning cleanups
170 * Revision 1.14 2003/02/12 13:59:15 matteo
171 * mer feb 12 14:56:57 CET 2003
173 * Revision 1.1.1.1 2003/02/12 13:59:15 matteo
174 * mer feb 12 14:56:57 CET 2003
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
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.
184 * Removed init function, since all initialization is now done in
185 * init_lpc10_decoder_state().
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. */
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. */
197 /* Revision 1.1 1996/02/07 14:43:51 jaf */
198 /* Initial revision */
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
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. */
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
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,
224 /* it returns pitch periods with a total length of at most */
225 /* LFRAME+JSAMP. */
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. */
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;
237 if (rci) {
238 rci_dim1 = *order;
239 rci_offset = rci_dim1 + 1;
240 rci -= rci_offset;
242 if (voice) {
243 --voice;
245 if (ivuv) {
246 --ivuv;
248 if (ipiti) {
249 --ipiti;
251 if (rmsi) {
252 --rmsi;
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);
263 if (*rms < 1.f) {
264 *rms = 1.f;
266 if (*rmso < 1.f) {
267 *rmso = 1.f;
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;
277 *nout = *lframe / *pitch;
278 *jsamp = *lframe - *nout * *pitch;
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. */
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. */
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];
295 ivuv[i__] = ivoice;
296 ipiti[i__] = *pitch;
297 rmsi[i__] = *rms;
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;
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;
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__];
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;
354 i__1 = *order;
355 for (i__ = 1; i__ <= i__1; ++i__) {
356 yarc[i__ - 1] = rc[i__];
357 rc[i__] = rco[i__ - 1];
359 ivoice = 1;
360 slope = 0.f;
361 vflag = 1;
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,
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
378 /* large enough number of bit errors in the channel could cause such a
380 /* thing to happen, and if so, could that cause NOUT to ever go over 1
381 1? */
383 /* Note that all of the 180 values in the table are really LFRAME, but
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. */
393 /* Note that LSAMP-JSAMP is given as the variable. This was just for
395 /* brevity, to avoid adding "+JSAMP" to all of the column entries. */
396 /* Similarly for NL-JSAMP. */
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 */
411 /* while_loop | once once once once twice twice */
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 */
425 /* UVPIT is always 0.0 on the first pass through the DO WHILE (.TRUE.)
427 /* loop below. */
429 /* The only possible non-0 value of SLOPE (in column 111) is */
430 /* (PITCH-IPITO)/FLOAT(LSAMP) */
432 /* Column 101 is identical to 100. Any good properties we can prove
434 /* for 100 will also hold for 101. Similarly for 010 and 011. */
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
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. */
445 while(TRUE_) {
447 /* JUSED is the total length of all pitch periods curr
448 ently */
449 /* in the output arrays, in samples. */
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: */
457 /* (I - MAXPIT) .LE. JUSED .LE. (I-1) */
459 /* Note that the final value of I is LSAMP+1, so that
460 after */
461 /* the DO loop is complete, we know: */
463 /* (LSAMP - MAXPIT + 1) .LE. JUSED .LE. LSAMP */
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;
472 if (ip <= i__ - jused) {
473 ++(*nout);
475 /* The following check is no longer nece
476 ssary, now that */
477 /* we can prove that NOUT will never go
478 over 16. */
480 /* IF (NOUT .GT. 16) STOP 'PITSYN: too many epochs'
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);
496 rmsi[*nout] = (real)(log(*rmso) + prop * (log(*rms) - log(*rmso)));
497 rmsi[*nout] = (real)exp(rmsi[*nout]);
500 if (vflag != 1) {
501 goto L100;
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. */
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. */
517 /* Given the loop invariant on JUSED above, we know th
518 at: */
520 /* (oldLSAMP - MAXPIT + 1) .LE. JUSED .LE. oldLSAMP */
522 /* ISTART is one more than this. */
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. */
530 /* Thus, the range of newLSAMP-ISTART is: */
532 /* (newLSAMP-(oldLSAMP+1)) .LE. newLSAMP-ISTART */
533 /* .LE. (newLSAMP-(oldLSAMP - MAXPIT + 2)) */
535 /* or: */
537 /* 46 .LE. newLSAMP-ISTART .LE. 133+MAXPIT .EQ. 289 */
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. */
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. */
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;
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];
566 L100:
567 *jsamp = lsamp - jused;
569 /* Given that the maximum pitch period MAXPIT .LT. LFRAME (this is
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__];
582 return 0;
583 } /* pitsyn_ */