1 /*
2 
3 $Log$
4 Revision 1.2  2006/08/01 13:06:51  rjongbloed
5 Added a raft of unvalidated audio codecs from OpenH323 tree
6 
7 Revision 1.1.2.1  2006/07/22 14:03:19  rjongbloed
8 Added more plug ins
9 
10 Revision 1.1.2.1  2006/05/08 13:49:58  rjongbloed
11 Imported all the audio codec plug ins from OpenH323
12 
13 Revision 1.1  2004/05/04 11:16:43  csoutheren
14 Initial version
15 
16 Revision 1.2  2002/02/15 03:57:55  yurik
17 Warnings removed during compilation, patch courtesy of Jehan Bing, jehan@bravobrava.com
18 
19 Revision 1.1  2000/06/05 04:45:12  robertj
20 Added LPC-10 2400bps codec
21 
22  * Revision 1.2  1996/08/20  20:45:00  jaf
23  * Removed all static local variables that were SAVE'd in the Fortran
24  * code, and put them in struct lpc10_encoder_state that is passed as an
25  * argument.
26  *
27  * Removed init function, since all initialization is now done in
28  * init_lpc10_encoder_state().
29  *
30  * Revision 1.1  1996/08/19  22:30:14  jaf
31  * Initial revision
32  *
33 
34 */
35 
36 #ifdef P_R_O_T_O_T_Y_P_E_S
37 extern int voicin_(integer *vwin, real *inbuf, real *lpbuf, integer *buflim, integer *half, real *minamd, real *maxamd, integer *mintau, real *ivrc, integer *obound, integer *voibuf, integer *af, struct lpc10_encoder_state *st);
38 /* comlen contrl_ 12 */
39 /*:ref: vparms_ 14 14 4 6 6 4 4 6 4 4 4 4 6 6 6 6 */
40 #endif
41 
42 /*  -- translated by f2c (version 19951025).
43    You must link the resulting object file with the libraries:
44 	-lf2c -lm   (in that order)
45 */
46 
47 #include "f2c.h"
48 
49 /* Common Block Declarations */
50 
51 extern struct {
52     integer order, lframe;
53     logical corrp;
54 } contrl_;
55 
56 #define contrl_1 contrl_
57 
58 /****************************************************************************/
59 
60 /* 	VOICIN Version 52 */
61 
62 /* $Log$
63 /* Revision 1.2  2006/08/01 13:06:51  rjongbloed
64 /* Added a raft of unvalidated audio codecs from OpenH323 tree
65 /*
66 /* Revision 1.1.2.1  2006/07/22 14:03:19  rjongbloed
67 /* Added more plug ins
68 /*
69 /* Revision 1.1.2.1  2006/05/08 13:49:58  rjongbloed
70 /* Imported all the audio codec plug ins from OpenH323
71 /*
72 /* Revision 1.1  2004/05/04 11:16:43  csoutheren
73 /* Initial version
74 /*
75 /* Revision 1.2  2002/02/15 03:57:55  yurik
76 /* Warnings removed during compilation, patch courtesy of Jehan Bing, jehan@bravobrava.com
77 /*
78 /* Revision 1.1  2000/06/05 04:45:12  robertj
79 /* Added LPC-10 2400bps codec
80 /*
81  * Revision 1.2  1996/08/20  20:45:00  jaf
82  * Removed all static local variables that were SAVE'd in the Fortran
83  * code, and put them in struct lpc10_encoder_state that is passed as an
84  * argument.
85  *
86  * Removed init function, since all initialization is now done in
87  * init_lpc10_encoder_state().
88  *
89  * Revision 1.1  1996/08/19  22:30:14  jaf
90  * Initial revision
91  * */
92 /* Revision 1.10  1996/03/29  17:59:14  jaf */
93 /* Avoided using VALUE(9), although it shouldn't affect the function of */
94 /* the code at all, because it was always multiplied by VDC(9,SNRL), */
95 /* which is 0 for all values of SNRL.  Still, if VALUE(9) had an initial */
96 /* value of IEEE NaN, it might cause trouble (I don't know how IEEE */
97 /* defines Nan * 0.  It should either be NaN or 0.) */
98 
99 /* Revision 1.9  1996/03/29  17:54:46  jaf */
100 /* Added a few comments about the accesses made to argument array VOIBUF */
101 /* and the local saved array VOICE. */
102 
103 /* Revision 1.8  1996/03/27  18:19:54  jaf */
104 /* Added an assignment to VSTATE that does not affect the function of the */
105 /* program at all.  The only reason I put it in was so that the tracing */
106 /* statements at the end, when enabled, will print a consistent value for */
107 /* VSTATE when HALF .EQ. 1, rather than a garbage value that could change */
108 /* from one call to the next. */
109 
110 /* Revision 1.7  1996/03/26  20:00:06  jaf */
111 /* Removed the inclusion of the file "vcomm.fh", and put its contents */
112 /* into this file.  It was included nowhere else but here. */
113 
114 /* Revision 1.6  1996/03/26  19:38:09  jaf */
115 /* Commented out trace statements. */
116 
117 /* Revision 1.5  1996/03/19  20:43:45  jaf */
118 /* Added comments about which indices of OBOUND and VOIBUF can be */
119 /* accessed, and whether they are read or written.  VOIBUF is fairly */
120 /* messy. */
121 
122 /* Revision 1.4  1996/03/19  15:00:58  jaf */
123 /* Moved the DATA statements for the *VDC* variables later, as it is */
124 /* apparently illegal to have DATA statements before local variable */
125 /* declarations. */
126 
127 /* Revision 1.3  1996/03/19  00:10:49  jaf */
128 /* Heavily commented the local variables that are saved from one */
129 /* invocation to the next, and how the local variable FIRST is used to */
130 /* avoid the need to assign most of them initial values with DATA */
131 /* statements. */
132 
133 /* A few should be initialized, but aren't.  I've guessed initial values */
134 /* for two of these, SFBUE and SLBUE, and I've convinced myself that for */
135 /* VOICE, the effects of uninitialized values will die out after 2 or 3 */
136 /* frame times.  It would still be good to choose initial values for */
137 /* these, but I don't know what reasonable values would be (0 comes to */
138 /* mind). */
139 
140 /* Revision 1.2  1996/03/13  16:09:28  jaf */
141 /* Comments added explaining which of the local variables of this */
142 /* subroutine need to be saved from one invocation to the next, and which */
143 /* do not. */
144 
145 /* WARNING!  Some of them that should are never given initial values in */
146 /* this code.  Hopefully, Fortran 77 defines initial values for them, but */
147 /* even so, giving them explicit initial values is preferable. */
148 
149 /* WARNING!  VALUE(9) is used, but never assigned a value.  It should */
150 /* probably be eliminated from the code. */
151 
152 /* Revision 1.1  1996/02/07 14:50:28  jaf */
153 /* Initial revision */
154 
155 
156 /****************************************************************************/
157 
158 /*        Voicing Detection (VOICIN) makes voicing decisions for each half */
159 /*  frame of input speech.  Tentative voicing decisions are made two frames*/
160 /*   in the future (2F) for each half frame.  These decisions are carried */
161 /*   through one frame in the future (1F) to the present (P) frame where */
162 /*   they are examined and smoothed, resulting in the final voicing */
163 /*   decisions for each half frame. */
164 /*        The voicing parameter (signal measurement) column vector (VALUE) */
165 /*   is based on a rectangular window of speech samples determined by the */
166 /*  window placement algorithm.  The voicing parameter vector contains the*/
167 /*  AMDF windowed maximum-to-minimum ratio, the zero crossing rate, energy*/
168 /*   measures, reflection coefficients, and prediction gains.  The voicing */
169 /*  window is placed to avoid contamination of the voicing parameter vector*/
170 /*   with speech onsets. */
171 /*        The input signal is then classified as unvoiced (including */
172 /*   silence) or voiced.  This decision is made by a linear discriminant */
173 /*   function consisting of a dot product of the voicing decision */
174 /*   coefficient (VDC) row vector with the measurement column vector */
175 /*  (VALUE).  The VDC vector is 2-dimensional, each row vector is optimized*/
176 /*   for a particular signal-to-noise ratio (SNR).  So, before the dot */
177 /*   product is performed, the SNR is estimated to select the appropriate */
178 /*   VDC vector. */
179 /*        The smoothing algorithm is a modified median smoother.  The */
180 /*  voicing discriminant function is used by the smoother to determine how*/
181 /*   strongly voiced or unvoiced a signal is.  The smoothing is further */
182 /*   modified if a speech onset and a voicing decision transition occur */
183 /*   within one half frame.  In this case, the voicing decision transition */
184 /*  is extended to the speech onset.  For transmission purposes, there are*/
185 /*   constraints on the duration and transition of voicing decisions.  The */
186 /*   smoother takes these constraints into account. */
187 /*        Finally, the energy estimates are updated along with the dither */
188 /*   threshold used to calculate the zero crossing rate (ZC). */
189 
190 /* Inputs: */
191 /*  VWIN      - Voicing window limits */
192 /*              The indices read of arrays VWIN, INBUF, LPBUF, and BUFLIM */
193 /*              are the same as those read by subroutine VPARMS. */
194 /*  INBUF     - Input speech buffer */
195 /*  LPBUF     - Low-pass filtered speech buffer */
196 /*  BUFLIM    - INBUF and LPBUF limits */
197 /*  HALF      - Present analysis half frame number */
198 /*  MINAMD    - Minimum value of the AMDF */
199 /*  MAXAMD    - Maximum value of the AMDF */
200 /*  MINTAU    - Pointer to the lag of the minimum AMDF value */
201 /*  IVRC(2)   - Inverse filter's RC's */
202 /*              Only index 2 of array IVRC read under normal operation. */
203 /*              (Index 1 is also read when debugging is turned on.) */
204 /*  OBOUND    - Onset boundary descriptions */
205 /*             Indices 1 through 3 read if (HALF .NE. 1), otherwise untouched.
206 */
207 /*  AF        - The analysis frame number */
208 /* Output: */
209 /*  VOIBUF(2,0:AF) - Buffer of voicing decisions */
210 /*              Index (HALF,3) written. */
211 /*              If (HALF .EQ. 1), skip down to "Read (HALF,3)" below. */
212 /*              Indices (1,2), (2,1), (1,2), and (2,2) read. */
213 /*              One of the following is then done: */
214 /*                 read (1,3) and possibly write (1,2) */
215 /*                 read (1,3) and write (1,2) or (2,2) */
216 /*                 write (2,1) */
217 /*                 write (2,1) or (1,2) */
218 /*                 read (1,0) and (1,3) and then write (2,2) or (1,1) */
219 /*                 no reads or writes on VOIBUF */
220 /*              Finally, read (HALF,3) */
221 /* Internal: */
222 /*  QS        - Ratio of preemphasized to full-band energies */
223 /*  RC1       - First reflection coefficient */
224 /* AR_B      - Product of the causal forward and reverse pitch prediction gain
225 s*/
226 /* AR_F      - Product of the noncausal forward and rev. pitch prediction gain
227 s*/
228 /*  ZC        - Zero crossing rate */
229 /*  DITHER    - Zero crossing threshold level */
230 /*  MAXMIN    - AMDF's 1 octave windowed maximum-to-minimum ratio */
231 /*  MINPTR    - Location  of minimum AMDF value */
232 /*  NVDC      - Number of elements in each VDC vector */
233 /*  NVDCL     - Number of VDC vectors */
234 /*  VDCL      - SNR values corresponding to the set of VDC's */
235 /*  VDC       - 2-D voicing decision coefficient vector */
236 /*  VALUE(9)  - Voicing Parameters */
237 /*  VOICE(2,3)- History of LDA results */
238 /*              On every call when (HALF .EQ. 1), VOICE(*,I+1) is */
239 /*              shifted back to VOICE(*,I), for I=1,2. */
240 /*              VOICE(HALF,3) is written on every call. */
241 /*              Depending on several conditions, one or more of */
242 /*              (1,1), (1,2), (2,1), and (2,2) might then be read. */
243 /*  LBE       - Ratio of low-band instantaneous to average energies */
244 /*  FBE       - Ratio of full-band instantaneous to average energies */
245 /*  LBVE      - Low band voiced energy */
246 /*  LBUE      - Low band unvoiced energy */
247 /*  FBVE      - Full band voiced energy */
248 /*  FBUE      - Full band unvoiced energy */
249 /*  OFBUE     - Previous full-band unvoiced energy */
250 /*  OLBUE     - Previous low-band unvoiced energy */
251 /*  REF       - Reference energy for initialization and DITHER threshold */
252 /*  SNR       - Estimate of signal-to-noise ratio */
253 /*  SNR2      - Estimate of low-band signal-to-noise ratio */
254 /*  SNRL      - SNR level number */
255 /*  OT        - Onset transition present */
256 /*  VSTATE    - Decimal interpretation of binary voicing classifications */
257 /*  FIRST     - First call flag */
258 
259 /* This subroutine maintains local state from one call to the next.  If */
260 /* you want to switch to using a new audio stream for this filter, or */
261 /* reinitialize its state for any other reason, call the ENTRY */
262 /* INITVOICIN. */
263 
voicin_(integer * vwin,real * inbuf,real * lpbuf,integer * buflim,integer * half,real * minamd,real * maxamd,integer * mintau,real * ivrc,integer * obound,integer * voibuf,integer * af,struct lpc10_encoder_state * st)264 /* Subroutine */ int voicin_(integer *vwin, real *inbuf, real *
265 	lpbuf, integer *buflim, integer *half, real *minamd, real *maxamd,
266 	integer *mintau, real *ivrc, integer *obound, integer *voibuf,
267 	integer *af, struct lpc10_encoder_state *st)
268 {
269     /* Initialized data */
270 
271     real *dither;
272     static real vdc[100]	/* was [10][10] */ = { 0.f,1714.f,-110.f,
273 	    334.f,-4096.f,-654.f,3752.f,3769.f,0.f,1181.f,0.f,874.f,-97.f,
274 	    300.f,-4096.f,-1021.f,2451.f,2527.f,0.f,-500.f,0.f,510.f,-70.f,
275 	    250.f,-4096.f,-1270.f,2194.f,2491.f,0.f,-1500.f,0.f,500.f,-10.f,
276 	    200.f,-4096.f,-1300.f,2e3f,2e3f,0.f,-2e3f,0.f,500.f,0.f,0.f,
277 	    -4096.f,-1300.f,2e3f,2e3f,0.f,-2500.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f,
278 	    0.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f,
279 	    0.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f,
280 	    0.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f };
281     static integer nvdcl = 5;
282     static real vdcl[10] = { 600.f,450.f,300.f,200.f,0.f,0.f,0.f,0.f,0.f,0.f }
283 	    ;
284 
285     /* System generated locals */
286     integer inbuf_offset, lpbuf_offset, i__1, i__2;
287     real r__1, r__2;
288 
289     /* Builtin functions */
290     integer i_nint(real *);
291     double sqrt(doublereal);
292 
293     /* Local variables */
294     real ar_b__, ar_f__;
295     integer *lbve, *lbue, *fbve, *fbue;
296     integer snrl, i__;
297     integer *ofbue, *sfbue;
298     real *voice;
299     integer *olbue, *slbue;
300     real value[9];
301     integer zc;
302     logical ot;
303     real qs;
304     real *maxmin;
305     integer vstate;
306     real rc1;
307     extern /* Subroutine */ int vparms_(integer *, real *, real *, integer *,
308 	    integer *, real *, integer *, integer *, integer *, integer *,
309 	    real *, real *, real *, real *);
310     integer fbe, lbe;
311     real *snr;
312     real snr2;
313 
314 /* 	Global Variables: */
315 /*       Arguments */
316 /* $Log$
317 /* Revision 1.2  2006/08/01 13:06:51  rjongbloed
318 /* Added a raft of unvalidated audio codecs from OpenH323 tree
319 /*
320 /* Revision 1.1.2.1  2006/07/22 14:03:19  rjongbloed
321 /* Added more plug ins
322 /*
323 /* Revision 1.1.2.1  2006/05/08 13:49:58  rjongbloed
324 /* Imported all the audio codec plug ins from OpenH323
325 /*
326 /* Revision 1.1  2004/05/04 11:16:43  csoutheren
327 /* Initial version
328 /*
329 /* Revision 1.2  2002/02/15 03:57:55  yurik
330 /* Warnings removed during compilation, patch courtesy of Jehan Bing, jehan@bravobrava.com
331 /*
332 /* Revision 1.1  2000/06/05 04:45:12  robertj
333 /* Added LPC-10 2400bps codec
334 /*
335  * Revision 1.2  1996/08/20  20:45:00  jaf
336  * Removed all static local variables that were SAVE'd in the Fortran
337  * code, and put them in struct lpc10_encoder_state that is passed as an
338  * argument.
339  *
340  * Removed init function, since all initialization is now done in
341  * init_lpc10_encoder_state().
342  *
343  * Revision 1.1  1996/08/19  22:30:14  jaf
344  * Initial revision
345  * */
346 /* Revision 1.3  1996/03/29  22:05:55  jaf */
347 /* Commented out the common block variables that are not needed by the */
348 /* embedded version. */
349 
350 /* Revision 1.2  1996/03/26  19:34:50  jaf */
351 /* Added comments indicating which constants are not needed in an */
352 /* application that uses the LPC-10 coder. */
353 
354 /* Revision 1.1  1996/02/07  14:44:09  jaf */
355 /* Initial revision */
356 
357 /*   LPC Processing control variables: */
358 
359 /* *** Read-only: initialized in setup */
360 
361 /*  Files for Speech, Parameter, and Bitstream Input & Output, */
362 /*    and message and debug outputs. */
363 
364 /* Here are the only files which use these variables: */
365 
366 /* lpcsim.f setup.f trans.f error.f vqsetup.f */
367 
368 /* Many files which use fdebug are not listed, since it is only used in */
369 /* those other files conditionally, to print trace statements. */
370 /* 	integer fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
371 /*  LPC order, Frame size, Quantization rate, Bits per frame, */
372 /*    Error correction */
373 /* Subroutine SETUP is the only place where order is assigned a value, */
374 /* and that value is 10.  It could increase efficiency 1% or so to */
375 /* declare order as a constant (i.e., a Fortran PARAMETER) instead of as
376 */
377 /* a variable in a COMMON block, since it is used in many places in the */
378 /* core of the coding and decoding routines.  Actually, I take that back.
379 */
380 /* At least when compiling with f2c, the upper bound of DO loops is */
381 /* stored in a local variable before the DO loop begins, and then that is
382 */
383 /* compared against on each iteration. */
384 /* Similarly for lframe, which is given a value of MAXFRM in SETUP. */
385 /* Similarly for quant, which is given a value of 2400 in SETUP.  quant */
386 /* is used in only a few places, and never in the core coding and */
387 /* decoding routines, so it could be eliminated entirely. */
388 /* nbits is similar to quant, and is given a value of 54 in SETUP. */
389 /* corrp is given a value of .TRUE. in SETUP, and is only used in the */
390 /* subroutines ENCODE and DECODE.  It doesn't affect the speed of the */
391 /* coder significantly whether it is .TRUE. or .FALSE., or whether it is
392 */
393 /* a constant or a variable, since it is only examined once per frame. */
394 /* Leaving it as a variable that is set to .TRUE.  seems like a good */
395 /* idea, since it does enable some error-correction capability for */
396 /* unvoiced frames, with no change in the coding rate, and no noticeable
397 */
398 /* quality difference in the decoded speech. */
399 /* 	integer quant, nbits */
400 /* *** Read/write: variables for debugging, not needed for LPC algorithm
401 */
402 
403 /*  Current frame, Unstable frames, Output clip count, Max onset buffer,
404 */
405 /*    Debug listing detail level, Line count on listing page */
406 
407 /* nframe is not needed for an embedded LPC10 at all. */
408 /* nunsfm is initialized to 0 in SETUP, and incremented in subroutine */
409 /* ERROR, which is only called from RCCHK.  When LPC10 is embedded into */
410 /* an application, I would recommend removing the call to ERROR in RCCHK,
411 */
412 /* and remove ERROR and nunsfm completely. */
413 /* iclip is initialized to 0 in SETUP, and incremented in entry SWRITE in
414 */
415 /* sread.f.  When LPC10 is embedded into an application, one might want */
416 /* to cause it to be incremented in a routine that takes the output of */
417 /* SYNTHS and sends it to an audio device.  It could be optionally */
418 /* displayed, for those that might want to know what it is. */
419 /* maxosp is never initialized to 0 in SETUP, although it probably should
420 */
421 /* be, and it is updated in subroutine ANALYS.  I doubt that its value */
422 /* would be of much interest to an application in which LPC10 is */
423 /* embedded. */
424 /* listl and lincnt are not needed for an embedded LPC10 at all. */
425 /* 	integer nframe, nunsfm, iclip, maxosp, listl, lincnt */
426 /* 	common /contrl/ fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
427 /* 	common /contrl/ quant, nbits */
428 /* 	common /contrl/ nframe, nunsfm, iclip, maxosp, listl, lincnt */
429 /* 	Parameters/constants */
430 /*       Voicing coefficient and Linear Discriminant Analysis variables:
431 */
432 /*       Max number of VDC's and VDC levels */
433 /*       The following are not Fortran PARAMETER's, but they are */
434 /*       initialized with DATA statements, and never modified. */
435 /*       Actual number of VDC's and levels */
436 /*       Local variables that need not be saved */
437 /*       Note: */
438 
439 /*       VALUE(1) through VALUE(8) are assigned values, but VALUE(9) */
440 /*       never is.  Yet VALUE(9) is read in the loop that begins "DO I =
441 */
442 /*       1, 9" below.  I believe that this doesn't cause any problems in
443 */
444 /*       this subroutine, because all VDC(9,*) array elements are 0, and
445 */
446 /*       this is what is multiplied by VALUE(9) in all cases.  Still, it
447 */
448 /*       would save a multiplication to change the loop to "DO I = 1, 8".
449 */
450 /*       Local state */
451 /*       WARNING! */
452 
453 /*       VOICE, SFBUE, and SLBUE should be saved from one invocation to */
454 /*       the next, but they are never given an initial value. */
455 
456 /*       Does Fortran 77 specify some default initial value, like 0, or */
457 /*       is it undefined?  If it is undefined, then this code should be */
458 /*       corrected to specify an initial value. */
459 
460 /*       For VOICE, note that it is "shifted" in the statement that */
461 /*       begins "IF (HALF .EQ. 1) THEN" below.  Also, uninitialized */
462 /*       values in the VOICE array can only affect entries in the VOIBUF
463 */
464 /*       array that are for the same frame, or for an older frame.  Thus
465 */
466 /*       the effects of uninitialized values in VOICE cannot linger on */
467 /*       for more than 2 or 3 frame times. */
468 
469 /*       For SFBUE and SLBUE, the effects of uninitialized values can */
470 /*       linger on for many frame times, because their previous values */
471 /*       are exponentially decayed.  Thus it is more important to choose
472 */
473 /*       initial values for these variables.  I would guess that a */
474 /*       reasonable initial value for SFBUE is REF/16, the same as used */
475 /*       for FBUE and OFBUE.  Similarly, SLBUE can be initialized to */
476 /*       REF/32, the same as for LBUE and OLBUE. */
477 
478 /*       These guessed initial values should be validated by re-running */
479 /*       the modified program on some audio samples. */
480 
481 /*   Declare and initialize filters: */
482 
483     dither = (&st->dither);
484     snr = (&st->snr);
485     maxmin = (&st->maxmin);
486     voice = (&st->voice[0]);
487     lbve = (&st->lbve);
488     lbue = (&st->lbue);
489     fbve = (&st->fbve);
490     fbue = (&st->fbue);
491     ofbue = (&st->ofbue);
492     olbue = (&st->olbue);
493     sfbue = (&st->sfbue);
494     slbue = (&st->slbue);
495 
496     /* Parameter adjustments */
497     if (vwin) {
498 	--vwin;
499 	}
500     if (buflim) {
501 	--buflim;
502 	}
503     if (inbuf) {
504 	inbuf_offset = buflim[1];
505 	inbuf -= inbuf_offset;
506 	}
507     if (lpbuf) {
508 	lpbuf_offset = buflim[3];
509 	lpbuf -= lpbuf_offset;
510 	}
511     if (ivrc) {
512 	--ivrc;
513 	}
514     if (obound) {
515 	--obound;
516 	}
517     if (voibuf) {
518 	--voibuf;
519 	}
520 
521     /* Function Body */
522 
523 /*       The following variables are saved from one invocation to the */
524 /*       next, but are not initialized with DATA statements.  This is */
525 /*       acceptable, because FIRST is initialized ot .TRUE., and the */
526 /*       first time that this subroutine is then called, they are all */
527 /*       given initial values. */
528 
529 /*       SNR */
530 /*       LBVE, LBUE, FBVE, FBUE, OFBUE, OLBUE */
531 
532 /*       MAXMIN is initialized on the first call, assuming that HALF */
533 /*       .EQ. 1 on first call.  This is how ANALYS calls this subroutine.
534 */
535 
536 /*   Voicing Decision Parameter vector (* denotes zero coefficient): */
537 
538 /* 	* MAXMIN */
539 /* 	  LBE/LBVE */
540 /* 	  ZC */
541 /* 	  RC1 */
542 /* 	  QS */
543 /* 	  IVRC2 */
544 /* 	  aR_B */
545 /* 	  aR_F */
546 /* 	* LOG(LBE/LBVE) */
547 /*  Define 2-D voicing decision coefficient vector according to the voicin
548 g*/
549 /*  parameter order above.  Each row (VDC vector) is optimized for a speci
550 fic*/
551 /*   SNR.  The last element of the vector is the constant. */
552 /* 	         E    ZC    RC1    Qs   IVRC2  aRb   aRf        c */
553 
554 /*  The VOICE array contains the result of the linear discriminant functio
555 n*/
556 /*   (analog values).  The VOIBUF array contains the hard-limited binary
557 */
558 /*   voicing decisions.  The VOICE and VOIBUF arrays, according to FORTRAN
559  */
560 /*   memory allocation, are addressed as: */
561 
562 /* 	   (half-frame number, future-frame number) */
563 
564 /* 	   |   Past    |  Present  |  Future1  |  Future2  | */
565 /* 	   | 1,0 | 2,0 | 1,1 | 2,1 | 1,2 | 2,2 | 1,3 | 2,3 |  --->  time */
566 
567 /*   Update linear discriminant function history each frame: */
568     if (*half == 1) {
569 	voice[0] = voice[2];
570 	voice[1] = voice[3];
571 	voice[2] = voice[4];
572 	voice[3] = voice[5];
573 	*maxmin = *maxamd / max(*minamd,1.f);
574     }
575 /*   Calculate voicing parameters twice per frame: */
576     vparms_(&vwin[1], &inbuf[inbuf_offset], &lpbuf[lpbuf_offset], &buflim[1],
577 	    half, dither, mintau, &zc, &lbe, &fbe, &qs, &rc1, &ar_b__, &
578 	    ar_f__);
579 /*   Estimate signal-to-noise ratio to select the appropriate VDC vector.
580 */
581 /*   The SNR is estimated as the running average of the ratio of the */
582 /*   running average full-band voiced energy to the running average */
583 /*   full-band unvoiced energy. SNR filter has gain of 63. */
584     r__1 = (*snr + *fbve / (real) max(*fbue,1)) * 63 / 64.f;
585     *snr = (real) i_nint(&r__1);
586     snr2 = *snr * *fbue / max(*lbue,1);
587 /*   Quantize SNR to SNRL according to VDCL thresholds. */
588     snrl = 1;
589     i__1 = nvdcl - 1;
590     for (snrl = 1; snrl <= i__1; ++snrl) {
591 	if (snr2 > vdcl[snrl - 1]) {
592 	    goto L69;
593 	}
594     }
595 /*   	(Note:  SNRL = NVDCL here) */
596 L69:
597 /*   Linear discriminant voicing parameters: */
598     value[0] = *maxmin;
599     value[1] = (real) lbe / max(*lbve,1);
600     value[2] = (real) zc;
601     value[3] = rc1;
602     value[4] = qs;
603     value[5] = ivrc[2];
604     value[6] = ar_b__;
605     value[7] = ar_f__;
606 /*   Evaluation of linear discriminant function: */
607     voice[*half + 3] = vdc[snrl * 10 - 1];
608     for (i__ = 1; i__ <= 8; ++i__) {
609 	voice[*half + 3] += vdc[i__ + snrl * 10 - 11] * value[i__ - 1];
610     }
611 /*   Classify as voiced if discriminant > 0, otherwise unvoiced */
612 /*   Voicing decision for current half-frame:  1 = Voiced; 0 = Unvoiced */
613     if (voice[*half + 3] > 0.f) {
614 	voibuf[*half + 6] = 1;
615     } else {
616 	voibuf[*half + 6] = 0;
617     }
618 /*   Skip voicing decision smoothing in first half-frame: */
619 /*     Give a value to VSTATE, so that trace statements below will print
620 */
621 /*     a consistent value from one call to the next when HALF .EQ. 1. */
622 /*     The value of VSTATE is not used for any other purpose when this is
623 */
624 /*     true. */
625     vstate = -1;
626     if (*half == 1) {
627 	goto L99;
628     }
629 /*   Voicing decision smoothing rules (override of linear combination): */
630 
631 /* 	Unvoiced half-frames:  At least two in a row. */
632 /* 	-------------------- */
633 
634 /* 	Voiced half-frames:    At least two in a row in one frame. */
635 /* 	-------------------    Otherwise at least three in a row. */
636 /* 			       (Due to the way transition frames are encoded) */
637 
638 /* 	In many cases, the discriminant function determines how to smooth. */
639 /*	In the following chart, the decisions marked with a * may be overridden
640 .*/
641 
642 /*   Voicing override of transitions at onsets: */
643 /* 	If a V/UV or UV/V voicing decision transition occurs within one-half
644 */
645 /* 	frame of an onset bounding a voicing window, then the transition is */
646 /* 	moved to occur at the onset. */
647 
648 /* 	P	1F */
649 /* 	-----	----- */
650 /* 	0   0   0   0 */
651 /* 	0   0   0*  1	(If there is an onset there) */
652 /* 	0   0   1*  0*	(Based on 2F and discriminant distance) */
653 /* 	0   0   1   1 */
654 /* 	0   1*  0   0	(Always) */
655 /* 	0   1*  0*  1	(Based on discriminant distance) */
656 /* 	0*  1   1   0*	(Based on past, 2F, and discriminant distance) */
657 /* 	0   1*  1   1	(If there is an onset there) */
658 /* 	1   0*  0   0	(If there is an onset there) */
659 /* 	1   0   0   1 */
660 /* 	1   0*  1*  0	(Based on discriminant distance) */
661 /* 	1   0*  1   1	(Always) */
662 /* 	1   1   0   0 */
663 /* 	1   1   0*  1*	(Based on 2F and discriminant distance) */
664 /* 	1   1   1*  0	(If there is an onset there) */
665 /* 	1   1   1   1 */
666 
667 /*   Determine if there is an onset transition between P and 1F. */
668 /*   OT (Onset Transition) is true if there is an onset between */
669 /*   P and 1F but not after 1F. */
670     ot = ((obound[1] & 2) != 0 || obound[2] == 1) && (obound[3] & 1) == 0;
671 /*   Multi-way dispatch on voicing decision history: */
672     vstate = (voibuf[3] << 3) + (voibuf[4] << 2) + (voibuf[5] << 1) + voibuf[
673 	    6];
674     switch (vstate + 1) {
675 	case 1:  goto L99;
676 	case 2:  goto L1;
677 	case 3:  goto L2;
678 	case 4:  goto L99;
679 	case 5:  goto L4;
680 	case 6:  goto L5;
681 	case 7:  goto L6;
682 	case 8:  goto L7;
683 	case 9:  goto L8;
684 	case 10:  goto L99;
685 	case 11:  goto L10;
686 	case 12:  goto L11;
687 	case 13:  goto L99;
688 	case 14:  goto L13;
689 	case 15:  goto L14;
690 	case 16:  goto L99;
691     }
692 L1:
693     if (ot && voibuf[7] == 1) {
694 	voibuf[5] = 1;
695     }
696     goto L99;
697 L2:
698     if (voibuf[7] == 0 || voice[2] < -voice[3]) {
699 	voibuf[5] = 0;
700     } else {
701 	voibuf[6] = 1;
702     }
703     goto L99;
704 L4:
705     voibuf[4] = 0;
706     goto L99;
707 L5:
708     if (voice[1] < -voice[2]) {
709 	voibuf[4] = 0;
710     } else {
711 	voibuf[5] = 1;
712     }
713     goto L99;
714 /*   VOIBUF(2,0) must be 0 */
715 L6:
716     if (voibuf[1] == 1 || voibuf[7] == 1 || voice[3] > voice[0]) {
717 	voibuf[6] = 1;
718     } else {
719 	voibuf[3] = 1;
720     }
721     goto L99;
722 L7:
723     if (ot) {
724 	voibuf[4] = 0;
725     }
726     goto L99;
727 L8:
728     if (ot) {
729 	voibuf[4] = 1;
730     }
731     goto L99;
732 L10:
733     if (voice[2] < -voice[1]) {
734 	voibuf[5] = 0;
735     } else {
736 	voibuf[4] = 1;
737     }
738     goto L99;
739 L11:
740     voibuf[4] = 1;
741     goto L99;
742 L13:
743     if (voibuf[7] == 0 && voice[3] < -voice[2]) {
744 	voibuf[6] = 0;
745     } else {
746 	voibuf[5] = 1;
747     }
748     goto L99;
749 L14:
750     if (ot && voibuf[7] == 0) {
751 	voibuf[5] = 0;
752     }
753 /* 	GOTO 99 */
754 L99:
755 /*   Now update parameters: */
756 /*   ---------------------- */
757 
758 /*  During unvoiced half-frames, update the low band and full band unvoice
759 d*/
760 /*   energy estimates (LBUE and FBUE) and also the zero crossing */
761 /*   threshold (DITHER).  (The input to the unvoiced energy filters is */
762 /*   restricted to be less than 10dB above the previous inputs of the */
763 /*   filters.) */
764 /*   During voiced half-frames, update the low-pass (LBVE) and all-pass */
765 /*   (FBVE) voiced energy estimates. */
766     if (voibuf[*half + 6] == 0) {
767 /* Computing MIN */
768 	i__1 = fbe, i__2 = *ofbue * 3;
769 	r__1 = (*sfbue * 63 + (min(i__1,i__2) << 3)) / 64.f;
770 	*sfbue = i_nint(&r__1);
771 	*fbue = *sfbue / 8;
772 	*ofbue = fbe;
773 /* Computing MIN */
774 	i__1 = lbe, i__2 = *olbue * 3;
775 	r__1 = (*slbue * 63 + (min(i__1,i__2) << 3)) / 64.f;
776 	*slbue = i_nint(&r__1);
777 	*lbue = *slbue / 8;
778 	*olbue = lbe;
779     } else {
780 	r__1 = (*lbve * 63 + lbe) / 64.f;
781 	*lbve = i_nint(&r__1);
782 	r__1 = (*fbve * 63 + fbe) / 64.f;
783 	*fbve = i_nint(&r__1);
784     }
785 /*   Set dither threshold to yield proper zero crossing rates in the */
786 /*   presence of low frequency noise and low level signal input. */
787 /*   NOTE: The divisor is a function of REF, the expected energies. */
788 /* Computing MIN */
789 /* Computing MAX */
790     r__2 = (real)(sqrt((real) (*lbue * *lbve)) * 64 / 3000);
791     r__1 = max(r__2,1.f);
792     *dither = min(r__1,20.f);
793 /*   Voicing decisions are returned in VOIBUF. */
794     return 0;
795 } /* voicin_ */
796