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

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