aboutsummaryrefslogtreecommitdiffstats
path: root/codecs/lpc10/pitsyn.c
blob: e0466ca67b94e2f393f3ed9b234de5448d3c345a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*

$Log$
Revision 1.3  2000/01/05 08:20:39  markster
Version 0.1.4 from FTP

Revision 1.2  2000/01/05 08:20:39  markster
Some OSS fixes and a few lpc changes to make it actually work

 * Revision 1.2  1996/08/20  20:40:12  jaf
 * Removed all static local variables that were SAVE'd in the Fortran
 * code, and put them in struct lpc10_decoder_state that is passed as an
 * argument.
 *
 * Removed init function, since all initialization is now done in
 * init_lpc10_decoder_state().
 *
 * Revision 1.1  1996/08/19  22:31:12  jaf
 * Initial revision
 *

*/

#ifdef P_R_O_T_O_T_Y_P_E_S
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);
#endif

/*  -- translated by f2c (version 19951025).
   You must link the resulting object file with the libraries:
	-lf2c -lm   (in that order)
*/

#include "f2c.h"

/* ***************************************************************** */

/* 	PITSYN Version 53 */

/* $Log$
 * Revision 1.3  2000/01/05 08:20:39  markster
 * Version 0.1.4 from FTP
 *
/* Revision 1.2  2000/01/05 08:20:39  markster
/* Some OSS fixes and a few lpc changes to make it actually work
/*
 * Revision 1.2  1996/08/20  20:40:12  jaf
 * Removed all static local variables that were SAVE'd in the Fortran
 * code, and put them in struct lpc10_decoder_state that is passed as an
 * argument.
 *
 * Removed init function, since all initialization is now done in
 * init_lpc10_decoder_state().
 *
 * Revision 1.1  1996/08/19  22:31:12  jaf
 * Initial revision
 * */
/* Revision 1.2  1996/03/25  18:49:07  jaf */
/* Added commments about which indices of array arguments are read or */
/* written. */

/* Rearranged local variable declarations to indicate which need to be */
/* saved from one invocation to the next.  Added entry INITPITSYN to */
/* reinitialize local state variables, if desired. */

/* Added lots of comments about proving that the maximum number of pitch */
/* periods (NOUT) that can be returned is 16.  The call to STOP that */
/* could happen if NOUT got too large was removed as a result. */

/* Also proved that the total number of samples returned from N calls, */
/* each with identical values of LFRAME, will always be in the range */
/* N*LFRAME-MAXPIT+1 to N*LFRAME. */

/* Revision 1.1  1996/02/07 14:48:18  jaf */
/* Initial revision */


/* ***************************************************************** */

/*   Synthesize a single pitch epoch */

/* Input: */
/*  ORDER  - Synthesis order (number of RC's) */
/*  VOICE  - Half frame voicing decisions */
/*           Indices 1 through 2 read. */
/*  LFRAME - Length of speech buffer */
/* Input/Output: */
/*  PITCH  - Pitch */
/*           This value should be in the range MINPIT (20) to MAXPIT */
/*           (156), inclusive. */
/*           PITCH can be modified under some conditions. */
/*  RMS    - Energy  (can be modified) */
/*           RMS is changed to 1 if the value passed in is less than 1. */
/*  RC     - Reflection coefficients */
/*           Indices 1 through ORDER can be temporarily overwritten with */
/*           RCO, and then replaced with original values, under some */
/*           conditions. */
/* Output: */
/*  IVUV   - Pitch epoch voicing decisions */
/*           Indices (I) of IVUV, IPITI, and RMSI are written, */
/*           and indices (J,I) of RCI are written, */
/*           where I ranges from 1 to NOUT, and J ranges from 1 to ORDER. */
/*  IPITI  - Pitch epoch length */
/*  RMSI   - Pitch epoch energy */
/*  RCI    - Pitch epoch RC's */
/*  NOUT   - Number of pitch periods in this frame */
/*           This is at least 0, at least 1 if MAXPIT .LT. LFRAME (this */
/*           is currently true on every call), and can never be more than */
/*           (LFRAME+MAXPIT-1)/PITCH, which is currently 16 with */
/*           LFRAME=180, MAXPIT=156, and PITCH .GE. 20, as SYNTHS */
/*           guarantees when it calls this subroutine. */
/*  RATIO  - Previous to present energy ratio */
/*           Always assigned a value. */

