3 * Revision 1.2 1996/08/20 20:25:29 jaf
4 * Removed all static local variables that were SAVE'd in the Fortran
5 * code, and put them in struct lpc10_encoder_state that is passed as an
8 * Removed init function, since all initialization is now done in
9 * init_lpc10_encoder_state().
11 * Revision 1.1 1996/08/19 22:32:26 jaf
17 /* -- translated by f2c (version 19951025).
18 You must link the resulting object file with the libraries:
19 -lf2c -lm (in that order)
24 extern int dyptrk_(real
*amdf
, integer
*ltau
, integer
*minptr
, integer
*voice
, integer
*pitch
, integer
*midx
, struct lpc10_encoder_state
*st
);
26 /* Common Block Declarations */
29 integer order
, lframe
;
33 #define contrl_1 contrl_
35 /* ********************************************************************* */
37 /* DYPTRK Version 52 */
40 * Revision 1.2 1996/08/20 20:25:29 jaf
41 * Removed all static local variables that were SAVE'd in the Fortran
42 * code, and put them in struct lpc10_encoder_state that is passed as an
45 * Removed init function, since all initialization is now done in
46 * init_lpc10_encoder_state().
48 * Revision 1.1 1996/08/19 22:32:26 jaf
51 /* Revision 1.5 1996/03/26 19:35:35 jaf */
52 /* Commented out trace statements. */
54 /* Revision 1.4 1996/03/19 18:03:22 jaf */
55 /* Replaced the initialization "DATA P/60*DEPTH*0/" with "DATA P/120*0/", */
56 /* because apparently Fortran (or at least f2c) can't handle expressions */
59 /* Revision 1.3 1996/03/19 17:38:32 jaf */
60 /* Added comments about the local variables that should be saved from one */
61 /* invocation to the next. None of them were given initial values in the */
62 /* original code, but from my testing, it appears that initializing them */
65 /* Added entry INITDYPTRK to reinitialize these local variables. */
67 /* Revision 1.2 1996/03/13 16:32:17 jaf */
68 /* Comments added explaining which of the local variables of this */
69 /* subroutine need to be saved from one invocation to the next, and which */
72 /* WARNING! Some of them that should are never given initial values in */
73 /* this code. Hopefully, Fortran 77 defines initial values for them, but */
74 /* even so, giving them explicit initial values is preferable. */
76 /* Revision 1.1 1996/02/07 14:45:14 jaf */
77 /* Initial revision */
80 /* ********************************************************************* */
82 /* Dynamic Pitch Tracker */
85 /* AMDF - Average Magnitude Difference Function array */
86 /* Indices 1 through LTAU read, and MINPTR */
87 /* LTAU - Number of lags in AMDF */
88 /* MINPTR - Location of minimum AMDF value */
89 /* VOICE - Voicing decision */
91 /* PITCH - Smoothed pitch value, 2 frames delayed */
92 /* MIDX - Initial estimate of current frame pitch */
93 /* Compile time constant: */
94 /* DEPTH - Number of frames to trace back */
96 /* This subroutine maintains local state from one call to the next. If */
97 /* you want to switch to using a new audio stream for this filter, or */
98 /* reinitialize its state for any other reason, call the ENTRY */
101 /* Subroutine */ int dyptrk_(real
*amdf
, integer
*ltau
, integer
*
102 minptr
, integer
*voice
, integer
*pitch
, integer
*midx
,
103 struct lpc10_encoder_state
*st
)
105 /* Initialized data */
112 /* System generated locals */
115 /* Local variables */
118 integer iptr
, i__
, j
;
119 real alpha
, minsc
, maxsc
;
123 /* LPC Processing control variables: */
125 /* *** Read-only: initialized in setup */
127 /* Files for Speech, Parameter, and Bitstream Input & Output, */
128 /* and message and debug outputs. */
130 /* Here are the only files which use these variables: */
132 /* lpcsim.f setup.f trans.f error.f vqsetup.f */
134 /* Many files which use fdebug are not listed, since it is only used in */
135 /* those other files conditionally, to print trace statements. */
136 /* integer fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
137 /* LPC order, Frame size, Quantization rate, Bits per frame, */
138 /* Error correction */
139 /* Subroutine SETUP is the only place where order is assigned a value, */
140 /* and that value is 10. It could increase efficiency 1% or so to */
141 /* declare order as a constant (i.e., a Fortran PARAMETER) instead of as
143 /* a variable in a COMMON block, since it is used in many places in the */
144 /* core of the coding and decoding routines. Actually, I take that back.
146 /* At least when compiling with f2c, the upper bound of DO loops is */
147 /* stored in a local variable before the DO loop begins, and then that is
149 /* compared against on each iteration. */
150 /* Similarly for lframe, which is given a value of MAXFRM in SETUP. */
151 /* Similarly for quant, which is given a value of 2400 in SETUP. quant */
152 /* is used in only a few places, and never in the core coding and */
153 /* decoding routines, so it could be eliminated entirely. */
154 /* nbits is similar to quant, and is given a value of 54 in SETUP. */
155 /* corrp is given a value of .TRUE. in SETUP, and is only used in the */
156 /* subroutines ENCODE and DECODE. It doesn't affect the speed of the */
157 /* coder significantly whether it is .TRUE. or .FALSE., or whether it is
159 /* a constant or a variable, since it is only examined once per frame. */
160 /* Leaving it as a variable that is set to .TRUE. seems like a good */
161 /* idea, since it does enable some error-correction capability for */
162 /* unvoiced frames, with no change in the coding rate, and no noticeable
164 /* quality difference in the decoded speech. */
165 /* integer quant, nbits */
166 /* *** Read/write: variables for debugging, not needed for LPC algorithm
169 /* Current frame, Unstable frames, Output clip count, Max onset buffer,
171 /* Debug listing detail level, Line count on listing page */
173 /* nframe is not needed for an embedded LPC10 at all. */
174 /* nunsfm is initialized to 0 in SETUP, and incremented in subroutine */
175 /* ERROR, which is only called from RCCHK. When LPC10 is embedded into */
176 /* an application, I would recommend removing the call to ERROR in RCCHK,
178 /* and remove ERROR and nunsfm completely. */
179 /* iclip is initialized to 0 in SETUP, and incremented in entry SWRITE in
181 /* sread.f. When LPC10 is embedded into an application, one might want */
182 /* to cause it to be incremented in a routine that takes the output of */
183 /* SYNTHS and sends it to an audio device. It could be optionally */
184 /* displayed, for those that might want to know what it is. */
185 /* maxosp is never initialized to 0 in SETUP, although it probably should
187 /* be, and it is updated in subroutine ANALYS. I doubt that its value */
188 /* would be of much interest to an application in which LPC10 is */
190 /* listl and lincnt are not needed for an embedded LPC10 at all. */
191 /* integer nframe, nunsfm, iclip, maxosp, listl, lincnt */
192 /* common /contrl/ fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
193 /* common /contrl/ quant, nbits */
194 /* common /contrl/ nframe, nunsfm, iclip, maxosp, listl, lincnt */
195 /* Parameters/constants */
196 /* Local variables that need not be saved */
197 /* Note that PATH is only used for debugging purposes, and can be */
200 /* It would be a bit more "general" to define S(LTAU), if Fortran */
201 /* allows the argument of a function to be used as the dimension of
203 /* a local array variable. */
204 /* IPOINT is always in the range 0 to DEPTH-1. */
207 /* In the original version of this subroutine, IPOINT, ALPHAX, */
208 /* every element of S, and potentially any element of P with the */
209 /* second index value .NE. IPTR were read without being given */
210 /* initial values (all indices of P with second index equal to */
211 /* IPTR are all written before being read in this subroutine). */
213 /* From examining the code carefully, it appears that all of these
215 /* should be saved from one invocation to the next. */
217 /* I've run lpcsim with the "-l 6" option to see all of the */
218 /* debugging information that is printed out by this subroutine */
219 /* below, and it appears that S, P, IPOINT, and ALPHAX are all */
220 /* initialized to 0 (these initial values would likely be different
222 /* on different platforms, compilers, etc.). Given that the output
224 /* of the coder sounds reasonable, I'm going to initialize these */
225 /* variables to 0 explicitly. */
229 ipoint
= &(st
->ipoint
);
230 alphax
= &(st
->alphax
);
233 /* Parameter adjustments */
240 /* Calculate the confidence factor ALPHA, used as a threshold slope in
242 /* SEESAW. If unvoiced, set high slope so that every point in P array
244 /* is marked as a potential pitch frequency. A scaled up version (ALPHAX
246 /* is used to maintain arithmetic precision. */
248 *alphax
= *alphax
* .75f
+ amdf
[*minptr
] / 2.f
;
252 alpha
= *alphax
/ 16;
253 if (*voice
== 0 && *alphax
< 128.f
) {
256 /* SEESAW: Construct a pitch pointer array and intermediate winner functio
258 /* Left to right pass: */
260 p
[iptr
* 60 - 60] = 1;
265 for (i__
= 1; i__
<= i__1
; ++i__
) {
267 if (sbar
< s
[i__
- 1]) {
269 p
[i__
+ iptr
* 60 - 61] = pbar
;
272 p
[i__
+ iptr
* 60 - 61] = i__
;
276 /* Right to left pass: */
281 if (sbar
< s
[i__
- 1]) {
283 p
[i__
+ iptr
* 60 - 61] = pbar
;
285 pbar
= p
[i__
+ iptr
* 60 - 61];
291 /* Update S using AMDF */
292 /* Find maximum, minimum, and location of minimum */
298 for (i__
= 2; i__
<= i__1
; ++i__
) {
299 s
[i__
- 1] += amdf
[i__
] / 2;
300 if (s
[i__
- 1] > maxsc
) {
303 if (s
[i__
- 1] < minsc
) {
308 /* Subtract MINSC from S to prevent overflow */
310 for (i__
= 1; i__
<= i__1
; ++i__
) {
314 /* Use higher octave pitch if significant null there */
316 for (i__
= 20; i__
<= 40; i__
+= 10) {
318 if (s
[*midx
- i__
- 1] < maxsc
/ 4) {
324 /* TRACE: look back two frames to find minimum cost pitch estimate */
327 for (i__
= 1; i__
<= 2; ++i__
) {
329 *pitch
= p
[*pitch
+ j
* 60 - 61];
332 /* The following statement subtracts one from IPOINT, mod DEPTH. I
334 /* think the author chose to add DEPTH-1, instead of subtracting 1,
336 /* because then it will work even if MOD doesn't work as desired on
338 /* negative arguments. */
340 *ipoint
= (*ipoint
+ 1) % 2;