dyptrk.c 13 KB

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