/* Subroutine */ 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)
{
    /* Initialized data */

    real *rmso;
    logical *first;

    /* System generated locals */
    integer rci_dim1, rci_offset, i__1, i__2;
    real r__1;

    /* Builtin functions */
    double log(doublereal), exp(doublereal);

    /* Local variables */
    real alrn, alro, yarc[10], prop;
    integer i__, j, vflag, jused, lsamp;
    integer *jsamp;
    real slope;
    integer *ipito;
    real uvpit;
    integer ip, nl, ivoice;
    integer *ivoico;
    integer istart;
    real *rco;
    real xxy;

/*       Arguments */
/* $Log$
 * Revision 1.3  2000/01/05 08:20:39  markster
 * Version 0.1.4 from FTP
 *
/* Revision 1.2  2000/01/05 08:20:39  markster
/* Some OSS fixes and a few lpc changes to make it actually work
/*
 * Revision 1.2  1996/08/20  20:40:12  jaf
 * Removed all static local variables that were SAVE'd in the Fortran
 * code, and put them in struct lpc10_decoder_state that is passed as an
 * argument.
 *
 * Removed init function, since all initialization is now done in
 * init_lpc10_decoder_state().
 *
 * Revision 1.1  1996/08/19  22:31:12  jaf
 * Initial revision
 * */
/* Revision 1.3  1996/03/29  22:03:47  jaf */
/* Removed definitions for any constants that were no longer used. */

/* Revision 1.2  1996/03/26  19:34:33  jaf */
/* Added comments indicating which constants are not needed in an */
/* application that uses the LPC-10 coder. */

/* Revision 1.1  1996/02/07  14:43:51  jaf */
/* Initial revision */

/*   LPC Configuration parameters: */
/* Frame size, Prediction order, Pitch period */
/*       Local variables that need not be saved */
/*       LSAMP is initialized in the IF (FIRST) THEN clause, but it is */
/*       not used the first time through, and it is given a value before 
*/
/*       use whenever FIRST is .FALSE., so it appears unnecessary to */
/*       assign it a value when FIRST is .TRUE. */
/*       Local state */
/* FIRST  - .TRUE. only on first call to PITSYN. */
/* IVOICO - Previous VOICE(2) value. */
/* IPITO  - Previous PITCH value. */
/* RMSO   - Previous RMS value. */
/* RCO    - Previous RC values. */

/* JSAMP  - If this routine is called N times with identical values of */
/*          LFRAME, then the total length of all pitch periods returned */
/*          is always N*LFRAME-JSAMP, and JSAMP is always in the range 0 
*/
/*          to MAXPIT-1 (see below for why this is so).  Thus JSAMP is */
/*          the number of samples "left over" from the previous call to */
/*          PITSYN, that haven't been "used" in a pitch period returned */
/*          from this subroutine.  Every time this subroutine is called, 
*/
/*          it returns pitch periods with a total length of at most */
/*          LFRAME+JSAMP. */

/* IVOICO, IPITO, RCO, and JSAMP need not be assigned an initial value */
/* with a DATA statement, because they are always initialized on the */
/* first call to PITSYN. */

/* FIRST and RMSO should be initialized with DATA statements, because */
/* even on the first call, they are used before being initialized. */
    /* Parameter adjustments */
    if (rc) {
	--rc;
	}
    if (rci) {
	rci_dim1 = *order;
	rci_offset = rci_dim1 + 1;
	rci -= rci_offset;
	}
    if (voice) {
	--voice;
	}
    if (ivuv) {
	--ivuv;
	}
    if (ipiti) {
	--ipiti;
	}
    if (rmsi) {
	--rmsi;
	}

    /* Function Body */
    ivoico = &(st->ivoico);
    ipito = &(st->ipito);
    rmso = &(st->rmso);
    rco = &(st->rco[0]);
    jsamp = &(st->jsamp);
    first = &(st->first_pitsyn);

    if (*rms < 1.f) {
	*rms = 1.f;
    }
    if (*rmso < 1.f) {
	*rmso = 1.f;
    }
    uvpit = 0.f;
    *ratio = *rms / (*rmso + 8.f);
    if (*first) {
	lsamp = 0;
	ivoice = voice[2];
	if (ivoice == 0) {
	    *pitch = *lframe / 4;
	}
	*nout = *lframe / *pitch;
	*jsamp = *lframe - *nout * *pitch;

/*          SYNTHS only calls this subroutine with PITCH in the range 
20 */
/*          to 156.  LFRAME = MAXFRM = 180, so NOUT is somewhere in th
e */
/*          range 1 to 9. */

/*          JSAMP is "LFRAME mod PITCH", so it is in the range 0 to */
/*          (PITCH-1), or 0 to MAXPIT-1=155, after the first call. */

	i__1 = *nout;
	for (i__ = 1; i__ <= i__1; ++i__) {
	    i__2 = *order;
	    for (j = 1; j <= i__2; ++j) {
		rci[j + i__ * rci_dim1] = rc[j];
	    }
	    ivuv[i__] = ivoice;
	    ipiti[i__] = *pitch;
	    rmsi[i__] = *rms;
	}
	*first = FALSE_;
    } else {
	vflag = 0;
	lsamp = *lframe + *jsamp;
	slope = (*pitch - *ipito) / (real) lsamp;
	*nout = 0;
	jused = 0;
	istart = 1;
	if (voice[1] == *ivoico && voice[2] == voice[1]) {
	    if (voice[2] == 0) {
/* SSUV - -   0  ,  0  ,  0 */
		*pitch = *lframe / 4;
		*ipito = *pitch;
		if (*ratio > 8.f) {
		    *rmso = *rms;
		}
	    }
/* SSVC - -   1  ,  1  ,  1 */
	    slope = (*pitch - *ipito) / (real) lsamp;
	    ivoice = voice[2];
	} else {
	    if (*ivoico != 1) {
		if (*ivoico == voice[1]) {
/* UV2VC2 - -  0  ,  0  ,  1 */
		    nl = lsamp - *lframe / 4;
		} else {
/* UV2VC1 - -  0  ,  1  ,  1 */
		    nl = lsamp - *lframe * 3 / 4;
		}
		ipiti[1] = nl / 2;
		ipiti[2] = nl - ipiti[1];
		ivuv[1] = 0;
		ivuv[2] = 0;
		rmsi[1] = *rmso;
		rmsi[2] = *rmso;
		i__1 = *order;
		for (i__ = 1; i__ <= i__1; ++i__) {
		    rci[i__ + rci_dim1] = rco[i__ - 1];
		    rci[i__ + (rci_dim1 << 1)] = rco[i__ - 1];
		    rco[i__ - 1] = rc[i__];
		}
		slope = 0.f;
		*nout = 2;
		*ipito = *pitch;
		jused = nl;
		istart = nl + 1;
		ivoice = 1;
	    } else {
		if (*ivoico != voice[1]) {
/* VC2UV1 - -   1  ,  0  ,  0 */
		    lsamp = *lframe / 4 + *jsamp;
		} else {
/* VC2UV2 - -   1  ,  1  ,  0 */
		    lsamp = *lframe * 3 / 4 + *jsamp;
		}
		i__1 = *order;
		for (i__ = 1; i__ <= i__1; ++i__) {
		    yarc[i__ - 1] = rc[i__];
		    rc[i__] = rco[i__ - 1];
		}
		ivoice = 1;
		slope = 0.f;
		vflag = 1;
	    }
	}
/* Here is the value of most variables that are used below, depending 
on */
/* the values of IVOICO, VOICE(1), and VOICE(2).  VOICE(1) and VOICE(2
) */
/* are input arguments, and IVOICO is the value of VOICE(2) on the */
/* previous call (see notes for the IF (NOUT .NE. 0) statement near th
e */
/* end).  Each of these three values is either 0 or 1.  These three */
/* values below are given as 3-bit long strings, in the order IVOICO, 
*/
/* VOICE(1), and VOICE(2).  It appears that the code above assumes tha
t */
/* the bit sequences 010 and 101 never occur, but I wonder whether a 
*/
/* large enough number of bit errors in the channel could cause such a
 */
/* thing to happen, and if so, could that cause NOUT to ever go over 1
1? */

/* Note that all of the 180 values in the table are really LFRAME, but
 */
/* 180 has fewer characters, and it makes the table a little more */
/* concrete.  If LFRAME is ever changed, keep this in mind.  Similarly
, */
/* 135's are 3*LFRAME/4, and 45's are LFRAME/4.  If LFRAME is not a */
/* multiple of 4, then the 135 for NL-JSAMP is actually LFRAME-LFRAME/
4, */
/* and the 45 for NL-JSAMP is actually LFRAME-3*LFRAME/4. */

/* Note that LSAMP-JSAMP is given as the variable.  This was just for 
*/
/* brevity, to avoid adding "+JSAMP" to all of the column entries. */
/* Similarly for NL-JSAMP. */

/* Variable    | 000  001    011,010  111       110       100,101 */
/* ------------+-------------------------------------------------- */
/* ISTART      | 1    NL+1   NL+1     1         1         1 */
/* LSAMP-JSAMP | 180  180    180      180       135       45 */
/* IPITO       | 45   PITCH  PITCH    oldPITCH  oldPITCH  oldPITCH */
/* SLOPE       | 0    0      0        seebelow  0         0 */
/* JUSED       | 0    NL     NL       0         0         0 */
/* PITCH       | 45   PITCH  PITCH    PITCH     PITCH     PITCH */
/* NL-JSAMP    | --   135    45       --        --        -- */
/* VFLAG       | 0    0      0        0         1         1 */
/* NOUT        | 0    2      2        0         0         0 */
/* IVOICE      | 0    1      1        1         1         1 */

/* while_loop  | once once   once     once      twice     twice */

/* ISTART      | --   --     --       --        JUSED+1   JUSED+1 */
/* LSAMP-JSAMP | --   --     --       --        180       180 */
/* IPITO       | --   --     --       --        oldPITCH  oldPITCH */
/* SLOPE       | --   --     --       --        0         0 */
/* JUSED       | --   --     --       --        ??        ?? */
/* PITCH       | --   --     --       --        PITCH     PITCH */
/* NL-JSAMP    | --   --     --       --        --        -- */
/* VFLAG       | --   --     --       --        0         0 */
/* NOUT        | --   --     --       --        ??        ?? */
/* IVOICE      | --   --     --       --        0         0 */


/* UVPIT is always 0.0 on the first pass through the DO WHILE (.TRUE.)
 */
/* loop below. */

/* The only possible non-0 value of SLOPE (in column 111) is */
/* (PITCH-IPITO)/FLOAT(LSAMP) */

/* Column 101 is identical to 100.  Any good properties we can prove 
*/
/* for 100 will also hold for 101.  Similarly for 010 and 011. */

/* SYNTHS calls this subroutine with PITCH restricted to the range 20 
to */
/* 156.  IPITO is similarly restricted to this range, after the first 
*/
/* call.  IP below is also restricted to this range, given the */
/* definitions of IPITO, SLOPE, UVPIT, and that I is in the range ISTA
RT */
/* to LSAMP. */

	while(TRUE_) {

/*             JUSED is the total length of all pitch periods curr
ently */
/*             in the output arrays, in samples. */

/*             An invariant of the DO I = ISTART,LSAMP loop below,
 under */
/*             the condition that IP is always in the range 1 thro
ugh */
/*             MAXPIT, is: */

/*             (I - MAXPIT) .LE. JUSED .LE. (I-1) */

/*             Note that the final value of I is LSAMP+1, so that 
after */
/*             the DO loop is complete, we know: */

/*             (LSAMP - MAXPIT + 1) .LE. JUSED .LE. LSAMP */

	    i__1 = lsamp;
	    for (i__ = istart; i__ <= i__1; ++i__) {
		r__1 = *ipito + slope * i__;
		ip = r__1 + .5f;
		if (uvpit != 0.f) {
		    ip = uvpit;
		}
		if (ip <= i__ - jused) {
		    ++(*nout);

/*                   The following check is no longer nece
ssary, now that */
/*                   we can prove that NOUT will never go 
over 16. */

/* 		    IF (NOUT .GT. 16) STOP 'PITSYN: too many epochs' 
*/

		    ipiti[*nout] = ip;
		    *pitch = ip;
		    ivuv[*nout] = ivoice;
		    jused += ip;
		    prop = (jused - ip / 2) / (real) lsamp;
		    i__2 = *order;
		    for (j = 1; j <= i__2; ++j) {
			alro = log((rco[j - 1] + 1) / (1 - rco[j - 1]));
			alrn = log((rc[j] + 1) / (1 - rc[j]));
			xxy = alro + prop * (alrn - alro);
			xxy = exp(xxy);
			rci[j + *nout * rci_dim1] = (xxy - 1) / (xxy + 1);
		    }
		    rmsi[*nout] = log(*rmso) + prop * (log(*rms) - log(*rmso));
		    rmsi[*nout] = exp(rmsi[*nout]);
		}
	    }
	    if (vflag != 1) {
		goto L100;
	    }

/*             I want to prove what range UVPIT must lie in after 
the */
/*             assignments to it below.  To do this, I must determ
ine */
/*             what range (LSAMP-ISTART) must lie in, after the */
/*             assignments to ISTART and LSAMP below. */

/*             Let oldLSAMP be the value of LSAMP at this point in
 the */
/*             execution.  This is 135+JSAMP in state 110, or 45+J
SAMP in */
/*             states 100 or 101. */

/*             Given the loop invariant on JUSED above, we know th
at: */

/*             (oldLSAMP - MAXPIT + 1) .LE. JUSED .LE. oldLSAMP */

/*             ISTART is one more than this. */

/*             Let newLSAMP be the value assigned to LSAMP below. 
 This */
/*             is 180+JSAMP.  Thus (newLSAMP-oldLSAMP) is either 4
5 or */
/*             135, depending on the state. */

/*             Thus, the range of newLSAMP-ISTART is: */

/*             (newLSAMP-(oldLSAMP+1)) .LE. newLSAMP-ISTART */
/*             .LE. (newLSAMP-(oldLSAMP - MAXPIT + 2)) */

/*             or: */

/*             46 .LE. newLSAMP-ISTART .LE. 133+MAXPIT .EQ. 289 */

/*             Therefore, UVPIT is in the range 23 to 144 after th
e first */
/*             assignment to UVPIT below, and after the conditiona
l */
/*             assignment, it is in the range 23 to 90. */

/*             The important thing is that it is in the range 20 t
o 156, */
/*             so that in the loop above, IP is always in this ran
ge. */

	    vflag = 0;
	    istart = jused + 1;
	    lsamp = *lframe + *jsamp;
	    slope = 0.f;
	    ivoice = 0;
	    uvpit = (real) ((lsamp - istart) / 2);
	    if (uvpit > 90.f) {
		uvpit /= 2;
	    }
	    *rmso = *rms;
	    i__1 = *order;
	    for (i__ = 1; i__ <= i__1; ++i__) {
		rc[i__] = yarc[i__ - 1];
		rco[i__ - 1] = yarc[i__ - 1];
	    }
	}
L100:
	*jsamp = lsamp - jused;
    }
/*       Given that the maximum pitch period MAXPIT .LT. LFRAME (this is 
*/
/*       currently true on every call, since SYNTHS always sets */
/*       LFRAME=180), NOUT will always be .GE. 1 at this point. */
    if (*nout != 0) {
	*ivoico = voice[2];
	*ipito = *pitch;
	*rmso = *rms;
	i__1 = *order;
	for (i__ = 1; i__ <= i__1; ++i__) {
	    rco[i__ - 1] = rc[i__];
	}
    }
    return 0;
} /* pitsyn_ */