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

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