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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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. speed.pause_factor = (256 * s1)/115; // full speed adjustment, used for pause length
  243. speed.clause_pause_factor = 0;
  244. if(wpm > 430)
  245. {
  246. speed.pause_factor = 12;
  247. // speed.clause_pause_factor = 15;
  248. }
  249. else
  250. if(wpm > 400)
  251. {
  252. speed.pause_factor = 13;
  253. // speed.clause_pause_factor = 15;
  254. }
  255. else
  256. if(wpm > 374)
  257. {
  258. speed.pause_factor = 14;
  259. }
  260. else
  261. if(wpm > 350)
  262. {
  263. speed.pause_factor = pause_factor_350[wpm - 350];
  264. }
  265. if(speed.clause_pause_factor == 0)
  266. {
  267. // restrict the reduction of pauses between clauses
  268. if((speed.clause_pause_factor = speed.pause_factor) < 16)
  269. speed.clause_pause_factor = 16;
  270. }
  271. }
  272. #ifdef TEST_SPEED
  273. //if(control==3)
  274. 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);
  275. #endif
  276. } // end of SetSpeed
  277. #else // not using sonic speed-up
  278. void SetSpeed(int control)
  279. {//=======================
  280. // This is the earlier version of SetSpeed() before sonic speed-up was added
  281. int x;
  282. int s1;
  283. int wpm;
  284. int wpm2;
  285. speed.loud_consonants = 0;
  286. speed.min_sample_len = 450;
  287. speed.lenmod_factor = 110; // controls the effect of FRFLAG_LEN_MOD reduce length change
  288. speed.lenmod2_factor = 100;
  289. wpm = embedded_value[EMBED_S];
  290. if(control == 2)
  291. wpm = embedded_value[EMBED_S2];
  292. #ifdef TEST_SPEED
  293. if(wpm > 1000)
  294. {
  295. // TESTING
  296. test = wpm / 1000;
  297. wpm = wpm % 1000;
  298. }
  299. #endif
  300. if(voice->speed_percent > 0)
  301. {
  302. wpm = (wpm * voice->speed_percent)/100;
  303. }
  304. if(wpm > 450)
  305. wpm = 450;
  306. if(wpm > 360)
  307. {
  308. speed.loud_consonants = (wpm - 360) / 8;
  309. }
  310. wpm2 = wpm;
  311. if(wpm > 359) wpm2 = 359;
  312. if(wpm < 80) wpm2 = 80;
  313. x = speed_lookup[wpm2-80];
  314. if(wpm >= 380)
  315. x = 7;
  316. if(wpm >= 400)
  317. x = 6;
  318. if(control & 1)
  319. {
  320. // set speed factors for different syllable positions within a word
  321. // these are used in CalcLengths()
  322. speed1 = (x * voice->speedf1)/256;
  323. speed2 = (x * voice->speedf2)/256;
  324. speed3 = (x * voice->speedf3)/256;
  325. if(x <= 7)
  326. {
  327. speed1 = x;
  328. speed2 = speed3 = x - 1;
  329. }
  330. }
  331. if(control & 2)
  332. {
  333. // these are used in synthesis file
  334. if(wpm > 350)
  335. {
  336. speed.lenmod_factor = 85 - (wpm - 350) / 3;
  337. speed.lenmod2_factor = 60 - (wpm - 350) / 8;
  338. }
  339. else
  340. if(wpm > 250)
  341. {
  342. speed.lenmod_factor = 110 - (wpm - 250)/4;
  343. speed.lenmod2_factor = 110 - (wpm - 250)/2;
  344. }
  345. s1 = (x * voice->speedf1)/256;
  346. if(wpm >= 170)
  347. speed.wav_factor = 110 + (150*s1)/128; // reduced speed adjustment, used for playing recorded sounds
  348. else
  349. speed.wav_factor = 128 + (128*s1)/130; // = 215 at 170 wpm
  350. if(wpm >= 350)
  351. {
  352. speed.wav_factor = wav_factor_350[wpm-350];
  353. }
  354. if(wpm >= 390)
  355. {
  356. speed.min_sample_len = 450 - (wpm - 400)/2;
  357. if(wpm > 440)
  358. speed.min_sample_len = 420 - (wpm - 440);
  359. }
  360. speed.pause_factor = (256 * s1)/115; // full speed adjustment, used for pause length
  361. speed.clause_pause_factor = 0;
  362. if(wpm > 430)
  363. {
  364. speed.pause_factor = 12;
  365. // speed.clause_pause_factor = 15;
  366. }
  367. else
  368. if(wpm > 400)
  369. {
  370. speed.pause_factor = 13;
  371. // speed.clause_pause_factor = 15;
  372. }
  373. else
  374. if(wpm > 374)
  375. {
  376. speed.pause_factor = 14;
  377. }
  378. else
  379. if(wpm > 350)
  380. {
  381. speed.pause_factor = pause_factor_350[wpm - 350];
  382. }
  383. if(speed.clause_pause_factor == 0)
  384. {
  385. // restrict the reduction of pauses between clauses
  386. if((speed.clause_pause_factor = speed.pause_factor) < 16)
  387. speed.clause_pause_factor = 16;
  388. }
  389. }
  390. #ifdef TEST_SPEED
  391. //if(control==3)
  392. 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);
  393. #endif
  394. } // end of SetSpeed
  395. #endif // of INCLUDE_SONIC
  396. #ifdef deleted
  397. void SetAmplitude(int amp)
  398. {//=======================
  399. 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 };
  400. if((amp >= 0) && (amp <= 20))
  401. {
  402. option_amplitude = (amplitude_factor[amp] * 480)/256;
  403. }
  404. }
  405. #endif
  406. void SetParameter(int parameter, int value, int relative)
  407. {//======================================================
  408. // parameter: reset-all, amp, pitch, speed, linelength, expression, capitals, number grouping
  409. // relative 0=absolute 1=relative
  410. int new_value = value;
  411. int default_value;
  412. if(relative)
  413. {
  414. if(parameter < 5)
  415. {
  416. default_value = param_defaults[parameter];
  417. new_value = default_value + (default_value * value)/100;
  418. }
  419. }
  420. param_stack[0].parameter[parameter] = new_value;
  421. saved_parameters[parameter] = new_value;
  422. switch(parameter)
  423. {
  424. case espeakRATE:
  425. embedded_value[EMBED_S] = new_value;
  426. embedded_value[EMBED_S2] = new_value;
  427. SetSpeed(3);
  428. break;
  429. case espeakVOLUME:
  430. embedded_value[EMBED_A] = new_value;
  431. GetAmplitude();
  432. break;
  433. case espeakPITCH:
  434. if(new_value > 99) new_value = 99;
  435. if(new_value < 0) new_value = 0;
  436. embedded_value[EMBED_P] = new_value;
  437. break;
  438. case espeakRANGE:
  439. if(new_value > 99) new_value = 99;
  440. embedded_value[EMBED_R] = new_value;
  441. break;
  442. case espeakLINELENGTH:
  443. option_linelength = new_value;
  444. break;
  445. case espeakWORDGAP:
  446. option_wordgap = new_value;
  447. break;
  448. case espeakINTONATION:
  449. if((new_value & 0xff) != 0)
  450. translator->langopts.intonation_group = new_value & 0xff;
  451. option_tone_flags = new_value;
  452. break;
  453. default:
  454. break;
  455. }
  456. } // end of SetParameter
  457. static void DoEmbedded2(int *embix)
  458. {//================================
  459. // There were embedded commands in the text at this point
  460. unsigned int word;
  461. do {
  462. word = embedded_list[(*embix)++];
  463. if((word & 0x1f) == EMBED_S)
  464. {
  465. // speed
  466. SetEmbedded(word & 0x7f, word >> 8); // adjusts embedded_value[EMBED_S]
  467. SetSpeed(1);
  468. }
  469. } while((word & 0x80) == 0);
  470. }
  471. void CalcLengths(Translator *tr)
  472. {//==============================
  473. int ix;
  474. int ix2;
  475. PHONEME_LIST *prev;
  476. PHONEME_LIST *next;
  477. PHONEME_LIST *next2;
  478. PHONEME_LIST *next3;
  479. PHONEME_LIST *p;
  480. PHONEME_LIST *p2;
  481. int stress;
  482. int type;
  483. static int more_syllables=0;
  484. int pre_sonorant=0;
  485. int pre_voiced=0;
  486. int last_pitch = 0;
  487. int pitch_start;
  488. int length_mod;
  489. int len;
  490. int env2;
  491. int end_of_clause;
  492. int embedded_ix = 0;
  493. int min_drop;
  494. int pitch1;
  495. int emphasized;
  496. int tone_mod;
  497. unsigned char *pitch_env=NULL;
  498. PHONEME_DATA phdata_tone;
  499. for(ix=1; ix<n_phoneme_list; ix++)
  500. {
  501. prev = &phoneme_list[ix-1];
  502. p = &phoneme_list[ix];
  503. stress = p->stresslevel & 0x7;
  504. emphasized = p->stresslevel & 0x8;
  505. next = &phoneme_list[ix+1];
  506. if(p->synthflags & SFLAG_EMBEDDED)
  507. {
  508. DoEmbedded2(&embedded_ix);
  509. }
  510. type = p->type;
  511. if(p->synthflags & SFLAG_SYLLABLE)
  512. type = phVOWEL;
  513. switch(type)
  514. {
  515. case phPAUSE:
  516. last_pitch = 0;
  517. break;
  518. case phSTOP:
  519. last_pitch = 0;
  520. if(prev->type == phFRICATIVE)
  521. p->prepause = 25;
  522. else
  523. if((more_syllables > 0) || (stress < 4))
  524. p->prepause = 48;
  525. else
  526. p->prepause = 60;
  527. if(prev->type == phSTOP)
  528. p->prepause = 60;
  529. if((tr->langopts.word_gap & 0x10) && (p->newword))
  530. p->prepause = 60;
  531. if(p->ph->phflags & phLENGTHENSTOP)
  532. p->prepause += 30;
  533. if(p->synthflags & SFLAG_LENGTHEN)
  534. p->prepause += tr->langopts.long_stop;
  535. break;
  536. case phVFRICATIVE:
  537. case phFRICATIVE:
  538. if(p->newword)
  539. {
  540. if((prev->type == phVOWEL) && (p->ph->phflags & phNOPAUSE))
  541. {
  542. }
  543. else
  544. {
  545. p->prepause = 15;
  546. }
  547. }
  548. if(next->type==phPAUSE && prev->type==phNASAL && !(p->ph->phflags&phFORTIS))
  549. p->prepause = 25;
  550. if(prev->ph->phflags & phBRKAFTER)
  551. p->prepause = 30;
  552. if((tr->langopts.word_gap & 0x10) && (p->newword))
  553. p->prepause = 30;
  554. if((p->ph->phflags & phSIBILANT) && next->type==phSTOP && !next->newword)
  555. {
  556. if(prev->type == phVOWEL)
  557. p->length = 200; // ?? should do this if it's from a prefix
  558. else
  559. p->length = 150;
  560. }
  561. else
  562. p->length = 256;
  563. if(type == phVFRICATIVE)
  564. {
  565. if(next->type==phVOWEL)
  566. {
  567. pre_voiced = 1;
  568. }
  569. if((prev->type==phVOWEL) || (prev->type == phLIQUID))
  570. {
  571. p->length = (255 + prev->length)/2;
  572. }
  573. }
  574. break;
  575. case phVSTOP:
  576. if(prev->type==phVFRICATIVE || prev->type==phFRICATIVE || (prev->ph->phflags & phSIBILANT) || (prev->type == phLIQUID))
  577. p->prepause = 30;
  578. if(next->type==phVOWEL || next->type==phLIQUID)
  579. {
  580. if((next->type==phVOWEL) || !next->newword)
  581. pre_voiced = 1;
  582. p->prepause = 40;
  583. if((prev->type == phPAUSE) || (prev->type == phVOWEL)) // || (prev->ph->mnemonic == ('/'*256+'r')))
  584. p->prepause = 0;
  585. else
  586. if(p->newword==0)
  587. {
  588. if(prev->type==phLIQUID)
  589. p->prepause = 20;
  590. if(prev->type==phNASAL)
  591. p->prepause = 12;
  592. if(prev->type==phSTOP && !(prev->ph->phflags & phFORTIS))
  593. p->prepause = 0;
  594. }
  595. }
  596. if((tr->langopts.word_gap & 0x10) && (p->newword) && (p->prepause < 20))
  597. p->prepause = 20;
  598. break;
  599. case phLIQUID:
  600. case phNASAL:
  601. p->amp = tr->stress_amps[0]; // unless changed later
  602. p->length = 256; // TEMPORARY
  603. min_drop = 0;
  604. if(p->newword)
  605. {
  606. if(prev->type==phLIQUID)
  607. p->prepause = 25;
  608. if(prev->type==phVOWEL)
  609. {
  610. if(!(p->ph->phflags & phNOPAUSE))
  611. p->prepause = 12;
  612. }
  613. }
  614. if(next->type==phVOWEL)
  615. {
  616. pre_sonorant = 1;
  617. }
  618. else
  619. {
  620. p->pitch2 = last_pitch;
  621. if((prev->type==phVOWEL) || (prev->type == phLIQUID))
  622. {
  623. p->length = prev->length;
  624. if(p->type == phLIQUID)
  625. {
  626. p->length = speed1;
  627. }
  628. if(next->type == phVSTOP)
  629. {
  630. p->length = (p->length * 160)/100;
  631. }
  632. if(next->type == phVFRICATIVE)
  633. {
  634. p->length = (p->length * 120)/100;
  635. }
  636. }
  637. else
  638. {
  639. for(ix2=ix; ix2<n_phoneme_list; ix2++)
  640. {
  641. if(phoneme_list[ix2].type == phVOWEL)
  642. {
  643. p->pitch2 = phoneme_list[ix2].pitch2;
  644. break;
  645. }
  646. }
  647. }
  648. p->pitch1 = p->pitch2-16;
  649. if(p->pitch2 < 16)
  650. {
  651. p->pitch1 = 0;
  652. }
  653. p->env = PITCHfall;
  654. pre_voiced = 0;
  655. }
  656. break;
  657. case phVOWEL:
  658. min_drop = 0;
  659. next2 = &phoneme_list[ix+2];
  660. next3 = &phoneme_list[ix+3];
  661. if(stress > 7) stress = 7;
  662. if(stress <= 1)
  663. {
  664. stress = stress ^ 1; // swap diminished and unstressed (until we swap stress_amps,stress_lengths in tr_languages)
  665. }
  666. if(pre_sonorant)
  667. p->amp = tr->stress_amps[stress]-1;
  668. else
  669. p->amp = tr->stress_amps[stress];
  670. if(emphasized)
  671. p->amp = 25;
  672. if(ix >= (n_phoneme_list-3))
  673. {
  674. // last phoneme of a clause, limit its amplitude
  675. if(p->amp > tr->langopts.param[LOPT_MAXAMP_EOC])
  676. p->amp = tr->langopts.param[LOPT_MAXAMP_EOC];
  677. }
  678. // is the last syllable of a word ?
  679. more_syllables=0;
  680. end_of_clause = 0;
  681. for(p2 = p+1; p2->newword== 0; p2++)
  682. {
  683. if((p2->type == phVOWEL) && !(p2->ph->phflags & phNONSYLLABIC))
  684. more_syllables++;
  685. if(p2->ph->code == phonPAUSE_CLAUSE)
  686. end_of_clause = 2;
  687. }
  688. if(p2->ph->code == phonPAUSE_CLAUSE)
  689. end_of_clause = 2;
  690. if((p2->newword & 2) && (more_syllables==0))
  691. {
  692. end_of_clause = 2;
  693. }
  694. // calc length modifier
  695. if((next->ph->code == phonPAUSE_VSHORT) && (next2->type == phPAUSE))
  696. {
  697. // if PAUSE_VSHORT is followed by a pause, then use that
  698. next = next2;
  699. next2 = next3;
  700. next3 = &phoneme_list[ix+4];
  701. }
  702. if(more_syllables==0)
  703. {
  704. len = tr->langopts.length_mods0[next2->ph->length_mod *10+ next->ph->length_mod];
  705. if((next->newword) && (tr->langopts.word_gap & 0x20))
  706. {
  707. // consider as a pause + first phoneme of the next word
  708. length_mod = (len + tr->langopts.length_mods0[next->ph->length_mod *10+ 1])/2;
  709. }
  710. else
  711. length_mod = len;
  712. }
  713. else
  714. {
  715. length_mod = tr->langopts.length_mods[next2->ph->length_mod *10+ next->ph->length_mod];
  716. if((next->type == phNASAL) && (next2->type == phSTOP || next2->type == phVSTOP) && (next3->ph->phflags & phFORTIS))
  717. length_mod -= 15;
  718. }
  719. if(more_syllables==0)
  720. length_mod *= speed1;
  721. else
  722. if(more_syllables==1)
  723. length_mod *= speed2;
  724. else
  725. length_mod *= speed3;
  726. length_mod = length_mod / 128;
  727. if(length_mod < 8)
  728. length_mod = 8; // restrict how much lengths can be reduced
  729. if(stress >= 7)
  730. {
  731. // tonic syllable, include a constant component so it doesn't decrease directly with speed
  732. length_mod += tr->langopts.lengthen_tonic;
  733. if(emphasized)
  734. length_mod += (tr->langopts.lengthen_tonic/2);
  735. }
  736. else
  737. if(emphasized)
  738. {
  739. length_mod += tr->langopts.lengthen_tonic;
  740. }
  741. if((len = tr->stress_lengths[stress]) == 0)
  742. len = tr->stress_lengths[6];
  743. length_mod = length_mod * len;
  744. if(p->tone_ph != 0)
  745. {
  746. if((tone_mod = phoneme_tab[p->tone_ph]->std_length) > 0)
  747. {
  748. // a tone phoneme specifies a percentage change to the length
  749. length_mod = (length_mod * tone_mod) / 100;
  750. }
  751. }
  752. if((end_of_clause == 2) && !(tr->langopts.stress_flags & S_NO_EOC_LENGTHEN))
  753. {
  754. // this is the last syllable in the clause, lengthen it - more for short vowels
  755. len = (p->ph->std_length * 2);
  756. if(tr->langopts.stress_flags & 0x40000)
  757. len=200; // don't lengthen short vowels more than long vowels at end-of-clause
  758. length_mod = length_mod * (256 + (280 - len)/3)/256;
  759. }
  760. if(length_mod > tr->langopts.max_lengthmod*speed1)
  761. {
  762. //limit the vowel length adjustment for some languages
  763. length_mod = (tr->langopts.max_lengthmod*speed1);
  764. }
  765. length_mod = length_mod / 128;
  766. if(p->type != phVOWEL)
  767. {
  768. length_mod = 256; // syllabic consonant
  769. min_drop = 16;
  770. }
  771. p->length = length_mod;
  772. if(p->env >= (N_ENVELOPE_DATA-1))
  773. {
  774. fprintf(stderr,"espeak: Bad intonation data\n");
  775. p->env = 0;
  776. }
  777. // pre-vocalic part
  778. // set last-pitch
  779. env2 = p->env + 1; // version for use with preceding semi-vowel
  780. if(p->tone_ph != 0)
  781. {
  782. InterpretPhoneme2(p->tone_ph, &phdata_tone);
  783. pitch_env = GetEnvelope(phdata_tone.pitch_env);
  784. }
  785. else
  786. {
  787. pitch_env = envelope_data[env2];
  788. }
  789. pitch_start = p->pitch1 + ((p->pitch2-p->pitch1)*pitch_env[0])/256;
  790. if(pre_sonorant || pre_voiced)
  791. {
  792. // set pitch for pre-vocalic part
  793. if(pitch_start == 255)
  794. last_pitch = pitch_start; // pitch is not set
  795. if(pitch_start - last_pitch > 16)
  796. last_pitch = pitch_start - 16;
  797. prev->pitch1 = last_pitch;
  798. prev->pitch2 = pitch_start;
  799. if(last_pitch < pitch_start)
  800. {
  801. prev->env = PITCHrise;
  802. p->env = env2;
  803. }
  804. else
  805. {
  806. prev->env = PITCHfall;
  807. }
  808. prev->length = length_mod;
  809. prev->amp = p->amp;
  810. if((prev->type != phLIQUID) && (prev->amp > 18))
  811. prev->amp = 18;
  812. }
  813. // vowel & post-vocalic part
  814. next->synthflags &= ~SFLAG_SEQCONTINUE;
  815. if(next->type == phNASAL && next2->type != phVOWEL)
  816. next->synthflags |= SFLAG_SEQCONTINUE;
  817. if(next->type == phLIQUID)
  818. {
  819. next->synthflags |= SFLAG_SEQCONTINUE;
  820. if(next2->type == phVOWEL)
  821. {
  822. next->synthflags &= ~SFLAG_SEQCONTINUE;
  823. }
  824. if(next2->type != phVOWEL)
  825. {
  826. if(next->ph->mnemonic == ('/'*256+'r'))
  827. {
  828. next->synthflags &= ~SFLAG_SEQCONTINUE;
  829. // min_drop = 15;
  830. }
  831. }
  832. }
  833. if((min_drop > 0) && ((p->pitch2 - p->pitch1) < min_drop))
  834. {
  835. pitch1 = p->pitch2 - min_drop;
  836. if(pitch1 < 0)
  837. pitch1 = 0;
  838. p->pitch1 = pitch1;
  839. }
  840. last_pitch = p->pitch1 + ((p->pitch2-p->pitch1)*envelope_data[p->env][127])/256;
  841. pre_sonorant = 0;
  842. pre_voiced = 0;
  843. break;
  844. }
  845. }
  846. } // end of CalcLengths