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

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