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

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