1 /*
2 
3 $Log$
4 Revision 1.2  2006/08/01 13:06:49  rjongbloed
5 Added a raft of unvalidated audio codecs from OpenH323 tree
6 
7 Revision 1.1.2.1  2006/07/22 14:03:15  rjongbloed
8 Added more plug ins
9 
10 Revision 1.1.2.1  2006/05/08 13:49:57  rjongbloed
11 Imported all the audio codec plug ins from OpenH323
12 
13 Revision 1.1  2004/05/04 11:16:42  csoutheren
14 Initial version
15 
16 Revision 1.1  2000/06/05 04:45:12  robertj
17 Added LPC-10 2400bps codec
18 
19  * Revision 1.2  1996/08/20  20:25:29  jaf
20  * Removed all static local variables that were SAVE'd in the Fortran
21  * code, and put them in struct lpc10_encoder_state that is passed as an
22  * argument.
23  *
24  * Removed init function, since all initialization is now done in
25  * init_lpc10_encoder_state().
26  *
27  * Revision 1.1  1996/08/19  22:32:26  jaf
28  * Initial revision
29  *
30 
31 */
32 
33 #ifdef P_R_O_T_O_T_Y_P_E_S
34 extern int dyptrk_(real *amdf, integer *ltau, integer *minptr, integer *voice, integer *pitch, integer *midx, struct lpc10_encoder_state *st);
35 /* comlen contrl_ 12 */
36 #endif
37 
38 /*  -- translated by f2c (version 19951025).
39    You must link the resulting object file with the libraries:
40 	-lf2c -lm   (in that order)
41 */
42 
43 #include "f2c.h"
44 
45 /* Common Block Declarations */
46 
47 extern struct {
48     integer order, lframe;
49     logical corrp;
50 } contrl_;
51 
52 #define contrl_1 contrl_
53 
54 /* ********************************************************************* */
55 
56 /* 	DYPTRK Version 52 */
57 
58 /* $Log$
59 /* Revision 1.2  2006/08/01 13:06:49  rjongbloed
60 /* Added a raft of unvalidated audio codecs from OpenH323 tree
61 /*
62 /* Revision 1.1.2.1  2006/07/22 14:03:15  rjongbloed
63 /* Added more plug ins
64 /*
65 /* Revision 1.1.2.1  2006/05/08 13:49:57  rjongbloed
66 /* Imported all the audio codec plug ins from OpenH323
67 /*
68 /* Revision 1.1  2004/05/04 11:16:42  csoutheren
69 /* Initial version
70 /*
71 /* Revision 1.1  2000/06/05 04:45:12  robertj
72 /* Added LPC-10 2400bps codec
73 /*
74  * Revision 1.2  1996/08/20  20:25:29  jaf
75  * Removed all static local variables that were SAVE'd in the Fortran
76  * code, and put them in struct lpc10_encoder_state that is passed as an
77  * argument.
78  *
79  * Removed init function, since all initialization is now done in
80  * init_lpc10_encoder_state().
81  *
82  * Revision 1.1  1996/08/19  22:32:26  jaf
83  * Initial revision
84  * */
85 /* Revision 1.5  1996/03/26  19:35:35  jaf */
86 /* Commented out trace statements. */
87 
88 /* Revision 1.4  1996/03/19  18:03:22  jaf */
89 /* Replaced the initialization "DATA P/60*DEPTH*0/" with "DATA P/120*0/", */
90 /* because apparently Fortran (or at least f2c) can't handle expressions */
91 /* like that. */
92 
93 /* Revision 1.3  1996/03/19  17:38:32  jaf */
94 /* Added comments about the local variables that should be saved from one */
95 /* invocation to the next.  None of them were given initial values in the */
96 /* original code, but from my testing, it appears that initializing them */
97 /* all to 0 works. */
98 
99 /* Added entry INITDYPTRK to reinitialize these local variables. */
100 
101 /* Revision 1.2  1996/03/13  16:32:17  jaf */
102 /* Comments added explaining which of the local variables of this */
103 /* subroutine need to be saved from one invocation to the next, and which */
104 /* do not. */
105 
106 /* WARNING!  Some of them that should are never given initial values in */
107 /* this code.  Hopefully, Fortran 77 defines initial values for them, but */
108 /* even so, giving them explicit initial values is preferable. */
109 
110 /* Revision 1.1  1996/02/07 14:45:14  jaf */
111 /* Initial revision */
112 
113 
114 /* ********************************************************************* */
115 
116 /*   Dynamic Pitch Tracker */
117 
118 /* Input: */
119 /*  AMDF   - Average Magnitude Difference Function array */
120 /*           Indices 1 through LTAU read, and MINPTR */
121 /*  LTAU   - Number of lags in AMDF */
122 /*  MINPTR - Location of minimum AMDF value */
123 /*  VOICE  - Voicing decision */
124 /* Output: */
125 /*  PITCH  - Smoothed pitch value, 2 frames delayed */
126 /*  MIDX   - Initial estimate of current frame pitch */
127 /* Compile time constant: */
128 /*  DEPTH  - Number of frames to trace back */
129 
130 /* This subroutine maintains local state from one call to the next.  If */
131 /* you want to switch to using a new audio stream for this filter, or */
132 /* reinitialize its state for any other reason, call the ENTRY */
133 /* INITDYPTRK. */
134 
dyptrk_(real * amdf,integer * ltau,integer * minptr,integer * voice,integer * pitch,integer * midx,struct lpc10_encoder_state * st)135 /* Subroutine */ int dyptrk_(real *amdf, integer *ltau, integer *
136 	minptr, integer *voice, integer *pitch, integer *midx,
137 			       struct lpc10_encoder_state *st)
138 {
139     /* Initialized data */
140 
141     real *s;
142     integer *p;
143     integer *ipoint;
144     real *alphax;
145 
146     /* System generated locals */
147     integer i__1;
148 
149     /* Local variables */
150     integer pbar;
151     real sbar;
152     integer path[2], iptr, i__, j;
153     real alpha, minsc, maxsc;
154 
155 /*       Arguments */
156 /* $Log$
157 /* Revision 1.2  2006/08/01 13:06:49  rjongbloed
158 /* Added a raft of unvalidated audio codecs from OpenH323 tree
159 /*
160 /* Revision 1.1.2.1  2006/07/22 14:03:15  rjongbloed
161 /* Added more plug ins
162 /*
163 /* Revision 1.1.2.1  2006/05/08 13:49:57  rjongbloed
164 /* Imported all the audio codec plug ins from OpenH323
165 /*
166 /* Revision 1.1  2004/05/04 11:16:42  csoutheren
167 /* Initial version
168 /*
169 /* Revision 1.1  2000/06/05 04:45:12  robertj
170 /* Added LPC-10 2400bps codec
171 /*
172  * Revision 1.2  1996/08/20  20:25:29  jaf
173  * Removed all static local variables that were SAVE'd in the Fortran
174  * code, and put them in struct lpc10_encoder_state that is passed as an
175  * argument.
176  *
177  * Removed init function, since all initialization is now done in
178  * init_lpc10_encoder_state().
179  *
180  * Revision 1.1  1996/08/19  22:32:26  jaf
181  * Initial revision
182  * */
183 /* Revision 1.3  1996/03/29  22:05:55  jaf */
184 /* Commented out the common block variables that are not needed by the */
185 /* embedded version. */
186 
187 /* Revision 1.2  1996/03/26  19:34:50  jaf */
188 /* Added comments indicating which constants are not needed in an */
189 /* application that uses the LPC-10 coder. */
190 
191 /* Revision 1.1  1996/02/07  14:44:09  jaf */
192 /* Initial revision */
193 
194 /*   LPC Processing control variables: */
195 
196 /* *** Read-only: initialized in setup */
197 
198 /*  Files for Speech, Parameter, and Bitstream Input & Output, */
199 /*    and message and debug outputs. */
200 
201 /* Here are the only files which use these variables: */
202 
203 /* lpcsim.f setup.f trans.f error.f vqsetup.f */
204 
205 /* Many files which use fdebug are not listed, since it is only used in */
206 /* those other files conditionally, to print trace statements. */
207 /* 	integer fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
208 /*  LPC order, Frame size, Quantization rate, Bits per frame, */
209 /*    Error correction */
210 /* Subroutine SETUP is the only place where order is assigned a value, */
211 /* and that value is 10.  It could increase efficiency 1% or so to */
212 /* declare order as a constant (i.e., a Fortran PARAMETER) instead of as
213 */
214 /* a variable in a COMMON block, since it is used in many places in the */
215 /* core of the coding and decoding routines.  Actually, I take that back.
216 */
217 /* At least when compiling with f2c, the upper bound of DO loops is */
218 /* stored in a local variable before the DO loop begins, and then that is
219 */
220 /* compared against on each iteration. */
221 /* Similarly for lframe, which is given a value of MAXFRM in SETUP. */
222 /* Similarly for quant, which is given a value of 2400 in SETUP.  quant */
223 /* is used in only a few places, and never in the core coding and */
224 /* decoding routines, so it could be eliminated entirely. */
225 /* nbits is similar to quant, and is given a value of 54 in SETUP. */
226 /* corrp is given a value of .TRUE. in SETUP, and is only used in the */
227 /* subroutines ENCODE and DECODE.  It doesn't affect the speed of the */
228 /* coder significantly whether it is .TRUE. or .FALSE., or whether it is
229 */
230 /* a constant or a variable, since it is only examined once per frame. */
231 /* Leaving it as a variable that is set to .TRUE.  seems like a good */
232 /* idea, since it does enable some error-correction capability for */
233 /* unvoiced frames, with no change in the coding rate, and no noticeable
234 */
235 /* quality difference in the decoded speech. */
236 /* 	integer quant, nbits */
237 /* *** Read/write: variables for debugging, not needed for LPC algorithm
238 */
239 
240 /*  Current frame, Unstable frames, Output clip count, Max onset buffer,
241 */
242 /*    Debug listing detail level, Line count on listing page */
243 
244 /* nframe is not needed for an embedded LPC10 at all. */
245 /* nunsfm is initialized to 0 in SETUP, and incremented in subroutine */
246 /* ERROR, which is only called from RCCHK.  When LPC10 is embedded into */
247 /* an application, I would recommend removing the call to ERROR in RCCHK,
248 */
249 /* and remove ERROR and nunsfm completely. */
250 /* iclip is initialized to 0 in SETUP, and incremented in entry SWRITE in
251 */
252 /* sread.f.  When LPC10 is embedded into an application, one might want */
253 /* to cause it to be incremented in a routine that takes the output of */
254 /* SYNTHS and sends it to an audio device.  It could be optionally */
255 /* displayed, for those that might want to know what it is. */
256 /* maxosp is never initialized to 0 in SETUP, although it probably should
257 */
258 /* be, and it is updated in subroutine ANALYS.  I doubt that its value */
259 /* would be of much interest to an application in which LPC10 is */
260 /* embedded. */
261 /* listl and lincnt are not needed for an embedded LPC10 at all. */
262 /* 	integer nframe, nunsfm, iclip, maxosp, listl, lincnt */
263 /* 	common /contrl/ fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
264 /* 	common /contrl/ quant, nbits */
265 /* 	common /contrl/ nframe, nunsfm, iclip, maxosp, listl, lincnt */
266 /* 	Parameters/constants */
267 /*       Local variables that need not be saved */
268 /*       Note that PATH is only used for debugging purposes, and can be */
269 /*       removed. */
270 /*       Local state */
271 /*       It would be a bit more "general" to define S(LTAU), if Fortran */
272 /*       allows the argument of a function to be used as the dimension of
273 */
274 /*       a local array variable. */
275 /*       IPOINT is always in the range 0 to DEPTH-1. */
276 /*       WARNING! */
277 
278 /*       In the original version of this subroutine, IPOINT, ALPHAX, */
279 /*       every element of S, and potentially any element of P with the */
280 /*       second index value .NE. IPTR were read without being given */
281 /*       initial values (all indices of P with second index equal to */
282 /*       IPTR are all written before being read in this subroutine). */
283 
284 /*       From examining the code carefully, it appears that all of these
285 */
286 /*       should be saved from one invocation to the next. */
287 
288 /*       I've run lpcsim with the "-l 6" option to see all of the */
289 /*       debugging information that is printed out by this subroutine */
290 /*       below, and it appears that S, P, IPOINT, and ALPHAX are all */
291 /*       initialized to 0 (these initial values would likely be different
292 */
293 /*       on different platforms, compilers, etc.).  Given that the output
294 */
295 /*       of the coder sounds reasonable, I'm going to initialize these */
296 /*       variables to 0 explicitly. */
297 
298     s = &(st->s[0]);
299     p = &(st->p[0]);
300     ipoint = &(st->ipoint);
301     alphax = &(st->alphax);
302 
303 
304     /* Parameter adjustments */
305     if (amdf) {
306 	--amdf;
307 	}
308 
309     /* Function Body */
310 
311 /*   Calculate the confidence factor ALPHA, used as a threshold slope in
312 */
313 /*   SEESAW.  If unvoiced, set high slope so that every point in P array
314 */
315 /*  is marked as a potential pitch frequency.  A scaled up version (ALPHAX
316 )*/
317 /*   is used to maintain arithmetic precision. */
318     if (*voice == 1) {
319 	*alphax = *alphax * .75f + amdf[*minptr] / 2.f;
320     } else {
321 	*alphax *= .984375f;
322     }
323     alpha = *alphax / 16;
324     if (*voice == 0 && *alphax < 128.f) {
325 	alpha = 8.f;
326     }
327 /* SEESAW: Construct a pitch pointer array and intermediate winner functio
328 n*/
329 /*   Left to right pass: */
330     iptr = *ipoint + 1;
331     p[iptr * 60 - 60] = 1;
332     i__ = 1;
333     pbar = 1;
334     sbar = s[0];
335     i__1 = *ltau;
336     for (i__ = 1; i__ <= i__1; ++i__) {
337 	sbar += alpha;
338 	if (sbar < s[i__ - 1]) {
339 	    s[i__ - 1] = sbar;
340 	    p[i__ + iptr * 60 - 61] = pbar;
341 	} else {
342 	    sbar = s[i__ - 1];
343 	    p[i__ + iptr * 60 - 61] = i__;
344 	    pbar = i__;
345 	}
346     }
347 /*   Right to left pass: */
348     i__ = pbar - 1;
349     sbar = s[i__];
350     while(i__ >= 1) {
351 	sbar += alpha;
352 	if (sbar < s[i__ - 1]) {
353 	    s[i__ - 1] = sbar;
354 	    p[i__ + iptr * 60 - 61] = pbar;
355 	} else {
356 	    pbar = p[i__ + iptr * 60 - 61];
357 	    i__ = pbar;
358 	    sbar = s[i__ - 1];
359 	}
360 	--i__;
361     }
362 /*   Update S using AMDF */
363 /*   Find maximum, minimum, and location of minimum */
364     s[0] += amdf[1] / 2;
365     minsc = s[0];
366     maxsc = minsc;
367     *midx = 1;
368     i__1 = *ltau;
369     for (i__ = 2; i__ <= i__1; ++i__) {
370 	s[i__ - 1] += amdf[i__] / 2;
371 	if (s[i__ - 1] > maxsc) {
372 	    maxsc = s[i__ - 1];
373 	}
374 	if (s[i__ - 1] < minsc) {
375 	    *midx = i__;
376 	    minsc = s[i__ - 1];
377 	}
378     }
379 /*   Subtract MINSC from S to prevent overflow */
380     i__1 = *ltau;
381     for (i__ = 1; i__ <= i__1; ++i__) {
382 	s[i__ - 1] -= minsc;
383     }
384     maxsc -= minsc;
385 /*   Use higher octave pitch if significant null there */
386     j = 0;
387     for (i__ = 20; i__ <= 40; i__ += 10) {
388 	if (*midx > i__) {
389 	    if (s[*midx - i__ - 1] < maxsc / 4) {
390 		j = i__;
391 	    }
392 	}
393     }
394     *midx -= j;
395 /*   TRACE: look back two frames to find minimum cost pitch estimate */
396     j = *ipoint;
397     *pitch = *midx;
398     for (i__ = 1; i__ <= 2; ++i__) {
399 	j = j % 2 + 1;
400 	*pitch = p[*pitch + j * 60 - 61];
401 	path[i__ - 1] = *pitch;
402     }
403 
404 /*       The following statement subtracts one from IPOINT, mod DEPTH.  I
405 */
406 /*       think the author chose to add DEPTH-1, instead of subtracting 1,
407 */
408 /*       because then it will work even if MOD doesn't work as desired on
409 */
410 /*       negative arguments. */
411 
412     *ipoint = (*ipoint + 1) % 2;
413     return 0;
414 } /* dyptrk_ */
415