Debian Codegraph @

external/espeak/synthesize.cpp

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