eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

klatt.c 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  1. /*
  2. * Copyright (C) 2008 by Jonathan Duddington
  3. * email: [email protected]
  4. * Copyright (C) 2013-2015 Reece H. Dunn
  5. *
  6. * Based on a re-implementation by:
  7. * (c) 1993,94 Jon Iles and Nick Ing-Simmons
  8. * of the Klatt cascade-parallel formant synthesizer
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, see: <http://www.gnu.org/licenses/>.
  22. */
  23. // See URL: ftp://svr-ftp.eng.cam.ac.uk/pub/comp.speech/synthesis/klatt.3.04.tar.gz
  24. #include "config.h"
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <math.h>
  28. #include <string.h>
  29. #if HAVE_STDINT_H
  30. #include <stdint.h>
  31. #endif
  32. #include "speak_lib.h"
  33. #include "speech.h"
  34. #include "klatt.h"
  35. #include "phoneme.h"
  36. #include "synthesize.h"
  37. #include "voice.h"
  38. extern unsigned char *out_ptr;
  39. extern unsigned char *out_start;
  40. extern unsigned char *out_end;
  41. extern WGEN_DATA wdata;
  42. static int nsamples;
  43. static int sample_count;
  44. #ifdef _MSC_VER
  45. #define getrandom(min, max) ((rand()%(int)(((max)+1)-(min)))+(min))
  46. #else
  47. #define getrandom(min, max) ((rand()%(long)(((max)+1)-(min)))+(min))
  48. #endif
  49. /* function prototypes for functions private to this file */
  50. static void flutter(klatt_frame_ptr);
  51. static double sampled_source(int);
  52. static double impulsive_source(void);
  53. static double natural_source(void);
  54. static void pitch_synch_par_reset(klatt_frame_ptr);
  55. static double gen_noise(double);
  56. static double DBtoLIN(long);
  57. static void frame_init(klatt_frame_ptr);
  58. static void setabc(long, long, resonator_ptr);
  59. static void setzeroabc(long, long, resonator_ptr);
  60. static klatt_frame_t kt_frame;
  61. static klatt_global_t kt_globals;
  62. #define NUMBER_OF_SAMPLES 100
  63. static int scale_wav_tab[] = { 45, 38, 45, 45, 55 }; // scale output from different voicing sources
  64. // For testing, this can be overwritten in KlattInit()
  65. static short natural_samples2[256] = {
  66. 2583, 2516, 2450, 2384, 2319, 2254, 2191, 2127,
  67. 2067, 2005, 1946, 1890, 1832, 1779, 1726, 1675,
  68. 1626, 1579, 1533, 1491, 1449, 1409, 1372, 1336,
  69. 1302, 1271, 1239, 1211, 1184, 1158, 1134, 1111,
  70. 1089, 1069, 1049, 1031, 1013, 996, 980, 965,
  71. 950, 936, 921, 909, 895, 881, 869, 855,
  72. 843, 830, 818, 804, 792, 779, 766, 754,
  73. 740, 728, 715, 702, 689, 676, 663, 651,
  74. 637, 626, 612, 601, 588, 576, 564, 552,
  75. 540, 530, 517, 507, 496, 485, 475, 464,
  76. 454, 443, 434, 424, 414, 404, 394, 385,
  77. 375, 366, 355, 347, 336, 328, 317, 308,
  78. 299, 288, 280, 269, 260, 250, 240, 231,
  79. 220, 212, 200, 192, 181, 172, 161, 152,
  80. 142, 133, 123, 113, 105, 94, 86, 76,
  81. 67, 57, 49, 39, 30, 22, 11, 4,
  82. -5, -14, -23, -32, -41, -50, -60, -69,
  83. -78, -87, -96, -107, -115, -126, -134, -144,
  84. -154, -164, -174, -183, -193, -203, -213, -222,
  85. -233, -242, -252, -262, -271, -281, -291, -301,
  86. -310, -320, -330, -339, -349, -357, -368, -377,
  87. -387, -397, -406, -417, -426, -436, -446, -456,
  88. -467, -477, -487, -499, -509, -521, -532, -543,
  89. -555, -567, -579, -591, -603, -616, -628, -641,
  90. -653, -666, -679, -692, -705, -717, -732, -743,
  91. -758, -769, -783, -795, -808, -820, -834, -845,
  92. -860, -872, -885, -898, -911, -926, -939, -955,
  93. -968, -986, -999, -1018, -1034, -1054, -1072, -1094,
  94. -1115, -1138, -1162, -1188, -1215, -1244, -1274, -1307,
  95. -1340, -1377, -1415, -1453, -1496, -1538, -1584, -1631,
  96. -1680, -1732, -1783, -1839, -1894, -1952, -2010, -2072,
  97. -2133, -2196, -2260, -2325, -2390, -2456, -2522, -2589,
  98. };
  99. static short natural_samples[100] = {
  100. -310, -400, 530, 356, 224, 89, 23, -10, -58, -16, 461, 599, 536, 701, 770,
  101. 605, 497, 461, 560, 404, 110, 224, 131, 104, -97, 155, 278, -154, -1165,
  102. -598, 737, 125, -592, 41, 11, -247, -10, 65, 92, 80, -304, 71, 167, -1, 122,
  103. 233, 161, -43, 278, 479, 485, 407, 266, 650, 134, 80, 236, 68, 260, 269, 179,
  104. 53, 140, 275, 293, 296, 104, 257, 152, 311, 182, 263, 245, 125, 314, 140, 44,
  105. 203, 230, -235, -286, 23, 107, 92, -91, 38, 464, 443, 176, 98, -784, -2449,
  106. -1891, -1045, -1600, -1462, -1384, -1261, -949, -730
  107. };
  108. /*
  109. function RESONATOR
  110. This is a generic resonator function. Internal memory for the resonator
  111. is stored in the globals structure.
  112. */
  113. static double resonator(resonator_ptr r, double input)
  114. {
  115. double x;
  116. x = (double)((double)r->a * (double)input + (double)r->b * (double)r->p1 + (double)r->c * (double)r->p2);
  117. r->p2 = (double)r->p1;
  118. r->p1 = (double)x;
  119. return (double)x;
  120. }
  121. static double resonator2(resonator_ptr r, double input)
  122. {
  123. double x;
  124. x = (double)((double)r->a * (double)input + (double)r->b * (double)r->p1 + (double)r->c * (double)r->p2);
  125. r->p2 = (double)r->p1;
  126. r->p1 = (double)x;
  127. r->a += r->a_inc;
  128. r->b += r->b_inc;
  129. r->c += r->c_inc;
  130. return (double)x;
  131. }
  132. static double antiresonator2(resonator_ptr r, double input)
  133. {
  134. register double x = (double)r->a * (double)input + (double)r->b * (double)r->p1 + (double)r->c * (double)r->p2;
  135. r->p2 = (double)r->p1;
  136. r->p1 = (double)input;
  137. r->a += r->a_inc;
  138. r->b += r->b_inc;
  139. r->c += r->c_inc;
  140. return (double)x;
  141. }
  142. /*
  143. function FLUTTER
  144. This function adds F0 flutter, as specified in:
  145. "Analysis, synthesis and perception of voice quality variations among
  146. female and male talkers" D.H. Klatt and L.C. Klatt JASA 87(2) February 1990.
  147. Flutter is added by applying a quasi-random element constructed from three
  148. slowly varying sine waves.
  149. */
  150. static void flutter(klatt_frame_ptr frame)
  151. {
  152. static int time_count;
  153. double delta_f0;
  154. double fla, flb, flc, fld, fle;
  155. fla = (double)kt_globals.f0_flutter / 50;
  156. flb = (double)kt_globals.original_f0 / 100;
  157. flc = sin(PI*12.7*time_count); // because we are calling flutter() more frequently, every 2.9mS
  158. fld = sin(PI*7.1*time_count);
  159. fle = sin(PI*4.7*time_count);
  160. delta_f0 = fla * flb * (flc + fld + fle) * 10;
  161. frame->F0hz10 = frame->F0hz10 + (long)delta_f0;
  162. time_count++;
  163. }
  164. /*
  165. function SAMPLED_SOURCE
  166. Allows the use of a glottal excitation waveform sampled from a real
  167. voice.
  168. */
  169. static double sampled_source(int source_num)
  170. {
  171. int itemp;
  172. double ftemp;
  173. double result;
  174. double diff_value;
  175. int current_value;
  176. int next_value;
  177. double temp_diff;
  178. short *samples;
  179. if (source_num == 0) {
  180. samples = natural_samples;
  181. kt_globals.num_samples = 100;
  182. } else {
  183. samples = natural_samples2;
  184. kt_globals.num_samples = 256;
  185. }
  186. if (kt_globals.T0 != 0) {
  187. ftemp = (double)kt_globals.nper;
  188. ftemp = ftemp / kt_globals.T0;
  189. ftemp = ftemp * kt_globals.num_samples;
  190. itemp = (int)ftemp;
  191. temp_diff = ftemp - (double)itemp;
  192. current_value = samples[itemp];
  193. next_value = samples[itemp+1];
  194. diff_value = (double)next_value - (double)current_value;
  195. diff_value = diff_value * temp_diff;
  196. result = samples[itemp] + diff_value;
  197. result = result * kt_globals.sample_factor;
  198. } else
  199. result = 0;
  200. return result;
  201. }
  202. /*
  203. function PARWAVE
  204. Converts synthesis parameters to a waveform.
  205. */
  206. static int parwave(klatt_frame_ptr frame)
  207. {
  208. double temp;
  209. int value;
  210. double outbypas;
  211. double out;
  212. long n4;
  213. double frics;
  214. double glotout;
  215. double aspiration;
  216. double casc_next_in;
  217. double par_glotout;
  218. static double noise;
  219. static double voice;
  220. static double vlast;
  221. static double glotlast;
  222. static double sourc;
  223. int ix;
  224. flutter(frame); /* add f0 flutter */
  225. /* MAIN LOOP, for each output sample of current frame: */
  226. for (kt_globals.ns = 0; kt_globals.ns < kt_globals.nspfr; kt_globals.ns++) {
  227. /* Get low-passed random number for aspiration and frication noise */
  228. noise = gen_noise(noise);
  229. /*
  230. Amplitude modulate noise (reduce noise amplitude during
  231. second half of glottal period) if voicing simultaneously present.
  232. */
  233. if (kt_globals.nper > kt_globals.nmod)
  234. noise *= (double)0.5;
  235. /* Compute frication noise */
  236. frics = kt_globals.amp_frica * noise;
  237. /*
  238. Compute voicing waveform. Run glottal source simulation at 4
  239. times normal sample rate to minimize quantization noise in
  240. period of female voice.
  241. */
  242. for (n4 = 0; n4 < 4; n4++) {
  243. switch (kt_globals.glsource)
  244. {
  245. case IMPULSIVE:
  246. voice = impulsive_source();
  247. break;
  248. case NATURAL:
  249. voice = natural_source();
  250. break;
  251. case SAMPLED:
  252. voice = sampled_source(0);
  253. break;
  254. case SAMPLED2:
  255. voice = sampled_source(1);
  256. break;
  257. }
  258. /* Reset period when counter 'nper' reaches T0 */
  259. if (kt_globals.nper >= kt_globals.T0) {
  260. kt_globals.nper = 0;
  261. pitch_synch_par_reset(frame);
  262. }
  263. /*
  264. Low-pass filter voicing waveform before downsampling from 4*samrate
  265. to samrate samples/sec. Resonator f=.09*samrate, bw=.06*samrate
  266. */
  267. voice = resonator(&(kt_globals.rsn[RLP]), voice);
  268. /* Increment counter that keeps track of 4*samrate samples per sec */
  269. kt_globals.nper++;
  270. }
  271. /*
  272. Tilt spectrum of voicing source down by soft low-pass filtering, amount
  273. of tilt determined by TLTdb
  274. */
  275. voice = (voice * kt_globals.onemd) + (vlast * kt_globals.decay);
  276. vlast = voice;
  277. /*
  278. Add breathiness during glottal open phase. Amount of breathiness
  279. determined by parameter Aturb Use nrand rather than noise because
  280. noise is low-passed.
  281. */
  282. if (kt_globals.nper < kt_globals.nopen)
  283. voice += kt_globals.amp_breth * kt_globals.nrand;
  284. /* Set amplitude of voicing */
  285. glotout = kt_globals.amp_voice * voice;
  286. par_glotout = kt_globals.par_amp_voice * voice;
  287. /* Compute aspiration amplitude and add to voicing source */
  288. aspiration = kt_globals.amp_aspir * noise;
  289. glotout += aspiration;
  290. par_glotout += aspiration;
  291. /*
  292. Cascade vocal tract, excited by laryngeal sources.
  293. Nasal antiresonator, then formants FNP, F5, F4, F3, F2, F1
  294. */
  295. out = 0;
  296. if (kt_globals.synthesis_model != ALL_PARALLEL) {
  297. casc_next_in = antiresonator2(&(kt_globals.rsn[Rnz]), glotout);
  298. casc_next_in = resonator(&(kt_globals.rsn[Rnpc]), casc_next_in);
  299. casc_next_in = resonator(&(kt_globals.rsn[R8c]), casc_next_in);
  300. casc_next_in = resonator(&(kt_globals.rsn[R7c]), casc_next_in);
  301. casc_next_in = resonator(&(kt_globals.rsn[R6c]), casc_next_in);
  302. casc_next_in = resonator2(&(kt_globals.rsn[R5c]), casc_next_in);
  303. casc_next_in = resonator2(&(kt_globals.rsn[R4c]), casc_next_in);
  304. casc_next_in = resonator2(&(kt_globals.rsn[R3c]), casc_next_in);
  305. casc_next_in = resonator2(&(kt_globals.rsn[R2c]), casc_next_in);
  306. out = resonator2(&(kt_globals.rsn[R1c]), casc_next_in);
  307. }
  308. /* Excite parallel F1 and FNP by voicing waveform */
  309. sourc = par_glotout; /* Source is voicing plus aspiration */
  310. /*
  311. Standard parallel vocal tract Formants F6,F5,F4,F3,F2,
  312. outputs added with alternating sign. Sound source for other
  313. parallel resonators is frication plus first difference of
  314. voicing waveform.
  315. */
  316. out += resonator(&(kt_globals.rsn[R1p]), sourc);
  317. out += resonator(&(kt_globals.rsn[Rnpp]), sourc);
  318. sourc = frics + par_glotout - glotlast;
  319. glotlast = par_glotout;
  320. for (ix = R2p; ix <= R6p; ix++)
  321. out = resonator(&(kt_globals.rsn[ix]), sourc) - out;
  322. outbypas = kt_globals.amp_bypas * sourc;
  323. out = outbypas - out;
  324. out = resonator(&(kt_globals.rsn[Rout]), out);
  325. temp = (int)(out * wdata.amplitude * kt_globals.amp_gain0); /* Convert back to integer */
  326. // mix with a recorded WAV if required for this phoneme
  327. {
  328. int z2;
  329. signed char c;
  330. int sample;
  331. z2 = 0;
  332. if (wdata.mix_wavefile_ix < wdata.n_mix_wavefile) {
  333. if (wdata.mix_wave_scale == 0) {
  334. // a 16 bit sample
  335. c = wdata.mix_wavefile[wdata.mix_wavefile_ix+1];
  336. sample = wdata.mix_wavefile[wdata.mix_wavefile_ix] + (c * 256);
  337. wdata.mix_wavefile_ix += 2;
  338. } else {
  339. // a 8 bit sample, scaled
  340. sample = (signed char)wdata.mix_wavefile[wdata.mix_wavefile_ix++] * wdata.mix_wave_scale;
  341. }
  342. z2 = sample * wdata.amplitude_v / 1024;
  343. z2 = (z2 * wdata.mix_wave_amp)/40;
  344. temp += z2;
  345. }
  346. }
  347. // if fadeout is set, fade to zero over 64 samples, to avoid clicks at end of synthesis
  348. if (kt_globals.fadeout > 0) {
  349. kt_globals.fadeout--;
  350. temp = (temp * kt_globals.fadeout) / 64;
  351. }
  352. value = (int)temp + ((echo_buf[echo_tail++]*echo_amp) >> 8);
  353. if (echo_tail >= N_ECHO_BUF)
  354. echo_tail = 0;
  355. if (value < -32768)
  356. value = -32768;
  357. if (value > 32767)
  358. value = 32767;
  359. *out_ptr++ = value;
  360. *out_ptr++ = value >> 8;
  361. echo_buf[echo_head++] = value;
  362. if (echo_head >= N_ECHO_BUF)
  363. echo_head = 0;
  364. sample_count++;
  365. if (out_ptr >= out_end)
  366. return 1;
  367. }
  368. return 0;
  369. }
  370. void KlattReset(int control)
  371. {
  372. int r_ix;
  373. if (control == 2) {
  374. // Full reset
  375. kt_globals.FLPhz = (950 * kt_globals.samrate) / 10000;
  376. kt_globals.BLPhz = (630 * kt_globals.samrate) / 10000;
  377. kt_globals.minus_pi_t = -PI / kt_globals.samrate;
  378. kt_globals.two_pi_t = -2.0 * kt_globals.minus_pi_t;
  379. setabc(kt_globals.FLPhz, kt_globals.BLPhz, &(kt_globals.rsn[RLP]));
  380. }
  381. if (control > 0) {
  382. kt_globals.nper = 0;
  383. kt_globals.T0 = 0;
  384. kt_globals.nopen = 0;
  385. kt_globals.nmod = 0;
  386. for (r_ix = RGL; r_ix < N_RSN; r_ix++) {
  387. kt_globals.rsn[r_ix].p1 = 0;
  388. kt_globals.rsn[r_ix].p2 = 0;
  389. }
  390. }
  391. for (r_ix = 0; r_ix <= R6p; r_ix++) {
  392. kt_globals.rsn[r_ix].p1 = 0;
  393. kt_globals.rsn[r_ix].p2 = 0;
  394. }
  395. }
  396. /*
  397. function FRAME_INIT
  398. Use parameters from the input frame to set up resonator coefficients.
  399. */
  400. static void frame_init(klatt_frame_ptr frame)
  401. {
  402. double amp_par[7];
  403. static double amp_par_factor[7] = { 0.6, 0.4, 0.15, 0.06, 0.04, 0.022, 0.03 };
  404. long Gain0_tmp;
  405. int ix;
  406. kt_globals.original_f0 = frame->F0hz10 / 10;
  407. frame->AVdb_tmp = frame->AVdb - 7;
  408. if (frame->AVdb_tmp < 0)
  409. frame->AVdb_tmp = 0;
  410. kt_globals.amp_aspir = DBtoLIN(frame->ASP) * 0.05;
  411. kt_globals.amp_frica = DBtoLIN(frame->AF) * 0.25;
  412. kt_globals.par_amp_voice = DBtoLIN(frame->AVpdb);
  413. kt_globals.amp_bypas = DBtoLIN(frame->AB) * 0.05;
  414. for (ix = 0; ix <= 6; ix++) {
  415. // parallel amplitudes F1 to F6, and parallel nasal pole
  416. amp_par[ix] = DBtoLIN(frame->Ap[ix]) * amp_par_factor[ix];
  417. }
  418. Gain0_tmp = frame->Gain0 - 3;
  419. if (Gain0_tmp <= 0)
  420. Gain0_tmp = 57;
  421. kt_globals.amp_gain0 = DBtoLIN(Gain0_tmp) / kt_globals.scale_wav;
  422. /* Set coefficients of variable cascade resonators */
  423. for (ix = 1; ix <= 9; ix++) {
  424. // formants 1 to 8, plus nasal pole
  425. setabc(frame->Fhz[ix], frame->Bhz[ix], &(kt_globals.rsn[ix]));
  426. if (ix <= 5) {
  427. setabc(frame->Fhz_next[ix], frame->Bhz_next[ix], &(kt_globals.rsn_next[ix]));
  428. kt_globals.rsn[ix].a_inc = (kt_globals.rsn_next[ix].a - kt_globals.rsn[ix].a) / 64.0;
  429. kt_globals.rsn[ix].b_inc = (kt_globals.rsn_next[ix].b - kt_globals.rsn[ix].b) / 64.0;
  430. kt_globals.rsn[ix].c_inc = (kt_globals.rsn_next[ix].c - kt_globals.rsn[ix].c) / 64.0;
  431. }
  432. }
  433. // nasal zero anti-resonator
  434. setzeroabc(frame->Fhz[F_NZ], frame->Bhz[F_NZ], &(kt_globals.rsn[Rnz]));
  435. setzeroabc(frame->Fhz_next[F_NZ], frame->Bhz_next[F_NZ], &(kt_globals.rsn_next[Rnz]));
  436. kt_globals.rsn[F_NZ].a_inc = (kt_globals.rsn_next[F_NZ].a - kt_globals.rsn[F_NZ].a) / 64.0;
  437. kt_globals.rsn[F_NZ].b_inc = (kt_globals.rsn_next[F_NZ].b - kt_globals.rsn[F_NZ].b) / 64.0;
  438. kt_globals.rsn[F_NZ].c_inc = (kt_globals.rsn_next[F_NZ].c - kt_globals.rsn[F_NZ].c) / 64.0;
  439. /* Set coefficients of parallel resonators, and amplitude of outputs */
  440. for (ix = 0; ix <= 6; ix++) {
  441. setabc(frame->Fhz[ix], frame->Bphz[ix], &(kt_globals.rsn[Rparallel+ix]));
  442. kt_globals.rsn[Rparallel+ix].a *= amp_par[ix];
  443. }
  444. /* output low-pass filter */
  445. setabc((long)0.0, (long)(kt_globals.samrate/2), &(kt_globals.rsn[Rout]));
  446. }
  447. /*
  448. function IMPULSIVE_SOURCE
  449. Generate a low pass filtered train of impulses as an approximation of
  450. a natural excitation waveform. Low-pass filter the differentiated impulse
  451. with a critically-damped second-order filter, time constant proportional
  452. to Kopen.
  453. */
  454. static double impulsive_source()
  455. {
  456. static double doublet[] = { 0.0, 13000000.0, -13000000.0 };
  457. static double vwave;
  458. if (kt_globals.nper < 3)
  459. vwave = doublet[kt_globals.nper];
  460. else
  461. vwave = 0.0;
  462. return resonator(&(kt_globals.rsn[RGL]), vwave);
  463. }
  464. /*
  465. function NATURAL_SOURCE
  466. Vwave is the differentiated glottal flow waveform, there is a weak
  467. spectral zero around 800 Hz, magic constants a,b reset pitch synchronously.
  468. */
  469. static double natural_source()
  470. {
  471. double lgtemp;
  472. static double vwave;
  473. if (kt_globals.nper < kt_globals.nopen) {
  474. kt_globals.pulse_shape_a -= kt_globals.pulse_shape_b;
  475. vwave += kt_globals.pulse_shape_a;
  476. lgtemp = vwave * 0.028;
  477. return lgtemp;
  478. }
  479. vwave = 0.0;
  480. return 0.0;
  481. }
  482. /*
  483. function PITCH_SYNC_PAR_RESET
  484. Reset selected parameters pitch-synchronously.
  485. Constant B0 controls shape of glottal pulse as a function
  486. of desired duration of open phase N0
  487. (Note that N0 is specified in terms of 40,000 samples/sec of speech)
  488. Assume voicing waveform V(t) has form: k1 t**2 - k2 t**3
  489. If the radiation characterivative, a temporal derivative
  490. is folded in, and we go from continuous time to discrete
  491. integers n: dV/dt = vwave[n]
  492. = sum over i=1,2,...,n of { a - (i * b) }
  493. = a n - b/2 n**2
  494. where the constants a and b control the detailed shape
  495. and amplitude of the voicing waveform over the open
  496. potion of the voicing cycle "nopen".
  497. Let integral of dV/dt have no net dc flow --> a = (b * nopen) / 3
  498. Let maximum of dUg(n)/dn be constant --> b = gain / (nopen * nopen)
  499. meaning as nopen gets bigger, V has bigger peak proportional to n
  500. Thus, to generate the table below for 40 <= nopen <= 263:
  501. B0[nopen - 40] = 1920000 / (nopen * nopen)
  502. */
  503. static void pitch_synch_par_reset(klatt_frame_ptr frame)
  504. {
  505. long temp;
  506. double temp1;
  507. static long skew;
  508. static short B0[224] = {
  509. 1200, 1142, 1088, 1038, 991, 948, 907, 869, 833, 799, 768, 738, 710, 683, 658,
  510. 634, 612, 590, 570, 551, 533, 515, 499, 483, 468, 454, 440, 427, 415, 403,
  511. 391, 380, 370, 360, 350, 341, 332, 323, 315, 307, 300, 292, 285, 278, 272,
  512. 265, 259, 253, 247, 242, 237, 231, 226, 221, 217, 212, 208, 204, 199, 195,
  513. 192, 188, 184, 180, 177, 174, 170, 167, 164, 161, 158, 155, 153, 150, 147,
  514. 145, 142, 140, 137, 135, 133, 131, 128, 126, 124, 122, 120, 119, 117, 115,
  515. 113, 111, 110, 108, 106, 105, 103, 102, 100, 99, 97, 96, 95, 93, 92, 91, 90,
  516. 88, 87, 86, 85, 84, 83, 82, 80, 79, 78, 77, 76, 75, 75, 74, 73, 72, 71,
  517. 70, 69, 68, 68, 67, 66, 65, 64, 64, 63, 62, 61, 61, 60, 59, 59, 58, 57,
  518. 57, 56, 56, 55, 55, 54, 54, 53, 53, 52, 52, 51, 51, 50, 50, 49, 49, 48, 48,
  519. 47, 47, 46, 46, 45, 45, 44, 44, 43, 43, 42, 42, 41, 41, 41, 41, 40, 40,
  520. 39, 39, 38, 38, 38, 38, 37, 37, 36, 36, 36, 36, 35, 35, 35, 35, 34, 34, 33,
  521. 33, 33, 33, 32, 32, 32, 32, 31, 31, 31, 31, 30, 30, 30, 30, 29, 29, 29, 29,
  522. 28, 28, 28, 28, 27, 27
  523. };
  524. if (frame->F0hz10 > 0) {
  525. /* T0 is 4* the number of samples in one pitch period */
  526. kt_globals.T0 = (40 * kt_globals.samrate) / frame->F0hz10;
  527. kt_globals.amp_voice = DBtoLIN(frame->AVdb_tmp);
  528. /* Duration of period before amplitude modulation */
  529. kt_globals.nmod = kt_globals.T0;
  530. if (frame->AVdb_tmp > 0)
  531. kt_globals.nmod >>= 1;
  532. /* Breathiness of voicing waveform */
  533. kt_globals.amp_breth = DBtoLIN(frame->Aturb) * 0.1;
  534. /* Set open phase of glottal period where 40 <= open phase <= 263 */
  535. kt_globals.nopen = 4 * frame->Kopen;
  536. if ((kt_globals.glsource == IMPULSIVE) && (kt_globals.nopen > 263))
  537. kt_globals.nopen = 263;
  538. if (kt_globals.nopen >= (kt_globals.T0-1))
  539. kt_globals.nopen = kt_globals.T0 - 2;
  540. if (kt_globals.nopen < 40) {
  541. /* F0 max = 1000 Hz */
  542. kt_globals.nopen = 40;
  543. }
  544. /* Reset a & b, which determine shape of "natural" glottal waveform */
  545. kt_globals.pulse_shape_b = B0[kt_globals.nopen-40];
  546. kt_globals.pulse_shape_a = (kt_globals.pulse_shape_b * kt_globals.nopen) * 0.333;
  547. /* Reset width of "impulsive" glottal pulse */
  548. temp = kt_globals.samrate / kt_globals.nopen;
  549. setabc((long)0, temp, &(kt_globals.rsn[RGL]));
  550. /* Make gain at F1 about constant */
  551. temp1 = kt_globals.nopen *.00833;
  552. kt_globals.rsn[RGL].a *= temp1 * temp1;
  553. /*
  554. Truncate skewness so as not to exceed duration of closed phase
  555. of glottal period.
  556. */
  557. temp = kt_globals.T0 - kt_globals.nopen;
  558. if (frame->Kskew > temp)
  559. frame->Kskew = temp;
  560. if (skew >= 0)
  561. skew = frame->Kskew;
  562. else
  563. skew = -frame->Kskew;
  564. /* Add skewness to closed portion of voicing period */
  565. kt_globals.T0 = kt_globals.T0 + skew;
  566. skew = -skew;
  567. } else {
  568. kt_globals.T0 = 4; /* Default for f0 undefined */
  569. kt_globals.amp_voice = 0.0;
  570. kt_globals.nmod = kt_globals.T0;
  571. kt_globals.amp_breth = 0.0;
  572. kt_globals.pulse_shape_a = 0.0;
  573. kt_globals.pulse_shape_b = 0.0;
  574. }
  575. /* Reset these pars pitch synchronously or at update rate if f0=0 */
  576. if ((kt_globals.T0 != 4) || (kt_globals.ns == 0)) {
  577. /* Set one-pole low-pass filter that tilts glottal source */
  578. kt_globals.decay = (0.033 * frame->TLTdb);
  579. if (kt_globals.decay > 0.0)
  580. kt_globals.onemd = 1.0 - kt_globals.decay;
  581. else
  582. kt_globals.onemd = 1.0;
  583. }
  584. }
  585. /*
  586. function SETABC
  587. Convert formant freqencies and bandwidth into resonator difference
  588. equation constants.
  589. */
  590. static void setabc(long int f, long int bw, resonator_ptr rp)
  591. {
  592. double r;
  593. double arg;
  594. /* Let r = exp(-pi bw t) */
  595. arg = kt_globals.minus_pi_t * bw;
  596. r = exp(arg);
  597. /* Let c = -r**2 */
  598. rp->c = -(r * r);
  599. /* Let b = r * 2*cos(2 pi f t) */
  600. arg = kt_globals.two_pi_t * f;
  601. rp->b = r * cos(arg) * 2.0;
  602. /* Let a = 1.0 - b - c */
  603. rp->a = 1.0 - rp->b - rp->c;
  604. }
  605. /*
  606. function SETZEROABC
  607. Convert formant freqencies and bandwidth into anti-resonator difference
  608. equation constants.
  609. */
  610. static void setzeroabc(long int f, long int bw, resonator_ptr rp)
  611. {
  612. double r;
  613. double arg;
  614. f = -f;
  615. /* First compute ordinary resonator coefficients */
  616. /* Let r = exp(-pi bw t) */
  617. arg = kt_globals.minus_pi_t * bw;
  618. r = exp(arg);
  619. /* Let c = -r**2 */
  620. rp->c = -(r * r);
  621. /* Let b = r * 2*cos(2 pi f t) */
  622. arg = kt_globals.two_pi_t * f;
  623. rp->b = r * cos(arg) * 2.;
  624. /* Let a = 1.0 - b - c */
  625. rp->a = 1.0 - rp->b - rp->c;
  626. /* Now convert to antiresonator coefficients (a'=1/a, b'=b/a, c'=c/a) */
  627. /* If f == 0 then rp->a gets set to 0 which makes a'=1/a set a', b' and c' to
  628. * INF, causing an audible sound spike when triggered (e.g. apiration with the
  629. * nasal register set to f=0, bw=0).
  630. */
  631. if (rp->a != 0) {
  632. /* Now convert to antiresonator coefficients (a'=1/a, b'=b/a, c'=c/a) */
  633. rp->a = 1.0 / rp->a;
  634. rp->c *= -rp->a;
  635. rp->b *= -rp->a;
  636. }
  637. }
  638. /*
  639. function GEN_NOISE
  640. Random number generator (return a number between -8191 and +8191)
  641. Noise spectrum is tilted down by soft low-pass filter having a pole near
  642. the origin in the z-plane, i.e. output = input + (0.75 * lastoutput)
  643. */
  644. static double gen_noise(double noise)
  645. {
  646. long temp;
  647. static double nlast;
  648. temp = (long)getrandom(-8191, 8191);
  649. kt_globals.nrand = (long)temp;
  650. noise = kt_globals.nrand + (0.75 * nlast);
  651. nlast = noise;
  652. return noise;
  653. }
  654. /*
  655. function DBTOLIN
  656. Convert from decibels to a linear scale factor
  657. Conversion table, db to linear, 87 dB --> 32767
  658. 86 dB --> 29491 (1 dB down = 0.5**1/6)
  659. ...
  660. 81 dB --> 16384 (6 dB down = 0.5)
  661. ...
  662. 0 dB --> 0
  663. The just noticeable difference for a change in intensity of a vowel
  664. is approximately 1 dB. Thus all amplitudes are quantized to 1 dB
  665. steps.
  666. */
  667. static double DBtoLIN(long dB)
  668. {
  669. static short amptable[88] = {
  670. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7,
  671. 8, 9, 10, 11, 13, 14, 16, 18, 20, 22, 25, 28, 32,
  672. 35, 40, 45, 51, 57, 64, 71, 80, 90, 101, 114, 128,
  673. 142, 159, 179, 202, 227, 256, 284, 318, 359, 405,
  674. 455, 512, 568, 638, 719, 881, 911, 1024, 1137, 1276,
  675. 1438, 1622, 1823, 2048, 2273, 2552, 2875, 3244, 3645,
  676. 4096, 4547, 5104, 5751, 6488, 7291, 8192, 9093, 10207,
  677. 11502, 12976, 14582, 16384, 18350, 20644, 23429,
  678. 26214, 29491, 32767
  679. };
  680. if ((dB < 0) || (dB > 87))
  681. return 0;
  682. return (double)(amptable[dB]) * 0.001;
  683. }
  684. extern voice_t *wvoice;
  685. static klatt_peaks_t peaks[N_PEAKS];
  686. static int end_wave;
  687. static int klattp[N_KLATTP];
  688. static double klattp1[N_KLATTP];
  689. static double klattp_inc[N_KLATTP];
  690. int Wavegen_Klatt(int resume)
  691. {
  692. int pk;
  693. int x;
  694. int ix;
  695. int fade;
  696. if (resume == 0)
  697. sample_count = 0;
  698. while (sample_count < nsamples) {
  699. kt_frame.F0hz10 = (wdata.pitch * 10) / 4096;
  700. // formants F6,F7,F8 are fixed values for cascade resonators, set in KlattInit()
  701. // but F6 is used for parallel resonator
  702. // F0 is used for the nasal zero
  703. for (ix = 0; ix < 6; ix++) {
  704. kt_frame.Fhz[ix] = peaks[ix].freq;
  705. if (ix < 4)
  706. kt_frame.Bhz[ix] = peaks[ix].bw;
  707. }
  708. for (ix = 1; ix < 7; ix++)
  709. kt_frame.Ap[ix] = peaks[ix].ap;
  710. kt_frame.AVdb = klattp[KLATT_AV];
  711. kt_frame.AVpdb = klattp[KLATT_AVp];
  712. kt_frame.AF = klattp[KLATT_Fric];
  713. kt_frame.AB = klattp[KLATT_FricBP];
  714. kt_frame.ASP = klattp[KLATT_Aspr];
  715. kt_frame.Aturb = klattp[KLATT_Turb];
  716. kt_frame.Kskew = klattp[KLATT_Skew];
  717. kt_frame.TLTdb = klattp[KLATT_Tilt];
  718. kt_frame.Kopen = klattp[KLATT_Kopen];
  719. // advance formants
  720. for (pk = 0; pk < N_PEAKS; pk++) {
  721. peaks[pk].freq1 += peaks[pk].freq_inc;
  722. peaks[pk].freq = (int)peaks[pk].freq1;
  723. peaks[pk].bw1 += peaks[pk].bw_inc;
  724. peaks[pk].bw = (int)peaks[pk].bw1;
  725. peaks[pk].bp1 += peaks[pk].bp_inc;
  726. peaks[pk].bp = (int)peaks[pk].bp1;
  727. peaks[pk].ap1 += peaks[pk].ap_inc;
  728. peaks[pk].ap = (int)peaks[pk].ap1;
  729. }
  730. // advance other parameters
  731. for (ix = 0; ix < N_KLATTP; ix++) {
  732. klattp1[ix] += klattp_inc[ix];
  733. klattp[ix] = (int)klattp1[ix];
  734. }
  735. for (ix = 0; ix <= 6; ix++) {
  736. kt_frame.Fhz_next[ix] = peaks[ix].freq;
  737. if (ix < 4)
  738. kt_frame.Bhz_next[ix] = peaks[ix].bw;
  739. }
  740. // advance the pitch
  741. wdata.pitch_ix += wdata.pitch_inc;
  742. if ((ix = wdata.pitch_ix>>8) > 127) ix = 127;
  743. x = wdata.pitch_env[ix] * wdata.pitch_range;
  744. wdata.pitch = (x>>8) + wdata.pitch_base;
  745. kt_globals.nspfr = (nsamples - sample_count);
  746. if (kt_globals.nspfr > STEPSIZE)
  747. kt_globals.nspfr = STEPSIZE;
  748. frame_init(&kt_frame); /* get parameters for next frame of speech */
  749. if (parwave(&kt_frame) == 1)
  750. return 1; // output buffer is full
  751. }
  752. if (end_wave > 0) {
  753. fade = 64; // not followd by formant synthesis
  754. // fade out to avoid a click
  755. kt_globals.fadeout = fade;
  756. end_wave = 0;
  757. sample_count -= fade;
  758. kt_globals.nspfr = fade;
  759. if (parwave(&kt_frame) == 1)
  760. return 1; // output buffer is full
  761. }
  762. return 0;
  763. }
  764. void SetSynth_Klatt(int length, int modn, frame_t *fr1, frame_t *fr2, voice_t *v, int control)
  765. {
  766. int ix;
  767. DOUBLEX next;
  768. int qix;
  769. int cmd;
  770. frame_t *fr3;
  771. static frame_t prev_fr;
  772. if (wvoice != NULL) {
  773. if ((wvoice->klattv[0] > 0) && (wvoice->klattv[0] <= 4 )) {
  774. kt_globals.glsource = wvoice->klattv[0];
  775. kt_globals.scale_wav = scale_wav_tab[kt_globals.glsource];
  776. }
  777. kt_globals.f0_flutter = wvoice->flutter/32;
  778. }
  779. end_wave = 0;
  780. if (control & 2)
  781. end_wave = 1; // fadeout at the end
  782. if (control & 1) {
  783. end_wave = 1;
  784. for (qix = wcmdq_head+1;; qix++) {
  785. if (qix >= N_WCMDQ) qix = 0;
  786. if (qix == wcmdq_tail) break;
  787. cmd = wcmdq[qix][0];
  788. if (cmd == WCMD_KLATT) {
  789. end_wave = 0; // next wave generation is from another spectrum
  790. fr3 = (frame_t *)wcmdq[qix][2];
  791. for (ix = 1; ix < 6; ix++) {
  792. if (fr3->ffreq[ix] != fr2->ffreq[ix]) {
  793. // there is a discontinuity in formants
  794. end_wave = 2;
  795. break;
  796. }
  797. }
  798. break;
  799. }
  800. if ((cmd == WCMD_WAVE) || (cmd == WCMD_PAUSE))
  801. break; // next is not from spectrum, so continue until end of wave cycle
  802. }
  803. }
  804. if (control & 1) {
  805. for (ix = 1; ix < 6; ix++) {
  806. if (prev_fr.ffreq[ix] != fr1->ffreq[ix]) {
  807. // Discontinuity in formants.
  808. // end_wave was set in SetSynth_Klatt() to fade out the previous frame
  809. KlattReset(0);
  810. break;
  811. }
  812. }
  813. memcpy(&prev_fr, fr2, sizeof(prev_fr));
  814. }
  815. for (ix = 0; ix < N_KLATTP; ix++) {
  816. if ((ix >= 5) && ((fr1->frflags & FRFLAG_KLATT) == 0)) {
  817. klattp1[ix] = klattp[ix] = 0;
  818. klattp_inc[ix] = 0;
  819. } else {
  820. klattp1[ix] = klattp[ix] = fr1->klattp[ix];
  821. klattp_inc[ix] = (double)((fr2->klattp[ix] - klattp[ix]) * STEPSIZE)/length;
  822. }
  823. }
  824. nsamples = length;
  825. for (ix = 1; ix < 6; ix++) {
  826. peaks[ix].freq1 = (fr1->ffreq[ix] * v->freq[ix] / 256.0) + v->freqadd[ix];
  827. peaks[ix].freq = (int)peaks[ix].freq1;
  828. next = (fr2->ffreq[ix] * v->freq[ix] / 256.0) + v->freqadd[ix];
  829. peaks[ix].freq_inc = ((next - peaks[ix].freq1) * STEPSIZE) / length;
  830. if (ix < 4) {
  831. // klatt bandwidth for f1, f2, f3 (others are fixed)
  832. peaks[ix].bw1 = fr1->bw[ix] * 2;
  833. peaks[ix].bw = (int)peaks[ix].bw1;
  834. next = fr2->bw[ix] * 2;
  835. peaks[ix].bw_inc = ((next - peaks[ix].bw1) * STEPSIZE) / length;
  836. }
  837. }
  838. // nasal zero frequency
  839. peaks[0].freq1 = fr1->klattp[KLATT_FNZ] * 2;
  840. if (peaks[0].freq1 == 0)
  841. peaks[0].freq1 = kt_frame.Fhz[F_NP]; // if no nasal zero, set it to same freq as nasal pole
  842. peaks[0].freq = (int)peaks[0].freq1;
  843. next = fr2->klattp[KLATT_FNZ] * 2;
  844. if (next == 0)
  845. next = kt_frame.Fhz[F_NP];
  846. peaks[0].freq_inc = ((next - peaks[0].freq1) * STEPSIZE) / length;
  847. peaks[0].bw1 = 89;
  848. peaks[0].bw = 89;
  849. peaks[0].bw_inc = 0;
  850. if (fr1->frflags & FRFLAG_KLATT) {
  851. // the frame contains additional parameters for parallel resonators
  852. for (ix = 1; ix < 7; ix++) {
  853. peaks[ix].bp1 = fr1->klatt_bp[ix] * 4; // parallel bandwidth
  854. peaks[ix].bp = (int)peaks[ix].bp1;
  855. next = fr2->klatt_bp[ix] * 4;
  856. peaks[ix].bp_inc = ((next - peaks[ix].bp1) * STEPSIZE) / length;
  857. peaks[ix].ap1 = fr1->klatt_ap[ix]; // parallal amplitude
  858. peaks[ix].ap = (int)peaks[ix].ap1;
  859. next = fr2->klatt_ap[ix];
  860. peaks[ix].ap_inc = ((next - peaks[ix].ap1) * STEPSIZE) / length;
  861. }
  862. }
  863. }
  864. int Wavegen_Klatt2(int length, int modulation, int resume, frame_t *fr1, frame_t *fr2)
  865. {
  866. if (resume == 0)
  867. SetSynth_Klatt(length, modulation, fr1, fr2, wvoice, 1);
  868. return Wavegen_Klatt(resume);
  869. }
  870. void KlattInit()
  871. {
  872. static short formant_hz[10] = { 280, 688, 1064, 2806, 3260, 3700, 6500, 7000, 8000, 280 };
  873. static short bandwidth[10] = { 89, 160, 70, 160, 200, 200, 500, 500, 500, 89 };
  874. static short parallel_amp[10] = { 0, 59, 59, 59, 59, 59, 59, 0, 0, 0 };
  875. static short parallel_bw[10] = { 59, 59, 89, 149, 200, 200, 500, 0, 0, 0 };
  876. int ix;
  877. sample_count = 0;
  878. kt_globals.synthesis_model = CASCADE_PARALLEL;
  879. kt_globals.samrate = 22050;
  880. kt_globals.glsource = IMPULSIVE; // IMPULSIVE, NATURAL, SAMPLED
  881. kt_globals.scale_wav = scale_wav_tab[kt_globals.glsource];
  882. kt_globals.natural_samples = natural_samples;
  883. kt_globals.num_samples = NUMBER_OF_SAMPLES;
  884. kt_globals.sample_factor = 3.0;
  885. kt_globals.nspfr = (kt_globals.samrate * 10) / 1000;
  886. kt_globals.outsl = 0;
  887. kt_globals.f0_flutter = 20;
  888. KlattReset(2);
  889. // set default values for frame parameters
  890. for (ix = 0; ix <= 9; ix++) {
  891. kt_frame.Fhz[ix] = formant_hz[ix];
  892. kt_frame.Bhz[ix] = bandwidth[ix];
  893. kt_frame.Ap[ix] = parallel_amp[ix];
  894. kt_frame.Bphz[ix] = parallel_bw[ix];
  895. }
  896. kt_frame.Bhz_next[F_NZ] = bandwidth[F_NZ];
  897. kt_frame.F0hz10 = 1000;
  898. kt_frame.AVdb = 59; // 59
  899. kt_frame.ASP = 0;
  900. kt_frame.Kopen = 40; // 40
  901. kt_frame.Aturb = 0;
  902. kt_frame.TLTdb = 0;
  903. kt_frame.AF = 50;
  904. kt_frame.Kskew = 0;
  905. kt_frame.AB = 0;
  906. kt_frame.AVpdb = 0;
  907. kt_frame.Gain0 = 62; // 60
  908. }