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.

synthesize.c 40KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  1. /*
  2. * Copyright (C) 2005 to 2014 by Jonathan Duddington
  3. * email: [email protected]
  4. * Copyright (C) 2015-2017 Reece H. Dunn
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see: <http://www.gnu.org/licenses/>.
  18. */
  19. #include "config.h"
  20. #include <ctype.h>
  21. #include <errno.h>
  22. #include <math.h>
  23. #include <stdbool.h>
  24. #include <stdint.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <espeak-ng/espeak_ng.h>
  29. #include <espeak-ng/speak_lib.h>
  30. #include <espeak-ng/encoding.h>
  31. #include "intonation.h"
  32. #include "setlengths.h"
  33. #include "synthdata.h"
  34. #include "phoneme.h"
  35. #include "voice.h"
  36. #include "synthesize.h"
  37. #include "translate.h"
  38. extern FILE *f_log;
  39. static void SmoothSpect(void);
  40. // list of phonemes in a clause
  41. int n_phoneme_list = 0;
  42. PHONEME_LIST phoneme_list[N_PHONEME_LIST+1];
  43. int mbrola_delay;
  44. char mbrola_name[20];
  45. SPEED_FACTORS speed;
  46. static int last_pitch_cmd;
  47. static int last_amp_cmd;
  48. static frame_t *last_frame;
  49. static int last_wcmdq;
  50. static int pitch_length;
  51. static int amp_length;
  52. static int modn_flags;
  53. static int fmt_amplitude = 0;
  54. static int syllable_start;
  55. static int syllable_end;
  56. static int syllable_centre;
  57. static voice_t *new_voice = NULL;
  58. int n_soundicon_tab = N_SOUNDICON_SLOTS;
  59. SOUND_ICON soundicon_tab[N_SOUNDICON_TAB];
  60. #define RMS_GLOTTAL1 35 // vowel before glottal stop
  61. #define RMS_START 28 // 28
  62. #define VOWEL_FRONT_LENGTH 50
  63. // a dummy phoneme_list entry which looks like a pause
  64. static PHONEME_LIST next_pause;
  65. const char *WordToString(unsigned int word)
  66. {
  67. // Convert a phoneme mnemonic word into a string
  68. int ix;
  69. static char buf[5];
  70. for (ix = 0; ix < 4; ix++)
  71. buf[ix] = word >> (ix*8);
  72. buf[4] = 0;
  73. return buf;
  74. }
  75. void SynthesizeInit()
  76. {
  77. last_pitch_cmd = 0;
  78. last_amp_cmd = 0;
  79. last_frame = NULL;
  80. syllable_centre = -1;
  81. // initialise next_pause, a dummy phoneme_list entry
  82. next_pause.type = phPAUSE;
  83. next_pause.newword = 0;
  84. }
  85. static void EndAmplitude(void)
  86. {
  87. if (amp_length > 0) {
  88. if (wcmdq[last_amp_cmd][1] == 0)
  89. wcmdq[last_amp_cmd][1] = amp_length;
  90. amp_length = 0;
  91. }
  92. }
  93. static void EndPitch(int voice_break)
  94. {
  95. // posssible end of pitch envelope, fill in the length
  96. if ((pitch_length > 0) && (last_pitch_cmd >= 0)) {
  97. if (wcmdq[last_pitch_cmd][1] == 0)
  98. wcmdq[last_pitch_cmd][1] = pitch_length;
  99. pitch_length = 0;
  100. }
  101. if (voice_break) {
  102. last_wcmdq = -1;
  103. last_frame = NULL;
  104. syllable_end = wcmdq_tail;
  105. SmoothSpect();
  106. syllable_centre = -1;
  107. memset(vowel_transition, 0, sizeof(vowel_transition));
  108. }
  109. }
  110. static void DoAmplitude(int amp, unsigned char *amp_env)
  111. {
  112. intptr_t *q;
  113. last_amp_cmd = wcmdq_tail;
  114. amp_length = 0; // total length of vowel with this amplitude envelope
  115. q = wcmdq[wcmdq_tail];
  116. q[0] = WCMD_AMPLITUDE;
  117. q[1] = 0; // fill in later from amp_length
  118. q[2] = (intptr_t)amp_env;
  119. q[3] = amp;
  120. WcmdqInc();
  121. }
  122. static void DoPitch(unsigned char *env, int pitch1, int pitch2)
  123. {
  124. intptr_t *q;
  125. EndPitch(0);
  126. if (pitch1 == 255) {
  127. // pitch was not set
  128. pitch1 = 55;
  129. pitch2 = 76;
  130. env = envelope_data[PITCHfall];
  131. }
  132. last_pitch_cmd = wcmdq_tail;
  133. pitch_length = 0; // total length of spect with this pitch envelope
  134. if (pitch2 < 0)
  135. pitch2 = 0;
  136. q = wcmdq[wcmdq_tail];
  137. q[0] = WCMD_PITCH;
  138. q[1] = 0; // length, fill in later from pitch_length
  139. q[2] = (intptr_t)env;
  140. q[3] = (pitch1 << 16) + pitch2;
  141. WcmdqInc();
  142. }
  143. int PauseLength(int pause, int control)
  144. {
  145. unsigned int len;
  146. if (control == 0) {
  147. if (pause >= 200)
  148. len = (pause * speed.clause_pause_factor)/256;
  149. else
  150. len = (pause * speed.pause_factor)/256;
  151. } else
  152. len = (pause * speed.wav_factor)/256;
  153. if (len < speed.min_pause)
  154. len = speed.min_pause; // mS, limit the amount to which pauses can be shortened
  155. return len;
  156. }
  157. static void DoPause(int length, int control)
  158. {
  159. // length in nominal mS
  160. // control = 1, less shortening at fast speeds
  161. unsigned int len;
  162. int srate2;
  163. if (length == 0)
  164. len = 0;
  165. else {
  166. len = PauseLength(length, control);
  167. if (len < 90000)
  168. len = (len * samplerate) / 1000; // convert from mS to number of samples
  169. else {
  170. srate2 = samplerate / 25; // avoid overflow
  171. len = (len * srate2) / 40;
  172. }
  173. }
  174. EndPitch(1);
  175. wcmdq[wcmdq_tail][0] = WCMD_PAUSE;
  176. wcmdq[wcmdq_tail][1] = len;
  177. WcmdqInc();
  178. last_frame = NULL;
  179. if (fmt_amplitude != 0) {
  180. wcmdq[wcmdq_tail][0] = WCMD_FMT_AMPLITUDE;
  181. wcmdq[wcmdq_tail][1] = fmt_amplitude = 0;
  182. WcmdqInc();
  183. }
  184. }
  185. extern int seq_len_adjust; // temporary fix to advance the start point for playing the wav sample
  186. static int DoSample2(int index, int which, int std_length, int control, int length_mod, int amp)
  187. {
  188. int length;
  189. int wav_length;
  190. int wav_scale;
  191. int min_length;
  192. int x;
  193. int len4;
  194. intptr_t *q;
  195. unsigned char *p;
  196. index = index & 0x7fffff;
  197. p = &wavefile_data[index];
  198. wav_scale = p[2];
  199. wav_length = (p[1] * 256);
  200. wav_length += p[0]; // length in bytes
  201. if (wav_length == 0)
  202. return 0;
  203. min_length = speed.min_sample_len;
  204. if (wav_scale == 0)
  205. min_length *= 2; // 16 bit samples
  206. if (std_length > 0) {
  207. std_length = (std_length * samplerate)/1000;
  208. if (wav_scale == 0)
  209. std_length *= 2;
  210. x = (min_length * std_length)/wav_length;
  211. if (x > min_length)
  212. min_length = x;
  213. } else {
  214. // no length specified, use the length of the stored sound
  215. std_length = wav_length;
  216. }
  217. if (length_mod > 0)
  218. std_length = (std_length * length_mod)/256;
  219. length = (std_length * speed.wav_factor)/256;
  220. if (control & pd_DONTLENGTHEN) {
  221. // this option is used for Stops, with short noise bursts.
  222. // Don't change their length much.
  223. if (length > std_length) {
  224. // don't let length exceed std_length
  225. length = std_length;
  226. }
  227. }
  228. if (length < min_length)
  229. length = min_length;
  230. if (wav_scale == 0) {
  231. // 16 bit samples
  232. length /= 2;
  233. wav_length /= 2;
  234. }
  235. if (amp < 0)
  236. return length;
  237. len4 = wav_length / 4;
  238. index += 4;
  239. if (which & 0x100) {
  240. // mix this with synthesised wave
  241. last_wcmdq = wcmdq_tail;
  242. q = wcmdq[wcmdq_tail];
  243. q[0] = WCMD_WAVE2;
  244. q[1] = length | (wav_length << 16); // length in samples
  245. q[2] = (intptr_t)(&wavefile_data[index]);
  246. q[3] = wav_scale + (amp << 8);
  247. WcmdqInc();
  248. return length;
  249. }
  250. if (length > wav_length) {
  251. x = len4*3;
  252. length -= x;
  253. } else {
  254. x = length;
  255. length = 0;
  256. }
  257. last_wcmdq = wcmdq_tail;
  258. q = wcmdq[wcmdq_tail];
  259. q[0] = WCMD_WAVE;
  260. q[1] = x; // length in samples
  261. q[2] = (intptr_t)(&wavefile_data[index]);
  262. q[3] = wav_scale + (amp << 8);
  263. WcmdqInc();
  264. while (length > len4*3) {
  265. x = len4;
  266. if (wav_scale == 0)
  267. x *= 2;
  268. last_wcmdq = wcmdq_tail;
  269. q = wcmdq[wcmdq_tail];
  270. q[0] = WCMD_WAVE;
  271. q[1] = len4*2; // length in samples
  272. q[2] = (intptr_t)(&wavefile_data[index+x]);
  273. q[3] = wav_scale + (amp << 8);
  274. WcmdqInc();
  275. length -= len4*2;
  276. }
  277. if (length > 0) {
  278. x = wav_length - length;
  279. if (wav_scale == 0)
  280. x *= 2;
  281. last_wcmdq = wcmdq_tail;
  282. q = wcmdq[wcmdq_tail];
  283. q[0] = WCMD_WAVE;
  284. q[1] = length; // length in samples
  285. q[2] = (intptr_t)(&wavefile_data[index+x]);
  286. q[3] = wav_scale + (amp << 8);
  287. WcmdqInc();
  288. }
  289. return length;
  290. }
  291. int DoSample3(PHONEME_DATA *phdata, int length_mod, int amp)
  292. {
  293. int amp2;
  294. int len;
  295. EndPitch(1);
  296. if (amp == -1) {
  297. // just get the length, don't produce sound
  298. amp2 = amp;
  299. } else {
  300. amp2 = phdata->sound_param[pd_WAV];
  301. if (amp2 == 0)
  302. amp2 = 100;
  303. amp2 = (amp2 * 32)/100;
  304. }
  305. seq_len_adjust = 0;
  306. if (phdata->sound_addr[pd_WAV] == 0)
  307. len = 0;
  308. else
  309. len = DoSample2(phdata->sound_addr[pd_WAV], 2, phdata->pd_param[pd_LENGTHMOD]*2, phdata->pd_control, length_mod, amp2);
  310. last_frame = NULL;
  311. return len;
  312. }
  313. static frame_t *AllocFrame()
  314. {
  315. // Allocate a temporary spectrum frame for the wavegen queue. Use a pool which is big
  316. // enough to use a round-robin without checks.
  317. // Only needed for modifying spectra for blending to consonants
  318. #define N_FRAME_POOL N_WCMDQ
  319. static int ix = 0;
  320. static frame_t frame_pool[N_FRAME_POOL];
  321. ix++;
  322. if (ix >= N_FRAME_POOL)
  323. ix = 0;
  324. return &frame_pool[ix];
  325. }
  326. static void set_frame_rms(frame_t *fr, int new_rms)
  327. {
  328. // Each frame includes its RMS amplitude value, so to set a new
  329. // RMS just adjust the formant amplitudes by the appropriate ratio
  330. int x;
  331. int h;
  332. int ix;
  333. static const short sqrt_tab[200] = {
  334. 0, 64, 90, 110, 128, 143, 156, 169, 181, 192, 202, 212, 221, 230, 239, 247,
  335. 256, 263, 271, 278, 286, 293, 300, 306, 313, 320, 326, 332, 338, 344, 350, 356,
  336. 362, 367, 373, 378, 384, 389, 394, 399, 404, 409, 414, 419, 424, 429, 434, 438,
  337. 443, 448, 452, 457, 461, 465, 470, 474, 478, 483, 487, 491, 495, 499, 503, 507,
  338. 512, 515, 519, 523, 527, 531, 535, 539, 543, 546, 550, 554, 557, 561, 565, 568,
  339. 572, 576, 579, 583, 586, 590, 593, 596, 600, 603, 607, 610, 613, 617, 620, 623,
  340. 627, 630, 633, 636, 640, 643, 646, 649, 652, 655, 658, 662, 665, 668, 671, 674,
  341. 677, 680, 683, 686, 689, 692, 695, 698, 701, 704, 706, 709, 712, 715, 718, 721,
  342. 724, 726, 729, 732, 735, 738, 740, 743, 746, 749, 751, 754, 757, 759, 762, 765,
  343. 768, 770, 773, 775, 778, 781, 783, 786, 789, 791, 794, 796, 799, 801, 804, 807,
  344. 809, 812, 814, 817, 819, 822, 824, 827, 829, 832, 834, 836, 839, 841, 844, 846,
  345. 849, 851, 853, 856, 858, 861, 863, 865, 868, 870, 872, 875, 877, 879, 882, 884,
  346. 886, 889, 891, 893, 896, 898, 900, 902
  347. };
  348. if (voice->klattv[0]) {
  349. if (new_rms == -1)
  350. fr->klattp[KLATT_AV] = 50;
  351. return;
  352. }
  353. if (fr->rms == 0) return; // check for divide by zero
  354. x = (new_rms * 64)/fr->rms;
  355. if (x >= 200) x = 199;
  356. x = sqrt_tab[x]; // sqrt(new_rms/fr->rms)*0x200;
  357. for (ix = 0; ix < 8; ix++) {
  358. h = fr->fheight[ix] * x;
  359. fr->fheight[ix] = h/0x200;
  360. }
  361. }
  362. static void formants_reduce_hf(frame_t *fr, int level)
  363. {
  364. // change height of peaks 2 to 8, percentage
  365. int ix;
  366. int x;
  367. if (voice->klattv[0])
  368. return;
  369. for (ix = 2; ix < 8; ix++) {
  370. x = fr->fheight[ix] * level;
  371. fr->fheight[ix] = x/100;
  372. }
  373. }
  374. static frame_t *CopyFrame(frame_t *frame1, int copy)
  375. {
  376. // create a copy of the specified frame in temporary buffer
  377. frame_t *frame2;
  378. if ((copy == 0) && (frame1->frflags & FRFLAG_COPIED)) {
  379. // this frame has already been copied in temporary rw memory
  380. return frame1;
  381. }
  382. frame2 = AllocFrame();
  383. if (frame2 != NULL) {
  384. memcpy(frame2, frame1, sizeof(frame_t));
  385. frame2->length = 0;
  386. frame2->frflags |= FRFLAG_COPIED;
  387. }
  388. return frame2;
  389. }
  390. static frame_t *DuplicateLastFrame(frameref_t *seq, int n_frames, int length)
  391. {
  392. frame_t *fr;
  393. seq[n_frames-1].length = length;
  394. fr = CopyFrame(seq[n_frames-1].frame, 1);
  395. seq[n_frames].frame = fr;
  396. seq[n_frames].length = 0;
  397. return fr;
  398. }
  399. static void AdjustFormants(frame_t *fr, int target, int min, int max, int f1_adj, int f3_adj, int hf_reduce, int flags)
  400. {
  401. int x;
  402. target = (target * voice->formant_factor)/256;
  403. x = (target - fr->ffreq[2]) / 2;
  404. if (x > max) x = max;
  405. if (x < min) x = min;
  406. fr->ffreq[2] += x;
  407. fr->ffreq[3] += f3_adj;
  408. if (flags & 0x20)
  409. f3_adj = -f3_adj; // reverse direction for f4,f5 change
  410. fr->ffreq[4] += f3_adj;
  411. fr->ffreq[5] += f3_adj;
  412. if (f1_adj == 1) {
  413. x = (235 - fr->ffreq[1]);
  414. if (x < -100) x = -100;
  415. if (x > -60) x = -60;
  416. fr->ffreq[1] += x;
  417. }
  418. if (f1_adj == 2) {
  419. x = (235 - fr->ffreq[1]);
  420. if (x < -300) x = -300;
  421. if (x > -150) x = -150;
  422. fr->ffreq[1] += x;
  423. fr->ffreq[0] += x;
  424. }
  425. if (f1_adj == 3) {
  426. x = (100 - fr->ffreq[1]);
  427. if (x < -400) x = -400;
  428. if (x > -300) x = -400;
  429. fr->ffreq[1] += x;
  430. fr->ffreq[0] += x;
  431. }
  432. formants_reduce_hf(fr, hf_reduce);
  433. }
  434. static int VowelCloseness(frame_t *fr)
  435. {
  436. // return a value 0-3 depending on the vowel's f1
  437. int f1;
  438. if ((f1 = fr->ffreq[1]) < 300)
  439. return 3;
  440. if (f1 < 400)
  441. return 2;
  442. if (f1 < 500)
  443. return 1;
  444. return 0;
  445. }
  446. int FormantTransition2(frameref_t *seq, int *n_frames, unsigned int data1, unsigned int data2, PHONEME_TAB *other_ph, int which)
  447. {
  448. int ix;
  449. int formant;
  450. int next_rms;
  451. int len;
  452. int rms;
  453. int f1;
  454. int f2;
  455. int f2_min;
  456. int f2_max;
  457. int f3_adj;
  458. int f3_amp;
  459. int flags;
  460. int vcolour;
  461. #define N_VCOLOUR 2
  462. // percentage change for each formant in 256ths
  463. static short vcolouring[N_VCOLOUR][5] = {
  464. { 243, 272, 256, 256, 256 }, // palatal consonant follows
  465. { 256, 256, 240, 240, 240 }, // retroflex
  466. };
  467. frame_t *fr = NULL;
  468. if (*n_frames < 2)
  469. return 0;
  470. len = (data1 & 0x3f) * 2;
  471. rms = (data1 >> 6) & 0x3f;
  472. flags = (data1 >> 12);
  473. f2 = (data2 & 0x3f) * 50;
  474. f2_min = (((data2 >> 6) & 0x1f) - 15) * 50;
  475. f2_max = (((data2 >> 11) & 0x1f) - 15) * 50;
  476. f3_adj = (((data2 >> 16) & 0x1f) - 15) * 50;
  477. f3_amp = ((data2 >> 21) & 0x1f) * 8;
  478. f1 = ((data2 >> 26) & 0x7);
  479. vcolour = (data2 >> 29);
  480. if ((other_ph != NULL) && (other_ph->mnemonic == '?'))
  481. flags |= 8;
  482. if (which == 1) {
  483. // entry to vowel
  484. fr = CopyFrame(seq[0].frame, 0);
  485. seq[0].frame = fr;
  486. seq[0].length = VOWEL_FRONT_LENGTH;
  487. if (len > 0)
  488. seq[0].length = len;
  489. seq[0].frflags |= FRFLAG_LEN_MOD2; // reduce length modification
  490. fr->frflags |= FRFLAG_LEN_MOD2;
  491. next_rms = seq[1].frame->rms;
  492. if (voice->klattv[0])
  493. fr->klattp[KLATT_AV] = seq[1].frame->klattp[KLATT_AV] - 4;
  494. if (f2 != 0) {
  495. if (rms & 0x20)
  496. set_frame_rms(fr, (next_rms * (rms & 0x1f))/30);
  497. AdjustFormants(fr, f2, f2_min, f2_max, f1, f3_adj, f3_amp, flags);
  498. if ((rms & 0x20) == 0)
  499. set_frame_rms(fr, rms*2);
  500. } else {
  501. if (flags & 8)
  502. set_frame_rms(fr, (next_rms*24)/32);
  503. else
  504. set_frame_rms(fr, RMS_START);
  505. }
  506. if (flags & 8)
  507. modn_flags = 0x800 + (VowelCloseness(fr) << 8);
  508. } else {
  509. // exit from vowel
  510. rms = rms*2;
  511. if ((f2 != 0) || (flags != 0)) {
  512. if (flags & 8) {
  513. fr = CopyFrame(seq[*n_frames-1].frame, 0);
  514. seq[*n_frames-1].frame = fr;
  515. rms = RMS_GLOTTAL1;
  516. // degree of glottal-stop effect depends on closeness of vowel (indicated by f1 freq)
  517. modn_flags = 0x400 + (VowelCloseness(fr) << 8);
  518. } else {
  519. fr = DuplicateLastFrame(seq, (*n_frames)++, len);
  520. if (len > 36)
  521. seq_len_adjust += (len - 36);
  522. if (f2 != 0)
  523. AdjustFormants(fr, f2, f2_min, f2_max, f1, f3_adj, f3_amp, flags);
  524. }
  525. set_frame_rms(fr, rms);
  526. if ((vcolour > 0) && (vcolour <= N_VCOLOUR)) {
  527. for (ix = 0; ix < *n_frames; ix++) {
  528. fr = CopyFrame(seq[ix].frame, 0);
  529. seq[ix].frame = fr;
  530. for (formant = 1; formant <= 5; formant++) {
  531. int x;
  532. x = fr->ffreq[formant] * vcolouring[vcolour-1][formant-1];
  533. fr->ffreq[formant] = x / 256;
  534. }
  535. }
  536. }
  537. }
  538. }
  539. if (fr != NULL) {
  540. if (flags & 4)
  541. fr->frflags |= FRFLAG_FORMANT_RATE;
  542. if (flags & 2)
  543. fr->frflags |= FRFLAG_BREAK; // don't merge with next frame
  544. }
  545. if (flags & 0x40)
  546. DoPause(20, 0); // add a short pause after the consonant
  547. if (flags & 16)
  548. return len;
  549. return 0;
  550. }
  551. static void SmoothSpect(void)
  552. {
  553. // Limit the rate of frequence change of formants, to reduce chirping
  554. intptr_t *q;
  555. frame_t *frame;
  556. frame_t *frame2;
  557. frame_t *frame1;
  558. frame_t *frame_centre;
  559. int ix;
  560. int len;
  561. int pk;
  562. bool modified;
  563. int allowed;
  564. int diff;
  565. if (syllable_start == syllable_end)
  566. return;
  567. if ((syllable_centre < 0) || (syllable_centre == syllable_start)) {
  568. syllable_start = syllable_end;
  569. return;
  570. }
  571. q = wcmdq[syllable_centre];
  572. frame_centre = (frame_t *)q[2];
  573. // backwards
  574. ix = syllable_centre -1;
  575. frame = frame2 = frame_centre;
  576. for (;;) {
  577. if (ix < 0) ix = N_WCMDQ-1;
  578. q = wcmdq[ix];
  579. if (q[0] == WCMD_PAUSE || q[0] == WCMD_WAVE)
  580. break;
  581. if (q[0] <= WCMD_SPECT2) {
  582. len = q[1] & 0xffff;
  583. frame1 = (frame_t *)q[3];
  584. if (frame1 == frame) {
  585. q[3] = (intptr_t)frame2;
  586. frame1 = frame2;
  587. } else
  588. break; // doesn't follow on from previous frame
  589. frame = frame2 = (frame_t *)q[2];
  590. modified = false;
  591. if (frame->frflags & FRFLAG_BREAK)
  592. break;
  593. if (frame->frflags & FRFLAG_FORMANT_RATE)
  594. len = (len * 12)/10; // allow slightly greater rate of change for this frame (was 12/10)
  595. for (pk = 0; pk < 6; pk++) {
  596. int f1, f2;
  597. if ((frame->frflags & FRFLAG_BREAK_LF) && (pk < 3))
  598. continue;
  599. f1 = frame1->ffreq[pk];
  600. f2 = frame->ffreq[pk];
  601. // backwards
  602. if ((diff = f2 - f1) > 0)
  603. allowed = f1*2 + f2;
  604. else
  605. allowed = f1 + f2*2;
  606. // the allowed change is specified as percentage (%*10) of the frequency
  607. // take "frequency" as 1/3 from the lower freq
  608. allowed = (allowed * formant_rate[pk])/3000;
  609. allowed = (allowed * len)/256;
  610. if (diff > allowed) {
  611. if (modified == false) {
  612. frame2 = CopyFrame(frame, 0);
  613. modified = true;
  614. }
  615. frame2->ffreq[pk] = frame1->ffreq[pk] + allowed;
  616. q[2] = (intptr_t)frame2;
  617. } else if (diff < -allowed) {
  618. if (modified == false) {
  619. frame2 = CopyFrame(frame, 0);
  620. modified = true;
  621. }
  622. frame2->ffreq[pk] = frame1->ffreq[pk] - allowed;
  623. q[2] = (intptr_t)frame2;
  624. }
  625. }
  626. }
  627. if (ix == syllable_start)
  628. break;
  629. ix--;
  630. }
  631. // forwards
  632. ix = syllable_centre;
  633. frame = NULL;
  634. for (;;) {
  635. q = wcmdq[ix];
  636. if (q[0] == WCMD_PAUSE || q[0] == WCMD_WAVE)
  637. break;
  638. if (q[0] <= WCMD_SPECT2) {
  639. len = q[1] & 0xffff;
  640. frame1 = (frame_t *)q[2];
  641. if (frame != NULL) {
  642. if (frame1 == frame) {
  643. q[2] = (intptr_t)frame2;
  644. frame1 = frame2;
  645. } else
  646. break; // doesn't follow on from previous frame
  647. }
  648. frame = frame2 = (frame_t *)q[3];
  649. modified = false;
  650. if (frame1->frflags & FRFLAG_BREAK)
  651. break;
  652. if (frame1->frflags & FRFLAG_FORMANT_RATE)
  653. len = (len *6)/5; // allow slightly greater rate of change for this frame
  654. for (pk = 0; pk < 6; pk++) {
  655. int f1, f2;
  656. f1 = frame1->ffreq[pk];
  657. f2 = frame->ffreq[pk];
  658. // forwards
  659. if ((diff = f2 - f1) > 0)
  660. allowed = f1*2 + f2;
  661. else
  662. allowed = f1 + f2*2;
  663. allowed = (allowed * formant_rate[pk])/3000;
  664. allowed = (allowed * len)/256;
  665. if (diff > allowed) {
  666. if (modified == false) {
  667. frame2 = CopyFrame(frame, 0);
  668. modified = true;
  669. }
  670. frame2->ffreq[pk] = frame1->ffreq[pk] + allowed;
  671. q[3] = (intptr_t)frame2;
  672. } else if (diff < -allowed) {
  673. if (modified == false) {
  674. frame2 = CopyFrame(frame, 0);
  675. modified = true;
  676. }
  677. frame2->ffreq[pk] = frame1->ffreq[pk] - allowed;
  678. q[3] = (intptr_t)frame2;
  679. }
  680. }
  681. }
  682. ix++;
  683. if (ix >= N_WCMDQ) ix = 0;
  684. if (ix == syllable_end)
  685. break;
  686. }
  687. syllable_start = syllable_end;
  688. }
  689. static void StartSyllable(void)
  690. {
  691. // start of syllable, if not already started
  692. if (syllable_end == syllable_start)
  693. syllable_end = wcmdq_tail;
  694. }
  695. int DoSpect2(PHONEME_TAB *this_ph, int which, FMT_PARAMS *fmt_params, PHONEME_LIST *plist, int modulation)
  696. {
  697. // which: 0 not a vowel, 1 start of vowel, 2 body and end of vowel
  698. // length_mod: 256 = 100%
  699. // modulation: -1 = don't write to wcmdq
  700. int n_frames;
  701. frameref_t *frames;
  702. int frameix;
  703. frame_t *frame1;
  704. frame_t *frame2;
  705. frame_t *fr;
  706. int ix;
  707. intptr_t *q;
  708. int len;
  709. int frame_length;
  710. int length_factor;
  711. int length_mod;
  712. int length_sum;
  713. int length_min;
  714. int total_len = 0;
  715. static int wave_flag = 0;
  716. int wcmd_spect = WCMD_SPECT;
  717. int frame_lengths[N_SEQ_FRAMES];
  718. if (fmt_params->fmt_addr == 0)
  719. return 0;
  720. length_mod = plist->length;
  721. if (length_mod == 0) length_mod = 256;
  722. length_min = (samplerate/70); // greater than one cycle at low pitch (Hz)
  723. if (which == 2) {
  724. if ((translator->langopts.param[LOPT_LONG_VOWEL_THRESHOLD] > 0) && ((this_ph->std_length >= translator->langopts.param[LOPT_LONG_VOWEL_THRESHOLD]) || (plist->synthflags & SFLAG_LENGTHEN) || (this_ph->phflags & phLONG)))
  725. length_min *= 2; // ensure long vowels are longer
  726. }
  727. if (which == 1) {
  728. // limit the shortening of sonorants before shortened (eg. unstressed vowels)
  729. if ((this_ph->type == phLIQUID) || (plist[-1].type == phLIQUID) || (plist[-1].type == phNASAL)) {
  730. if (length_mod < (len = translator->langopts.param[LOPT_SONORANT_MIN]))
  731. length_mod = len;
  732. }
  733. }
  734. modn_flags = 0;
  735. frames = LookupSpect(this_ph, which, fmt_params, &n_frames, plist);
  736. if (frames == NULL)
  737. return 0; // not found
  738. if (fmt_params->fmt_amp != fmt_amplitude) {
  739. // an amplitude adjustment is specified for this sequence
  740. q = wcmdq[wcmdq_tail];
  741. q[0] = WCMD_FMT_AMPLITUDE;
  742. q[1] = fmt_amplitude = fmt_params->fmt_amp;
  743. WcmdqInc();
  744. }
  745. frame1 = frames[0].frame;
  746. if (voice->klattv[0])
  747. wcmd_spect = WCMD_KLATT;
  748. wavefile_ix = fmt_params->wav_addr;
  749. if (fmt_params->wav_amp == 0)
  750. wavefile_amp = 32;
  751. else
  752. wavefile_amp = (fmt_params->wav_amp * 32)/100;
  753. if (wavefile_ix == 0) {
  754. if (wave_flag) {
  755. // cancel any wavefile that was playing previously
  756. wcmd_spect = WCMD_SPECT2;
  757. if (voice->klattv[0])
  758. wcmd_spect = WCMD_KLATT2;
  759. wave_flag = 0;
  760. } else {
  761. wcmd_spect = WCMD_SPECT;
  762. if (voice->klattv[0])
  763. wcmd_spect = WCMD_KLATT;
  764. }
  765. }
  766. if (last_frame != NULL) {
  767. if (((last_frame->length < 2) || (last_frame->frflags & FRFLAG_VOWEL_CENTRE))
  768. && !(last_frame->frflags & FRFLAG_BREAK)) {
  769. // last frame of previous sequence was zero-length, replace with first of this sequence
  770. wcmdq[last_wcmdq][3] = (intptr_t)frame1;
  771. if (last_frame->frflags & FRFLAG_BREAK_LF) {
  772. // but flag indicates keep HF peaks in last segment
  773. fr = CopyFrame(frame1, 1);
  774. for (ix = 3; ix < 8; ix++) {
  775. if (ix < 7)
  776. fr->ffreq[ix] = last_frame->ffreq[ix];
  777. fr->fheight[ix] = last_frame->fheight[ix];
  778. }
  779. wcmdq[last_wcmdq][3] = (intptr_t)fr;
  780. }
  781. }
  782. }
  783. if ((this_ph->type == phVOWEL) && (which == 2)) {
  784. SmoothSpect(); // process previous syllable
  785. // remember the point in the output queue of the centre of the vowel
  786. syllable_centre = wcmdq_tail;
  787. }
  788. length_sum = 0;
  789. for (frameix = 1; frameix < n_frames; frameix++) {
  790. length_factor = length_mod;
  791. if (frames[frameix-1].frflags & FRFLAG_LEN_MOD) // reduce effect of length mod
  792. length_factor = (length_mod*(256-speed.lenmod_factor) + 256*speed.lenmod_factor)/256;
  793. else if (frames[frameix-1].frflags & FRFLAG_LEN_MOD2) // reduce effect of length mod, used for the start of a vowel
  794. length_factor = (length_mod*(256-speed.lenmod2_factor) + 256*speed.lenmod2_factor)/256;
  795. frame_length = frames[frameix-1].length;
  796. len = (frame_length * samplerate)/1000;
  797. len = (len * length_factor)/256;
  798. length_sum += len;
  799. frame_lengths[frameix] = len;
  800. }
  801. if ((length_sum > 0) && (length_sum < length_min)) {
  802. // lengthen, so that the sequence is greater than one cycle at low pitch
  803. for (frameix = 1; frameix < n_frames; frameix++)
  804. frame_lengths[frameix] = (frame_lengths[frameix] * length_min) / length_sum;
  805. }
  806. for (frameix = 1; frameix < n_frames; frameix++) {
  807. frame2 = frames[frameix].frame;
  808. if ((fmt_params->wav_addr != 0) && ((frame1->frflags & FRFLAG_DEFER_WAV) == 0)) {
  809. // there is a wave file to play along with this synthesis
  810. seq_len_adjust = 0;
  811. DoSample2(fmt_params->wav_addr, which+0x100, 0, fmt_params->fmt_control, 0, wavefile_amp);
  812. wave_flag = 1;
  813. wavefile_ix = 0;
  814. fmt_params->wav_addr = 0;
  815. }
  816. if (modulation >= 0) {
  817. if (frame1->frflags & FRFLAG_MODULATE)
  818. modulation = 6;
  819. if ((frameix == n_frames-1) && (modn_flags & 0xf00))
  820. modulation |= modn_flags; // before or after a glottal stop
  821. }
  822. len = frame_lengths[frameix];
  823. pitch_length += len;
  824. amp_length += len;
  825. if (len == 0) {
  826. last_frame = NULL;
  827. frame1 = frame2;
  828. } else {
  829. last_wcmdq = wcmdq_tail;
  830. if (modulation >= 0) {
  831. q = wcmdq[wcmdq_tail];
  832. q[0] = wcmd_spect;
  833. q[1] = len + (modulation << 16);
  834. q[2] = (intptr_t)frame1;
  835. q[3] = (intptr_t)frame2;
  836. WcmdqInc();
  837. }
  838. last_frame = frame1 = frame2;
  839. total_len += len;
  840. }
  841. }
  842. if ((which != 1) && (fmt_amplitude != 0)) {
  843. q = wcmdq[wcmdq_tail];
  844. q[0] = WCMD_FMT_AMPLITUDE;
  845. q[1] = fmt_amplitude = 0;
  846. WcmdqInc();
  847. }
  848. return total_len;
  849. }
  850. void DoMarker(int type, int char_posn, int length, int value)
  851. {
  852. // This could be used to return an index to the word currently being spoken
  853. // Type 1=word, 2=sentence, 3=named marker, 4=play audio, 5=end
  854. if (WcmdqFree() > 5) {
  855. wcmdq[wcmdq_tail][0] = WCMD_MARKER + (type << 8);
  856. wcmdq[wcmdq_tail][1] = (char_posn & 0xffffff) | (length << 24);
  857. wcmdq[wcmdq_tail][2] = value;
  858. WcmdqInc();
  859. }
  860. }
  861. void DoPhonemeMarker(int type, int char_posn, int length, char *name)
  862. {
  863. // This could be used to return an index to the word currently being spoken
  864. // Type 7=phoneme
  865. int *p;
  866. if (WcmdqFree() > 5) {
  867. wcmdq[wcmdq_tail][0] = WCMD_MARKER + (type << 8);
  868. wcmdq[wcmdq_tail][1] = (char_posn & 0xffffff) | (length << 24);
  869. p = (int *)name;
  870. wcmdq[wcmdq_tail][2] = p[0]; // up to 8 bytes of UTF8 characters
  871. wcmdq[wcmdq_tail][3] = p[1];
  872. WcmdqInc();
  873. }
  874. }
  875. #if HAVE_SONIC_H
  876. void DoSonicSpeed(int value)
  877. {
  878. // value, multiplier * 1024
  879. wcmdq[wcmdq_tail][0] = WCMD_SONIC_SPEED;
  880. wcmdq[wcmdq_tail][1] = value;
  881. WcmdqInc();
  882. }
  883. #endif
  884. espeak_ng_STATUS DoVoiceChange(voice_t *v)
  885. {
  886. // allocate memory for a copy of the voice data, and free it in wavegenfill()
  887. voice_t *v2;
  888. if ((v2 = (voice_t *)malloc(sizeof(voice_t))) == NULL)
  889. return ENOMEM;
  890. memcpy(v2, v, sizeof(voice_t));
  891. wcmdq[wcmdq_tail][0] = WCMD_VOICE;
  892. wcmdq[wcmdq_tail][2] = (intptr_t)v2;
  893. WcmdqInc();
  894. return ENS_OK;
  895. }
  896. void DoEmbedded(int *embix, int sourceix)
  897. {
  898. // There were embedded commands in the text at this point
  899. unsigned int word; // bit 7=last command for this word, bits 5,6 sign, bits 0-4 command
  900. unsigned int value;
  901. int command;
  902. do {
  903. word = embedded_list[*embix];
  904. value = word >> 8;
  905. command = word & 0x7f;
  906. if (command == 0)
  907. return; // error
  908. (*embix)++;
  909. switch (command & 0x1f)
  910. {
  911. case EMBED_S: // speed
  912. SetEmbedded((command & 0x60) + EMBED_S2, value); // adjusts embedded_value[EMBED_S2]
  913. SetSpeed(2);
  914. break;
  915. case EMBED_I: // play dynamically loaded wav data (sound icon)
  916. if ((int)value < n_soundicon_tab) {
  917. if (soundicon_tab[value].length != 0) {
  918. DoPause(10, 0); // ensure a break in the speech
  919. wcmdq[wcmdq_tail][0] = WCMD_WAVE;
  920. wcmdq[wcmdq_tail][1] = soundicon_tab[value].length;
  921. wcmdq[wcmdq_tail][2] = (intptr_t)soundicon_tab[value].data + 44; // skip WAV header
  922. wcmdq[wcmdq_tail][3] = 0x1500; // 16 bit data, amp=21
  923. WcmdqInc();
  924. }
  925. }
  926. break;
  927. case EMBED_M: // named marker
  928. DoMarker(espeakEVENT_MARK, (sourceix & 0x7ff) + clause_start_char, 0, value);
  929. break;
  930. case EMBED_U: // play sound
  931. DoMarker(espeakEVENT_PLAY, count_characters+1, 0, value); // always occurs at end of clause
  932. break;
  933. default:
  934. DoPause(10, 0); // ensure a break in the speech
  935. wcmdq[wcmdq_tail][0] = WCMD_EMBEDDED;
  936. wcmdq[wcmdq_tail][1] = command;
  937. wcmdq[wcmdq_tail][2] = value;
  938. WcmdqInc();
  939. break;
  940. }
  941. } while ((word & 0x80) == 0);
  942. }
  943. int Generate(PHONEME_LIST *phoneme_list, int *n_ph, bool resume)
  944. {
  945. static int ix;
  946. static int embedded_ix;
  947. static int word_count;
  948. PHONEME_LIST *prev;
  949. PHONEME_LIST *next;
  950. PHONEME_LIST *next2;
  951. PHONEME_LIST *p;
  952. bool released;
  953. int stress;
  954. int modulation;
  955. bool pre_voiced;
  956. int free_min;
  957. int value;
  958. unsigned char *pitch_env = NULL;
  959. unsigned char *amp_env;
  960. PHONEME_TAB *ph;
  961. int use_ipa = 0;
  962. bool done_phoneme_marker;
  963. int vowelstart_prev;
  964. char phoneme_name[16];
  965. static int sourceix = 0;
  966. PHONEME_DATA phdata;
  967. PHONEME_DATA phdata_prev;
  968. PHONEME_DATA phdata_next;
  969. PHONEME_DATA phdata_tone;
  970. FMT_PARAMS fmtp;
  971. static WORD_PH_DATA worddata;
  972. if (option_phoneme_events & espeakINITIALIZE_PHONEME_IPA)
  973. use_ipa = 1;
  974. if (mbrola_name[0] != 0)
  975. return MbrolaGenerate(phoneme_list, n_ph, resume);
  976. if (resume == false) {
  977. ix = 1;
  978. embedded_ix = 0;
  979. word_count = 0;
  980. pitch_length = 0;
  981. amp_length = 0;
  982. last_frame = NULL;
  983. last_wcmdq = -1;
  984. syllable_start = wcmdq_tail;
  985. syllable_end = wcmdq_tail;
  986. syllable_centre = -1;
  987. last_pitch_cmd = -1;
  988. memset(vowel_transition, 0, sizeof(vowel_transition));
  989. memset(&worddata, 0, sizeof(worddata));
  990. DoPause(0, 0); // isolate from the previous clause
  991. }
  992. while ((ix < (*n_ph)) && (ix < N_PHONEME_LIST-2)) {
  993. p = &phoneme_list[ix];
  994. if (p->type == phPAUSE)
  995. free_min = 10;
  996. else if (p->type != phVOWEL)
  997. free_min = 15; // we need less Q space for non-vowels, and we need to generate phonemes after a vowel so that the pitch_length is filled in
  998. else
  999. free_min = MIN_WCMDQ;
  1000. if (WcmdqFree() <= free_min)
  1001. return 1; // wait
  1002. prev = &phoneme_list[ix-1];
  1003. next = &phoneme_list[ix+1];
  1004. next2 = &phoneme_list[ix+2];
  1005. if (p->synthflags & SFLAG_EMBEDDED)
  1006. DoEmbedded(&embedded_ix, p->sourceix);
  1007. if (p->newword) {
  1008. if (((p->type == phVOWEL) && (translator->langopts.param[LOPT_WORD_MERGE] & 1)) ||
  1009. (p->ph->phflags & phNOPAUSE)) {
  1010. } else
  1011. last_frame = NULL;
  1012. sourceix = (p->sourceix & 0x7ff) + clause_start_char;
  1013. if (p->newword & 4)
  1014. DoMarker(espeakEVENT_SENTENCE, sourceix, 0, count_sentences); // start of sentence
  1015. if (p->newword & 1)
  1016. DoMarker(espeakEVENT_WORD, sourceix, p->sourceix >> 11, clause_start_word + word_count++); // NOTE, this count doesn't include multiple-word pronunciations in *_list. eg (of a)
  1017. }
  1018. EndAmplitude();
  1019. if ((p->prepause > 0) && !(p->ph->phflags & phPREVOICE))
  1020. DoPause(p->prepause, 1);
  1021. done_phoneme_marker = false;
  1022. if (option_phoneme_events && (p->ph->code != phonEND_WORD)) {
  1023. if ((p->type == phVOWEL) && (prev->type == phLIQUID || prev->type == phNASAL)) {
  1024. // For vowels following a liquid or nasal, do the phoneme event after the vowel-start
  1025. } else {
  1026. WritePhMnemonic(phoneme_name, p->ph, p, use_ipa, NULL);
  1027. DoPhonemeMarker(espeakEVENT_PHONEME, sourceix, 0, phoneme_name);
  1028. done_phoneme_marker = true;
  1029. }
  1030. }
  1031. switch (p->type)
  1032. {
  1033. case phPAUSE:
  1034. DoPause(p->length, 0);
  1035. p->std_length = p->ph->std_length;
  1036. break;
  1037. case phSTOP:
  1038. released = false;
  1039. ph = p->ph;
  1040. if (next->type == phVOWEL)
  1041. released = true;
  1042. else if (!next->newword) {
  1043. if (next->type == phLIQUID) released = true;
  1044. }
  1045. if (released == false)
  1046. p->synthflags |= SFLAG_NEXT_PAUSE;
  1047. if (ph->phflags & phPREVOICE) {
  1048. // a period of voicing before the release
  1049. memset(&fmtp, 0, sizeof(fmtp));
  1050. InterpretPhoneme(NULL, 0x01, p, &phdata, &worddata);
  1051. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1052. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1053. if (last_pitch_cmd < 0) {
  1054. DoAmplitude(next->amp, NULL);
  1055. DoPitch(envelope_data[p->env], next->pitch1, next->pitch2);
  1056. }
  1057. DoSpect2(ph, 0, &fmtp, p, 0);
  1058. }
  1059. InterpretPhoneme(NULL, 0, p, &phdata, &worddata);
  1060. phdata.pd_control |= pd_DONTLENGTHEN;
  1061. DoSample3(&phdata, 0, 0);
  1062. break;
  1063. case phFRICATIVE:
  1064. InterpretPhoneme(NULL, 0, p, &phdata, &worddata);
  1065. if (p->synthflags & SFLAG_LENGTHEN)
  1066. DoSample3(&phdata, p->length, 0); // play it twice for [s:] etc.
  1067. DoSample3(&phdata, p->length, 0);
  1068. break;
  1069. case phVSTOP:
  1070. ph = p->ph;
  1071. memset(&fmtp, 0, sizeof(fmtp));
  1072. fmtp.fmt_control = pd_DONTLENGTHEN;
  1073. pre_voiced = false;
  1074. if (next->type == phVOWEL) {
  1075. DoAmplitude(p->amp, NULL);
  1076. DoPitch(envelope_data[p->env], p->pitch1, p->pitch2);
  1077. pre_voiced = true;
  1078. } else if ((next->type == phLIQUID) && !next->newword) {
  1079. DoAmplitude(next->amp, NULL);
  1080. DoPitch(envelope_data[next->env], next->pitch1, next->pitch2);
  1081. pre_voiced = true;
  1082. } else {
  1083. if (last_pitch_cmd < 0) {
  1084. DoAmplitude(next->amp, NULL);
  1085. DoPitch(envelope_data[p->env], p->pitch1, p->pitch2);
  1086. }
  1087. }
  1088. if ((prev->type == phVOWEL) || (ph->phflags & phPREVOICE)) {
  1089. // a period of voicing before the release
  1090. InterpretPhoneme(NULL, 0x01, p, &phdata, &worddata);
  1091. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1092. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1093. DoSpect2(ph, 0, &fmtp, p, 0);
  1094. if (p->synthflags & SFLAG_LENGTHEN) {
  1095. DoPause(25, 1);
  1096. DoSpect2(ph, 0, &fmtp, p, 0);
  1097. }
  1098. } else {
  1099. if (p->synthflags & SFLAG_LENGTHEN)
  1100. DoPause(50, 0);
  1101. }
  1102. if (pre_voiced) {
  1103. // followed by a vowel, or liquid + vowel
  1104. StartSyllable();
  1105. } else
  1106. p->synthflags |= SFLAG_NEXT_PAUSE;
  1107. InterpretPhoneme(NULL, 0, p, &phdata, &worddata);
  1108. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1109. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1110. fmtp.wav_addr = phdata.sound_addr[pd_ADDWAV];
  1111. fmtp.wav_amp = phdata.sound_param[pd_ADDWAV];
  1112. DoSpect2(ph, 0, &fmtp, p, 0);
  1113. if ((p->newword == 0) && (next2->newword == 0)) {
  1114. if (next->type == phVFRICATIVE)
  1115. DoPause(20, 0);
  1116. if (next->type == phFRICATIVE)
  1117. DoPause(12, 0);
  1118. }
  1119. break;
  1120. case phVFRICATIVE:
  1121. if (next->type == phVOWEL) {
  1122. DoAmplitude(p->amp, NULL);
  1123. DoPitch(envelope_data[p->env], p->pitch1, p->pitch2);
  1124. } else if (next->type == phLIQUID) {
  1125. DoAmplitude(next->amp, NULL);
  1126. DoPitch(envelope_data[next->env], next->pitch1, next->pitch2);
  1127. } else {
  1128. if (last_pitch_cmd < 0) {
  1129. DoAmplitude(p->amp, NULL);
  1130. DoPitch(envelope_data[p->env], p->pitch1, p->pitch2);
  1131. }
  1132. }
  1133. if ((next->type == phVOWEL) || ((next->type == phLIQUID) && (next->newword == 0))) // ?? test 14.Aug.2007
  1134. StartSyllable();
  1135. else
  1136. p->synthflags |= SFLAG_NEXT_PAUSE;
  1137. InterpretPhoneme(NULL, 0, p, &phdata, &worddata);
  1138. memset(&fmtp, 0, sizeof(fmtp));
  1139. fmtp.std_length = phdata.pd_param[i_SET_LENGTH]*2;
  1140. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1141. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1142. fmtp.wav_addr = phdata.sound_addr[pd_ADDWAV];
  1143. fmtp.wav_amp = phdata.sound_param[pd_ADDWAV];
  1144. if (p->synthflags & SFLAG_LENGTHEN)
  1145. DoSpect2(p->ph, 0, &fmtp, p, 0);
  1146. DoSpect2(p->ph, 0, &fmtp, p, 0);
  1147. break;
  1148. case phNASAL:
  1149. memset(&fmtp, 0, sizeof(fmtp));
  1150. if (!(p->synthflags & SFLAG_SEQCONTINUE)) {
  1151. DoAmplitude(p->amp, NULL);
  1152. DoPitch(envelope_data[p->env], p->pitch1, p->pitch2);
  1153. }
  1154. if (prev->type == phNASAL)
  1155. last_frame = NULL;
  1156. InterpretPhoneme(NULL, 0, p, &phdata, &worddata);
  1157. fmtp.std_length = phdata.pd_param[i_SET_LENGTH]*2;
  1158. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1159. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1160. if (next->type == phVOWEL) {
  1161. StartSyllable();
  1162. DoSpect2(p->ph, 0, &fmtp, p, 0);
  1163. } else if (prev->type == phVOWEL && (p->synthflags & SFLAG_SEQCONTINUE))
  1164. DoSpect2(p->ph, 0, &fmtp, p, 0);
  1165. else {
  1166. last_frame = NULL; // only for nasal ?
  1167. DoSpect2(p->ph, 0, &fmtp, p, 0);
  1168. last_frame = NULL;
  1169. }
  1170. break;
  1171. case phLIQUID:
  1172. memset(&fmtp, 0, sizeof(fmtp));
  1173. modulation = 0;
  1174. if (p->ph->phflags & phTRILL)
  1175. modulation = 5;
  1176. if (!(p->synthflags & SFLAG_SEQCONTINUE)) {
  1177. DoAmplitude(p->amp, NULL);
  1178. DoPitch(envelope_data[p->env], p->pitch1, p->pitch2);
  1179. }
  1180. if (prev->type == phNASAL)
  1181. last_frame = NULL;
  1182. if (next->type == phVOWEL)
  1183. StartSyllable();
  1184. InterpretPhoneme(NULL, 0, p, &phdata, &worddata);
  1185. if ((value = (phdata.pd_param[i_PAUSE_BEFORE] - p->prepause)) > 0)
  1186. DoPause(value, 1);
  1187. fmtp.std_length = phdata.pd_param[i_SET_LENGTH]*2;
  1188. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1189. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1190. fmtp.wav_addr = phdata.sound_addr[pd_ADDWAV];
  1191. fmtp.wav_amp = phdata.sound_param[pd_ADDWAV];
  1192. DoSpect2(p->ph, 0, &fmtp, p, modulation);
  1193. break;
  1194. case phVOWEL:
  1195. ph = p->ph;
  1196. stress = p->stresslevel & 0xf;
  1197. memset(&fmtp, 0, sizeof(fmtp));
  1198. InterpretPhoneme(NULL, 0, p, &phdata, &worddata);
  1199. fmtp.std_length = phdata.pd_param[i_SET_LENGTH] * 2;
  1200. vowelstart_prev = 0;
  1201. if (((fmtp.fmt_addr = phdata.sound_addr[pd_VWLSTART]) != 0) && ((phdata.pd_control & pd_FORNEXTPH) == 0)) {
  1202. // a vowel start has been specified by the Vowel program
  1203. fmtp.fmt_length = phdata.sound_param[pd_VWLSTART];
  1204. } else if (prev->type != phPAUSE) {
  1205. // check the previous phoneme
  1206. InterpretPhoneme(NULL, 0, prev, &phdata_prev, NULL);
  1207. if (((fmtp.fmt_addr = phdata_prev.sound_addr[pd_VWLSTART]) != 0) && (phdata_prev.pd_control & pd_FORNEXTPH)) {
  1208. // a vowel start has been specified by the previous phoneme
  1209. vowelstart_prev = 1;
  1210. fmtp.fmt2_lenadj = phdata_prev.sound_param[pd_VWLSTART];
  1211. }
  1212. fmtp.transition0 = phdata_prev.vowel_transition[0];
  1213. fmtp.transition1 = phdata_prev.vowel_transition[1];
  1214. }
  1215. if (fmtp.fmt_addr == 0) {
  1216. // use the default start for this vowel
  1217. fmtp.use_vowelin = 1;
  1218. fmtp.fmt_control = 1;
  1219. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1220. }
  1221. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1222. pitch_env = envelope_data[p->env];
  1223. amp_env = NULL;
  1224. if (p->tone_ph != 0) {
  1225. InterpretPhoneme2(p->tone_ph, &phdata_tone);
  1226. pitch_env = GetEnvelope(phdata_tone.pitch_env);
  1227. if (phdata_tone.amp_env > 0)
  1228. amp_env = GetEnvelope(phdata_tone.amp_env);
  1229. }
  1230. StartSyllable();
  1231. modulation = 2;
  1232. if (stress <= 1)
  1233. modulation = 1; // 16ths
  1234. else if (stress >= 7)
  1235. modulation = 3;
  1236. if (prev->type == phVSTOP || prev->type == phVFRICATIVE) {
  1237. DoAmplitude(p->amp, amp_env);
  1238. DoPitch(pitch_env, p->pitch1, p->pitch2); // don't use prevocalic rising tone
  1239. DoSpect2(ph, 1, &fmtp, p, modulation);
  1240. } else if (prev->type == phLIQUID || prev->type == phNASAL) {
  1241. DoAmplitude(p->amp, amp_env);
  1242. DoSpect2(ph, 1, &fmtp, p, modulation); // continue with pre-vocalic rising tone
  1243. DoPitch(pitch_env, p->pitch1, p->pitch2);
  1244. } else if (vowelstart_prev) {
  1245. // VowelStart from the previous phoneme, but not phLIQUID or phNASAL
  1246. DoPitch(envelope_data[PITCHrise], p->pitch2 - 15, p->pitch2);
  1247. DoAmplitude(p->amp-1, amp_env);
  1248. DoSpect2(ph, 1, &fmtp, p, modulation); // continue with pre-vocalic rising tone
  1249. DoPitch(pitch_env, p->pitch1, p->pitch2);
  1250. } else {
  1251. if (!(p->synthflags & SFLAG_SEQCONTINUE)) {
  1252. DoAmplitude(p->amp, amp_env);
  1253. DoPitch(pitch_env, p->pitch1, p->pitch2);
  1254. }
  1255. DoSpect2(ph, 1, &fmtp, p, modulation);
  1256. }
  1257. if ((option_phoneme_events) && (done_phoneme_marker == false)) {
  1258. WritePhMnemonic(phoneme_name, p->ph, p, use_ipa, NULL);
  1259. DoPhonemeMarker(espeakEVENT_PHONEME, sourceix, 0, phoneme_name);
  1260. }
  1261. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1262. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1263. fmtp.transition0 = 0;
  1264. fmtp.transition1 = 0;
  1265. if ((fmtp.fmt2_addr = phdata.sound_addr[pd_VWLEND]) != 0)
  1266. fmtp.fmt2_lenadj = phdata.sound_param[pd_VWLEND];
  1267. else if (next->type != phPAUSE) {
  1268. fmtp.fmt2_lenadj = 0;
  1269. InterpretPhoneme(NULL, 0, next, &phdata_next, NULL);
  1270. fmtp.use_vowelin = 1;
  1271. fmtp.transition0 = phdata_next.vowel_transition[2]; // always do vowel_transition, even if ph_VWLEND ?? consider [N]
  1272. fmtp.transition1 = phdata_next.vowel_transition[3];
  1273. if ((fmtp.fmt2_addr = phdata_next.sound_addr[pd_VWLEND]) != 0)
  1274. fmtp.fmt2_lenadj = phdata_next.sound_param[pd_VWLEND];
  1275. }
  1276. DoSpect2(ph, 2, &fmtp, p, modulation);
  1277. break;
  1278. }
  1279. ix++;
  1280. }
  1281. EndPitch(1);
  1282. if (*n_ph > 0) {
  1283. DoMarker(espeakEVENT_END, count_characters, 0, count_sentences); // end of clause
  1284. *n_ph = 0;
  1285. }
  1286. return 0; // finished the phoneme list
  1287. }
  1288. int SpeakNextClause(int control)
  1289. {
  1290. // Speak text from memory (text_in)
  1291. // control 0: start
  1292. // text_in is set
  1293. // The other calls have text_in = NULL
  1294. // control 1: speak next text
  1295. // 2: stop
  1296. int clause_tone;
  1297. char *voice_change;
  1298. const char *phon_out;
  1299. if (control == 2) {
  1300. // stop speaking
  1301. n_phoneme_list = 0;
  1302. WcmdqStop();
  1303. return 0;
  1304. }
  1305. if (text_decoder_eof(p_decoder)) {
  1306. skipping_text = false;
  1307. return 0;
  1308. }
  1309. if (current_phoneme_table != voice->phoneme_tab_ix)
  1310. SelectPhonemeTable(voice->phoneme_tab_ix);
  1311. // read the next clause from the input text file, translate it, and generate
  1312. // entries in the wavegen command queue
  1313. TranslateClause(translator, &clause_tone, &voice_change);
  1314. CalcPitches(translator, clause_tone);
  1315. CalcLengths(translator);
  1316. if ((option_phonemes & 0xf) || (phoneme_callback != NULL)) {
  1317. phon_out = GetTranslatedPhonemeString(option_phonemes);
  1318. if (option_phonemes & 0xf)
  1319. fprintf(f_trans, "%s\n", phon_out);
  1320. if (phoneme_callback != NULL)
  1321. phoneme_callback(phon_out);
  1322. }
  1323. if (skipping_text) {
  1324. n_phoneme_list = 0;
  1325. return 1;
  1326. }
  1327. Generate(phoneme_list, &n_phoneme_list, 0);
  1328. if (voice_change != NULL) {
  1329. // voice change at the end of the clause (i.e. clause was terminated by a voice change)
  1330. new_voice = LoadVoiceVariant(voice_change, 0); // add a Voice instruction to wavegen at the end of the clause
  1331. }
  1332. if (new_voice) {
  1333. // finished the current clause, now change the voice if there was an embedded
  1334. // change voice command at the end of it (i.e. clause was broken at the change voice command)
  1335. DoVoiceChange(voice);
  1336. new_voice = NULL;
  1337. }
  1338. return 1;
  1339. }