synths.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. $Log$
  3. Revision 1.16 2004/06/26 03:50:14 markster
  4. Merge source cleanups (bug #1911)
  5. Revision 1.15 2003/09/27 02:45:37 markster
  6. Fix various compiler warnings (bug #322)
  7. Revision 1.2 2003/09/27 02:45:37 markster
  8. Fix various compiler warnings (bug #322)
  9. Revision 1.1.1.1 2003/02/12 13:59:15 matteo
  10. mer feb 12 14:56:57 CET 2003
  11. Revision 1.2 2000/01/05 08:20:39 markster
  12. Some OSS fixes and a few lpc changes to make it actually work
  13. * Revision 1.2 1996/08/20 20:42:59 jaf
  14. * Removed all static local variables that were SAVE'd in the Fortran
  15. * code, and put them in struct lpc10_decoder_state that is passed as an
  16. * argument.
  17. *
  18. * Removed init function, since all initialization is now done in
  19. * init_lpc10_decoder_state().
  20. *
  21. * Revision 1.1 1996/08/19 22:30:33 jaf
  22. * Initial revision
  23. *
  24. */
  25. /* -- translated by f2c (version 19951025).
  26. You must link the resulting object file with the libraries:
  27. -lf2c -lm (in that order)
  28. */
  29. #include "f2c.h"
  30. #ifdef P_R_O_T_O_T_Y_P_E_S
  31. extern int synths_(integer *voice, integer *pitch, real *rms, real *rc, real *speech, integer *k, struct lpc10_decoder_state *st);
  32. /* comlen contrl_ 12 */
  33. /*:ref: pitsyn_ 14 12 4 4 4 6 6 4 4 4 6 6 4 6 */
  34. /*:ref: irc2pc_ 14 5 6 6 4 6 6 */
  35. /*:ref: bsynz_ 14 7 6 4 4 6 6 6 6 */
  36. /*:ref: deemp_ 14 2 6 4 */
  37. /*:ref: initpitsyn_ 14 0 */
  38. /*:ref: initbsynz_ 14 0 */
  39. /*:ref: initdeemp_ 14 0 */
  40. #endif
  41. /* Common Block Declarations */
  42. extern struct {
  43. integer order, lframe;
  44. logical corrp;
  45. } contrl_;
  46. #define contrl_1 contrl_
  47. /* Table of constant values */
  48. static real c_b2 = .7f;
  49. /* ***************************************************************** */
  50. /* SYNTHS Version 54 */
  51. /* $Log$
  52. * Revision 1.16 2004/06/26 03:50:14 markster
  53. * Merge source cleanups (bug #1911)
  54. *
  55. * Revision 1.15 2003/09/27 02:45:37 markster
  56. * Fix various compiler warnings (bug #322)
  57. *
  58. * Revision 1.2 2003/09/27 02:45:37 markster
  59. * Fix various compiler warnings (bug #322)
  60. *
  61. * Revision 1.1.1.1 2003/02/12 13:59:15 matteo
  62. * mer feb 12 14:56:57 CET 2003
  63. *
  64. * Revision 1.2 2000/01/05 08:20:39 markster
  65. * Some OSS fixes and a few lpc changes to make it actually work
  66. *
  67. * Revision 1.2 1996/08/20 20:42:59 jaf
  68. * Removed all static local variables that were SAVE'd in the Fortran
  69. * code, and put them in struct lpc10_decoder_state that is passed as an
  70. * argument.
  71. *
  72. * Removed init function, since all initialization is now done in
  73. * init_lpc10_decoder_state().
  74. *
  75. * Revision 1.1 1996/08/19 22:30:33 jaf
  76. * Initial revision
  77. * */
  78. /* Revision 1.5 1996/03/26 19:31:58 jaf */
  79. /* Commented out trace statements. */
  80. /* Revision 1.4 1996/03/25 19:41:01 jaf */
  81. /* Changed so that MAXFRM samples are always returned in the output array */
  82. /* SPEECH. */
  83. /* This required delaying the returned samples by MAXFRM sample times, */
  84. /* and remembering any "left over" samples returned by PITSYN from one */
  85. /* call of SYNTHS to the next. */
  86. /* Changed size of SPEECH from 2*MAXFRM to MAXFRM. Removed local */
  87. /* variable SOUT. Added local state variables BUF and BUFLEN. */
  88. /* Revision 1.3 1996/03/25 19:20:10 jaf */
  89. /* Added comments about the range of possible return values for argument */
  90. /* K, and increased the size of the arrays filled in by PITSYN from 11 to */
  91. /* 16, as has been already done inside of PITSYN. */
  92. /* Revision 1.2 1996/03/22 00:18:18 jaf */
  93. /* Added comments explaining meanings of input and output parameters, and */
  94. /* indicating which array indices can be read or written. */
  95. /* Added entry INITSYNTHS, which does nothing except call the */
  96. /* corresponding initialization entries for subroutines PITSYN, BSYNZ, */
  97. /* and DEEMP. */
  98. /* Revision 1.1 1996/02/07 14:49:44 jaf */
  99. /* Initial revision */
  100. /* ***************************************************************** */
  101. /* The note below is from the distributed version of the LPC10 coder. */
  102. /* The version of the code below has been modified so that SYNTHS always */
  103. /* has a constant frame length output of MAXFRM. */
  104. /* Also, BSYNZ and DEEMP need not be modified to work on variable */
  105. /* positions within an array. It is only necessary to pass the first */
  106. /* index desired as the array argument. What actually gets passed is the */
  107. /* address of that array position, which the subroutine treats as the */
  108. /* first index of the array. */
  109. /* This technique is used in subroutine ANALYS when calling PREEMP, so it */
  110. /* appears that multiple people wrote different parts of this LPC10 code, */
  111. /* and that they didn't necessarily have equivalent knowledge of Fortran */
  112. /* (not surprising). */
  113. /* NOTE: There is excessive buffering here, BSYNZ and DEEMP should be */
  114. /* changed to operate on variable positions within SOUT. Also, */
  115. /* the output length parameter is bogus, and PITSYN should be */
  116. /* rewritten to allow a constant frame length output. */
  117. /* Input: */
  118. /* VOICE - Half frame voicing decisions */
  119. /* Indices 1 through 2 read. */
  120. /* Input/Output: */
  121. /* PITCH - Pitch */
  122. /* PITCH is restricted to range 20 to 156, inclusive, */
  123. /* before calling subroutine PITSYN, and then PITSYN */
  124. /* can modify it further under some conditions. */
  125. /* RMS - Energy */
  126. /* Only use is for debugging, and passed to PITSYN. */
  127. /* See comments there for how it can be modified. */
  128. /* RC - Reflection coefficients */
  129. /* Indices 1 through ORDER restricted to range -.99 to .99, */
  130. /* before calling subroutine PITSYN, and then PITSYN */
  131. /* can modify it further under some conditions. */
  132. /* Output: */
  133. /* SPEECH - Synthesized speech samples. */
  134. /* Indices 1 through the final value of K are written. */
  135. /* K - Number of samples placed into array SPEECH. */
  136. /* This is always MAXFRM. */
  137. /* Subroutine */ int synths_(integer *voice, integer *pitch, real *
  138. rms, real *rc, real *speech, integer *k, struct lpc10_decoder_state *st)
  139. {
  140. /* Initialized data */
  141. real *buf;
  142. integer *buflen;
  143. /* System generated locals */
  144. integer i__1;
  145. real r__1, r__2;
  146. /* Local variables */
  147. real rmsi[16];
  148. integer nout, ivuv[16], i__, j;
  149. extern /* Subroutine */ int deemp_(real *, integer *, struct lpc10_decoder_state *);
  150. real ratio;
  151. integer ipiti[16];
  152. real g2pass;
  153. real pc[10];
  154. extern /* Subroutine */ int pitsyn_(integer *, integer *, integer *, real
  155. *, real *, integer *, integer *, integer *, real *, real *,
  156. integer *, real *, struct lpc10_decoder_state *);
  157. real rci[160] /* was [10][16] */;
  158. /* $Log$
  159. * Revision 1.16 2004/06/26 03:50:14 markster
  160. * Merge source cleanups (bug #1911)
  161. *
  162. * Revision 1.15 2003/09/27 02:45:37 markster
  163. * Fix various compiler warnings (bug #322)
  164. *
  165. * Revision 1.2 2003/09/27 02:45:37 markster
  166. * Fix various compiler warnings (bug #322)
  167. *
  168. * Revision 1.1.1.1 2003/02/12 13:59:15 matteo
  169. * mer feb 12 14:56:57 CET 2003
  170. *
  171. * Revision 1.2 2000/01/05 08:20:39 markster
  172. * Some OSS fixes and a few lpc changes to make it actually work
  173. *
  174. * Revision 1.2 1996/08/20 20:42:59 jaf
  175. * Removed all static local variables that were SAVE'd in the Fortran
  176. * code, and put them in struct lpc10_decoder_state that is passed as an
  177. * argument.
  178. *
  179. * Removed init function, since all initialization is now done in
  180. * init_lpc10_decoder_state().
  181. *
  182. * Revision 1.1 1996/08/19 22:30:33 jaf
  183. * Initial revision
  184. * */
  185. /* Revision 1.3 1996/03/29 22:03:47 jaf */
  186. /* Removed definitions for any constants that were no longer used. */
  187. /* Revision 1.2 1996/03/26 19:34:33 jaf */
  188. /* Added comments indicating which constants are not needed in an */
  189. /* application that uses the LPC-10 coder. */
  190. /* Revision 1.1 1996/02/07 14:43:51 jaf */
  191. /* Initial revision */
  192. /* LPC Configuration parameters: */
  193. /* Frame size, Prediction order, Pitch period */
  194. /* Arguments */
  195. /* $Log$
  196. * Revision 1.16 2004/06/26 03:50:14 markster
  197. * Merge source cleanups (bug #1911)
  198. *
  199. * Revision 1.15 2003/09/27 02:45:37 markster
  200. * Fix various compiler warnings (bug #322)
  201. *
  202. * Revision 1.2 2003/09/27 02:45:37 markster
  203. * Fix various compiler warnings (bug #322)
  204. *
  205. * Revision 1.1.1.1 2003/02/12 13:59:15 matteo
  206. * mer feb 12 14:56:57 CET 2003
  207. *
  208. * Revision 1.2 2000/01/05 08:20:39 markster
  209. * Some OSS fixes and a few lpc changes to make it actually work
  210. *
  211. * Revision 1.2 1996/08/20 20:42:59 jaf
  212. * Removed all static local variables that were SAVE'd in the Fortran
  213. * code, and put them in struct lpc10_decoder_state that is passed as an
  214. * argument.
  215. *
  216. * Removed init function, since all initialization is now done in
  217. * init_lpc10_decoder_state().
  218. *
  219. * Revision 1.1 1996/08/19 22:30:33 jaf
  220. * Initial revision
  221. * */
  222. /* Revision 1.3 1996/03/29 22:05:55 jaf */
  223. /* Commented out the common block variables that are not needed by the */
  224. /* embedded version. */
  225. /* Revision 1.2 1996/03/26 19:34:50 jaf */
  226. /* Added comments indicating which constants are not needed in an */
  227. /* application that uses the LPC-10 coder. */
  228. /* Revision 1.1 1996/02/07 14:44:09 jaf */
  229. /* Initial revision */
  230. /* LPC Processing control variables: */
  231. /* *** Read-only: initialized in setup */
  232. /* Files for Speech, Parameter, and Bitstream Input & Output, */
  233. /* and message and debug outputs. */
  234. /* Here are the only files which use these variables: */
  235. /* lpcsim.f setup.f trans.f error.f vqsetup.f */
  236. /* Many files which use fdebug are not listed, since it is only used in */
  237. /* those other files conditionally, to print trace statements. */
  238. /* integer fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
  239. /* LPC order, Frame size, Quantization rate, Bits per frame, */
  240. /* Error correction */
  241. /* Subroutine SETUP is the only place where order is assigned a value, */
  242. /* and that value is 10. It could increase efficiency 1% or so to */
  243. /* declare order as a constant (i.e., a Fortran PARAMETER) instead of as
  244. */
  245. /* a variable in a COMMON block, since it is used in many places in the */
  246. /* core of the coding and decoding routines. Actually, I take that back.
  247. */
  248. /* At least when compiling with f2c, the upper bound of DO loops is */
  249. /* stored in a local variable before the DO loop begins, and then that is
  250. */
  251. /* compared against on each iteration. */
  252. /* Similarly for lframe, which is given a value of MAXFRM in SETUP. */
  253. /* Similarly for quant, which is given a value of 2400 in SETUP. quant */
  254. /* is used in only a few places, and never in the core coding and */
  255. /* decoding routines, so it could be eliminated entirely. */
  256. /* nbits is similar to quant, and is given a value of 54 in SETUP. */
  257. /* corrp is given a value of .TRUE. in SETUP, and is only used in the */
  258. /* subroutines ENCODE and DECODE. It doesn't affect the speed of the */
  259. /* coder significantly whether it is .TRUE. or .FALSE., or whether it is
  260. */
  261. /* a constant or a variable, since it is only examined once per frame. */
  262. /* Leaving it as a variable that is set to .TRUE. seems like a good */
  263. /* idea, since it does enable some error-correction capability for */
  264. /* unvoiced frames, with no change in the coding rate, and no noticeable
  265. */
  266. /* quality difference in the decoded speech. */
  267. /* integer quant, nbits */
  268. /* *** Read/write: variables for debugging, not needed for LPC algorithm
  269. */
  270. /* Current frame, Unstable frames, Output clip count, Max onset buffer,
  271. */
  272. /* Debug listing detail level, Line count on listing page */
  273. /* nframe is not needed for an embedded LPC10 at all. */
  274. /* nunsfm is initialized to 0 in SETUP, and incremented in subroutine */
  275. /* ERROR, which is only called from RCCHK. When LPC10 is embedded into */
  276. /* an application, I would recommend removing the call to ERROR in RCCHK,
  277. */
  278. /* and remove ERROR and nunsfm completely. */
  279. /* iclip is initialized to 0 in SETUP, and incremented in entry SWRITE in
  280. */
  281. /* sread.f. When LPC10 is embedded into an application, one might want */
  282. /* to cause it to be incremented in a routine that takes the output of */
  283. /* SYNTHS and sends it to an audio device. It could be optionally */
  284. /* displayed, for those that might want to know what it is. */
  285. /* maxosp is never initialized to 0 in SETUP, although it probably should
  286. */
  287. /* be, and it is updated in subroutine ANALYS. I doubt that its value */
  288. /* would be of much interest to an application in which LPC10 is */
  289. /* embedded. */
  290. /* listl and lincnt are not needed for an embedded LPC10 at all. */
  291. /* integer nframe, nunsfm, iclip, maxosp, listl, lincnt */
  292. /* common /contrl/ fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
  293. /* common /contrl/ quant, nbits */
  294. /* common /contrl/ nframe, nunsfm, iclip, maxosp, listl, lincnt */
  295. /* Parameters/constants */
  296. /* Local variables that need not be saved */
  297. /* Local state */
  298. /* BUF is a buffer of speech samples that would have been returned
  299. */
  300. /* by the older version of SYNTHS, but the newer version doesn't, */
  301. /* so that the newer version can always return MAXFRM samples on */
  302. /* every call. This has the effect of delaying the return of */
  303. /* samples for one additional frame time. */
  304. /* Indices 1 through BUFLEN contain samples that are left over from
  305. */
  306. /* the last call to SYNTHS. Given the way that PITSYN works, */
  307. /* BUFLEN should always be in the range MAXFRM-MAXPIT+1 through */
  308. /* MAXFRM, inclusive, after a call to SYNTHS is complete. */
  309. /* On the first call to SYNTHS (or the first call after */
  310. /* reinitializing with the entry INITSYNTHS), BUFLEN is MAXFRM, and
  311. */
  312. /* a frame of silence is always returned. */
  313. /* Parameter adjustments */
  314. if (voice) {
  315. --voice;
  316. }
  317. if (rc) {
  318. --rc;
  319. }
  320. if (speech) {
  321. --speech;
  322. }
  323. /* Function Body */
  324. buf = &(st->buf[0]);
  325. buflen = &(st->buflen);
  326. /* Computing MAX */
  327. i__1 = min(*pitch,156);
  328. *pitch = max(i__1,20);
  329. i__1 = contrl_1.order;
  330. for (i__ = 1; i__ <= i__1; ++i__) {
  331. /* Computing MAX */
  332. /* Computing MIN */
  333. r__2 = rc[i__];
  334. r__1 = min(r__2,.99f);
  335. rc[i__] = max(r__1,-.99f);
  336. }
  337. pitsyn_(&contrl_1.order, &voice[1], pitch, rms, &rc[1], &contrl_1.lframe,
  338. ivuv, ipiti, rmsi, rci, &nout, &ratio, st);
  339. if (nout > 0) {
  340. i__1 = nout;
  341. for (j = 1; j <= i__1; ++j) {
  342. /* Add synthesized speech for pitch period J to the en
  343. d of */
  344. /* BUF. */
  345. irc2pc_(&rci[j * 10 - 10], pc, &contrl_1.order, &c_b2, &g2pass);
  346. bsynz_(pc, &ipiti[j - 1], &ivuv[j - 1], &buf[*buflen], &rmsi[j - 1]
  347. , &ratio, &g2pass, st);
  348. deemp_(&buf[*buflen], &ipiti[j - 1], st);
  349. *buflen += ipiti[j - 1];
  350. }
  351. /* Copy first MAXFRM samples from BUF to output array SPEECH
  352. */
  353. /* (scaling them), and then remove them from the beginning of
  354. */
  355. /* BUF. */
  356. for (i__ = 1; i__ <= 180; ++i__) {
  357. speech[i__] = buf[i__ - 1] / 4096.f;
  358. }
  359. *k = 180;
  360. *buflen += -180;
  361. i__1 = *buflen;
  362. for (i__ = 1; i__ <= i__1; ++i__) {
  363. buf[i__ - 1] = buf[i__ + 179];
  364. }
  365. }
  366. return 0;
  367. } /* synths_ */