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