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.

synthesize.c 41KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  1. /*
  2. * Copyright (C) 2005 to 2014 by Jonathan Duddington
  3. * email: [email protected]
  4. * Copyright (C) 2015-2017 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 <ctype.h>
  21. #include <errno.h>
  22. #include <math.h>
  23. #include <stdbool.h>
  24. #include <stdint.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <espeak-ng/espeak_ng.h>
  29. #include <espeak-ng/speak_lib.h>
  30. #include <espeak-ng/encoding.h>
  31. #include "synthesize.h"
  32. #include "dictionary.h" // for WritePhMnemonic, GetTranslatedPhone...
  33. #include "intonation.h" // for CalcPitches
  34. #include "mbrola.h" // for MbrolaGenerate, mbrola_name
  35. #include "phoneme.h" // for PHONEME_TAB, phVOWEL, phLIQUID, phN...
  36. #include "setlengths.h" // for CalcLengths
  37. #include "soundicon.h" // for soundicon_tab, n_soundicon
  38. #include "synthdata.h" // for InterpretPhoneme, GetEnvelope, Inte...
  39. #include "translate.h" // for translator, LANGUAGE_OPTIONS, Trans...
  40. #include "voice.h" // for voice_t, voice, LoadVoiceVariant
  41. #include "wavegen.h" // for WcmdqInc, WcmdqFree, WcmdqStop
  42. #include "speech.h" // for MAKE_MEM_UNDEFINED
  43. static void SmoothSpect(void);
  44. // list of phonemes in a clause
  45. int n_phoneme_list = 0;
  46. PHONEME_LIST phoneme_list[N_PHONEME_LIST+1];
  47. SPEED_FACTORS speed;
  48. static int last_pitch_cmd;
  49. static int last_amp_cmd;
  50. static frame_t *last_frame;
  51. static int last_wcmdq;
  52. static int pitch_length;
  53. static int amp_length;
  54. static int modn_flags;
  55. static int fmt_amplitude = 0;
  56. static int syllable_start;
  57. static int syllable_end;
  58. static int syllable_centre;
  59. static voice_t *new_voice = NULL;
  60. #define RMS_GLOTTAL1 35 // vowel before glottal stop
  61. #define RMS_START 28 // 28
  62. #define VOWEL_FRONT_LENGTH 50
  63. const char *WordToString(unsigned int word)
  64. {
  65. // Convert a phoneme mnemonic word into a string
  66. int ix;
  67. static char buf[5];
  68. MAKE_MEM_UNDEFINED(&buf, sizeof(buf));
  69. for (ix = 0; ix < 4; ix++)
  70. buf[ix] = word >> (ix*8);
  71. buf[4] = 0;
  72. return buf;
  73. }
  74. void SynthesizeInit()
  75. {
  76. last_pitch_cmd = 0;
  77. last_amp_cmd = 0;
  78. last_frame = NULL;
  79. syllable_centre = -1;
  80. }
  81. static void EndAmplitude(void)
  82. {
  83. if (amp_length > 0) {
  84. if (wcmdq[last_amp_cmd][1] == 0)
  85. wcmdq[last_amp_cmd][1] = amp_length;
  86. amp_length = 0;
  87. }
  88. }
  89. static void EndPitch(int voice_break)
  90. {
  91. // possible end of pitch envelope, fill in the length
  92. if ((pitch_length > 0) && (last_pitch_cmd >= 0)) {
  93. if (wcmdq[last_pitch_cmd][1] == 0)
  94. wcmdq[last_pitch_cmd][1] = pitch_length;
  95. pitch_length = 0;
  96. }
  97. if (voice_break) {
  98. last_wcmdq = -1;
  99. last_frame = NULL;
  100. syllable_end = wcmdq_tail;
  101. SmoothSpect();
  102. syllable_centre = -1;
  103. }
  104. }
  105. static void DoAmplitude(int amp, unsigned char *amp_env)
  106. {
  107. intptr_t *q;
  108. last_amp_cmd = wcmdq_tail;
  109. amp_length = 0; // total length of vowel with this amplitude envelope
  110. q = wcmdq[wcmdq_tail];
  111. q[0] = WCMD_AMPLITUDE;
  112. q[1] = 0; // fill in later from amp_length
  113. q[2] = (intptr_t)amp_env;
  114. q[3] = amp;
  115. WcmdqInc();
  116. }
  117. static void DoPhonemeAlignment(char* pho, int type)
  118. {
  119. wcmdq[wcmdq_tail][0] = WCMD_PHONEME_ALIGNMENT;
  120. wcmdq[wcmdq_tail][1] = (intptr_t)pho;
  121. wcmdq[wcmdq_tail][2] = type;
  122. WcmdqInc();
  123. }
  124. static void DoPitch(unsigned char *env, int pitch1, int pitch2)
  125. {
  126. intptr_t *q;
  127. EndPitch(0);
  128. if (pitch1 == 255) {
  129. // pitch was not set
  130. pitch1 = 55;
  131. pitch2 = 76;
  132. env = envelope_data[PITCHfall];
  133. }
  134. last_pitch_cmd = wcmdq_tail;
  135. pitch_length = 0; // total length of spect with this pitch envelope
  136. if (pitch2 < 0)
  137. pitch2 = 0;
  138. q = wcmdq[wcmdq_tail];
  139. q[0] = WCMD_PITCH;
  140. q[1] = 0; // length, fill in later from pitch_length
  141. q[2] = (intptr_t)env;
  142. q[3] = (pitch1 << 16) + pitch2;
  143. WcmdqInc();
  144. }
  145. int PauseLength(int pause, int control)
  146. {
  147. unsigned int len;
  148. if (control == 0) {
  149. if (pause >= 200)
  150. len = (pause * speed.clause_pause_factor)/256;
  151. else
  152. len = (pause * speed.pause_factor)/256;
  153. } else
  154. len = (pause * speed.wav_factor)/256;
  155. if (len < speed.min_pause)
  156. len = speed.min_pause; // mS, limit the amount to which pauses can be shortened
  157. return len;
  158. }
  159. static void DoPause(int length, int control)
  160. {
  161. // length in nominal mS
  162. // control = 1, less shortening at fast speeds
  163. unsigned int len;
  164. int srate2;
  165. if (length == 0)
  166. len = 0;
  167. else {
  168. len = PauseLength(length, control);
  169. if (len < 90000)
  170. len = (len * samplerate) / 1000; // convert from mS to number of samples
  171. else {
  172. srate2 = samplerate / 25; // avoid overflow
  173. len = (len * srate2) / 40;
  174. }
  175. }
  176. EndPitch(1);
  177. wcmdq[wcmdq_tail][0] = WCMD_PAUSE;
  178. wcmdq[wcmdq_tail][1] = len;
  179. WcmdqInc();
  180. last_frame = NULL;
  181. if (fmt_amplitude != 0) {
  182. wcmdq[wcmdq_tail][0] = WCMD_FMT_AMPLITUDE;
  183. wcmdq[wcmdq_tail][1] = fmt_amplitude = 0;
  184. WcmdqInc();
  185. }
  186. }
  187. extern int seq_len_adjust; // temporary fix to advance the start point for playing the wav sample
  188. static int DoSample2(int index, int which, int std_length, int control, int length_mod, int amp)
  189. {
  190. int length;
  191. int wav_length;
  192. int wav_scale;
  193. int min_length;
  194. int x;
  195. int len4;
  196. intptr_t *q;
  197. unsigned char *p;
  198. index = index & 0x7fffff;
  199. p = &wavefile_data[index];
  200. wav_scale = p[2];
  201. wav_length = (p[1] * 256);
  202. wav_length += p[0]; // length in bytes
  203. if (wav_length == 0)
  204. return 0;
  205. min_length = speed.min_sample_len;
  206. if (wav_scale == 0)
  207. min_length *= 2; // 16 bit samples
  208. if (std_length > 0) {
  209. std_length = (std_length * samplerate)/1000;
  210. if (wav_scale == 0)
  211. std_length *= 2;
  212. x = (min_length * std_length)/wav_length;
  213. if (x > min_length)
  214. min_length = x;
  215. } else {
  216. // no length specified, use the length of the stored sound
  217. std_length = wav_length;
  218. }
  219. if (length_mod > 0)
  220. std_length = (std_length * length_mod)/256;
  221. length = (std_length * speed.wav_factor)/256;
  222. if (control & pd_DONTLENGTHEN) {
  223. // this option is used for Stops, with short noise bursts.
  224. // Don't change their length much.
  225. if (length > std_length) {
  226. // don't let length exceed std_length
  227. length = std_length;
  228. }
  229. }
  230. if (length < min_length)
  231. length = min_length;
  232. if (wav_scale == 0) {
  233. // 16 bit samples
  234. length /= 2;
  235. wav_length /= 2;
  236. }
  237. if (amp < 0)
  238. return length;
  239. len4 = wav_length / 4;
  240. index += 4;
  241. if (which & 0x100) {
  242. // mix this with synthesised wave
  243. last_wcmdq = wcmdq_tail;
  244. q = wcmdq[wcmdq_tail];
  245. q[0] = WCMD_WAVE2;
  246. q[1] = length | (wav_length << 16); // length in samples
  247. q[2] = (intptr_t)(&wavefile_data[index]);
  248. q[3] = wav_scale + (amp << 8);
  249. WcmdqInc();
  250. return length;
  251. }
  252. if (length > wav_length) {
  253. x = len4*3;
  254. length -= x;
  255. } else {
  256. x = length;
  257. length = 0;
  258. }
  259. last_wcmdq = wcmdq_tail;
  260. q = wcmdq[wcmdq_tail];
  261. q[0] = WCMD_WAVE;
  262. q[1] = x; // length in samples
  263. q[2] = (intptr_t)(&wavefile_data[index]);
  264. q[3] = wav_scale + (amp << 8);
  265. WcmdqInc();
  266. while (length > len4*3) {
  267. x = len4;
  268. if (wav_scale == 0)
  269. x *= 2;
  270. last_wcmdq = wcmdq_tail;
  271. q = wcmdq[wcmdq_tail];
  272. q[0] = WCMD_WAVE;
  273. q[1] = len4*2; // length in samples
  274. q[2] = (intptr_t)(&wavefile_data[index+x]);
  275. q[3] = wav_scale + (amp << 8);
  276. WcmdqInc();
  277. length -= len4*2;
  278. }
  279. if (length > 0) {
  280. x = wav_length - length;
  281. if (wav_scale == 0)
  282. x *= 2;
  283. last_wcmdq = wcmdq_tail;
  284. q = wcmdq[wcmdq_tail];
  285. q[0] = WCMD_WAVE;
  286. q[1] = length; // length in samples
  287. q[2] = (intptr_t)(&wavefile_data[index+x]);
  288. q[3] = wav_scale + (amp << 8);
  289. WcmdqInc();
  290. }
  291. return length;
  292. }
  293. int DoSample3(PHONEME_DATA *phdata, int length_mod, int amp)
  294. {
  295. int amp2;
  296. int len;
  297. EndPitch(1);
  298. if (amp == -1) {
  299. // just get the length, don't produce sound
  300. amp2 = amp;
  301. } else {
  302. amp2 = phdata->sound_param[pd_WAV];
  303. if (amp2 == 0)
  304. amp2 = 100;
  305. amp2 = (amp2 * 32)/100;
  306. }
  307. seq_len_adjust = 0;
  308. if (phdata->sound_addr[pd_WAV] == 0)
  309. len = 0;
  310. else
  311. len = DoSample2(phdata->sound_addr[pd_WAV], 2, phdata->pd_param[pd_LENGTHMOD]*2, phdata->pd_control, length_mod, amp2);
  312. last_frame = NULL;
  313. return len;
  314. }
  315. static frame_t *AllocFrame()
  316. {
  317. // Allocate a temporary spectrum frame for the wavegen queue. Use a pool which is big
  318. // enough to use a round-robin without checks.
  319. // Only needed for modifying spectra for blending to consonants
  320. #define N_FRAME_POOL N_WCMDQ
  321. static int ix = 0;
  322. static frame_t frame_pool[N_FRAME_POOL];
  323. ix++;
  324. if (ix >= N_FRAME_POOL)
  325. ix = 0;
  326. MAKE_MEM_UNDEFINED(&frame_pool[ix], sizeof(frame_pool[ix]));
  327. return &frame_pool[ix];
  328. }
  329. static void set_frame_rms(frame_t *fr, int new_rms)
  330. {
  331. // Each frame includes its RMS amplitude value, so to set a new
  332. // RMS just adjust the formant amplitudes by the appropriate ratio
  333. int x;
  334. int h;
  335. int ix;
  336. static const short sqrt_tab[200] = {
  337. 0, 64, 90, 110, 128, 143, 156, 169, 181, 192, 202, 212, 221, 230, 239, 247,
  338. 256, 263, 271, 278, 286, 293, 300, 306, 313, 320, 326, 332, 338, 344, 350, 356,
  339. 362, 367, 373, 378, 384, 389, 394, 399, 404, 409, 414, 419, 424, 429, 434, 438,
  340. 443, 448, 452, 457, 461, 465, 470, 474, 478, 483, 487, 491, 495, 499, 503, 507,
  341. 512, 515, 519, 523, 527, 531, 535, 539, 543, 546, 550, 554, 557, 561, 565, 568,
  342. 572, 576, 579, 583, 586, 590, 593, 596, 600, 603, 607, 610, 613, 617, 620, 623,
  343. 627, 630, 633, 636, 640, 643, 646, 649, 652, 655, 658, 662, 665, 668, 671, 674,
  344. 677, 680, 683, 686, 689, 692, 695, 698, 701, 704, 706, 709, 712, 715, 718, 721,
  345. 724, 726, 729, 732, 735, 738, 740, 743, 746, 749, 751, 754, 757, 759, 762, 765,
  346. 768, 770, 773, 775, 778, 781, 783, 786, 789, 791, 794, 796, 799, 801, 804, 807,
  347. 809, 812, 814, 817, 819, 822, 824, 827, 829, 832, 834, 836, 839, 841, 844, 846,
  348. 849, 851, 853, 856, 858, 861, 863, 865, 868, 870, 872, 875, 877, 879, 882, 884,
  349. 886, 889, 891, 893, 896, 898, 900, 902
  350. };
  351. if (voice->klattv[0]) {
  352. if (new_rms == -1)
  353. fr->klattp[KLATT_AV] = 50;
  354. return;
  355. }
  356. if (fr->rms == 0) return; // check for divide by zero
  357. x = (new_rms * 64)/fr->rms;
  358. if (x >= 200) x = 199;
  359. x = sqrt_tab[x]; // sqrt(new_rms/fr->rms)*0x200;
  360. for (ix = 0; ix < 8; ix++) {
  361. h = fr->fheight[ix] * x;
  362. fr->fheight[ix] = h/0x200;
  363. }
  364. }
  365. static void formants_reduce_hf(frame_t *fr, int level)
  366. {
  367. // change height of peaks 2 to 8, percentage
  368. int ix;
  369. int x;
  370. if (voice->klattv[0])
  371. return;
  372. for (ix = 2; ix < 8; ix++) {
  373. x = fr->fheight[ix] * level;
  374. fr->fheight[ix] = x/100;
  375. }
  376. }
  377. static frame_t *CopyFrame(frame_t *frame1, int copy)
  378. {
  379. // create a copy of the specified frame in temporary buffer
  380. frame_t *frame2;
  381. if ((copy == 0) && (frame1->frflags & FRFLAG_COPIED)) {
  382. // this frame has already been copied in temporary rw memory
  383. return frame1;
  384. }
  385. frame2 = AllocFrame();
  386. if (frame2 != NULL) {
  387. memcpy(frame2, frame1, sizeof(frame_t));
  388. frame2->length = 0;
  389. frame2->frflags |= FRFLAG_COPIED;
  390. }
  391. return frame2;
  392. }
  393. static frame_t *DuplicateLastFrame(frameref_t *seq, int n_frames, int length)
  394. {
  395. frame_t *fr;
  396. seq[n_frames-1].length = length;
  397. fr = CopyFrame(seq[n_frames-1].frame, 1);
  398. seq[n_frames].frame = fr;
  399. seq[n_frames].length = 0;
  400. return fr;
  401. }
  402. static void AdjustFormants(frame_t *fr, int target, int min, int max, int f1_adj, int f3_adj, int hf_reduce, int flags)
  403. {
  404. int x;
  405. target = (target * voice->formant_factor)/256;
  406. x = (target - fr->ffreq[2]) / 2;
  407. if (x > max) x = max;
  408. if (x < min) x = min;
  409. fr->ffreq[2] += x;
  410. fr->ffreq[3] += f3_adj;
  411. if (flags & 0x20)
  412. f3_adj = -f3_adj; // reverse direction for f4,f5 change
  413. fr->ffreq[4] += f3_adj;
  414. fr->ffreq[5] += f3_adj;
  415. if (f1_adj == 1) {
  416. x = (235 - fr->ffreq[1]);
  417. if (x < -100) x = -100;
  418. if (x > -60) x = -60;
  419. fr->ffreq[1] += x;
  420. }
  421. if (f1_adj == 2) {
  422. x = (235 - fr->ffreq[1]);
  423. if (x < -300) x = -300;
  424. if (x > -150) x = -150;
  425. fr->ffreq[1] += x;
  426. fr->ffreq[0] += x;
  427. }
  428. if (f1_adj == 3) {
  429. x = (100 - fr->ffreq[1]);
  430. if (x < -400) x = -400;
  431. if (x > -300) x = -400;
  432. fr->ffreq[1] += x;
  433. fr->ffreq[0] += x;
  434. }
  435. formants_reduce_hf(fr, hf_reduce);
  436. }
  437. static int VowelCloseness(frame_t *fr)
  438. {
  439. // return a value 0-3 depending on the vowel's f1
  440. int f1;
  441. if ((f1 = fr->ffreq[1]) < 300)
  442. return 3;
  443. if (f1 < 400)
  444. return 2;
  445. if (f1 < 500)
  446. return 1;
  447. return 0;
  448. }
  449. int FormantTransition2(frameref_t *seq, int *n_frames, unsigned int data1, unsigned int data2, PHONEME_TAB *other_ph, int which)
  450. {
  451. int ix;
  452. int formant;
  453. int next_rms;
  454. int len;
  455. int rms;
  456. int f1;
  457. int f2;
  458. int f2_min;
  459. int f2_max;
  460. int f3_adj;
  461. int f3_amp;
  462. int flags;
  463. int vcolour;
  464. #define N_VCOLOUR 2
  465. // percentage change for each formant in 256ths
  466. static const short vcolouring[N_VCOLOUR][5] = {
  467. { 243, 272, 256, 256, 256 }, // palatal consonant follows
  468. { 256, 256, 240, 240, 240 }, // retroflex
  469. };
  470. frame_t *fr = NULL;
  471. if (*n_frames < 2)
  472. return 0;
  473. len = (data1 & 0x3f) * 2;
  474. rms = (data1 >> 6) & 0x3f;
  475. flags = (data1 >> 12);
  476. f2 = (data2 & 0x3f) * 50;
  477. f2_min = (((data2 >> 6) & 0x1f) - 15) * 50;
  478. f2_max = (((data2 >> 11) & 0x1f) - 15) * 50;
  479. f3_adj = (((data2 >> 16) & 0x1f) - 15) * 50;
  480. f3_amp = ((data2 >> 21) & 0x1f) * 8;
  481. f1 = ((data2 >> 26) & 0x7);
  482. vcolour = (data2 >> 29);
  483. if ((other_ph != NULL) && (other_ph->mnemonic == '?'))
  484. flags |= 8;
  485. if (which == 1) {
  486. // entry to vowel
  487. fr = CopyFrame(seq[0].frame, 0);
  488. seq[0].frame = fr;
  489. seq[0].length = VOWEL_FRONT_LENGTH;
  490. if (len > 0)
  491. seq[0].length = len;
  492. seq[0].frflags |= FRFLAG_LEN_MOD2; // reduce length modification
  493. fr->frflags |= FRFLAG_LEN_MOD2;
  494. next_rms = seq[1].frame->rms;
  495. if (voice->klattv[0])
  496. fr->klattp[KLATT_AV] = seq[1].frame->klattp[KLATT_AV] - 4;
  497. if (f2 != 0) {
  498. if (rms & 0x20)
  499. set_frame_rms(fr, (next_rms * (rms & 0x1f))/30);
  500. AdjustFormants(fr, f2, f2_min, f2_max, f1, f3_adj, f3_amp, flags);
  501. if ((rms & 0x20) == 0)
  502. set_frame_rms(fr, rms*2);
  503. } else {
  504. if (flags & 8)
  505. set_frame_rms(fr, (next_rms*24)/32);
  506. else
  507. set_frame_rms(fr, RMS_START);
  508. }
  509. if (flags & 8)
  510. modn_flags = 0x800 + (VowelCloseness(fr) << 8);
  511. } else {
  512. // exit from vowel
  513. rms = rms*2;
  514. if ((f2 != 0) || (flags != 0)) {
  515. if (flags & 8) {
  516. fr = CopyFrame(seq[*n_frames-1].frame, 0);
  517. seq[*n_frames-1].frame = fr;
  518. rms = RMS_GLOTTAL1;
  519. // degree of glottal-stop effect depends on closeness of vowel (indicated by f1 freq)
  520. modn_flags = 0x400 + (VowelCloseness(fr) << 8);
  521. } else {
  522. fr = DuplicateLastFrame(seq, (*n_frames)++, len);
  523. if (len > 36)
  524. seq_len_adjust += (len - 36);
  525. if (f2 != 0)
  526. AdjustFormants(fr, f2, f2_min, f2_max, f1, f3_adj, f3_amp, flags);
  527. }
  528. set_frame_rms(fr, rms);
  529. if ((vcolour > 0) && (vcolour <= N_VCOLOUR)) {
  530. for (ix = 0; ix < *n_frames; ix++) {
  531. fr = CopyFrame(seq[ix].frame, 0);
  532. seq[ix].frame = fr;
  533. for (formant = 1; formant <= 5; formant++) {
  534. int x;
  535. x = fr->ffreq[formant] * vcolouring[vcolour-1][formant-1];
  536. fr->ffreq[formant] = x / 256;
  537. }
  538. }
  539. }
  540. }
  541. }
  542. if (fr != NULL) {
  543. if (flags & 4)
  544. fr->frflags |= FRFLAG_FORMANT_RATE;
  545. if (flags & 2)
  546. fr->frflags |= FRFLAG_BREAK; // don't merge with next frame
  547. }
  548. if (flags & 0x40)
  549. DoPause(20, 0); // add a short pause after the consonant
  550. if (flags & 16)
  551. return len;
  552. return 0;
  553. }
  554. static void SmoothSpect(void)
  555. {
  556. // Limit the rate of frequence change of formants, to reduce chirping
  557. intptr_t *q;
  558. frame_t *frame;
  559. frame_t *frame2;
  560. frame_t *frame1;
  561. frame_t *frame_centre;
  562. int ix;
  563. int len;
  564. int pk;
  565. bool modified;
  566. int allowed;
  567. int diff;
  568. if (syllable_start == syllable_end)
  569. return;
  570. if ((syllable_centre < 0) || (syllable_centre == syllable_start)) {
  571. syllable_start = syllable_end;
  572. return;
  573. }
  574. q = wcmdq[syllable_centre];
  575. frame_centre = (frame_t *)q[2];
  576. // backwards
  577. ix = syllable_centre -1;
  578. frame = frame2 = frame_centre;
  579. for (;;) {
  580. if (ix < 0) ix = N_WCMDQ-1;
  581. q = wcmdq[ix];
  582. if (q[0] == WCMD_PAUSE || q[0] == WCMD_WAVE)
  583. break;
  584. if (q[0] <= WCMD_SPECT2) {
  585. len = q[1] & 0xffff;
  586. frame1 = (frame_t *)q[3];
  587. if (frame1 == frame) {
  588. q[3] = (intptr_t)frame2;
  589. frame1 = frame2;
  590. } else
  591. break; // doesn't follow on from previous frame
  592. frame = frame2 = (frame_t *)q[2];
  593. modified = false;
  594. if (frame->frflags & FRFLAG_BREAK)
  595. break;
  596. if (frame->frflags & FRFLAG_FORMANT_RATE)
  597. len = (len * 12)/10; // allow slightly greater rate of change for this frame (was 12/10)
  598. for (pk = 0; pk < 6; pk++) {
  599. int f1, f2;
  600. if ((frame->frflags & FRFLAG_BREAK_LF) && (pk < 3))
  601. continue;
  602. f1 = frame1->ffreq[pk];
  603. f2 = frame->ffreq[pk];
  604. // backwards
  605. if ((diff = f2 - f1) > 0)
  606. allowed = f1*2 + f2;
  607. else
  608. allowed = f1 + f2*2;
  609. // the allowed change is specified as percentage (%*10) of the frequency
  610. // take "frequency" as 1/3 from the lower freq
  611. allowed = (allowed * formant_rate[pk])/3000;
  612. allowed = (allowed * len)/256;
  613. if (diff > allowed) {
  614. if (modified == false) {
  615. frame2 = CopyFrame(frame, 0);
  616. modified = true;
  617. }
  618. frame2->ffreq[pk] = frame1->ffreq[pk] + allowed;
  619. q[2] = (intptr_t)frame2;
  620. } else if (diff < -allowed) {
  621. if (modified == false) {
  622. frame2 = CopyFrame(frame, 0);
  623. modified = true;
  624. }
  625. frame2->ffreq[pk] = frame1->ffreq[pk] - allowed;
  626. q[2] = (intptr_t)frame2;
  627. }
  628. }
  629. }
  630. if (ix == syllable_start)
  631. break;
  632. ix--;
  633. }
  634. // forwards
  635. ix = syllable_centre;
  636. frame = NULL;
  637. for (;;) {
  638. q = wcmdq[ix];
  639. if (q[0] == WCMD_PAUSE || q[0] == WCMD_WAVE)
  640. break;
  641. if (q[0] <= WCMD_SPECT2) {
  642. len = q[1] & 0xffff;
  643. frame1 = (frame_t *)q[2];
  644. if (frame != NULL) {
  645. if (frame1 == frame) {
  646. q[2] = (intptr_t)frame2;
  647. frame1 = frame2;
  648. } else
  649. break; // doesn't follow on from previous frame
  650. }
  651. frame = frame2 = (frame_t *)q[3];
  652. modified = false;
  653. if (frame1->frflags & FRFLAG_BREAK)
  654. break;
  655. if (frame1->frflags & FRFLAG_FORMANT_RATE)
  656. len = (len *6)/5; // allow slightly greater rate of change for this frame
  657. for (pk = 0; pk < 6; pk++) {
  658. int f1, f2;
  659. f1 = frame1->ffreq[pk];
  660. f2 = frame->ffreq[pk];
  661. // forwards
  662. if ((diff = f2 - f1) > 0)
  663. allowed = f1*2 + f2;
  664. else
  665. allowed = f1 + f2*2;
  666. allowed = (allowed * formant_rate[pk])/3000;
  667. allowed = (allowed * len)/256;
  668. if (diff > allowed) {
  669. if (modified == false) {
  670. frame2 = CopyFrame(frame, 0);
  671. modified = true;
  672. }
  673. frame2->ffreq[pk] = frame1->ffreq[pk] + allowed;
  674. q[3] = (intptr_t)frame2;
  675. } else if (diff < -allowed) {
  676. if (modified == false) {
  677. frame2 = CopyFrame(frame, 0);
  678. modified = true;
  679. }
  680. frame2->ffreq[pk] = frame1->ffreq[pk] - allowed;
  681. q[3] = (intptr_t)frame2;
  682. }
  683. }
  684. }
  685. ix++;
  686. if (ix >= N_WCMDQ) ix = 0;
  687. if (ix == syllable_end)
  688. break;
  689. }
  690. syllable_start = syllable_end;
  691. }
  692. static void StartSyllable(void)
  693. {
  694. // start of syllable, if not already started
  695. if (syllable_end == syllable_start)
  696. syllable_end = wcmdq_tail;
  697. }
  698. int DoSpect2(PHONEME_TAB *this_ph, int which, FMT_PARAMS *fmt_params, PHONEME_LIST *plist, int modulation)
  699. {
  700. // which: 0 not a vowel, 1 start of vowel, 2 body and end of vowel
  701. // length_mod: 256 = 100%
  702. // modulation: -1 = don't write to wcmdq
  703. int n_frames;
  704. frameref_t *frames;
  705. int frameix;
  706. frame_t *frame1;
  707. frame_t *frame2;
  708. frame_t *fr;
  709. int ix;
  710. intptr_t *q;
  711. int len;
  712. int frame_length;
  713. int length_factor;
  714. int length_mod;
  715. int length_sum;
  716. int length_min;
  717. int total_len = 0;
  718. static int wave_flag = 0;
  719. int wcmd_spect = WCMD_SPECT;
  720. int frame_lengths[N_SEQ_FRAMES];
  721. if (fmt_params->fmt_addr == 0)
  722. return 0;
  723. length_mod = plist->length;
  724. if (length_mod == 0) length_mod = 256;
  725. length_min = (samplerate/70); // greater than one cycle at low pitch (Hz)
  726. if (which == 2) {
  727. if ((translator->langopts.param[LOPT_LONG_VOWEL_THRESHOLD] > 0) && ((this_ph->std_length >= translator->langopts.param[LOPT_LONG_VOWEL_THRESHOLD]) || (plist->synthflags & SFLAG_LENGTHEN) || (this_ph->phflags & phLONG)))
  728. length_min *= 2; // ensure long vowels are longer
  729. }
  730. if (which == 1) {
  731. // limit the shortening of sonorants before shortened (eg. unstressed vowels)
  732. if ((this_ph->type == phLIQUID) || (plist[-1].type == phLIQUID) || (plist[-1].type == phNASAL)) {
  733. if (length_mod < (len = translator->langopts.param[LOPT_SONORANT_MIN]))
  734. length_mod = len;
  735. }
  736. }
  737. modn_flags = 0;
  738. frames = LookupSpect(this_ph, which, fmt_params, &n_frames, plist);
  739. if (frames == NULL)
  740. return 0; // not found
  741. if (fmt_params->fmt_amp != fmt_amplitude) {
  742. // an amplitude adjustment is specified for this sequence
  743. q = wcmdq[wcmdq_tail];
  744. q[0] = WCMD_FMT_AMPLITUDE;
  745. q[1] = fmt_amplitude = fmt_params->fmt_amp;
  746. WcmdqInc();
  747. }
  748. frame1 = frames[0].frame;
  749. if (voice->klattv[0])
  750. wcmd_spect = WCMD_KLATT;
  751. if (fmt_params->wav_addr == 0) {
  752. if (wave_flag) {
  753. // cancel any wavefile that was playing previously
  754. wcmd_spect = WCMD_SPECT2;
  755. if (voice->klattv[0])
  756. wcmd_spect = WCMD_KLATT2;
  757. wave_flag = 0;
  758. } else {
  759. wcmd_spect = WCMD_SPECT;
  760. if (voice->klattv[0])
  761. wcmd_spect = WCMD_KLATT;
  762. }
  763. }
  764. if (last_frame != NULL) {
  765. if (((last_frame->length < 2) || (last_frame->frflags & FRFLAG_VOWEL_CENTRE))
  766. && !(last_frame->frflags & FRFLAG_BREAK)) {
  767. // last frame of previous sequence was zero-length, replace with first of this sequence
  768. wcmdq[last_wcmdq][3] = (intptr_t)frame1;
  769. if (last_frame->frflags & FRFLAG_BREAK_LF) {
  770. // but flag indicates keep HF peaks in last segment
  771. fr = CopyFrame(frame1, 1);
  772. for (ix = 3; ix < 8; ix++) {
  773. if (ix < 7)
  774. fr->ffreq[ix] = last_frame->ffreq[ix];
  775. fr->fheight[ix] = last_frame->fheight[ix];
  776. }
  777. wcmdq[last_wcmdq][3] = (intptr_t)fr;
  778. }
  779. }
  780. }
  781. if ((this_ph->type == phVOWEL) && (which == 2)) {
  782. SmoothSpect(); // process previous syllable
  783. // remember the point in the output queue of the centre of the vowel
  784. syllable_centre = wcmdq_tail;
  785. }
  786. length_sum = 0;
  787. for (frameix = 1; frameix < n_frames; frameix++) {
  788. length_factor = length_mod;
  789. if (frames[frameix-1].frflags & FRFLAG_LEN_MOD) // reduce effect of length mod
  790. length_factor = (length_mod*(256-speed.lenmod_factor) + 256*speed.lenmod_factor)/256;
  791. else if (frames[frameix-1].frflags & FRFLAG_LEN_MOD2) // reduce effect of length mod, used for the start of a vowel
  792. length_factor = (length_mod*(256-speed.lenmod2_factor) + 256*speed.lenmod2_factor)/256;
  793. frame_length = frames[frameix-1].length;
  794. len = (frame_length * samplerate)/1000;
  795. len = (len * length_factor)/256;
  796. length_sum += len;
  797. frame_lengths[frameix] = len;
  798. }
  799. if ((length_sum > 0) && (length_sum < length_min)) {
  800. // lengthen, so that the sequence is greater than one cycle at low pitch
  801. for (frameix = 1; frameix < n_frames; frameix++)
  802. frame_lengths[frameix] = (frame_lengths[frameix] * length_min) / length_sum;
  803. }
  804. for (frameix = 1; frameix < n_frames; frameix++) {
  805. frame2 = frames[frameix].frame;
  806. if ((fmt_params->wav_addr != 0) && ((frame1->frflags & FRFLAG_DEFER_WAV) == 0)) {
  807. // there is a wave file to play along with this synthesis
  808. seq_len_adjust = 0;
  809. int wavefile_amp;
  810. if (fmt_params->wav_amp == 0)
  811. wavefile_amp = 32;
  812. else
  813. wavefile_amp = (fmt_params->wav_amp * 32)/100;
  814. DoSample2(fmt_params->wav_addr, which+0x100, 0, fmt_params->fmt_control, 0, wavefile_amp);
  815. wave_flag = 1;
  816. fmt_params->wav_addr = 0;
  817. }
  818. if (modulation >= 0) {
  819. if (frame1->frflags & FRFLAG_MODULATE)
  820. modulation = 6;
  821. if ((frameix == n_frames-1) && (modn_flags & 0xf00))
  822. modulation |= modn_flags; // before or after a glottal stop
  823. }
  824. len = frame_lengths[frameix];
  825. pitch_length += len;
  826. amp_length += len;
  827. if (len == 0) {
  828. last_frame = NULL;
  829. frame1 = frame2;
  830. } else {
  831. last_wcmdq = wcmdq_tail;
  832. if (modulation >= 0) {
  833. q = wcmdq[wcmdq_tail];
  834. q[0] = wcmd_spect;
  835. q[1] = len + (modulation << 16);
  836. q[2] = (intptr_t)frame1;
  837. q[3] = (intptr_t)frame2;
  838. WcmdqInc();
  839. }
  840. last_frame = frame1 = frame2;
  841. total_len += len;
  842. }
  843. }
  844. if ((which != 1) && (fmt_amplitude != 0)) {
  845. q = wcmdq[wcmdq_tail];
  846. q[0] = WCMD_FMT_AMPLITUDE;
  847. q[1] = fmt_amplitude = 0;
  848. WcmdqInc();
  849. }
  850. return total_len;
  851. }
  852. void DoMarker(int type, int char_posn, int length, int value)
  853. {
  854. // This could be used to return an index to the word currently being spoken
  855. // Type 1=word, 2=sentence, 3=named marker, 4=play audio, 5=end
  856. if (WcmdqFree() > 5) {
  857. wcmdq[wcmdq_tail][0] = WCMD_MARKER + (type << 8);
  858. wcmdq[wcmdq_tail][1] = (char_posn & 0xffffff) | (length << 24);
  859. wcmdq[wcmdq_tail][2] = value;
  860. WcmdqInc();
  861. }
  862. }
  863. void DoPhonemeMarker(int type, int char_posn, int length, char *name)
  864. {
  865. // This could be used to return an index to the word currently being spoken
  866. // Type 7=phoneme
  867. int *p;
  868. if (WcmdqFree() > 5) {
  869. wcmdq[wcmdq_tail][0] = WCMD_MARKER + (type << 8);
  870. wcmdq[wcmdq_tail][1] = (char_posn & 0xffffff) | (length << 24);
  871. p = (int *)name;
  872. wcmdq[wcmdq_tail][2] = p[0]; // up to 8 bytes of UTF8 characters
  873. wcmdq[wcmdq_tail][3] = p[1];
  874. WcmdqInc();
  875. }
  876. }
  877. #if HAVE_SONIC_H
  878. void DoSonicSpeed(int value)
  879. {
  880. // value, multiplier * 1024
  881. wcmdq[wcmdq_tail][0] = WCMD_SONIC_SPEED;
  882. wcmdq[wcmdq_tail][1] = value;
  883. WcmdqInc();
  884. }
  885. #endif
  886. espeak_ng_STATUS DoVoiceChange(voice_t *v)
  887. {
  888. // allocate memory for a copy of the voice data, and free it in wavegenfill()
  889. voice_t *v2;
  890. if ((v2 = (voice_t *)malloc(sizeof(voice_t))) == NULL)
  891. return ENOMEM;
  892. memcpy(v2, v, sizeof(voice_t));
  893. wcmdq[wcmdq_tail][0] = WCMD_VOICE;
  894. wcmdq[wcmdq_tail][2] = (intptr_t)v2;
  895. WcmdqInc();
  896. return ENS_OK;
  897. }
  898. void DoEmbedded(int *embix, int sourceix)
  899. {
  900. // There were embedded commands in the text at this point
  901. unsigned int word; // bit 7=last command for this word, bits 5,6 sign, bits 0-4 command
  902. unsigned int value;
  903. int command;
  904. do {
  905. word = embedded_list[*embix];
  906. value = word >> 8;
  907. command = word & 0x7f;
  908. if (command == 0)
  909. return; // error
  910. (*embix)++;
  911. switch (command & 0x1f)
  912. {
  913. case EMBED_S: // speed
  914. SetEmbedded((command & 0x60) + EMBED_S2, value); // adjusts embedded_value[EMBED_S2]
  915. SetSpeed(2);
  916. break;
  917. case EMBED_I: // play dynamically loaded wav data (sound icon)
  918. if ((int)value < n_soundicon_tab) {
  919. if (soundicon_tab[value].length != 0) {
  920. DoPause(10, 0); // ensure a break in the speech
  921. wcmdq[wcmdq_tail][0] = WCMD_WAVE;
  922. wcmdq[wcmdq_tail][1] = soundicon_tab[value].length;
  923. wcmdq[wcmdq_tail][2] = (intptr_t)soundicon_tab[value].data + 44; // skip WAV header
  924. wcmdq[wcmdq_tail][3] = 0x1500; // 16 bit data, amp=21
  925. WcmdqInc();
  926. }
  927. }
  928. break;
  929. case EMBED_M: // named marker
  930. DoMarker(espeakEVENT_MARK, (sourceix & 0x7ff) + clause_start_char, 0, value);
  931. break;
  932. case EMBED_U: // play sound
  933. DoMarker(espeakEVENT_PLAY, count_characters+1, 0, value); // always occurs at end of clause
  934. break;
  935. default:
  936. DoPause(10, 0); // ensure a break in the speech
  937. wcmdq[wcmdq_tail][0] = WCMD_EMBEDDED;
  938. wcmdq[wcmdq_tail][1] = command;
  939. wcmdq[wcmdq_tail][2] = value;
  940. WcmdqInc();
  941. break;
  942. }
  943. } while ((word & 0x80) == 0);
  944. }
  945. extern espeak_ng_OUTPUT_HOOKS* output_hooks;
  946. int Generate(PHONEME_LIST *phoneme_list, int *n_ph, bool resume)
  947. {
  948. static int ix;
  949. static int embedded_ix;
  950. static int word_count;
  951. PHONEME_LIST *prev;
  952. PHONEME_LIST *next;
  953. PHONEME_LIST *next2;
  954. PHONEME_LIST *p;
  955. bool released;
  956. int stress;
  957. int modulation;
  958. bool pre_voiced;
  959. int free_min;
  960. int value;
  961. unsigned char *pitch_env = NULL;
  962. unsigned char *amp_env;
  963. PHONEME_TAB *ph;
  964. int use_ipa = 0;
  965. bool done_phoneme_marker;
  966. int vowelstart_prev;
  967. char phoneme_name[16];
  968. static int sourceix = 0;
  969. PHONEME_DATA phdata;
  970. PHONEME_DATA phdata_prev;
  971. PHONEME_DATA phdata_next;
  972. PHONEME_DATA phdata_tone;
  973. FMT_PARAMS fmtp;
  974. static WORD_PH_DATA worddata;
  975. if (option_phoneme_events & espeakINITIALIZE_PHONEME_IPA)
  976. use_ipa = 1;
  977. if (mbrola_name[0] != 0)
  978. return MbrolaGenerate(phoneme_list, n_ph, resume);
  979. if (resume == false) {
  980. ix = 1;
  981. embedded_ix = 0;
  982. word_count = 0;
  983. pitch_length = 0;
  984. amp_length = 0;
  985. last_frame = NULL;
  986. last_wcmdq = -1;
  987. syllable_start = wcmdq_tail;
  988. syllable_end = wcmdq_tail;
  989. syllable_centre = -1;
  990. last_pitch_cmd = -1;
  991. memset(&worddata, 0, sizeof(worddata));
  992. DoPause(0, 0); // isolate from the previous clause
  993. }
  994. while ((ix < (*n_ph)) && (ix < N_PHONEME_LIST-2)) {
  995. p = &phoneme_list[ix];
  996. if(output_hooks && output_hooks->outputPhoSymbol)
  997. {
  998. char buf[30];
  999. int dummy=0;
  1000. WritePhMnemonic(buf, p->ph, p, 0, &dummy);
  1001. DoPhonemeAlignment(strdup(buf),p->type);
  1002. }
  1003. if (p->type == phPAUSE)
  1004. free_min = 10;
  1005. else if (p->type != phVOWEL)
  1006. free_min = 15; // we need less Q space for non-vowels, and we need to generate phonemes after a vowel so that the pitch_length is filled in
  1007. else
  1008. free_min = MIN_WCMDQ;
  1009. if (WcmdqFree() <= free_min)
  1010. return 1; // wait
  1011. prev = &phoneme_list[ix-1];
  1012. next = &phoneme_list[ix+1];
  1013. next2 = &phoneme_list[ix+2];
  1014. if (p->synthflags & SFLAG_EMBEDDED)
  1015. DoEmbedded(&embedded_ix, p->sourceix);
  1016. if (p->newword) {
  1017. if (((p->type == phVOWEL) && (translator->langopts.param[LOPT_WORD_MERGE] & 1)) ||
  1018. (p->ph->phflags & phNOPAUSE)) {
  1019. } else
  1020. last_frame = NULL;
  1021. sourceix = (p->sourceix & 0x7ff) + clause_start_char;
  1022. if (p->newword & PHLIST_START_OF_SENTENCE)
  1023. DoMarker(espeakEVENT_SENTENCE, sourceix, 0, count_sentences); // start of sentence
  1024. if (p->newword & PHLIST_START_OF_WORD)
  1025. DoMarker(espeakEVENT_WORD, sourceix, p->sourceix >> 11, clause_start_word + word_count++); // NOTE, this count doesn't include multiple-word pronunciations in *_list. eg (of a)
  1026. }
  1027. EndAmplitude();
  1028. if ((p->prepause > 0) && !(p->ph->phflags & phPREVOICE))
  1029. DoPause(p->prepause, 1);
  1030. done_phoneme_marker = false;
  1031. if (option_phoneme_events && (p->ph->code != phonEND_WORD)) {
  1032. if ((p->type == phVOWEL) && (prev->type == phLIQUID || prev->type == phNASAL)) {
  1033. // For vowels following a liquid or nasal, do the phoneme event after the vowel-start
  1034. } else {
  1035. WritePhMnemonic(phoneme_name, p->ph, p, use_ipa, NULL);
  1036. DoPhonemeMarker(espeakEVENT_PHONEME, sourceix, 0, phoneme_name);
  1037. done_phoneme_marker = true;
  1038. }
  1039. }
  1040. switch (p->type)
  1041. {
  1042. case phPAUSE:
  1043. DoPause(p->length, 0);
  1044. p->std_length = p->ph->std_length;
  1045. break;
  1046. case phSTOP:
  1047. released = false;
  1048. ph = p->ph;
  1049. if (next->type == phVOWEL)
  1050. released = true;
  1051. else if (!next->newword) {
  1052. if (next->type == phLIQUID) released = true;
  1053. }
  1054. if (released == false)
  1055. p->synthflags |= SFLAG_NEXT_PAUSE;
  1056. if (ph->phflags & phPREVOICE) {
  1057. // a period of voicing before the release
  1058. memset(&fmtp, 0, sizeof(fmtp));
  1059. InterpretPhoneme(NULL, 0x01, p, &phdata, &worddata);
  1060. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1061. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1062. if (last_pitch_cmd < 0) {
  1063. DoAmplitude(next->amp, NULL);
  1064. DoPitch(envelope_data[p->env], next->pitch1, next->pitch2);
  1065. }
  1066. DoSpect2(ph, 0, &fmtp, p, 0);
  1067. }
  1068. InterpretPhoneme(NULL, 0, p, &phdata, &worddata);
  1069. phdata.pd_control |= pd_DONTLENGTHEN;
  1070. DoSample3(&phdata, 0, 0);
  1071. break;
  1072. case phFRICATIVE:
  1073. InterpretPhoneme(NULL, 0, p, &phdata, &worddata);
  1074. if (p->synthflags & SFLAG_LENGTHEN)
  1075. DoSample3(&phdata, p->length, 0); // play it twice for [s:] etc.
  1076. DoSample3(&phdata, p->length, 0);
  1077. break;
  1078. case phVSTOP:
  1079. ph = p->ph;
  1080. memset(&fmtp, 0, sizeof(fmtp));
  1081. fmtp.fmt_control = pd_DONTLENGTHEN;
  1082. pre_voiced = false;
  1083. if (next->type == phVOWEL) {
  1084. DoAmplitude(p->amp, NULL);
  1085. DoPitch(envelope_data[p->env], p->pitch1, p->pitch2);
  1086. pre_voiced = true;
  1087. } else if ((next->type == phLIQUID) && !next->newword) {
  1088. DoAmplitude(next->amp, NULL);
  1089. DoPitch(envelope_data[next->env], next->pitch1, next->pitch2);
  1090. pre_voiced = true;
  1091. } else {
  1092. if (last_pitch_cmd < 0) {
  1093. DoAmplitude(next->amp, NULL);
  1094. DoPitch(envelope_data[p->env], p->pitch1, p->pitch2);
  1095. }
  1096. }
  1097. if ((prev->type == phVOWEL) || (ph->phflags & phPREVOICE)) {
  1098. // a period of voicing before the release
  1099. InterpretPhoneme(NULL, 0x01, p, &phdata, &worddata);
  1100. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1101. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1102. DoSpect2(ph, 0, &fmtp, p, 0);
  1103. if (p->synthflags & SFLAG_LENGTHEN) {
  1104. DoPause(25, 1);
  1105. DoSpect2(ph, 0, &fmtp, p, 0);
  1106. }
  1107. } else {
  1108. if (p->synthflags & SFLAG_LENGTHEN)
  1109. DoPause(50, 0);
  1110. }
  1111. if (pre_voiced) {
  1112. // followed by a vowel, or liquid + vowel
  1113. StartSyllable();
  1114. } else
  1115. p->synthflags |= SFLAG_NEXT_PAUSE;
  1116. InterpretPhoneme(NULL, 0, p, &phdata, &worddata);
  1117. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1118. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1119. fmtp.wav_addr = phdata.sound_addr[pd_ADDWAV];
  1120. fmtp.wav_amp = phdata.sound_param[pd_ADDWAV];
  1121. DoSpect2(ph, 0, &fmtp, p, 0);
  1122. if ((p->newword == 0) && (next2->newword == 0)) {
  1123. if (next->type == phVFRICATIVE)
  1124. DoPause(20, 0);
  1125. if (next->type == phFRICATIVE)
  1126. DoPause(12, 0);
  1127. }
  1128. break;
  1129. case phVFRICATIVE:
  1130. if (next->type == phVOWEL) {
  1131. DoAmplitude(p->amp, NULL);
  1132. DoPitch(envelope_data[p->env], p->pitch1, p->pitch2);
  1133. } else if (next->type == phLIQUID) {
  1134. DoAmplitude(next->amp, NULL);
  1135. DoPitch(envelope_data[next->env], next->pitch1, next->pitch2);
  1136. } else {
  1137. if (last_pitch_cmd < 0) {
  1138. DoAmplitude(p->amp, NULL);
  1139. DoPitch(envelope_data[p->env], p->pitch1, p->pitch2);
  1140. }
  1141. }
  1142. if ((next->type == phVOWEL) || ((next->type == phLIQUID) && (next->newword == 0))) // ?? test 14.Aug.2007
  1143. StartSyllable();
  1144. else
  1145. p->synthflags |= SFLAG_NEXT_PAUSE;
  1146. InterpretPhoneme(NULL, 0, p, &phdata, &worddata);
  1147. memset(&fmtp, 0, sizeof(fmtp));
  1148. fmtp.std_length = phdata.pd_param[i_SET_LENGTH]*2;
  1149. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1150. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1151. fmtp.wav_addr = phdata.sound_addr[pd_ADDWAV];
  1152. fmtp.wav_amp = phdata.sound_param[pd_ADDWAV];
  1153. if (p->synthflags & SFLAG_LENGTHEN)
  1154. DoSpect2(p->ph, 0, &fmtp, p, 0);
  1155. DoSpect2(p->ph, 0, &fmtp, p, 0);
  1156. break;
  1157. case phNASAL:
  1158. memset(&fmtp, 0, sizeof(fmtp));
  1159. if (!(p->synthflags & SFLAG_SEQCONTINUE)) {
  1160. DoAmplitude(p->amp, NULL);
  1161. DoPitch(envelope_data[p->env], p->pitch1, p->pitch2);
  1162. }
  1163. if (prev->type == phNASAL)
  1164. last_frame = NULL;
  1165. InterpretPhoneme(NULL, 0, p, &phdata, &worddata);
  1166. fmtp.std_length = phdata.pd_param[i_SET_LENGTH]*2;
  1167. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1168. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1169. if (next->type == phVOWEL) {
  1170. StartSyllable();
  1171. DoSpect2(p->ph, 0, &fmtp, p, 0);
  1172. } else if (prev->type == phVOWEL && (p->synthflags & SFLAG_SEQCONTINUE))
  1173. DoSpect2(p->ph, 0, &fmtp, p, 0);
  1174. else {
  1175. last_frame = NULL; // only for nasal ?
  1176. DoSpect2(p->ph, 0, &fmtp, p, 0);
  1177. last_frame = NULL;
  1178. }
  1179. break;
  1180. case phLIQUID:
  1181. memset(&fmtp, 0, sizeof(fmtp));
  1182. modulation = 0;
  1183. if (p->ph->phflags & phTRILL)
  1184. modulation = 5;
  1185. if (!(p->synthflags & SFLAG_SEQCONTINUE)) {
  1186. DoAmplitude(p->amp, NULL);
  1187. DoPitch(envelope_data[p->env], p->pitch1, p->pitch2);
  1188. }
  1189. if (prev->type == phNASAL)
  1190. last_frame = NULL;
  1191. if (next->type == phVOWEL)
  1192. StartSyllable();
  1193. InterpretPhoneme(NULL, 0, p, &phdata, &worddata);
  1194. if ((value = (phdata.pd_param[i_PAUSE_BEFORE] - p->prepause)) > 0)
  1195. DoPause(value, 1);
  1196. fmtp.std_length = phdata.pd_param[i_SET_LENGTH]*2;
  1197. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1198. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1199. fmtp.wav_addr = phdata.sound_addr[pd_ADDWAV];
  1200. fmtp.wav_amp = phdata.sound_param[pd_ADDWAV];
  1201. DoSpect2(p->ph, 0, &fmtp, p, modulation);
  1202. break;
  1203. case phVOWEL:
  1204. ph = p->ph;
  1205. stress = p->stresslevel & 0xf;
  1206. memset(&fmtp, 0, sizeof(fmtp));
  1207. InterpretPhoneme(NULL, 0, p, &phdata, &worddata);
  1208. fmtp.std_length = phdata.pd_param[i_SET_LENGTH] * 2;
  1209. vowelstart_prev = 0;
  1210. if (((fmtp.fmt_addr = phdata.sound_addr[pd_VWLSTART]) != 0) && ((phdata.pd_control & pd_FORNEXTPH) == 0)) {
  1211. // a vowel start has been specified by the Vowel program
  1212. fmtp.fmt_length = phdata.sound_param[pd_VWLSTART];
  1213. } else if (prev->type != phPAUSE) {
  1214. // check the previous phoneme
  1215. InterpretPhoneme(NULL, 0, prev, &phdata_prev, NULL);
  1216. if (((fmtp.fmt_addr = phdata_prev.sound_addr[pd_VWLSTART]) != 0) && (phdata_prev.pd_control & pd_FORNEXTPH)) {
  1217. // a vowel start has been specified by the previous phoneme
  1218. vowelstart_prev = 1;
  1219. fmtp.fmt2_lenadj = phdata_prev.sound_param[pd_VWLSTART];
  1220. }
  1221. fmtp.transition0 = phdata_prev.vowel_transition[0];
  1222. fmtp.transition1 = phdata_prev.vowel_transition[1];
  1223. }
  1224. if (fmtp.fmt_addr == 0) {
  1225. // use the default start for this vowel
  1226. fmtp.use_vowelin = 1;
  1227. fmtp.fmt_control = 1;
  1228. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1229. }
  1230. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1231. pitch_env = envelope_data[p->env];
  1232. amp_env = NULL;
  1233. if (p->tone_ph != 0) {
  1234. InterpretPhoneme2(p->tone_ph, &phdata_tone);
  1235. pitch_env = GetEnvelope(phdata_tone.pitch_env);
  1236. if (phdata_tone.amp_env > 0)
  1237. amp_env = GetEnvelope(phdata_tone.amp_env);
  1238. }
  1239. StartSyllable();
  1240. modulation = 2;
  1241. if (stress <= 1)
  1242. modulation = 1; // 16ths
  1243. else if (stress >= 7)
  1244. modulation = 3;
  1245. if (prev->type == phVSTOP || prev->type == phVFRICATIVE) {
  1246. DoAmplitude(p->amp, amp_env);
  1247. DoPitch(pitch_env, p->pitch1, p->pitch2); // don't use prevocalic rising tone
  1248. DoSpect2(ph, 1, &fmtp, p, modulation);
  1249. } else if (prev->type == phLIQUID || prev->type == phNASAL) {
  1250. DoAmplitude(p->amp, amp_env);
  1251. DoSpect2(ph, 1, &fmtp, p, modulation); // continue with pre-vocalic rising tone
  1252. DoPitch(pitch_env, p->pitch1, p->pitch2);
  1253. } else if (vowelstart_prev) {
  1254. // VowelStart from the previous phoneme, but not phLIQUID or phNASAL
  1255. DoPitch(envelope_data[PITCHrise], p->pitch2 - 15, p->pitch2);
  1256. DoAmplitude(p->amp-1, amp_env);
  1257. DoSpect2(ph, 1, &fmtp, p, modulation); // continue with pre-vocalic rising tone
  1258. DoPitch(pitch_env, p->pitch1, p->pitch2);
  1259. } else {
  1260. if (!(p->synthflags & SFLAG_SEQCONTINUE)) {
  1261. DoAmplitude(p->amp, amp_env);
  1262. DoPitch(pitch_env, p->pitch1, p->pitch2);
  1263. }
  1264. DoSpect2(ph, 1, &fmtp, p, modulation);
  1265. }
  1266. if ((option_phoneme_events) && (done_phoneme_marker == false)) {
  1267. WritePhMnemonic(phoneme_name, p->ph, p, use_ipa, NULL);
  1268. DoPhonemeMarker(espeakEVENT_PHONEME, sourceix, 0, phoneme_name);
  1269. }
  1270. fmtp.fmt_addr = phdata.sound_addr[pd_FMT];
  1271. fmtp.fmt_amp = phdata.sound_param[pd_FMT];
  1272. fmtp.transition0 = 0;
  1273. fmtp.transition1 = 0;
  1274. if ((fmtp.fmt2_addr = phdata.sound_addr[pd_VWLEND]) != 0)
  1275. fmtp.fmt2_lenadj = phdata.sound_param[pd_VWLEND];
  1276. else if (next->type != phPAUSE) {
  1277. fmtp.fmt2_lenadj = 0;
  1278. InterpretPhoneme(NULL, 0, next, &phdata_next, NULL);
  1279. fmtp.use_vowelin = 1;
  1280. fmtp.transition0 = phdata_next.vowel_transition[2]; // always do vowel_transition, even if ph_VWLEND ?? consider [N]
  1281. fmtp.transition1 = phdata_next.vowel_transition[3];
  1282. if ((fmtp.fmt2_addr = phdata_next.sound_addr[pd_VWLEND]) != 0)
  1283. fmtp.fmt2_lenadj = phdata_next.sound_param[pd_VWLEND];
  1284. }
  1285. DoSpect2(ph, 2, &fmtp, p, modulation);
  1286. break;
  1287. }
  1288. ix++;
  1289. }
  1290. EndPitch(1);
  1291. if (*n_ph > 0) {
  1292. DoMarker(espeakEVENT_END, count_characters, 0, count_sentences); // end of clause
  1293. *n_ph = 0;
  1294. }
  1295. return 0; // finished the phoneme list
  1296. }
  1297. int SpeakNextClause(int control)
  1298. {
  1299. // Speak text from memory (text_in)
  1300. // control 0: start
  1301. // text_in is set
  1302. // The other calls have text_in = NULL
  1303. // control 1: speak next text
  1304. // 2: stop
  1305. int clause_tone;
  1306. char *voice_change;
  1307. const char *phon_out;
  1308. if (control == 2) {
  1309. // stop speaking
  1310. n_phoneme_list = 0;
  1311. WcmdqStop();
  1312. return 0;
  1313. }
  1314. if (text_decoder_eof(p_decoder)) {
  1315. skipping_text = false;
  1316. return 0;
  1317. }
  1318. if (current_phoneme_table != voice->phoneme_tab_ix)
  1319. SelectPhonemeTable(voice->phoneme_tab_ix);
  1320. // read the next clause from the input text file, translate it, and generate
  1321. // entries in the wavegen command queue
  1322. TranslateClause(translator, &clause_tone, &voice_change);
  1323. CalcPitches(translator, clause_tone);
  1324. CalcLengths(translator);
  1325. if ((option_phonemes & 0xf) || (phoneme_callback != NULL)) {
  1326. phon_out = GetTranslatedPhonemeString(option_phonemes);
  1327. if (option_phonemes & 0xf)
  1328. fprintf(f_trans, "%s\n", phon_out);
  1329. if (phoneme_callback != NULL)
  1330. phoneme_callback(phon_out);
  1331. }
  1332. if (skipping_text) {
  1333. n_phoneme_list = 0;
  1334. return 1;
  1335. }
  1336. Generate(phoneme_list, &n_phoneme_list, 0);
  1337. if (voice_change != NULL) {
  1338. // voice change at the end of the clause (i.e. clause was terminated by a voice change)
  1339. new_voice = LoadVoiceVariant(voice_change, 0); // add a Voice instruction to wavegen at the end of the clause
  1340. }
  1341. if (new_voice) {
  1342. // finished the current clause, now change the voice if there was an embedded
  1343. // change voice command at the end of it (i.e. clause was broken at the change voice command)
  1344. DoVoiceChange(voice);
  1345. new_voice = NULL;
  1346. }
  1347. return 1;
  1348. }