-
Notifications
You must be signed in to change notification settings - Fork 879
/
compiledata.c
2792 lines (2396 loc) · 69.9 KB
/
compiledata.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2005 to 2014 by Jonathan Duddington
* email: jonsd@users.sourceforge.net
* Copyright (C) 2013-2017 Reece H. Dunn
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see: <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <espeak-ng/espeak_ng.h>
#include <espeak-ng/speak_lib.h>
#include <espeak-ng/encoding.h>
#include "common.h" // for GetFileLength, strncpy0, ...c
#include "error.h" // for create_file_error_context
#include "mnemonics.h" // for LookupMnemName, MNEM_TAB
#include "phoneme.h" // for PHONEME_TAB, PHONEME_TAB_LIST
#include "spect.h" // for SpectFrame, peak_t, SpectSeq
#include "speech.h" // for path_home, GetFileLength
#include "synthdata.h" // for LoadPhData
#include "synthesize.h" // for TUNE, frame_t, CONDITION_IS_OTHER
#include "translate.h" // for utf8_out, utf8_in
#include "voice.h" // for LoadVoice, voice
#include "wavegen.h" // for WavegenInit, WavegenSetVoice
static int CalculateSample(unsigned char c3, int c1);
#define N_ITEM_STRING 256
typedef struct {
unsigned int value;
char *name;
} NAMETAB;
typedef struct {
const char *mnem;
int type;
int data;
} keywtab_t;
#define k_AND 1
#define k_OR 2
#define k_THEN 3
#define k_NOT 4
#define kTHISSTRESS 0x800
// keyword types
enum {
tPHONEME_TYPE = 1,
tPHONEME_FLAG,
tTRANSITION,
tSTATEMENT,
tINSTRN1,
tWHICH_PHONEME,
tTEST,
};
static const keywtab_t k_conditions[] = {
{ "AND", 0, k_AND },
{ "OR", 0, k_OR },
{ "THEN", 0, k_THEN },
{ "NOT", 0, k_NOT },
{ "prevPh", tWHICH_PHONEME, 0 },
{ "thisPh", tWHICH_PHONEME, 1 },
{ "nextPh", tWHICH_PHONEME, 2 },
{ "next2Ph", tWHICH_PHONEME, 3 },
{ "nextPhW", tWHICH_PHONEME, 4 },
{ "prevPhW", tWHICH_PHONEME, 5 },
{ "next2PhW", tWHICH_PHONEME, 6 },
{ "nextVowel", tWHICH_PHONEME, 7 },
{ "prevVowel", tWHICH_PHONEME, 8 },
{ "next3PhW", tWHICH_PHONEME, 9 },
{ "prev2PhW", tWHICH_PHONEME, 10 },
{ "PreVoicing", tTEST, 0xf01 },
{ "KlattSynth", tTEST, 0xf02 },
{ "MbrolaSynth", tTEST, 0xf03 },
{ NULL, 0, 0 }
};
static const keywtab_t k_properties[] = {
{ "isPause", 0, CONDITION_IS_PHONEME_TYPE | phPAUSE },
{ "isVowel", 0, CONDITION_IS_PHONEME_TYPE | phVOWEL },
{ "isNasal", 0, CONDITION_IS_PHONEME_TYPE | phNASAL },
{ "isLiquid", 0, CONDITION_IS_PHONEME_TYPE | phLIQUID },
{ "isUStop", 0, CONDITION_IS_PHONEME_TYPE | phSTOP },
{ "isVStop", 0, CONDITION_IS_PHONEME_TYPE | phVSTOP },
{ "isVFricative", 0, CONDITION_IS_PHONEME_TYPE | phVFRICATIVE },
{ "isPalatal", 0, CONDITION_IS_PHFLAG_SET | phFLAGBIT_PALATAL },
{ "isLong", 0, CONDITION_IS_PHFLAG_SET | phFLAGBIT_LONG },
{ "isRhotic", 0, CONDITION_IS_PHFLAG_SET | phFLAGBIT_RHOTIC },
{ "isSibilant", 0, CONDITION_IS_PHFLAG_SET | phFLAGBIT_SIBILANT },
{ "isFlag1", 0, CONDITION_IS_PHFLAG_SET | phFLAGBIT_FLAG1 },
{ "isFlag2", 0, CONDITION_IS_PHFLAG_SET | phFLAGBIT_FLAG2 },
{ "isVelar", 0, CONDITION_IS_PLACE_OF_ARTICULATION | phPLACE_VELAR },
{ "isDiminished", 0, CONDITION_IS_OTHER | STRESS_IS_DIMINISHED },
{ "isUnstressed", 0, CONDITION_IS_OTHER | STRESS_IS_UNSTRESSED },
{ "isNotStressed", 0, CONDITION_IS_OTHER | STRESS_IS_NOT_STRESSED },
{ "isStressed", 0, CONDITION_IS_OTHER | STRESS_IS_SECONDARY },
{ "isMaxStress", 0, CONDITION_IS_OTHER | STRESS_IS_PRIMARY },
{ "isPause2", 0, CONDITION_IS_OTHER | isBreak },
{ "isWordStart", 0, CONDITION_IS_OTHER | isWordStart },
{ "isWordEnd", 0, CONDITION_IS_OTHER | isWordEnd },
{ "isAfterStress", 0, CONDITION_IS_OTHER | isAfterStress },
{ "isNotVowel", 0, CONDITION_IS_OTHER | isNotVowel },
{ "isFinalVowel", 0, CONDITION_IS_OTHER | isFinalVowel },
{ "isVoiced", 0, CONDITION_IS_OTHER | isVoiced },
{ "isFirstVowel", 0, CONDITION_IS_OTHER | isFirstVowel },
{ "isSecondVowel", 0, CONDITION_IS_OTHER | isSecondVowel },
{ "isTranslationGiven", 0, CONDITION_IS_OTHER | isTranslationGiven },
{ NULL, 0, 0 }
};
enum {
kPHONEMESTART = 1,
kUTF8_BOM,
kPROCEDURE,
kENDPHONEME,
kENDPROCEDURE,
kPHONEMETABLE,
kINCLUDE,
kIMPORT_PH,
kSTARTTYPE,
kENDTYPE,
kSTRESSTYPE,
kVOICINGSWITCH,
kIF,
kELSE,
kELIF,
kENDIF,
kCALLPH,
kSWITCH_PREVVOWEL,
kSWITCH_NEXTVOWEL,
kENDSWITCH,
kFMT,
kWAV,
kVOWELSTART,
kVOWELENDING,
kANDWAV,
kVOWELIN,
kVOWELOUT,
kTONESPEC,
kRETURN,
kCONTINUE,
};
enum {
kTUNE = 1,
kENDTUNE,
kTUNE_PREHEAD,
kTUNE_ONSET,
kTUNE_HEAD,
kTUNE_HEADENV,
kTUNE_HEADEXTEND,
kTUNE_HEADLAST,
kTUNE_NUCLEUS0,
kTUNE_NUCLEUS1,
kTUNE_SPLIT,
};
static const unsigned char utf8_bom[] = { 0xef, 0xbb, 0xbf, 0 };
static const keywtab_t k_intonation[] = {
{ "tune", 0, kTUNE },
{ "endtune", 0, kENDTUNE },
{ "prehead", 0, kTUNE_PREHEAD },
{ "onset", 0, kTUNE_ONSET },
{ "head", 0, kTUNE_HEAD },
{ "headenv", 0, kTUNE_HEADENV },
{ "headextend", 0, kTUNE_HEADEXTEND },
{ "headlast", 0, kTUNE_HEADLAST },
{ "nucleus0", 0, kTUNE_NUCLEUS0 },
{ "nucleus", 0, kTUNE_NUCLEUS1 },
{ "split", 0, kTUNE_SPLIT },
{ NULL, 0, -1 }
};
static const keywtab_t keywords[] = {
{ "liquid", tPHONEME_TYPE, phLIQUID },
{ "pause", tPHONEME_TYPE, phPAUSE },
{ "stress", tPHONEME_TYPE, phSTRESS },
{ "virtual", tPHONEME_TYPE, phVIRTUAL },
{ "delete_phoneme", tPHONEME_TYPE, phDELETED },
// keywords
{ "phonemetable", tSTATEMENT, kPHONEMETABLE },
{ "include", tSTATEMENT, kINCLUDE },
{ (const char *)utf8_bom, tSTATEMENT, kUTF8_BOM },
{ "phoneme", tSTATEMENT, kPHONEMESTART },
{ "procedure", tSTATEMENT, kPROCEDURE },
{ "endphoneme", tSTATEMENT, kENDPHONEME },
{ "endprocedure", tSTATEMENT, kENDPROCEDURE },
{ "import_phoneme", tSTATEMENT, kIMPORT_PH },
{ "stress_type", tSTATEMENT, kSTRESSTYPE },
{ "starttype", tSTATEMENT, kSTARTTYPE },
{ "endtype", tSTATEMENT, kENDTYPE },
{ "voicingswitch", tSTATEMENT, kVOICINGSWITCH },
{ "IF", tSTATEMENT, kIF },
{ "ELSE", tSTATEMENT, kELSE },
{ "ELIF", tSTATEMENT, kELIF },
{ "ELSEIF", tSTATEMENT, kELIF }, // same as ELIF
{ "ENDIF", tSTATEMENT, kENDIF },
{ "CALL", tSTATEMENT, kCALLPH },
{ "RETURN", tSTATEMENT, kRETURN },
{ "PrevVowelEndings", tSTATEMENT, kSWITCH_PREVVOWEL },
{ "NextVowelStarts", tSTATEMENT, kSWITCH_NEXTVOWEL },
{ "EndSwitch", tSTATEMENT, kENDSWITCH },
{ "Tone", tSTATEMENT, kTONESPEC },
{ "FMT", tSTATEMENT, kFMT },
{ "WAV", tSTATEMENT, kWAV },
{ "VowelStart", tSTATEMENT, kVOWELSTART },
{ "VowelEnding", tSTATEMENT, kVOWELENDING },
{ "addWav", tSTATEMENT, kANDWAV },
{ "Vowelin", tSTATEMENT, kVOWELIN },
{ "Vowelout", tSTATEMENT, kVOWELOUT },
{ "Continue", tSTATEMENT, kCONTINUE },
{ "ChangePhoneme", tINSTRN1, i_CHANGE_PHONEME },
{ "ChangeNextPhoneme", tINSTRN1, i_REPLACE_NEXT_PHONEME },
{ "InsertPhoneme", tINSTRN1, i_INSERT_PHONEME },
{ "AppendPhoneme", tINSTRN1, i_APPEND_PHONEME },
{ "IfNextVowelAppend", tINSTRN1, i_APPEND_IFNEXTVOWEL },
{ "ChangeIfDiminished", tINSTRN1, i_CHANGE_IF | STRESS_IS_DIMINISHED },
{ "ChangeIfUnstressed", tINSTRN1, i_CHANGE_IF | STRESS_IS_UNSTRESSED },
{ "ChangeIfNotStressed", tINSTRN1, i_CHANGE_IF | STRESS_IS_NOT_STRESSED },
{ "ChangeIfStressed", tINSTRN1, i_CHANGE_IF | STRESS_IS_SECONDARY },
{ "ChangeIfStressed", tINSTRN1, i_CHANGE_IF | STRESS_IS_PRIMARY },
{ "PauseBefore", tINSTRN1, i_PAUSE_BEFORE },
{ "PauseAfter", tINSTRN1, i_PAUSE_AFTER },
{ "length", tINSTRN1, i_SET_LENGTH },
{ "LongLength", tINSTRN1, i_LONG_LENGTH },
{ "LengthAdd", tINSTRN1, i_ADD_LENGTH },
{ "lengthmod", tINSTRN1, i_LENGTH_MOD },
{ "ipa", tINSTRN1, i_IPA_NAME },
// flags
{ "unstressed", tPHONEME_FLAG, phUNSTRESSED },
{ "nolink", tPHONEME_FLAG, phNOLINK },
{ "brkafter", tPHONEME_FLAG, phBRKAFTER },
{ "rhotic", tPHONEME_FLAG, phRHOTIC },
{ "lengthenstop", tPHONEME_FLAG, phLENGTHENSTOP },
{ "nopause", tPHONEME_FLAG, phNOPAUSE },
{ "prevoice", tPHONEME_FLAG, phPREVOICE },
{ "flag1", tPHONEME_FLAG, phFLAG1 },
{ "flag2", tPHONEME_FLAG, phFLAG2 },
// vowel transition attributes
{ "len=", tTRANSITION, 1 },
{ "rms=", tTRANSITION, 2 },
{ "f1=", tTRANSITION, 3 },
{ "f2=", tTRANSITION, 4 },
{ "f3=", tTRANSITION, 5 },
{ "brk", tTRANSITION, 6 },
{ "rate", tTRANSITION, 7 },
{ "glstop", tTRANSITION, 8 },
{ "lenadd", tTRANSITION, 9 },
{ "f4", tTRANSITION, 10 },
{ "gpaus", tTRANSITION, 11 },
{ "colr=", tTRANSITION, 12 },
{ "amp=", tTRANSITION, 13 }, // set rms of 1st frame as fraction of rms of 2nd frame (1/30ths)
{ NULL, 0, -1 }
};
static const keywtab_t *keyword_tabs[] = {
keywords, k_conditions, k_properties, k_intonation
};
typedef struct {
void *link;
int value;
int ph_mnemonic;
short ph_table;
char string[1];
} REF_HASH_TAB;
typedef struct {
FILE *file;
int linenum;
char fname[80];
} STACK;
typedef struct {
unsigned short *p_then;
unsigned short *p_else;
bool returned;
} IF_STACK;
enum {
tENDFILE = 1,
tSTRING,
tNUMBER,
tSIGNEDNUMBER,
tPHONEMEMNEM,
tOPENBRACKET,
tKEYWORD,
tCONDITION,
tPROPERTIES,
tINTONATION,
};
typedef struct CompileContext {
PHONEME_TAB *phoneme_out;
int n_phcodes_list[N_PHONEME_TABS];
PHONEME_TAB_LIST phoneme_tab_list2[N_PHONEME_TABS];
PHONEME_TAB *phoneme_tab2;
int phoneme_flags;
#define N_PROCS 50
int n_procs;
int proc_addr[N_PROCS];
char proc_names[N_ITEM_STRING+1][N_PROCS];
#define MAX_PROG_BUF 2000
unsigned short *prog_out;
unsigned short *prog_out_max;
unsigned short prog_buf[MAX_PROG_BUF+20];
int n_phoneme_tabs;
int n_phcodes;
// outout files
FILE *f_phdata;
FILE *f_phindex;
FILE *f_phtab;
FILE *f_phcontents;
FILE *f_errors;
FILE *f_prog_log;
FILE *f_in;
int f_in_linenum;
int f_in_displ;
int linenum;
int count_references;
int duplicate_references;
int count_frames;
int error_count;
int then_count;
bool after_if;
char current_fname[80];
REF_HASH_TAB *ref_hash_tab[256];
#define N_STACK 12
int stack_ix;
STACK stack[N_STACK];
#define N_IF_STACK 12
int if_level;
IF_STACK if_stack[N_IF_STACK];
int item_type;
int item_terminator;
char item_string[N_ITEM_STRING];
NAMETAB *manifest;
int n_manifest;
char phsrc[sizeof(path_home)+40]; // Source: path to the 'phonemes' source file.
} CompileContext;
static void clean_context(CompileContext *ctx) {
for (int i = 0; i < 256; i++) {
REF_HASH_TAB *p;
while ((p = ctx->ref_hash_tab[i])) {
ctx->ref_hash_tab[i] = (REF_HASH_TAB*)p->link;
free(p);
}
}
for (int i = 0; i < ctx->n_manifest; i++) {
free(ctx->manifest[i].name);
}
free(ctx->manifest);
free(ctx);
}
static void error(CompileContext *ctx, const char *format, ...)
{
va_list args;
va_start(args, format);
fprintf(ctx->f_errors, "%s(%d): ", ctx->current_fname, ctx->linenum-1);
vfprintf(ctx->f_errors, format, args);
fprintf(ctx->f_errors, "\n");
ctx->error_count++;
va_end(args);
}
static void error_from_status(CompileContext *ctx, espeak_ng_STATUS status, const char *context)
{
char message[512];
espeak_ng_GetStatusCodeMessage(status, message, sizeof(message));
if (context)
error(ctx, "%s: '%s'.", message, context);
else
error(ctx, "%s.", message);
}
static espeak_ng_STATUS ReadPhondataManifest(CompileContext *ctx, espeak_ng_ERROR_CONTEXT *context)
{
// Read the phondata-manifest file
FILE *f;
int n_lines = 0;
char *p;
unsigned int value;
char buf[sizeof(path_home)+40];
char name[120];
sprintf(buf, "%s%c%s", path_home, PATHSEP, "phondata-manifest");
if ((f = fopen(buf, "r")) == NULL)
return create_file_error_context(context, errno, buf);
while (fgets(buf, sizeof(buf), f) != NULL)
n_lines++;
rewind(f);
if (ctx->manifest != NULL) {
for (int ix = 0; ix < ctx->n_manifest; ix++)
free(ctx->manifest[ix].name);
}
if (n_lines == 0) {
fclose(f);
return ENS_EMPTY_PHONEME_MANIFEST;
}
NAMETAB *new_manifest = (NAMETAB *)realloc(ctx->manifest, n_lines * sizeof(NAMETAB));
if (new_manifest == NULL) {
fclose(f);
free(ctx->manifest);
return ENOMEM;
} else
ctx->manifest = new_manifest;
ctx->n_manifest = 0;
while (fgets(buf, sizeof(buf), f) != NULL) {
if (!isalpha(buf[0]))
continue;
if (sscanf(&buf[2], "%x %s", &value, name) == 2) {
if ((p = (char *)malloc(strlen(name)+1)) != NULL) {
strcpy(p, name);
ctx->manifest[ctx->n_manifest].value = value;
ctx->manifest[ctx->n_manifest].name = p;
ctx->n_manifest++;
}
}
}
fclose(f);
return ENS_OK;
}
static const MNEM_TAB reserved_phonemes[] = {
{ "_\001", phonCONTROL }, // NOT USED
{ "%", phonSTRESS_U },
{ "%%", phonSTRESS_D },
{ ",", phonSTRESS_2 },
{ ",,", phonSTRESS_3 },
{ "'", phonSTRESS_P },
{ "''", phonSTRESS_P2 },
{ "=", phonSTRESS_PREV }, // stress previous syllable
{ "_:", phonPAUSE }, // pause
{ "_", phonPAUSE_SHORT }, // short pause
{ "_!", phonPAUSE_NOLINK }, // short pause, no link
{ ":", phonLENGTHEN },
{ "@", phonSCHWA },
{ "@-", phonSCHWA_SHORT },
{ "||", phonEND_WORD },
{ "1", phonDEFAULTTONE }, // (numeral 1) default tone (for tone language)
{ "#X1", phonCAPITAL }, // capital letter indication
{ "?", phonGLOTTALSTOP }, // glottal stop
{ "-", phonSYLLABIC }, // syllabic consonant
{ "_^_", phonSWITCH }, // Change language
{ "_X1", phonX1 }, // a language specific action
{ "_|", phonPAUSE_VSHORT }, // very short pause
{ "_::", phonPAUSE_LONG }, // long pause
{ "t#", phonT_REDUCED }, // reduced version of [t]
{ "'!", phonSTRESS_TONIC }, // stress - emphasized
{ "_;_", phonPAUSE_CLAUSE }, // clause pause
{ "#@", phonVOWELTYPES }, // vowel type groups, these must be consecutive
{ "#a", phonVOWELTYPES+1 },
{ "#e", phonVOWELTYPES+2 },
{ "#i", phonVOWELTYPES+3 },
{ "#o", phonVOWELTYPES+4 },
{ "#u", phonVOWELTYPES+5 },
{ NULL, 0 }
};
static void ReservePhCodes(CompileContext *ctx)
{
// Reserve phoneme codes which have fixed numbers so that they can be
// referred to from the program code.
const MNEM_TAB *p;
p = reserved_phonemes;
while (p->mnem != NULL) {
ctx->phoneme_tab2[p->value].mnemonic = StringToWord(p->mnem);
ctx->phoneme_tab2[p->value].code = p->value;
if (ctx->n_phcodes <= p->value)
ctx->n_phcodes = p->value+1;
p++;
}
}
static int LookupPhoneme(CompileContext *ctx, const char *string, int control)
{
// control = 0 explicit declaration
// control = 1 declare phoneme if not found
// control = 2 start looking after control & stress phonemes
int ix;
int start;
int use;
unsigned int word;
if (strcmp(string, "NULL") == 0)
return 1;
ix = strlen(string);
if ((ix == 0) || (ix > 4))
error(ctx, "Bad phoneme name '%s'", string);
word = StringToWord(string);
// don't use phoneme number 0, reserved for string terminator
start = 1;
if (control == 2) {
// don't look for control and stress phonemes (allows these characters to be
// used for other purposes)
start = 8;
}
use = 0;
for (ix = start; ix < ctx->n_phcodes; ix++) {
if (ctx->phoneme_tab2[ix].mnemonic == word)
return ix;
if ((use == 0) && (ctx->phoneme_tab2[ix].mnemonic == 0))
use = ix;
}
if (use == 0) {
if (control == 0)
return -1;
if (ctx->n_phcodes >= N_PHONEME_TAB-1)
return -1; // phoneme table is full
use = ctx->n_phcodes++;
}
// add this phoneme to the phoneme table
ctx->phoneme_tab2[use].mnemonic = word;
ctx->phoneme_tab2[use].type = phINVALID;
ctx->phoneme_tab2[use].program = ctx->linenum; // for error report if the phoneme remains undeclared
return use;
}
static unsigned int get_char(CompileContext *ctx)
{
unsigned int c;
c = fgetc(ctx->f_in);
if (c == '\n')
ctx->linenum++;
return c;
}
static void unget_char(CompileContext *ctx, unsigned int c)
{
ungetc(c, ctx->f_in);
if (c == '\n')
ctx->linenum--;
}
static int CheckNextChar(CompileContext *ctx)
{
int c;
while (((c = get_char(ctx)) == ' ') || (c == '\t'))
;
unget_char(ctx, c);
return c;
}
static int NextItem(CompileContext *ctx, int type)
{
unsigned char c = 0;
unsigned char c2;
int ix;
const keywtab_t *pk;
ctx->item_type = -1;
ctx->f_in_displ = ftell(ctx->f_in);
ctx->f_in_linenum = ctx->linenum;
while (!feof(ctx->f_in)) {
c = get_char(ctx);
if (c == '/') {
if ((c2 = get_char(ctx)) == '/') {
// comment, ignore to end of line
while (!feof(ctx->f_in) && ((c = get_char(ctx)) != '\n'))
;
} else
unget_char(ctx, c2);
}
if (!isspace(c))
break;
}
if (feof(ctx->f_in))
return -2;
if (c == '(') {
if (type == tOPENBRACKET)
return 1;
return -1;
}
ix = 0;
while (!feof(ctx->f_in) && !isspace(c) && (c != '(') && (c != ')') && (c != ',')) {
if (c == '\\')
c = get_char(ctx);
ctx->item_string[ix++] = c;
c = get_char(ctx);
if (feof(ctx->f_in))
break;
if (ctx->item_string[ix-1] == '=')
break;
}
ctx->item_string[ix] = 0;
while (isspace(c))
c = get_char(ctx);
ctx->item_terminator = ' ';
if ((c == ')') || (c == '(') || (c == ','))
ctx->item_terminator = c;
if (!feof(ctx->f_in) && !(c == ')' || c == ','))
unget_char(ctx, c);
if (type == tSTRING)
return 0;
if ((type == tNUMBER) || (type == tSIGNEDNUMBER)) {
int acc = 0;
int sign = 1;
char *p;
p = ctx->item_string;
if ((*p == '-') && (type == tSIGNEDNUMBER)) {
sign = -1;
p++;
}
if (!isdigit(*p)) {
if ((type == tNUMBER) && (*p == '-'))
error(ctx, "Expected an unsigned number");
else
error(ctx, "Expected a number");
}
while (isdigit(*p)) {
acc *= 10;
acc += (*p - '0');
p++;
}
return acc * sign;
}
if ((type >= tKEYWORD) && (type <= tINTONATION)) {
pk = keyword_tabs[type-tKEYWORD];
while (pk->mnem != NULL) {
if (strcmp(ctx->item_string, pk->mnem) == 0) {
ctx->item_type = pk->type;
return pk->data;
}
pk++;
}
ctx->item_type = -1;
return -1; // keyword not found
}
if (type == tPHONEMEMNEM)
return LookupPhoneme(ctx, ctx->item_string, 2);
return -1;
}
static int NextItemMax(CompileContext *ctx, int max)
{
// Get a number, but restrict value to max
int value;
value = NextItem(ctx, tNUMBER);
if (value > max) {
error(ctx, "Value %d is greater than maximum %d", value, max);
value = max;
}
return value;
}
static int NextItemBrackets(CompileContext *ctx, int type, int control)
{
// Expect a parameter inside parentheses
// control: bit 0 0= need (
// bit 1 1= allow comma
int value;
if ((control & 1) == 0) {
if (!NextItem(ctx, tOPENBRACKET))
error(ctx, "Expected '('");
}
value = NextItem(ctx, type);
if ((control & 2) && (ctx->item_terminator == ','))
return value;
if (ctx->item_terminator != ')')
error(ctx, "Expected ')'");
return value;
}
static void UngetItem(CompileContext *ctx)
{
fseek(ctx->f_in, ctx->f_in_displ, SEEK_SET);
ctx->linenum = ctx->f_in_linenum;
}
static int Range(int value, int divide, int min, int max)
{
if (value < 0)
value -= divide/2;
else
value += divide/2;
value = value / divide;
if (value > max)
value = max;
if (value < min)
value = min;
return value - min;
}
static int CompileVowelTransition(CompileContext *ctx, int which)
{
// Compile a vowel transition
int len = 0;
int rms = 0;
int f1 = 0;
int f2 = 0;
int f2_min = 0;
int f2_max = 0;
int f3_adj = 0;
int f3_amp = 0;
int flags = 0;
int vcolour = 0;
int x;
int instn = i_VOWELIN;
int word1;
int word2;
if (which == 1) {
len = 50 / 2; // defaults for transition into vowel
rms = 25 / 2;
if (ctx->phoneme_out->type == phSTOP) {
len = 42 / 2; // defaults for transition into vowel
rms = 30 / 2;
}
} else if (which == 2) {
instn = i_VOWELOUT;
len = 36 / 2; // defaults for transition out of vowel
rms = 16 / 2;
}
for (;;) {
int key = NextItem(ctx, tKEYWORD);
if (ctx->item_type != tTRANSITION) {
UngetItem(ctx);
break;
}
switch (key & 0xf)
{
case 1:
len = Range(NextItem(ctx, tNUMBER), 2, 0, 63) & 0x3f;
flags |= 1;
break;
case 2:
rms = Range(NextItem(ctx, tNUMBER), 2, 0, 31) & 0x1f;
flags |= 1;
break;
case 3:
f1 = NextItem(ctx, tNUMBER);
break;
case 4:
f2 = Range(NextItem(ctx, tNUMBER), 50, 0, 63) & 0x3f;
f2_min = Range(NextItem(ctx, tSIGNEDNUMBER), 50, -15, 15) & 0x1f;
f2_max = Range(NextItem(ctx, tSIGNEDNUMBER), 50, -15, 15) & 0x1f;
if (f2_min > f2_max) {
x = f2_min;
f2_min = f2_max;
f2_max = x;
}
break;
case 5:
f3_adj = Range(NextItem(ctx, tSIGNEDNUMBER), 50, -15, 15) & 0x1f;
f3_amp = Range(NextItem(ctx, tNUMBER), 8, 0, 15) & 0x1f;
break;
case 6:
flags |= 2; // break
break;
case 7:
flags |= 4; // rate
break;
case 8:
flags |= 8; // glstop
break;
case 9:
flags |= 16; // lenadd
break;
case 10:
flags |= 32; // f4
break;
case 11:
flags |= 64; // pause
break;
case 12:
vcolour = NextItem(ctx, tNUMBER);
break;
case 13:
// set rms of 1st frame as fraction of rms of 2nd frame (1/30ths)
rms = (Range(NextItem(ctx, tNUMBER), 1, 0, 31) & 0x1f) | 0x20;
flags |= 1;
break;
}
}
word1 = len + (rms << 6) + (flags << 12);
word2 = f2 + (f2_min << 6) + (f2_max << 11) + (f3_adj << 16) + (f3_amp << 21) + (f1 << 26) + (vcolour << 29);
ctx->prog_out[0] = instn + ((word1 >> 16) & 0xff);
ctx->prog_out[1] = word1;
ctx->prog_out[2] = word2 >> 16;
ctx->prog_out[3] = word2;
ctx->prog_out += 4;
return 0;
}
static espeak_ng_STATUS LoadSpect(CompileContext *ctx, const char *path, int control, int *addr)
{
SpectSeq *spectseq;
int peak;
int frame;
int n_frames;
int ix;
int x, x2;
int rms;
float total;
float pkheight;
int marker1_set = 0;
int frame_vowelbreak = 0;
int klatt_flag = 0;
SpectFrame *fr;
frame_t *fr_out;
char filename[sizeof(path_home)+20];
SPECT_SEQ seq_out;
SPECT_SEQK seqk_out;
// create SpectSeq and import data
spectseq = SpectSeqCreate();
if (spectseq == NULL)
return ENOMEM;
snprintf(filename, sizeof(filename), "%s/%s", ctx->phsrc, path);
espeak_ng_STATUS status = LoadSpectSeq(spectseq, filename);
if (status != ENS_OK) {
error(ctx, "Bad vowel file: '%s'", path);
SpectSeqDestroy(spectseq);
return status;
}
// do we need additional klatt data ?
for (frame = 0; frame < spectseq->numframes; frame++) {
for (ix = 5; ix < N_KLATTP2; ix++) {
if (spectseq->frames[frame]->klatt_param[ix] != 0)
klatt_flag = FRFLAG_KLATT;
}
}
*addr = ftell(ctx->f_phdata);
seq_out.n_frames = 0;
seq_out.sqflags = 0;
seq_out.length_total = 0;
total = 0;
for (frame = 0; frame < spectseq->numframes; frame++) {
if (spectseq->frames[frame]->keyframe) {
if (seq_out.n_frames == 1)
frame_vowelbreak = frame;
if (spectseq->frames[frame]->markers & 0x2) {
// marker 1 is set
marker1_set = 1;
}
seq_out.n_frames++;
if (frame > 0)
total += spectseq->frames[frame-1]->length;
}
}
seq_out.length_total = (int)total;
if ((control & 1) && (marker1_set == 0)) {
// This is a vowel, but no Vowel Break marker is set
// set a marker flag for the second frame of a vowel
spectseq->frames[frame_vowelbreak]->markers |= FRFLAG_VOWEL_CENTRE;
}
n_frames = 0;
for (frame = 0; frame < spectseq->numframes; frame++) {
fr = spectseq->frames[frame];
if (fr->keyframe) {
if (klatt_flag)
fr_out = &seqk_out.frame[n_frames];
else
fr_out = (frame_t *)&seq_out.frame[n_frames];
x = (int)(fr->length + 0.5); // round to nearest mS
if (x > 255) x = 255;
fr_out->length = x;
fr_out->frflags = fr->markers | klatt_flag;
rms = (int)GetFrameRms(fr, spectseq->amplitude);
if (rms > 255) rms = 255;
fr_out->rms = rms;
if (n_frames == (seq_out.n_frames-1))
fr_out->length = 0; // give last frame zero length
// write: peak data
ctx->count_frames++;
for (peak = 0; peak < 8; peak++) {
if (peak < 7)
fr_out->ffreq[peak] = fr->peaks[peak].pkfreq;
pkheight = spectseq->amplitude * fr->amp_adjust * fr->peaks[peak].pkheight;
pkheight = pkheight/640000;
if (pkheight > 255) pkheight = 255;
fr_out->fheight[peak] = (int)pkheight;