1 /* -- translated by f2c (version 19951025).
2 You must link the resulting object file with the libraries:
3 -lf2c -lm (in that order)
4 */
5
6 #include "f2c.h"
7
8 int analys_(real *speech, integer *voice, integer *pitch, real *rms, real *rc, struct lpc10_encoder_state *st);
9
10 /* Common Block Declarations */
11
12 extern struct {
13 integer order, lframe;
14 logical corrp;
15 } contrl_;
16
17 #define contrl_1 contrl_
18
19 /* Table of constant values */
20
21 static integer c__10 = 10;
22 static integer c__181 = 181;
23 static integer c__720 = 720;
24 static integer c__3 = 3;
25 static integer c__90 = 90;
26 static integer c__156 = 156;
27 static integer c__307 = 307;
28 static integer c__462 = 462;
29 static integer c__312 = 312;
30 static integer c__60 = 60;
31 static integer c__1 = 1;
32
33 /* ****************************************************************** */
34
35 /* ANALYS Version 55 */
36
37 /* Revision 1.9 1996/05/23 19:41:07 jaf */
38 /* Commented out some unnecessary lines that were reading uninitialized */
39 /* values. */
40
41 /* Revision 1.8 1996/03/27 23:57:55 jaf */
42 /* Added some comments about which indices of the local buffers INBUF, */
43 /* LPBUF, etc., get read or modified by some of the subroutine calls. I */
44 /* just did this while trying to figure out the discrepancy between the */
45 /* embedded code compiled with all local variables implicitly saved, and */
46 /* without. */
47
48 /* I added some debugging write statements in hopes of finding a problem. */
49 /* None of them ever printed anything while running with the long input */
50 /* speech file dam9.spd provided in the distribution. */
51
52 /* Revision 1.7 1996/03/27 18:06:20 jaf */
53 /* Commented out access to MAXOSP, which is just a debugging variable */
54 /* that was defined in the COMMON block CONTRL in contrl.fh. */
55
56 /* Revision 1.6 1996/03/26 19:31:33 jaf */
57 /* Commented out trace statements. */
58
59 /* Revision 1.5 1996/03/21 15:19:35 jaf */
60 /* Added comments for ENTRY PITDEC. */
61
62 /* Revision 1.4 1996/03/19 20:54:27 jaf */
63 /* Added a line to INITANALYS. See comments there. */
64
65 /* Revision 1.3 1996/03/19 20:52:49 jaf */
66 /* Rearranged the order of the local variables quite a bit, to separate */
67 /* them into groups of "constants", "locals that don't need to be saved */
68 /* from one call to the next", and "local that do need to be saved from */
69 /* one call to the next". */
70
71 /* Several locals in the last set should have been given initial values, */
72 /* but weren't. I gave them all initial values of 0. */
73
74 /* Added a separate ENTRY INITANALYS that initializes all local state */
75 /* that should be, and also calls the corresponding entries of the */
76 /* subroutines called by ANALYS that also have local state. */
77
78 /* There used to be DATA statements in ANALYS. I got rid of most of */
79 /* them, and added a local logical variable FIRST that calls the entry */
80 /* INITANALYS on the first call to ANALYS. This is just so that one need */
81 /* not remember to call INITANALYS first in order for the state to be */
82 /* initialized. */
83
84 /* Revision 1.2 1996/03/11 23:29:32 jaf */
85 /* Added several comments with my own personal questions about the */
86 /* Fortran 77 meaning of the parameters passed to the subroutine PREEMP. */
87
88 /* Revision 1.1 1996/02/07 14:42:29 jaf */
89 /* Initial revision */
90
91
92 /* ****************************************************************** */
93
94 /* SUBROUTINE ANALYS */
95
96 /* Input: */
97 /* SPEECH */
98 /* Indices 1 through LFRAME read. */
99 /* Output: */
100 /* VOICE */
101 /* Indices 1 through 2 written. */
102 /* PITCH */
103 /* Written in subroutine DYPTRK, and then perhaps read and written */
104 /* some more. */
105 /* RMS */
106 /* Written. */
107 /* RC */
108 /* Indices 1 through ORDER written (ORDER defined in contrl.fh). */
109
110 /* This subroutine maintains local state from one call to the next. If */
111 /* you want to switch to using a new audio stream for this filter, or */
112 /* reinitialize its state for any other reason, call the ENTRY */
113 /* INITANALYS. */
114
115
116 /* ENTRY PITDEC */
117
118 /* Input: */
119 /* PITCH - Encoded pitch index */
120 /* Output: */
121 /* PTAU - Decoded pitch period */
122
123 /* This entry has no local state. It accesses a "constant" array */
124 /* declared in ANALYS. */
125
analys_(real * speech,integer * voice,integer * pitch,real * rms,real * rc,struct lpc10_encoder_state * st)126 /* Subroutine */ int analys_(real *speech, integer *voice, integer
127 *pitch, real *rms, real *rc, struct lpc10_encoder_state *st)
128 {
129 /* Initialized data */
130
131 static integer tau[60] = { 20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,
132 35,36,37,38,39,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,
133 74,76,78,80,84,88,92,96,100,104,108,112,116,120,124,128,132,136,
134 140,144,148,152,156 };
135 static integer buflim[4] = { 181,720,25,720 };
136 static real precoef = .9375f;
137
138 /* System generated locals */
139 integer i__1;
140
141 /* Local variables */
142 real amdf[60];
143 integer half;
144 real abuf[156];
145 real *bias;
146 extern /* Subroutine */ int tbdm_(real *, integer *, integer *, integer *,
147 real *, integer *, integer *, integer *);
148 integer *awin;
149 integer midx, ewin[6] /* was [2][3] */;
150 real ivrc[2], temp;
151 real *zpre;
152 integer *vwin;
153 integer i__, j, lanal;
154 extern /* Subroutine */ int rcchk_(integer *, real *, real *), mload_(
155 integer *, integer *, integer *, real *, real *, real *);
156 real *inbuf, *pebuf;
157 real *lpbuf, *ivbuf;
158 real *rcbuf;
159 integer *osbuf;
160 extern /* Subroutine */ int onset_(real *, integer *, integer *, integer *
161 , integer *, integer *, integer *, struct lpc10_encoder_state *);
162 integer *osptr;
163 extern int placea_(integer *, integer *
164 , integer *, integer *, integer *, integer *, integer *, integer *
165 , integer *), dcbias_(integer *, real *, real *), placev_(integer
166 *, integer *, integer *, integer *, integer *, integer *, integer
167 *, integer *, integer *, integer *, integer *);
168 integer ipitch;
169 integer *obound;
170 extern /* Subroutine */ int preemp_(real *, real *, integer *, real *,
171 real *), voicin_(integer *, real *, real *, integer *, integer *,
172 real *, real *, integer *, real *, integer *, integer *, integer *,
173 struct lpc10_encoder_state *);
174 integer *voibuf;
175 integer mintau;
176 real *rmsbuf;
177 extern /* Subroutine */ int lpfilt_(real *, real *, integer *, integer *),
178 ivfilt_(real *, real *, integer *, integer *, real *), energy_(
179 integer *, real *, real *), invert_(integer *, real *, real *,
180 real *);
181 integer minptr, maxptr;
182 extern /* Subroutine */ int dyptrk_(real *, integer *, integer *, integer
183 *, integer *, integer *, struct lpc10_encoder_state *);
184 real phi[100] /* was [10][10] */, psi[10];
185
186 /* LPC Processing control variables: */
187
188 /* *** Read-only: initialized in setup */
189
190 /* Files for Speech, Parameter, and Bitstream Input & Output, */
191 /* and message and debug outputs. */
192
193 /* Here are the only files which use these variables: */
194
195 /* lpcsim.f setup.f trans.f error.f vqsetup.f */
196
197 /* Many files which use fdebug are not listed, since it is only used in */
198 /* those other files conditionally, to print trace statements. */
199 /* integer fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
200 /* LPC order, Frame size, Quantization rate, Bits per frame, */
201 /* Error correction */
202 /* Subroutine SETUP is the only place where order is assigned a value, */
203 /* and that value is 10. It could increase efficiency 1% or so to */
204 /* declare order as a constant (i.e., a Fortran PARAMETER) instead of as
205 */
206 /* a variable in a COMMON block, since it is used in many places in the */
207 /* core of the coding and decoding routines. Actually, I take that back.
208 */
209 /* At least when compiling with f2c, the upper bound of DO loops is */
210 /* stored in a local variable before the DO loop begins, and then that is
211 */
212 /* compared against on each iteration. */
213 /* Similarly for lframe, which is given a value of MAXFRM in SETUP. */
214 /* Similarly for quant, which is given a value of 2400 in SETUP. quant */
215 /* is used in only a few places, and never in the core coding and */
216 /* decoding routines, so it could be eliminated entirely. */
217 /* nbits is similar to quant, and is given a value of 54 in SETUP. */
218 /* corrp is given a value of .TRUE. in SETUP, and is only used in the */
219 /* subroutines ENCODE and DECODE. It doesn't affect the speed of the */
220 /* coder significantly whether it is .TRUE. or .FALSE., or whether it is
221 */
222 /* a constant or a variable, since it is only examined once per frame. */
223 /* Leaving it as a variable that is set to .TRUE. seems like a good */
224 /* idea, since it does enable some error-correction capability for */
225 /* unvoiced frames, with no change in the coding rate, and no noticeable
226 */
227 /* quality difference in the decoded speech. */
228 /* integer quant, nbits */
229 /* *** Read/write: variables for debugging, not needed for LPC algorithm
230 */
231
232 /* Current frame, Unstable frames, Output clip count, Max onset buffer,
233 */
234 /* Debug listing detail level, Line count on listing page */
235
236 /* nframe is not needed for an embedded LPC10 at all. */
237 /* nunsfm is initialized to 0 in SETUP, and incremented in subroutine */
238 /* ERROR, which is only called from RCCHK. When LPC10 is embedded into */
239 /* an application, I would recommend removing the call to ERROR in RCCHK,
240 */
241 /* and remove ERROR and nunsfm completely. */
242 /* iclip is initialized to 0 in SETUP, and incremented in entry SWRITE in
243 */
244 /* sread.f. When LPC10 is embedded into an application, one might want */
245 /* to cause it to be incremented in a routine that takes the output of */
246 /* SYNTHS and sends it to an audio device. It could be optionally */
247 /* displayed, for those that might want to know what it is. */
248 /* maxosp is never initialized to 0 in SETUP, although it probably should
249 */
250 /* be, and it is updated in subroutine ANALYS. I doubt that its value */
251 /* would be of much interest to an application in which LPC10 is */
252 /* embedded. */
253 /* listl and lincnt are not needed for an embedded LPC10 at all. */
254 /* integer nframe, nunsfm, iclip, maxosp, listl, lincnt */
255 /* common /contrl/ fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
256 /* common /contrl/ quant, nbits */
257 /* common /contrl/ nframe, nunsfm, iclip, maxosp, listl, lincnt */
258 /* Arguments to entry PITDEC (below) */
259 /* Parameters/constants */
260 /* Constants */
261 /* NF = Number of frames */
262 /* AF = Frame in which analysis is done */
263 /* OSLEN = Length of the onset buffer */
264 /* LTAU = Number of pitch lags */
265 /* SBUFL, SBUFH = Start and end index of speech buffers */
266 /* LBUFL, LBUFH = Start and end index of LPF speech buffer */
267 /* MINWIN, MAXWIN = Min and Max length of voicing (and analysis) windows
268 */
269 /* PWLEN, PWINH, PWINL = Length, upper and lower limits of pitch window
270 */
271 /* DVWINL, DVWINH = Default lower and upper limits of voicing window */
272 /* The tables TAU and BUFLIM, and the variable PRECOEF, are not */
273 /* Fortran PARAMETER's, but they are initialized with DATA */
274 /* statements, and never modified. Thus, they need not have SAVE */
275 /* statements for them to keep their values from one invocation to
276 */
277 /* the next. */
278 /* Local variables that need not be saved */
279 /* Local state */
280 /* Data Buffers */
281 /* INBUF Raw speech (with DC bias removed each frame) */
282 /* PEBUF Preemphasized speech */
283 /* LPBUF Low pass speech buffer */
284 /* IVBUF Inverse filtered speech */
285 /* OSBUF Indexes of onsets in speech buffers */
286 /* VWIN Voicing window indices */
287 /* AWIN Analysis window indices */
288 /* EWIN Energy window indices */
289 /* VOIBUF Voicing decisions on windows in VWIN */
290 /* RMSBUF RMS energy */
291 /* RCBUF Reflection Coefficients */
292
293 /* Pitch is handled separately from the above parameters. */
294 /* The following variables deal with pitch: */
295 /* MIDX Encoded initial pitch estimate for analysis frame */
296 /* IPITCH Initial pitch computed for frame AF (decoded from MIDX) */
297 /* PITCH The encoded pitch value (index into TAU) for the present */
298 /* frame (delayed and smoothed by Dyptrack) */
299 /* Parameter adjustments */
300 if (speech) {
301 --speech;
302 }
303 if (voice) {
304 --voice;
305 }
306 if (rc) {
307 --rc;
308 }
309
310 /* Function Body */
311
312 /* Calculations are done on future frame due to requirements */
313 /* of the pitch tracker. Delay RMS and RC's 2 frames to give */
314 /* current frame parameters on return. */
315 /* Update all buffers */
316
317 inbuf = &(st->inbuf[0]);
318 pebuf = &(st->pebuf[0]);
319 lpbuf = &(st->lpbuf[0]);
320 ivbuf = &(st->ivbuf[0]);
321 bias = &(st->bias);
322 osbuf = &(st->osbuf[0]);
323 osptr = &(st->osptr);
324 obound = &(st->obound[0]);
325 vwin = &(st->vwin[0]);
326 awin = &(st->awin[0]);
327 voibuf = &(st->voibuf[0]);
328 rmsbuf = &(st->rmsbuf[0]);
329 rcbuf = &(st->rcbuf[0]);
330 zpre = &(st->zpre);
331
332 i__1 = 720 - contrl_1.lframe;
333 for (i__ = 181; i__ <= i__1; ++i__) {
334 inbuf[i__ - 181] = inbuf[contrl_1.lframe + i__ - 181];
335 pebuf[i__ - 181] = pebuf[contrl_1.lframe + i__ - 181];
336 }
337 i__1 = 540 - contrl_1.lframe;
338 for (i__ = 229; i__ <= i__1; ++i__) {
339 ivbuf[i__ - 229] = ivbuf[contrl_1.lframe + i__ - 229];
340 }
341 i__1 = 720 - contrl_1.lframe;
342 for (i__ = 25; i__ <= i__1; ++i__) {
343 lpbuf[i__ - 25] = lpbuf[contrl_1.lframe + i__ - 25];
344 }
345 j = 1;
346 i__1 = (*osptr) - 1;
347 for (i__ = 1; i__ <= i__1; ++i__) {
348 if (osbuf[i__ - 1] > contrl_1.lframe) {
349 osbuf[j - 1] = osbuf[i__ - 1] - contrl_1.lframe;
350 ++j;
351 }
352 }
353 *osptr = j;
354 voibuf[0] = voibuf[2];
355 voibuf[1] = voibuf[3];
356 for (i__ = 1; i__ <= 2; ++i__) {
357 vwin[(i__ << 1) - 2] = vwin[((i__ + 1) << 1) - 2] - contrl_1.lframe;
358 vwin[(i__ << 1) - 1] = vwin[((i__ + 1) << 1) - 1] - contrl_1.lframe;
359 awin[(i__ << 1) - 2] = awin[((i__ + 1) << 1) - 2] - contrl_1.lframe;
360 awin[(i__ << 1) - 1] = awin[((i__ + 1) << 1) - 1] - contrl_1.lframe;
361 /* EWIN(*,J) is unused for J .NE. AF, so the following shift is
362 */
363 /* unnecessary. It also causes error messages when the C versio
364 n */
365 /* of the code created from this by f2c is run with Purify. It
366 */
367 /* correctly complains that uninitialized memory is being read.
368 */
369 /* EWIN(1,I) = EWIN(1,I+1) - LFRAME */
370 /* EWIN(2,I) = EWIN(2,I+1) - LFRAME */
371 obound[i__ - 1] = obound[i__];
372 voibuf[i__ * 2] = voibuf[(i__ + 1) * 2];
373 voibuf[(i__ << 1) + 1] = voibuf[((i__ + 1) << 1) + 1];
374 rmsbuf[i__ - 1] = rmsbuf[i__];
375 i__1 = contrl_1.order;
376 for (j = 1; j <= i__1; ++j) {
377 rcbuf[j + i__ * 10 - 11] = rcbuf[j + (i__ + 1) * 10 - 11];
378 }
379 }
380 /* Copy input speech, scale to sign+12 bit integers */
381 /* Remove long term DC bias. */
382 /* If the average value in the frame was over 1/4096 (after current
383 */
384 /* BIAS correction), then subtract that much more from samples in */
385 /* next frame. If the average value in the frame was under */
386 /* -1/4096, add 1/4096 more to samples in next frame. In all other
387 */
388 /* cases, keep BIAS the same. */
389 temp = 0.f;
390 i__1 = contrl_1.lframe;
391 for (i__ = 1; i__ <= i__1; ++i__) {
392 inbuf[720 - contrl_1.lframe + i__ - 181] = speech[i__] * 4096.f -
393 (*bias);
394 temp += inbuf[720 - contrl_1.lframe + i__ - 181];
395 }
396 if (temp > (real) contrl_1.lframe) {
397 *bias += 1;
398 }
399 if (temp < (real) (-contrl_1.lframe)) {
400 *bias += -1;
401 }
402 /* Place Voicing Window */
403 i__ = 721 - contrl_1.lframe;
404 preemp_(&inbuf[i__ - 181], &pebuf[i__ - 181], &contrl_1.lframe, &precoef,
405 zpre);
406 onset_(pebuf, osbuf, osptr, &c__10, &c__181, &c__720, &contrl_1.lframe, st);
407
408 /* MAXOSP is just a debugging variable. */
409
410 /* MAXOSP = MAX( MAXOSP, OSPTR ) */
411
412 placev_(osbuf, osptr, &c__10, &obound[2], vwin, &c__3, &contrl_1.lframe,
413 &c__90, &c__156, &c__307, &c__462);
414 /* The Pitch Extraction algorithm estimates the pitch for a frame
415 */
416 /* of speech by locating the minimum of the average magnitude difference
417 */
418 /* function (AMDF). The AMDF operates on low-pass, inverse filtered */
419 /* speech. (The low-pass filter is an 800 Hz, 19 tap, equiripple, FIR
420 */
421 /* filter and the inverse filter is a 2nd-order LPC filter.) The pitch
422 */
423 /* estimate is later refined by dynamic programming (DYPTRK). However,
424 */
425 /* since some of DYPTRK's parameters are a function of the voicing */
426 /* decisions, a voicing decision must precede the final pitch estimation.
427 */
428 /* See subroutines LPFILT, IVFILT, and TBDM. */
429 /* LPFILT reads indices LBUFH-LFRAME-29 = 511 through LBUFH = 720 */
430 /* of INBUF, and writes indices LBUFH+1-LFRAME = 541 through LBUFH
431 */
432 /* = 720 of LPBUF. */
433 lpfilt_(&inbuf[228], &lpbuf[384], &c__312, &contrl_1.lframe);
434 /* IVFILT reads indices (PWINH-LFRAME-7) = 353 through PWINH = 540
435 */
436 /* of LPBUF, and writes indices (PWINH-LFRAME+1) = 361 through */
437 /* PWINH = 540 of IVBUF. */
438 ivfilt_(&lpbuf[204], ivbuf, &c__312, &contrl_1.lframe, ivrc);
439 /* TBDM reads indices PWINL = 229 through */
440 /* (PWINL-1)+MAXWIN+(TAU(LTAU)-TAU(1))/2 = 452 of IVBUF, and writes
441 */
442 /* indices 1 through LTAU = 60 of AMDF. */
443 tbdm_(ivbuf, &c__156, tau, &c__60, amdf, &minptr, &maxptr, &mintau);
444 /* Voicing decisions are made for each half frame of input speech.
445 */
446 /* An initial voicing classification is made for each half of the */
447 /* analysis frame, and the voicing decisions for the present frame */
448 /* are finalized. See subroutine VOICIN. */
449 /* The voicing detector (VOICIN) classifies the input signal as */
450 /* unvoiced (including silence) or voiced using the AMDF windowed */
451 /* maximum-to-minimum ratio, the zero crossing rate, energy measures, */
452 /* reflection coefficients, and prediction gains. */
453 /* The pitch and voicing rules apply smoothing and isolated */
454 /* corrections to the pitch and voicing estimates and, in the process,
455 */
456 /* introduce two frames of delay into the corrected pitch estimates and
457 */
458 /* voicing decisions. */
459 for (half = 1; half <= 2; ++half) {
460 voicin_(&vwin[4], inbuf, lpbuf, buflim, &half, &amdf[minptr - 1], &
461 amdf[maxptr - 1], &mintau, ivrc, obound, voibuf, &c__3, st);
462 }
463 /* Find the minimum cost pitch decision over several frames */
464 /* given the current voicing decision and the AMDF array */
465 dyptrk_(amdf, &c__60, &minptr, &voibuf[7], pitch, &midx, st);
466 ipitch = tau[midx - 1];
467 /* Place spectrum analysis and energy windows */
468 placea_(&ipitch, voibuf, &obound[2], &c__3, vwin, awin, ewin, &
469 contrl_1.lframe, &c__156);
470 /* Remove short term DC bias over the analysis window, Put result in ABUF
471 */
472 lanal = awin[5] + 1 - awin[4];
473 dcbias_(&lanal, &pebuf[awin[4] - 181], abuf);
474 /* ABUF(1:LANAL) is now defined. It is equal to */
475 /* PEBUF(AWIN(1,AF):AWIN(2,AF)) corrected for short term DC bias. */
476 /* Compute RMS over integer number of pitch periods within the */
477 /* analysis window. */
478 /* Note that in a hardware implementation this computation may be */
479 /* simplified by using diagonal elements of PHI computed by MLOAD. */
480 i__1 = ewin[5] - ewin[4] + 1;
481 energy_(&i__1, &abuf[ewin[4] - awin[4]], &rmsbuf[2]);
482 /* Matrix load and invert, check RC's for stability */
483 mload_(&contrl_1.order, &c__1, &lanal, abuf, phi, psi);
484 invert_(&contrl_1.order, phi, psi, &rcbuf[20]);
485 rcchk_(&contrl_1.order, &rcbuf[10], &rcbuf[20]);
486 /* Set return parameters */
487 voice[1] = voibuf[2];
488 voice[2] = voibuf[3];
489 *rms = rmsbuf[0];
490 i__1 = contrl_1.order;
491 for (i__ = 1; i__ <= i__1; ++i__) {
492 rc[i__] = rcbuf[i__ - 1];
493 }
494 return 0;
495 } /* analys_ */
496