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.cpp 20KB

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