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.

setlengths.c 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /*
  2. * Copyright (C) 2005 to 2011 by Jonathan Duddington
  3. * email: [email protected]
  4. * Copyright (C) 2015-2016 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 <errno.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <wctype.h>
  24. #if HAVE_STDINT_H
  25. #include <stdint.h>
  26. #endif
  27. #include <espeak-ng/espeak_ng.h>
  28. #include <espeak/speak_lib.h>
  29. #include "speech.h"
  30. #include "phoneme.h"
  31. #include "synthesize.h"
  32. #include "voice.h"
  33. #include "translate.h"
  34. extern int GetAmplitude(void);
  35. extern void DoSonicSpeed(int value);
  36. extern int saved_parameters[];
  37. // convert from words-per-minute to internal speed factor
  38. // Use this to calibrate speed for wpm 80-350
  39. static unsigned char speed_lookup[] = {
  40. 255, 255, 255, 255, 255, // 80
  41. 253, 249, 245, 242, 238, // 85
  42. 235, 232, 228, 225, 222, // 90
  43. 218, 216, 213, 210, 207, // 95
  44. 204, 201, 198, 196, 193, // 100
  45. 191, 188, 186, 183, 181, // 105
  46. 179, 176, 174, 172, 169, // 110
  47. 168, 165, 163, 161, 159, // 115
  48. 158, 155, 153, 152, 150, // 120
  49. 148, 146, 145, 143, 141, // 125
  50. 139, 137, 136, 135, 133, // 130
  51. 131, 130, 129, 127, 126, // 135
  52. 124, 123, 122, 120, 119, // 140
  53. 118, 117, 115, 114, 113, // 145
  54. 112, 111, 110, 109, 107, // 150
  55. 106, 105, 104, 103, 102, // 155
  56. 101, 100, 99, 98, 97, // 160
  57. 96, 95, 94, 93, 92, // 165
  58. 91, 90, 89, 89, 88, // 170
  59. 87, 86, 85, 84, 83, // 175
  60. 82, 82, 81, 80, 80, // 180
  61. 79, 78, 77, 76, 76, // 185
  62. 75, 75, 74, 73, 72, // 190
  63. 71, 71, 70, 69, 69, // 195
  64. 68, 67, 67, 66, 66, // 200
  65. 65, 64, 64, 63, 62, // 205
  66. 62, 61, 61, 60, 59, // 210
  67. 59, 58, 58, 57, 57, // 215
  68. 56, 56, 55, 54, 54, // 220
  69. 53, 53, 52, 52, 52, // 225
  70. 51, 50, 50, 49, 49, // 230
  71. 48, 48, 47, 47, 46, // 235
  72. 46, 46, 45, 45, 44, // 240
  73. 44, 44, 43, 43, 42, // 245
  74. 41, 40, 40, 40, 39, // 250
  75. 39, 39, 38, 38, 38, // 255
  76. 37, 37, 37, 36, 36, // 260
  77. 35, 35, 35, 35, 34, // 265
  78. 34, 34, 33, 33, 33, // 270
  79. 32, 32, 31, 31, 31, // 275
  80. 30, 30, 30, 29, 29, // 280
  81. 29, 29, 28, 28, 27, // 285
  82. 27, 27, 27, 26, 26, // 290
  83. 26, 26, 25, 25, 25, // 295
  84. 24, 24, 24, 24, 23, // 300
  85. 23, 23, 23, 22, 22, // 305
  86. 22, 21, 21, 21, 21, // 310
  87. 20, 20, 20, 20, 19, // 315
  88. 19, 19, 18, 18, 17, // 320
  89. 17, 17, 16, 16, 16, // 325
  90. 16, 16, 16, 15, 15, // 330
  91. 15, 15, 14, 14, 14, // 335
  92. 13, 13, 13, 12, 12, // 340
  93. 12, 12, 11, 11, 11, // 345
  94. 11, 10, 10, 10, 9, // 350
  95. 9, 9, 8, 8, 8, // 355
  96. };
  97. // speed_factor1 adjustments for speeds 350 to 374: pauses
  98. static unsigned char pause_factor_350[] = {
  99. 22, 22, 22, 22, 22, 22, 22, 21, 21, 21, // 350
  100. 21, 20, 20, 19, 19, 18, 17, 16, 15, 15, // 360
  101. 15, 15, 15, 15, 15 // 370
  102. };
  103. // wav_factor adjustments for speeds 350 to 450
  104. // Use this to calibrate speed for wpm 350-450
  105. static unsigned char wav_factor_350[] = {
  106. 120, 121, 120, 119, 119, // 350
  107. 118, 118, 117, 116, 116, // 355
  108. 115, 114, 113, 112, 112, // 360
  109. 111, 111, 110, 109, 108, // 365
  110. 107, 106, 106, 104, 103, // 370
  111. 103, 102, 102, 102, 101, // 375
  112. 101, 99, 98, 98, 97, // 380
  113. 96, 96, 95, 94, 93, // 385
  114. 91, 90, 91, 90, 89, // 390
  115. 88, 86, 85, 86, 85, // 395
  116. 85, 84, 82, 81, 80, // 400
  117. 79, 77, 78, 78, 76, // 405
  118. 77, 75, 75, 74, 73, // 410
  119. 71, 72, 70, 69, 69, // 415
  120. 69, 67, 65, 64, 63, // 420
  121. 63, 63, 61, 61, 59, // 425
  122. 59, 59, 58, 56, 57, // 430
  123. 58, 56, 54, 53, 52, // 435
  124. 52, 53, 52, 52, 50, // 440
  125. 48, 47, 47, 45, 46, // 445
  126. 45 // 450
  127. };
  128. static int speed1 = 130;
  129. static int speed2 = 121;
  130. static int speed3 = 118;
  131. #if HAVE_SONIC_H
  132. void SetSpeed(int control)
  133. {
  134. int x;
  135. int s1;
  136. int wpm;
  137. int wpm2;
  138. int wpm_value;
  139. double sonic;
  140. speed.loud_consonants = 0;
  141. speed.min_sample_len = 450;
  142. speed.lenmod_factor = 110; // controls the effect of FRFLAG_LEN_MOD reduce length change
  143. speed.lenmod2_factor = 100;
  144. speed.min_pause = 5;
  145. wpm = embedded_value[EMBED_S];
  146. if (control == 2)
  147. wpm = embedded_value[EMBED_S2];
  148. wpm_value = wpm;
  149. if (voice->speed_percent > 0)
  150. wpm = (wpm * voice->speed_percent)/100;
  151. if (control & 2)
  152. DoSonicSpeed(1 * 1024);
  153. if ((wpm_value >= 450) || ((wpm_value > speed.fast_settings[0]) && (wpm > 350))) {
  154. wpm2 = wpm;
  155. wpm = 175;
  156. // set special eSpeak speed parameters for Sonic use
  157. // The eSpeak output will be speeded up by at least x2
  158. x = 73;
  159. if (control & 1) {
  160. speed1 = (x * voice->speedf1)/256;
  161. speed2 = (x * voice->speedf2)/256;
  162. speed3 = (x * voice->speedf3)/256;
  163. }
  164. if (control & 2) {
  165. sonic = ((double)wpm2)/wpm;
  166. DoSonicSpeed((int)(sonic * 1024));
  167. speed.pause_factor = 85;
  168. speed.clause_pause_factor = 80;
  169. speed.min_pause = 22;
  170. speed.min_sample_len = 450*2;
  171. speed.wav_factor = 211;
  172. speed.lenmod_factor = 210;
  173. speed.lenmod2_factor = 170;
  174. }
  175. return;
  176. }
  177. if (wpm > 450)
  178. wpm = 450;
  179. if (wpm > 360)
  180. speed.loud_consonants = (wpm - 360) / 8;
  181. wpm2 = wpm;
  182. if (wpm > 359) wpm2 = 359;
  183. if (wpm < 80) wpm2 = 80;
  184. x = speed_lookup[wpm2-80];
  185. if (wpm >= 380)
  186. x = 7;
  187. if (wpm >= 400)
  188. x = 6;
  189. if (control & 1) {
  190. // set speed factors for different syllable positions within a word
  191. // these are used in CalcLengths()
  192. speed1 = (x * voice->speedf1)/256;
  193. speed2 = (x * voice->speedf2)/256;
  194. speed3 = (x * voice->speedf3)/256;
  195. if (x <= 7) {
  196. speed1 = x;
  197. speed2 = speed3 = x - 1;
  198. }
  199. }
  200. if (control & 2) {
  201. // these are used in synthesis file
  202. if (wpm > 350) {
  203. speed.lenmod_factor = 85 - (wpm - 350) / 3;
  204. speed.lenmod2_factor = 60 - (wpm - 350) / 8;
  205. } else if (wpm > 250) {
  206. speed.lenmod_factor = 110 - (wpm - 250)/4;
  207. speed.lenmod2_factor = 110 - (wpm - 250)/2;
  208. }
  209. s1 = (x * voice->speedf1)/256;
  210. if (wpm >= 170)
  211. speed.wav_factor = 110 + (150*s1)/128; // reduced speed adjustment, used for playing recorded sounds
  212. else
  213. speed.wav_factor = 128 + (128*s1)/130; // = 215 at 170 wpm
  214. if (wpm >= 350)
  215. speed.wav_factor = wav_factor_350[wpm-350];
  216. if (wpm >= 390) {
  217. speed.min_sample_len = 450 - (wpm - 400)/2;
  218. if (wpm > 440)
  219. speed.min_sample_len = 420 - (wpm - 440);
  220. }
  221. // adjust for different sample rates
  222. speed.min_sample_len = (speed.min_sample_len * samplerate_native) / 22050;
  223. speed.pause_factor = (256 * s1)/115; // full speed adjustment, used for pause length
  224. speed.clause_pause_factor = 0;
  225. if (wpm > 430)
  226. speed.pause_factor = 12;
  227. else if (wpm > 400)
  228. speed.pause_factor = 13;
  229. else if (wpm > 374)
  230. speed.pause_factor = 14;
  231. else if (wpm > 350)
  232. speed.pause_factor = pause_factor_350[wpm - 350];
  233. if (speed.clause_pause_factor == 0) {
  234. // restrict the reduction of pauses between clauses
  235. if ((speed.clause_pause_factor = speed.pause_factor) < 16)
  236. speed.clause_pause_factor = 16;
  237. }
  238. }
  239. }
  240. #else
  241. void SetSpeed(int control)
  242. {
  243. // This is the earlier version of SetSpeed() before sonic speed-up was added
  244. int x;
  245. int s1;
  246. int wpm;
  247. int wpm2;
  248. speed.loud_consonants = 0;
  249. speed.min_sample_len = 450;
  250. speed.lenmod_factor = 110; // controls the effect of FRFLAG_LEN_MOD reduce length change
  251. speed.lenmod2_factor = 100;
  252. wpm = embedded_value[EMBED_S];
  253. if (control == 2)
  254. wpm = embedded_value[EMBED_S2];
  255. if (voice->speed_percent > 0)
  256. wpm = (wpm * voice->speed_percent)/100;
  257. if (wpm > 450)
  258. wpm = 450;
  259. if (wpm > 360)
  260. speed.loud_consonants = (wpm - 360) / 8;
  261. wpm2 = wpm;
  262. if (wpm > 359) wpm2 = 359;
  263. if (wpm < 80) wpm2 = 80;
  264. x = speed_lookup[wpm2-80];
  265. if (wpm >= 380)
  266. x = 7;
  267. if (wpm >= 400)
  268. x = 6;
  269. if (control & 1) {
  270. // set speed factors for different syllable positions within a word
  271. // these are used in CalcLengths()
  272. speed1 = (x * voice->speedf1)/256;
  273. speed2 = (x * voice->speedf2)/256;
  274. speed3 = (x * voice->speedf3)/256;
  275. if (x <= 7) {
  276. speed1 = x;
  277. speed2 = speed3 = x - 1;
  278. }
  279. }
  280. if (control & 2) {
  281. // these are used in synthesis file
  282. if (wpm > 350) {
  283. speed.lenmod_factor = 85 - (wpm - 350) / 3;
  284. speed.lenmod2_factor = 60 - (wpm - 350) / 8;
  285. } else if (wpm > 250) {
  286. speed.lenmod_factor = 110 - (wpm - 250)/4;
  287. speed.lenmod2_factor = 110 - (wpm - 250)/2;
  288. }
  289. s1 = (x * voice->speedf1)/256;
  290. if (wpm >= 170)
  291. speed.wav_factor = 110 + (150*s1)/128; // reduced speed adjustment, used for playing recorded sounds
  292. else
  293. speed.wav_factor = 128 + (128*s1)/130; // = 215 at 170 wpm
  294. if (wpm >= 350)
  295. speed.wav_factor = wav_factor_350[wpm-350];
  296. if (wpm >= 390) {
  297. speed.min_sample_len = 450 - (wpm - 400)/2;
  298. if (wpm > 440)
  299. speed.min_sample_len = 420 - (wpm - 440);
  300. }
  301. speed.pause_factor = (256 * s1)/115; // full speed adjustment, used for pause length
  302. speed.clause_pause_factor = 0;
  303. if (wpm > 430)
  304. speed.pause_factor = 12;
  305. else if (wpm > 400)
  306. speed.pause_factor = 13;
  307. else if (wpm > 374)
  308. speed.pause_factor = 14;
  309. else if (wpm > 350)
  310. speed.pause_factor = pause_factor_350[wpm - 350];
  311. if (speed.clause_pause_factor == 0) {
  312. // restrict the reduction of pauses between clauses
  313. if ((speed.clause_pause_factor = speed.pause_factor) < 16)
  314. speed.clause_pause_factor = 16;
  315. }
  316. }
  317. }
  318. #endif
  319. espeak_ng_STATUS SetParameter(int parameter, int value, int relative)
  320. {
  321. // parameter: reset-all, amp, pitch, speed, linelength, expression, capitals, number grouping
  322. // relative 0=absolute 1=relative
  323. int new_value = value;
  324. int default_value;
  325. if (relative) {
  326. if (parameter < 5) {
  327. default_value = param_defaults[parameter];
  328. new_value = default_value + (default_value * value)/100;
  329. }
  330. }
  331. param_stack[0].parameter[parameter] = new_value;
  332. saved_parameters[parameter] = new_value;
  333. switch (parameter)
  334. {
  335. case espeakRATE:
  336. embedded_value[EMBED_S] = new_value;
  337. embedded_value[EMBED_S2] = new_value;
  338. SetSpeed(3);
  339. break;
  340. case espeakVOLUME:
  341. embedded_value[EMBED_A] = new_value;
  342. GetAmplitude();
  343. break;
  344. case espeakPITCH:
  345. if (new_value > 99) new_value = 99;
  346. if (new_value < 0) new_value = 0;
  347. embedded_value[EMBED_P] = new_value;
  348. break;
  349. case espeakRANGE:
  350. if (new_value > 99) new_value = 99;
  351. embedded_value[EMBED_R] = new_value;
  352. break;
  353. case espeakLINELENGTH:
  354. option_linelength = new_value;
  355. break;
  356. case espeakWORDGAP:
  357. option_wordgap = new_value;
  358. break;
  359. case espeakINTONATION:
  360. if ((new_value & 0xff) != 0)
  361. translator->langopts.intonation_group = new_value & 0xff;
  362. option_tone_flags = new_value;
  363. break;
  364. default:
  365. return EINVAL;
  366. }
  367. return ENS_OK;
  368. }
  369. static void DoEmbedded2(int *embix)
  370. {
  371. // There were embedded commands in the text at this point
  372. unsigned int word;
  373. do {
  374. word = embedded_list[(*embix)++];
  375. if ((word & 0x1f) == EMBED_S) {
  376. // speed
  377. SetEmbedded(word & 0x7f, word >> 8); // adjusts embedded_value[EMBED_S]
  378. SetSpeed(1);
  379. }
  380. } while ((word & 0x80) == 0);
  381. }
  382. void CalcLengths(Translator *tr)
  383. {
  384. int ix;
  385. int ix2;
  386. PHONEME_LIST *prev;
  387. PHONEME_LIST *next;
  388. PHONEME_LIST *next2;
  389. PHONEME_LIST *next3;
  390. PHONEME_LIST *p;
  391. PHONEME_LIST *p2;
  392. int stress;
  393. int type;
  394. static int more_syllables = 0;
  395. int pre_sonorant = 0;
  396. int pre_voiced = 0;
  397. int last_pitch = 0;
  398. int pitch_start;
  399. int length_mod;
  400. int next2type;
  401. int len;
  402. int env2;
  403. int end_of_clause;
  404. int embedded_ix = 0;
  405. int min_drop;
  406. int pitch1;
  407. int emphasized;
  408. int tone_mod;
  409. unsigned char *pitch_env = NULL;
  410. PHONEME_DATA phdata_tone;
  411. for (ix = 1; ix < n_phoneme_list; ix++) {
  412. prev = &phoneme_list[ix-1];
  413. p = &phoneme_list[ix];
  414. stress = p->stresslevel & 0x7;
  415. emphasized = p->stresslevel & 0x8;
  416. next = &phoneme_list[ix+1];
  417. if (p->synthflags & SFLAG_EMBEDDED)
  418. DoEmbedded2(&embedded_ix);
  419. type = p->type;
  420. if (p->synthflags & SFLAG_SYLLABLE)
  421. type = phVOWEL;
  422. switch (type)
  423. {
  424. case phPAUSE:
  425. last_pitch = 0;
  426. break;
  427. case phSTOP:
  428. last_pitch = 0;
  429. if (prev->type == phFRICATIVE)
  430. p->prepause = 25;
  431. else if ((more_syllables > 0) || (stress < 4))
  432. p->prepause = 48;
  433. else
  434. p->prepause = 60;
  435. if (prev->type == phSTOP)
  436. p->prepause = 60;
  437. if ((tr->langopts.word_gap & 0x10) && (p->newword))
  438. p->prepause = 60;
  439. if (p->ph->phflags & phLENGTHENSTOP)
  440. p->prepause += 30;
  441. if (p->synthflags & SFLAG_LENGTHEN)
  442. p->prepause += tr->langopts.long_stop;
  443. break;
  444. case phVFRICATIVE:
  445. case phFRICATIVE:
  446. if (p->newword) {
  447. if ((prev->type == phVOWEL) && (p->ph->phflags & phNOPAUSE)) {
  448. } else
  449. p->prepause = 15;
  450. }
  451. if (next->type == phPAUSE && prev->type == phNASAL && !(p->ph->phflags&phFORTIS))
  452. p->prepause = 25;
  453. if (prev->ph->phflags & phBRKAFTER)
  454. p->prepause = 30;
  455. if ((tr->langopts.word_gap & 0x10) && (p->newword))
  456. p->prepause = 30;
  457. if ((p->ph->phflags & phSIBILANT) && next->type == phSTOP && !next->newword) {
  458. if (prev->type == phVOWEL)
  459. p->length = 200; // ?? should do this if it's from a prefix
  460. else
  461. p->length = 150;
  462. } else
  463. p->length = 256;
  464. if (type == phVFRICATIVE) {
  465. if (next->type == phVOWEL)
  466. pre_voiced = 1;
  467. if ((prev->type == phVOWEL) || (prev->type == phLIQUID))
  468. p->length = (255 + prev->length)/2;
  469. }
  470. break;
  471. case phVSTOP:
  472. if (prev->type == phVFRICATIVE || prev->type == phFRICATIVE || (prev->ph->phflags & phSIBILANT) || (prev->type == phLIQUID))
  473. p->prepause = 30;
  474. if (next->type == phVOWEL || next->type == phLIQUID) {
  475. if ((next->type == phVOWEL) || !next->newword)
  476. pre_voiced = 1;
  477. p->prepause = 40;
  478. if (prev->type == phVOWEL) {
  479. p->prepause = 0; // use murmur instead to link from the preceding vowel
  480. } else if (prev->type == phPAUSE) {
  481. // reduce by the length of the preceding pause
  482. if (prev->length < p->prepause)
  483. p->prepause -= prev->length;
  484. else
  485. p->prepause = 0;
  486. } else if (p->newword == 0) {
  487. if (prev->type == phLIQUID)
  488. p->prepause = 20;
  489. if (prev->type == phNASAL)
  490. p->prepause = 12;
  491. if (prev->type == phSTOP && !(prev->ph->phflags & phFORTIS))
  492. p->prepause = 0;
  493. }
  494. }
  495. if ((tr->langopts.word_gap & 0x10) && (p->newword) && (p->prepause < 20))
  496. p->prepause = 20;
  497. break;
  498. case phLIQUID:
  499. case phNASAL:
  500. p->amp = tr->stress_amps[0]; // unless changed later
  501. p->length = 256; // TEMPORARY
  502. min_drop = 0;
  503. if (p->newword) {
  504. if (prev->type == phLIQUID)
  505. p->prepause = 25;
  506. if (prev->type == phVOWEL) {
  507. if (!(p->ph->phflags & phNOPAUSE))
  508. p->prepause = 12;
  509. }
  510. }
  511. if (next->type == phVOWEL)
  512. pre_sonorant = 1;
  513. else {
  514. p->pitch2 = last_pitch;
  515. if ((prev->type == phVOWEL) || (prev->type == phLIQUID)) {
  516. p->length = prev->length;
  517. if (p->type == phLIQUID)
  518. p->length = speed1;
  519. if (next->type == phVSTOP)
  520. p->length = (p->length * 160)/100;
  521. if (next->type == phVFRICATIVE)
  522. p->length = (p->length * 120)/100;
  523. } else {
  524. for (ix2 = ix; ix2 < n_phoneme_list; ix2++) {
  525. if (phoneme_list[ix2].type == phVOWEL) {
  526. p->pitch2 = phoneme_list[ix2].pitch2;
  527. break;
  528. }
  529. }
  530. }
  531. p->pitch1 = p->pitch2-16;
  532. if (p->pitch2 < 16)
  533. p->pitch1 = 0;
  534. p->env = PITCHfall;
  535. pre_voiced = 0;
  536. }
  537. break;
  538. case phVOWEL:
  539. min_drop = 0;
  540. next2 = &phoneme_list[ix+2];
  541. next3 = &phoneme_list[ix+3];
  542. if (stress > 7) stress = 7;
  543. if (stress <= 1)
  544. stress = stress ^ 1; // swap diminished and unstressed (until we swap stress_amps,stress_lengths in tr_languages)
  545. if (pre_sonorant)
  546. p->amp = tr->stress_amps[stress]-1;
  547. else
  548. p->amp = tr->stress_amps[stress];
  549. if (emphasized)
  550. p->amp = 25;
  551. if (ix >= (n_phoneme_list-3)) {
  552. // last phoneme of a clause, limit its amplitude
  553. if (p->amp > tr->langopts.param[LOPT_MAXAMP_EOC])
  554. p->amp = tr->langopts.param[LOPT_MAXAMP_EOC];
  555. }
  556. // is the last syllable of a word ?
  557. more_syllables = 0;
  558. end_of_clause = 0;
  559. for (p2 = p+1; p2->newword == 0; p2++) {
  560. if ((p2->type == phVOWEL) && !(p2->ph->phflags & phNONSYLLABIC))
  561. more_syllables++;
  562. if (p2->ph->code == phonPAUSE_CLAUSE)
  563. end_of_clause = 2;
  564. }
  565. if (p2->ph->code == phonPAUSE_CLAUSE)
  566. end_of_clause = 2;
  567. if ((p2->newword & 2) && (more_syllables == 0))
  568. end_of_clause = 2;
  569. // calc length modifier
  570. if ((next->ph->code == phonPAUSE_VSHORT) && (next2->type == phPAUSE)) {
  571. // if PAUSE_VSHORT is followed by a pause, then use that
  572. next = next2;
  573. next2 = next3;
  574. next3 = &phoneme_list[ix+4];
  575. }
  576. next2type = next2->ph->length_mod;
  577. if (more_syllables == 0) {
  578. if (next->newword || next2->newword) {
  579. // don't use 2nd phoneme over a word boundary, unless it's a pause
  580. if (next2type != 1)
  581. next2type = 0;
  582. }
  583. len = tr->langopts.length_mods0[next2type *10+ next->ph->length_mod];
  584. if ((next->newword) && (tr->langopts.word_gap & 0x20)) {
  585. // consider as a pause + first phoneme of the next word
  586. length_mod = (len + tr->langopts.length_mods0[next->ph->length_mod *10+ 1])/2;
  587. } else
  588. length_mod = len;
  589. } else {
  590. length_mod = tr->langopts.length_mods[next2type *10+ next->ph->length_mod];
  591. if ((next->type == phNASAL) && (next2->type == phSTOP || next2->type == phVSTOP) && (next3->ph->phflags & phFORTIS))
  592. length_mod -= 15;
  593. }
  594. if (more_syllables == 0)
  595. length_mod *= speed1;
  596. else if (more_syllables == 1)
  597. length_mod *= speed2;
  598. else
  599. length_mod *= speed3;
  600. length_mod = length_mod / 128;
  601. if (length_mod < 8)
  602. length_mod = 8; // restrict how much lengths can be reduced
  603. if (stress >= 7) {
  604. // tonic syllable, include a constant component so it doesn't decrease directly with speed
  605. length_mod += tr->langopts.lengthen_tonic;
  606. if (emphasized)
  607. length_mod += (tr->langopts.lengthen_tonic/2);
  608. } else if (emphasized)
  609. length_mod += tr->langopts.lengthen_tonic;
  610. if ((len = tr->stress_lengths[stress]) == 0)
  611. len = tr->stress_lengths[6];
  612. length_mod = length_mod * len;
  613. if (p->tone_ph != 0) {
  614. if ((tone_mod = phoneme_tab[p->tone_ph]->std_length) > 0) {
  615. // a tone phoneme specifies a percentage change to the length
  616. length_mod = (length_mod * tone_mod) / 100;
  617. }
  618. }
  619. if ((end_of_clause == 2) && !(tr->langopts.stress_flags & S_NO_EOC_LENGTHEN)) {
  620. // this is the last syllable in the clause, lengthen it - more for short vowels
  621. len = (p->ph->std_length * 2);
  622. if (tr->langopts.stress_flags & S_EO_CLAUSE1)
  623. len = 200; // don't lengthen short vowels more than long vowels at end-of-clause
  624. length_mod = length_mod * (256 + (280 - len)/3)/256;
  625. }
  626. if (length_mod > tr->langopts.max_lengthmod*speed1) {
  627. // limit the vowel length adjustment for some languages
  628. length_mod = (tr->langopts.max_lengthmod*speed1);
  629. }
  630. length_mod = length_mod / 128;
  631. if (p->type != phVOWEL) {
  632. length_mod = 256; // syllabic consonant
  633. min_drop = 16;
  634. }
  635. p->length = length_mod;
  636. if (p->env >= (N_ENVELOPE_DATA-1)) {
  637. fprintf(stderr, "espeak: Bad intonation data\n");
  638. p->env = 0;
  639. }
  640. // pre-vocalic part
  641. // set last-pitch
  642. env2 = p->env + 1; // version for use with preceding semi-vowel
  643. if (p->tone_ph != 0) {
  644. InterpretPhoneme2(p->tone_ph, &phdata_tone);
  645. pitch_env = GetEnvelope(phdata_tone.pitch_env);
  646. } else
  647. pitch_env = envelope_data[env2];
  648. pitch_start = p->pitch1 + ((p->pitch2-p->pitch1)*pitch_env[0])/256;
  649. if (pre_sonorant || pre_voiced) {
  650. // set pitch for pre-vocalic part
  651. if (pitch_start == 255)
  652. last_pitch = pitch_start; // pitch is not set
  653. if (pitch_start - last_pitch > 16)
  654. last_pitch = pitch_start - 16;
  655. prev->pitch1 = last_pitch;
  656. prev->pitch2 = pitch_start;
  657. if (last_pitch < pitch_start) {
  658. prev->env = PITCHrise;
  659. p->env = env2;
  660. } else
  661. prev->env = PITCHfall;
  662. prev->length = length_mod;
  663. prev->amp = p->amp;
  664. if ((prev->type != phLIQUID) && (prev->amp > 18))
  665. prev->amp = 18;
  666. }
  667. // vowel & post-vocalic part
  668. next->synthflags &= ~SFLAG_SEQCONTINUE;
  669. if (next->type == phNASAL && next2->type != phVOWEL)
  670. next->synthflags |= SFLAG_SEQCONTINUE;
  671. if (next->type == phLIQUID) {
  672. next->synthflags |= SFLAG_SEQCONTINUE;
  673. if (next2->type == phVOWEL)
  674. next->synthflags &= ~SFLAG_SEQCONTINUE;
  675. if (next2->type != phVOWEL) {
  676. if (next->ph->mnemonic == ('/'*256+'r'))
  677. next->synthflags &= ~SFLAG_SEQCONTINUE;
  678. }
  679. }
  680. if ((min_drop > 0) && ((p->pitch2 - p->pitch1) < min_drop)) {
  681. pitch1 = p->pitch2 - min_drop;
  682. if (pitch1 < 0)
  683. pitch1 = 0;
  684. p->pitch1 = pitch1;
  685. }
  686. last_pitch = p->pitch1 + ((p->pitch2-p->pitch1)*envelope_data[p->env][127])/256;
  687. pre_sonorant = 0;
  688. pre_voiced = 0;
  689. break;
  690. }
  691. }
  692. }