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 32KB

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