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