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

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