-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
2032 lines (1513 loc) · 53.8 KB
/
main.lua
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
-- (c) 2020 David Cuny
-- Voice and Transition data from Software Automatic Mouth, by Don't Ask Software
-- The voiced portion of the voice is a sum of sine waves, one
-- for each harmonic up to a maximum frequency. The amplitude of
-- each sine wave is determined by the contribution of each formant,
-- which is specified by amplitude and center frequency. The bandwidth
-- is calculated from an ad-hoc equation I came up with, and the
-- falloff is assumed to be a sine curve.
-- The unvoiced portion is gaussian noise run through a series of
-- resonators (see Klatt) using the same formant values, but using
-- bandwidths calculated using a tube equation.
-- Unvoiced consonants are simulated using a single filter with
-- noise passed through it.
-- Do not include the .pho entension!
local songName = "red river valley"
-- Audio parameters-- CD sample quality
SAMPLE_RATE = 44100
-- Used to calculate filter response
TWO_PI = 2 * math.pi
MINUS_PI_T = -math.pi / SAMPLE_RATE
TWO_PI_T = TWO_PI / SAMPLE_RATE
-- Flags
local debugFlag = true -- true if debug messages should be printed
-- frame resolution 1/(frames per second)
local secondsPerFrame = 1/100
-- Build the phoneme database
local pData = {}
local pIndexByName = {}
local function buildPhoneme( data )
-- insert into the pData table
table.insert( pData, data )
-- set the total steps
data.parts = data.part
-- if part == 0, insert into name lookup
if data.part == 1 then
pIndexByName[data.token] = #pData
else
-- get the index of the primary
local i = pIndexByName[data.token]
if i == nil then
error("Phoneme '"..data.token.."' not yet in table.")
end
-- set the steps
pData[i].parts = data.parts
end
end
-- Return index of phoneme **s** in **pName** table.
-- Throws error if phoneme is not found.
local function getPhoneIndex( s )
local i = pIndexByName[s]
if not i then
error("Phoneme '"..s.."' not found.")
end
return i
end
-----------------------
-- THE PHONEME TABLE --
-----------------------
-- token name of the phoeneme
-- part some phonemes have multiple parts
-- f1 formant 1 frequency
-- f2 formant 2 frequency
-- f3 formant 3 frequency
-- a1 amplitude of formant 1
-- a2 amplitude of formant 2
-- a3 amplitude of formant 3
-- length duration, in frames
-- blendRank precendece of phoneme for selecting transition
-- blendIn number of frames to transition in from prior phoneme
-- blendOut number of frames to transition out to next phoneme
buildPhoneme{ token=" ", part=1, f1=0, f2=0, f3=0, a1=0, a2=0, a3=0, length=0, blendRank=0, blendIn=0, blendOut=0 }
buildPhoneme{ token=".", part=1, f1=520, f2=1836, f3=2494, a1=0, a2=0, a3=0, length=18, blendRank=31, blendIn=2, blendOut=2 }
buildPhoneme{ token="?", part=1, f1=520, f2=1836, f3=2494, a1=0, a2=0, a3=0, length=18, blendRank=31, blendIn=2, blendOut=2 }
buildPhoneme{ token=",", part=1, f1=520, f2=1836, f3=2494, a1=0, a2=0, a3=0, length=18, blendRank=31, blendIn=2, blendOut=2 }
buildPhoneme{ token="-", part=1, f1=520, f2=1836, f3=2494, a1=0, a2=0, a3=0, length=8, blendRank=31, blendIn=2, blendOut=2 }
buildPhoneme{ token="IY", part=1, f1=274, f2=2303, f3=3015, a1=11, a2=6, a3=4, length=8, blendRank=2, blendIn=4, blendOut=4 }
buildPhoneme{ token="IH", part=1, f1=383, f2=1974, f3=2549, a1=11, a2=8, a3=4, length=8, blendRank=2, blendIn=4, blendOut=4 }
buildPhoneme{ token="EH", part=1, f1=493, f2=1809, f3=2494, a1=14, a2=11, a3=4, length=8, blendRank=2, blendIn=4, blendOut=4 }
buildPhoneme{ token="AE", part=1, f1=658, f2=1699, f3=2412, a1=16, a2=14, a3=4, length=8, blendRank=2, blendIn=4, blendOut=4 }
buildPhoneme{ token="AA", part=1, f1=712, f2=1096, f3=2440, a1=16, a2=11, a3=1, length=11, blendRank=2, blendIn=4, blendOut=4 }
buildPhoneme{ token="AH", part=1, f1=603, f2=1206, f3=2385, a1=16, a2=9, a3=1, length=6, blendRank=2, blendIn=4, blendOut=4 }
buildPhoneme{ token="AO", part=1, f1=548, f2=822, f3=2412, a1=16, a2=9, a3=0, length=12, blendRank=2, blendIn=4, blendOut=4 }
buildPhoneme{ token="UH", part=1, f1=438, f2=987, f3=2248, a1=16, a2=8, a3=1, length=10, blendRank=2, blendIn=4, blendOut=4 }
buildPhoneme{ token="AX", part=1, f1=548, f2=1206, f3=2440, a1=9, a2=5, a3=0, length=5, blendRank=2, blendIn=4, blendOut=4 }
buildPhoneme{ token="IX", part=1, f1=383, f2=1974, f3=2549, a1=11, a2=8, a3=4, length=5, blendRank=5, blendIn=4, blendOut=4 }
buildPhoneme{ token="ER", part=1, f1=493, f2=1316, f3=1699, a1=9, a2=8, a3=3, length=11, blendRank=5, blendIn=4, blendOut=4 }
buildPhoneme{ token="UX", part=1, f1=383, f2=987, f3=2248, a1=16, a2=9, a3=1, length=10, blendRank=2, blendIn=4, blendOut=4 }
buildPhoneme{ token="OH", part=1, f1=493, f2=822, f3=2412, a1=16, a2=9, a3=0, length=10, blendRank=10, blendIn=4, blendOut=4 }
buildPhoneme{ token="RX", part=1, f1=493, f2=1370, f3=1699, a1=11, a2=9, a3=3, length=10, blendRank=2, blendIn=3, blendOut=3 }
buildPhoneme{ token="LX", part=1, f1=438, f2=987, f3=3015, a1=11, a2=4, a3=1, length=9, blendRank=8, blendIn=3, blendOut=2 }
buildPhoneme{ token="WX", part=1, f1=329, f2=767, f3=2193, a1=11, a2=4, a3=0, length=8, blendRank=5, blendIn=4, blendOut=4 }
buildPhoneme{ token="YX", part=1, f1=383, f2=1864, f3=2549, a1=14, a2=9, a3=4, length=7, blendRank=5, blendIn=4, blendOut=4 }
buildPhoneme{ token="WH", part=1, f1=274, f2=658, f3=2467, a1=11, a2=4, a3=0, length=9, blendRank=11, blendIn=3, blendOut=2 }
buildPhoneme{ token="R", part=1, f1=493, f2=1370, f3=1645, a1=9, a2=6, a3=3, length=7, blendRank=10, blendIn=3, blendOut=2 }
buildPhoneme{ token="L", part=1, f1=383, f2=822, f3=3015, a1=11, a2=4, a3=1, length=6, blendRank=9, blendIn=3, blendOut=2 }
buildPhoneme{ token="W", part=1, f1=274, f2=658, f3=2467, a1=11, a2=4, a3=0, length=8, blendRank=8, blendIn=3, blendOut=2 }
buildPhoneme{ token="Y", part=1, f1=219, f2=2248, f3=3015, a1=11, a2=6, a3=4, length=6, blendRank=8, blendIn=3, blendOut=2 }
buildPhoneme{ token="M", part=1, f1=164, f2=1261, f3=2220, a1=9, a2=2, a3=0, length=7, blendRank=160, blendIn=1, blendOut=1 }
buildPhoneme{ token="N", part=1, f1=164, f2=1480, f3=3317, a1=5, a2=5, a3=0, length=7, blendRank=8, blendIn=2, blendOut=1 }
buildPhoneme{ token="NX", part=1, f1=164, f2=2357, f3=2769, a1=5, a2=3, a3=2, length=7, blendRank=8, blendIn=3, blendOut=1 }
buildPhoneme{ token="DX", part=1, f1=164, f2=1480, f3=3317, a1=0, a2=0, a3=0, length=2, blendRank=23, blendIn=2, blendOut=1 }
buildPhoneme{ token="Q", part=1, f1=466, f2=1836, f3=2494, a1=0, a2=0, a3=0, length=5, blendRank=31, blendIn=1, blendOut=1 }
--buildPhoneme{ token="S", part=1, f1=164, f2=2001, f3=2714, a1=0, a2=0, a3=0, length=2, blendRank=18, blendIn=3, blendOut=1 }
buildPhoneme{ token="S", part=1, f1=164, f2=2001, f3=2714, a1=0, a2=0, a3=0, length=4, blendRank=18, blendIn=3, blendOut=1 }
--buildPhoneme{ token="SH", part=1, f1=164, f2=2165, f3=2906, a1=0, a2=0, a3=0, length=2, blendRank=18, blendIn=3, blendOut=1 }
buildPhoneme{ token="SH", part=1, f1=164, f2=2165, f3=2906, a1=0, a2=0, a3=0, length=4, blendRank=18, blendIn=3, blendOut=1 }
--buildPhoneme{ token="F", part=1, f1=164, f2=712, f3=2220, a1=0, a2=0, a3=0, length=2, blendRank=18, blendIn=3, blendOut=1 }
buildPhoneme{ token="F", part=1, f1=164, f2=712, f3=2220, a1=0, a2=0, a3=0, length=4, blendRank=18, blendIn=3, blendOut=1 }
buildPhoneme{ token="TH", part=1, f1=164, f2=1809, f3=3317, a1=0, a2=0, a3=0, length=2, blendRank=18, blendIn=3, blendOut=1 }
buildPhoneme{ token="HH", part=1, f1=383, f2=2001, f3=2549, a1=0, a2=0, a3=0, length=2, blendRank=30, blendIn=1, blendOut=1 }
buildPhoneme{ token="QX", part=1, f1=438, f2=1014, f3=2248, a1=0, a2=0, a3=0, length=2, blendRank=30, blendIn=1, blendOut=1 }
buildPhoneme{ token="Z", part=1, f1=246, f2=1398, f3=2549, a1=8, a2=2, a3=0, length=6, blendRank=20, blendIn=3, blendOut=2 }
buildPhoneme{ token="ZH", part=1, f1=274, f2=1809, f3=2823, a1=8, a2=3, a3=1, length=6, blendRank=20, blendIn=3, blendOut=2 }
buildPhoneme{ token="V", part=1, f1=219, f2=1096, f3=2083, a1=8, a2=2, a3=0, length=7, blendRank=20, blendIn=3, blendOut=2 }
buildPhoneme{ token="DH", part=1, f1=274, f2=1288, f3=2549, a1=8, a2=2, a3=0, length=6, blendRank=20, blendIn=2, blendOut=1 }
buildPhoneme{ token="CH", part=1, f1=164, f2=2165, f3=2769, a1=0, a2=0, a3=0, length=6, blendRank=23, blendIn=2, blendOut=0 }
buildPhoneme{ token="CH", part=2, f1=164, f2=2165, f3=2769, a1=0, a2=0, a3=0, length=2, blendRank=23, blendIn=3, blendOut=1 }
buildPhoneme{ token="J", part=1, f1=164, f2=1809, f3=3317, a1=1, a2=0, a3=0, length=8, blendRank=26, blendIn=2, blendOut=0 }
buildPhoneme{ token="J", part=2, f1=137, f2=2165, f3=2769, a1=8, a2=3, a3=1, length=3, blendRank=26, blendIn=3, blendOut=1 }
buildPhoneme{ token="J", part=3, f1=164, f2=3015, f3=3317, a1=0, a2=6, a3=14, length=1, blendRank=29, blendIn=0, blendOut=0 }
buildPhoneme{ token="J", part=4, f1=0, f2=0, f3=0, a1=2, a2=2, a3=1, length=30, blendRank=29, blendIn=0, blendOut=5 }
buildPhoneme{ token="EY", part=1, f1=493, f2=1974, f3=2467, a1=14, a2=14, a3=5, length=13, blendRank=2, blendIn=5, blendOut=5 }
buildPhoneme{ token="AY", part=1, f1=712, f2=1041, f3=2412, a1=16, a2=11, a3=1, length=12, blendRank=2, blendIn=5, blendOut=5 }
buildPhoneme{ token="OY", part=1, f1=548, f2=822, f3=2412, a1=16, a2=9, a3=0, length=12, blendRank=2, blendIn=5, blendOut=5 }
buildPhoneme{ token="AW", part=1, f1=712, f2=1151, f3=2412, a1=16, a2=11, a3=1, length=12, blendRank=2, blendIn=5, blendOut=5 }
buildPhoneme{ token="OW", part=1, f1=493, f2=822, f3=2412, a1=16, a2=9, a3=0, length=14, blendRank=2, blendIn=4, blendOut=4 }
buildPhoneme{ token="UW", part=1, f1=329, f2=932, f3=2248, a1=11, a2=4, a3=0, length=9, blendRank=2, blendIn=4, blendOut=4 }
buildPhoneme{ token="B", part=1, f1=164, f2=712, f3=2220, a1=2, a2=0, a3=0, length=6, blendRank=26, blendIn=2, blendOut=2 }
buildPhoneme{ token="B", part=2, f1=164, f2=712, f3=2220, a1=2, a2=1, a3=0, length=1, blendRank=29, blendIn=0, blendOut=0 }
buildPhoneme{ token="B", part=3, f1=164, f2=712, f3=2220, a1=0, a2=0, a3=0, length=2, blendRank=27, blendIn=2, blendOut=1 }
--buildPhoneme{ token="D", part=1, f1=164, f2=1809, f3=3317, a1=2, a2=0, a3=0, length=5, blendRank=26, blendIn=2, blendOut=2 }
buildPhoneme{ token="D", part=1, f1=164, f2=1809, f3=3317, a1=2, a2=0, a3=0, length=4, blendRank=26, blendIn=1, blendOut=1 }
buildPhoneme{ token="D", part=2, f1=164, f2=1809, f3=3317, a1=2, a2=1, a3=0, length=1, blendRank=29, blendIn=0, blendOut=0 }
buildPhoneme{ token="D", part=3, f1=164, f2=1809, f3=3317, a1=0, a2=0, a3=0, length=1, blendRank=27, blendIn=3, blendOut=1 }
buildPhoneme{ token="G", part=1, f1=164, f2=3015, f3=3070, a1=1, a2=0, a3=0, length=6, blendRank=26, blendIn=2, blendOut=2 }
buildPhoneme{ token="G", part=2, f1=164, f2=3015, f3=3015, a1=2, a2=1, a3=0, length=1, blendRank=29, blendIn=0, blendOut=0 }
buildPhoneme{ token="G", part=3, f1=164, f2=3015, f3=3015, a1=0, a2=0, a3=0, length=2, blendRank=27, blendIn=4, blendOut=1 }
buildPhoneme{ token="GX", part=1, f1=164, f2=2303, f3=2577, a1=1, a2=0, a3=0, length=6, blendRank=26, blendIn=2, blendOut=2 }
buildPhoneme{ token="GX", part=2, f1=164, f2=2303, f3=2577, a1=2, a2=1, a3=0, length=1, blendRank=29, blendIn=0, blendOut=0 }
buildPhoneme{ token="GX", part=3, f1=164, f2=2303, f3=2577, a1=0, a2=0, a3=0, length=2, blendRank=27, blendIn=3, blendOut=1 }
buildPhoneme{ token="P", part=1, f1=164, f2=712, f3=2220, a1=0, a2=0, a3=0, length=8, blendRank=23, blendIn=2, blendOut=2 }
buildPhoneme{ token="P", part=2, f1=164, f2=712, f3=2220, a1=0, a2=0, a3=0, length=2, blendRank=29, blendIn=0, blendOut=0 }
buildPhoneme{ token="P", part=3, f1=164, f2=712, f3=2220, a1=0, a2=0, a3=0, length=2, blendRank=23, blendIn=2, blendOut=2 }
--buildPhoneme{ token="T", part=1, f1=164, f2=1809, f3=3317, a1=0, a2=0, a3=0, length=4, blendRank=23, blendIn=2, blendOut=2 }
buildPhoneme{ token="T", part=1, f1=164, f2=1809, f3=3317, a1=0, a2=0, a3=0, length=1, blendRank=23, blendIn=2, blendOut=2 }
buildPhoneme{ token="T", part=2, f1=164, f2=1809, f3=3317, a1=0, a2=0, a3=0, length=2, blendRank=29, blendIn=0, blendOut=0 }
buildPhoneme{ token="T", part=3, f1=164, f2=1809, f3=3317, a1=0, a2=0, a3=0, length=2, blendRank=23, blendIn=2, blendOut=1 }
buildPhoneme{ token="K", part=1, f1=164, f2=2988, f3=2769, a1=0, a2=0, a3=0, length=6, blendRank=23, blendIn=3, blendOut=3 }
buildPhoneme{ token="K", part=2, f1=274, f2=2357, f3=2769, a1=9, a2=6, a3=4, length=1, blendRank=29, blendIn=0, blendOut=0 }
buildPhoneme{ token="K", part=3, f1=274, f2=2988, f3=3070, a1=0, a2=0, a3=0, length=4, blendRank=23, blendIn=3, blendOut=2 }
buildPhoneme{ token="KX", part=1, f1=164, f2=2303, f3=2577, a1=0, a2=0, a3=0, length=6, blendRank=23, blendIn=3, blendOut=3 }
buildPhoneme{ token="KX", part=2, f1=164, f2=2303, f3=2577, a1=0, a2=6, a3=3, length=1, blendRank=29, blendIn=0, blendOut=0 }
buildPhoneme{ token="KX", part=3, f1=164, f2=2303, f3=2577, a1=0, a2=0, a3=0, length=4, blendRank=23, blendIn=3, blendOut=2 }
buildPhoneme{ token="UL", part=1, f1=1206, f2=3481, f3=219, a1=15, a2=0, a3=19, length=199, blendRank=23, blendIn=176, blendOut=160 }
buildPhoneme{ token="UM", part=1, f1=520, f2=3481, f3=27, a1=15, a2=0, a3=16, length=255, blendRank=23, blendIn=160, blendOut=160 }
buildPhoneme{ token="UN", part=1, f1=0, f2=0, f3=0, a1=0, a2=0, a3=0, length=0, blendRank=0, blendIn=0, blendOut=0 }
buildPhoneme{ token="IL", part=1, f1=44, f2=127, f3=8, a1=15, a2=0, a3=19, length=199, blendRank=23, blendIn=176, blendOut=160 }
buildPhoneme{ token="IM", part=1, f1=19, f2=127, f3=1, a1=15, a2=0, a3=16, length=255, blendRank=23, blendIn=160, blendOut=160 }
buildPhoneme{ token="IN", part=1, f1=0, f2=0, f3=0, a1=0, a2=0, a3=0, length=0, blendRank=0, blendIn=0, blendOut=0 }
-- Set the fricative for the phoneme
local function setFricative( token, part, frequency, bandwidth, amplitude )
-- find root token
local p = getPhoneIndex(token)
if pData[p+part-1].part ~= part then
error("Phoneme '"..token.."' does not have a part "..part)
else
-- offset to the part
p = p + part - 1
end
-- store the values
pData[p].fricFrq = frequency
pData[p].fricBw = bandwidth
pData[p].fricAmp = amplitude
end
-- Add frication data to phonemes
setFricative( "S", 1, 4500, 300, 1 )
setFricative( "SH", 1, 2600, 250, 1 )
setFricative( "CH", 1, 4500, 100, 1 )
setFricative( "CH", 2, 4500, 100, 1 )
setFricative( "TH", 1, 1200, 500, 1 )
setFricative( "F", 1, 1200, 500, 1 )
setFricative( "HH", 1, 500, 500, 1 )
-- Add "stop" frication data to phonemes
setFricative( "P", 1, 2600, 50, 0 )
setFricative( "K", 1, 2600, 50, 0 )
setFricative( "G", 1, 2600, 50, 0 )
setFricative( "KX", 1, 2600, 50, 0 )
setFricative( "GX", 1, 2600, 50, 0 )
setFricative( "B", 1, 2600, 50, 0 )
setFricative( "P", 1, 2600, 50, 0 )
setFricative( "T", 1, 4500, 100, 0)
setFricative( "D", 1, 4500, 100, 0 )
-- Fricative data following stop
setFricative( "K", 2, 2600, 50, 1 )
setFricative( "KX", 2, 2600, 50, 1 )
setFricative( "G", 2, 2600, 50, 1 )
setFricative( "GX", 2, 2600, 50, 1 )
setFricative( "B", 2, 2600, 50, 1 )
setFricative( "P", 2, 2600, 50, 1 )
setFricative( "T", 2, 4500, 100, 1 )
setFricative( "D", 2, 4500, 100, 1 )
-- release aspiration portion of phoneme
setFricative( "K", 3, 500, 500, 1 )
setFricative( "KX", 3, 500, 500, 1 )
setFricative( "G", 3, 500, 500, 1 )
setFricative( "GX", 3, 500, 500, 1 )
setFricative( "B", 3, 500, 500, 1 )
setFricative( "P", 3, 500, 500, 1 )
setFricative( "T", 3, 500, 500, 1 )
-- return a table which returns true if key is in the table
local function flagTable(phoneNames)
-- the table to build
local t = {}
-- set the indexes to true for each phoneme name
for _, name in ipairs(phoneNames) do
t[getPhoneIndex(name)] = true
end
-- return the table
return t
end
-- classification data for phonemes
local isUnvoicedPlosive = flagTable{"P","T","K","KX"}
local isDiphthong = flagTable{"EY","AY","OY","AW","OW","UW"}
local isBackVowel = flagTable{"IY","IH","EH","AE","AA","AH","AX","IX","EY","AY","OY"}
local isVowel = flagTable{"IY","IH","EH","AE","AA","AH","AO","UH","AX","IX","ER","UX","OH","RX","LX",
"WX","YX","EY","AY","OY","AW","OW","UW","UL","UM","UN"}
local isAlveolar = flagTable{"N","DX","S","TH","Z","DH","D","T"}
-- Text input text file is converted to this format
local outIndex = {} -- index to the pData vocal information
local outLength = {} -- the duration, in frames, for the phoneme
local outPitch = {} -- the pitch of the phonemes
local outBlendLeft = {} -- duration of blend in
local outBlendRight = {}
local gTempo = 120 -- default tempo
local gPitch = 220 -- default pitch
local gTranspose = 0 -- current transposition
local gPercent = 1 -- note duration can be modified by percent
local gBeats = 0 -- beat the nucleus gets
-- return true if value is inTable
local function inTable( theValue, theTable)
for _, value in ipairs( theTable ) do
if value == theValue then return true end
end
return false
end
-- phonemes, arranged by character length
local validPhones = {
-- vowels
"IY", "AE", "AH", "ER", "QX", "IH", "AA", "UH", "AX", "EH", "AO", "OH", "IX", "UX",
-- diphthongs
"EY", "AW", "AY", "OW", "OY", "UW",
-- contractions,
"UL", "IM", "UN", "IL", "IM", "IN",
-- consonants
"ZH", "WH", "DH", "DX", "HH", "NX", "SH", "CH", "TH",
"R", "Y", "M", "S", "B", "K", "J", "L", "N", "Z", "F", "T",
"G", "W", "V", "P", "D", "CH", "Q"
}
-- phonemes that are nucleus phonemes (core vowels)
local isNucleus = {
"Q",
"IY", "AE", "AH", "ER", "QX", "IH", "AA", "UH", "AX", "EH", "AO", "OH", "IX", "UX",
"EY", "AW", "AY", "OW", "OY", "UW",
"UL", "IM", "UN", "IL", "IM", "IN"
}
-------------------------------------------
-- CONVERT A .PHO FILE INTO PHONEME DATA --
-------------------------------------------
-- throw an error if the parameter to cmd is not integer
local function isNumericParm(cmd, parm, low, high)
local i = tonumber( parm )
if not i then
-- not a number
error("Expected number, not '"..parm.."' in command "..cmd)
elseif (low and i < low) or (high and i > high) then
-- out of range
error("Parameter '"..parm.."' in command "..cmd.." must be between "..low.." and "..high)
end
end
-- throw an error if the parameter to cmd is not integer
local function isIntegerParm(cmd, parm, low, high)
local i = tonumber( parm )
-- numeric and integer
if not i or math.floor(i) ~= i then
error("Expected integer, not '"..parm.."' in command "..cmd)
end
isNumericParm(cmd, parm, low, high)
end
-- Return the frequency of a note
function noteToFrequency( step, octave )
local pitch, scale
-- fix case
step = string.lower(step)
-- relative to octave 3
if step == "b#" or step == "c" then
pitch = 130.81
elseif step == "c#" or step == "db" then
pitch = 138.59
elseif step == "d" then
pitch = 146.83
elseif step == "d#" or step == "eb" then
pitch = 155.56
elseif step == "e" then
pitch = 164.81
elseif step == "e#" or step == "f" then
pitch = 174.61
elseif step == "f#" or step == "gb" then
pitch = 185.00
elseif step == "g" then
pitch = 196.00
elseif step == "g#" or step == "ab" then
pitch = 207.65
elseif step == "a" then
pitch = 220.00
elseif step == "a#" or step == "bb" then
pitch = 233.08
elseif step == "b" then
pitch = 246.94
else
error("Unknown step '"..step.."' in note '"..step..octave.."'" )
end
if octave == "1" then
scale = .25
elseif octave == "2" then
scale = .5
elseif octave == "3" then
scale = 1
elseif octave == "4" then
scale = 2
elseif octave == "5" then
scale = 4
else
error("Unknown octave '"..step.."' in note '"..step..octave.."'" )
end
return pitch * scale
end
-- return the frequency plus the given number of cents
function transpose(frequency, halfSteps)
return frequency * math.pow(2, halfSteps/12)
end
local function splitOnSpaces( s )
local words = {}
local word = ""
-- loop through the text
for i = 1, string.len(s) do
-- get a character
local c = string.sub(s, i, i)
-- space?
if c == " " then
-- is there a word accumulated?
if word ~= "" then
-- insert into the word table and clear the word
table.insert( words, word )
word = ""
end
else
-- add the character to the word
word = word .. c
end
end
-- final word?
if word ~= "" then
-- add to the table
table.insert(words, word)
end
return words
end
local function doCmd( text )
-- split text into words
local words = splitOnSpaces( text )
-- get command and optional parameter
local cmd = words[1]
local parm = words[2]
local parmValue = tonumber(parm)
-- check length
if #words == 0 then
return
elseif #words == 1
and string.sub( cmd, -1 ) == "%" then
-- percent duration change
gPercent = tonumber(string.sub(cmd, 1, string.len(cmd)-1))/100
return
elseif #words == 1 then
-- note
local step, accidental, octave
if string.len( cmd ) == 2 then
step, octave = string.sub(cmd, 1, 1), string.sub(cmd,2,2)
else
step, accidental, octave = string.sub(cmd, 1, 2), string.sub(cmd,2,2), string.sub(cmd,3,3)
end
print("Note", step, accidental, octave)
-- get the pitch frequency
gPitch = noteToFrequency( step, octave )
-- transpose?
if gTranspose ~= 0 then
gPitch = transpose( gPitch, gTranspose )
end
return
elseif #words > 2 then
error("Wrong number of parameters in command {"..text.."}")
end
print( "Cmd", cmd, parm, parmValue)
-- nothing
if cmd == "ahbias" then
isNumericParm(cmd, parmValue)
elseif cmd == "avbias" then
isNumericParm(cmd, parmValue)
elseif cmd == "f0style" then
-- FIXME
elseif cmd == "mod" then
isNumericParm(cmd, parmValue)
elseif cmd == "modramp" then
isNumericParm(cmd, parmValue)
elseif cmd == "modrate" then
isNumericParm(cmd, parmValue)
elseif cmd == "perturb" then
isNumericParm(cmd, parmValue)
elseif cmd == "rate" then
isNumericParm(cmd, parmValue)
elseif cmd == "tempo" then
isIntegerParm(cmd, parmValue, 20, 360)
-- set the global tempo
gTempo = parmValue
elseif cmd == "tonebias" then
isNumericParm(cmd, parmValue)
elseif cmd == "tonelo" then
isNumericParm(cmd, parmValue)
elseif cmd == "transpose" then
isIntegerParm(cmd, parmValue, -36, 36)
gTranspose = parmValue
elseif cmd == "tremelo" then
isNumericParm(cmd, parmValue)
elseif cmd == "vfactor" then
isNumericParm(cmd, parmValue)
elseif cmd == "vibrato" then
isNumericParm(cmd, parmValue)
elseif cmd == "voice" then
if not inTable( parm, {"male", "female", "child"}) then
error("Bad parameter '"..parm.."' in command f0style.")
end
else
error("Unknown command '"..cmd.."'")
end
end
-- parse out the semi-colon demlimited commands
local function parseCommand( text )
local at = 1
while true do
-- look for a semicolon
local semiAt = string.find( text, ";", at, true )
-- none found?
if not semiAt then
-- execute the remainder of the string
doCmd( string.sub( text, at ))
return
end
-- execute the portion to the semicolon
doCmd( string.sub( text, at, semiAt-1 ))
-- advance
at = semiAt + 1
end
end
-- parse out the contents of a .pho file
local function parsePho( text )
-- fixme: split into commands
local at = 1
while at <= string.len( text ) do
-- get a character from the string
local c = string.sub(text, at, at)
-- start of an embedded command?
if c == "{" then
-- find the closing brace
local closeAt = string.find( text, "}", at+1, true )
if not closeAt then
error("Missing '}' in string following "..string.sub(text, at))
end
-- get the command
local cmd = string.sub( text, at+1, closeAt-1 )
-- run the command
parseCommand( cmd )
-- move ahead
at = closeAt+1
elseif c == " "
or c == "\n"
or c == "\r" then
-- ignore whitespace
at = at + 1
elseif c == "*"
or c == "/" then
-- eat beat markers
gBeats = 0
while true do
-- get the beat character
local beatChar = string.sub(text, at, at)
if beatChar == "*" then
-- 1 beat
gBeats = gBeats + 1
elseif beatChar == "/" then
-- half beat
gBeats = gBeats + .5
else
break
end
-- move past beat character
at = at + 1
end
-- eat dots
local dots = 0
while string.sub(text, at, at) == "." do
dots = dots + 1
at = at + 1
end
if dots == 1 then
gBeats = gBeats * 1.5
end
-- convert into seconds
gBeats = gBeats * 60 / gTempo
-- scale by percent and reset percent
gBeats = gBeats * gPercent
gPercent = 1
print("Beats", gBeats)
elseif c == "'" then
-- emphasis flag?
at = at + 1
else
-- try matching to a phoneme
local matchPhone = nil
for _, value in ipairs(validPhones) do
if value == string.sub(text, at, at+string.len(value)-1) then
matchPhone = value
break
end
end
-- no match?
if not matchPhone then
error("Unknown text '"..string.sub(text, at, at+20).."'")
end
-- move past match
at = at + string.len(matchPhone)
-- emphasis?
local emphasis = nil
c = string.sub(text, at, at)
if c >= "0" and c <= "9" then
-- FIXME: How to use emphasis?
local emphasis = c
at = at + 1
end
-- debug
if debugFlag then
print("Phone", matchPhone, emphasis)
end
-- find phoneme index
local phoneIndex = pIndexByName[matchPhone]
if not phoneIndex then
error("Unable to match phoneme '"..matchPhone.."'")
end
-- insert the index
table.insert( outIndex, phoneIndex )
-- nucleus?
if inTable( matchPhone, isNucleus ) then
-- set the beats and pitch
table.insert( outLength, gBeats )
table.insert( outPitch, gPitch )
else
-- set to zero
table.insert( outLength, 0 )
table.insert( outPitch, 0 )
end
end
end
end
-- Print debug **msg** if debug flag is true
local function debugPrint( msg )
if debugFlag then print( msg ) end
end
--------------------------------------
-- REPLACE PHONEMES WITH ALLOPHONES --
--------------------------------------
-- Insert at **position** a phoneme with a given index, and insert zeros
-- in the outLength and outPitch
local function insert( position, phoneName )
table.insert( outIndex, position, getPhoneIndex( phoneName ) )
table.insert( outLength, position, 0 )
table.insert( outPitch, position, 0 )
end
-- Apply allophonic replacements
local function applyAllophones()
debugPrint("applyRules")
local pos = 0
-- Loop through phonemes
while pos < #outIndex do
-- increment position and get the phoneme
pos = pos + 1
local p = outIndex[pos]
if p == nil then
-- safety check, should not happen
error("nil found in outIndex, missing end of line token")
elseif p == getPhoneIndex(" ") then
-- skip spaces
elseif isDiphthong[p] then
-- RULE:
-- <DIPHTHONG> -> <DIPHTHONG> WX | YX
-- Example: OIL, COW
-- Ends in IY sound?
if isBackVowel[p] then
-- insert YX after
debugPrint("RULE: insert YX following *Y diphthong")
insert(pos+1, "YX" )
else
-- insert WX after
debugPrint("RULE: insert WX following *W diphthong")
insert(pos+1, "WX" )
end
elseif p == getPhoneIndex("UL") then
-- RULE:
-- UL -> AX L
-- Example: MEDDLE
debugPrint("RULE: UL -> AX L")
outIndex[pos] = getPhoneIndex("AX")
insert(pos+1, "L" )
elseif p == getPhoneIndex("UM") then
-- RULE:
-- UM -> AX M
-- Example: ASTRONOMY
debugPrint("RULE: UM -> AX M")
outIndex[pos] = getPhoneIndex("AX")
insert(pos+1, "M" )
-- don't advance
pos = pos - 1
elseif p == getPhoneIndex("UN") then
-- RULE:
-- UN -> AX N
-- Example: FUNCTION
debugPrint("RULE: UN -> AX N")
outIndex[pos] = getPhoneIndex("AX")
insert(pos+1, "N" )
-- don't advance
pos = pos - 1
elseif p == getPhoneIndex("IL") then
-- RULE:
-- IL -> IX L
-- Example: HASTILY
debugPrint("RULE: IL -> IX L")
outIndex[pos] = getPhoneIndex("IX")
insert(pos+1, "L" )
elseif p == getPhoneIndex("IM") then
-- RULE:
-- IM -> IX M
-- Example: ANIMAL
debugPrint("RULE: IM -> IX M")
outIndex[pos] = getPhoneIndex("IX")
insert(pos+1, "M" )
-- don't advance
pos = pos - 1
elseif p == getPhoneIndex("IN") then
-- RULE:
-- IN -> IX N
-- Example: CABIN
debugPrint("RULE: IN -> IX N")
outIndex[pos] = getPhoneIndex("IX")
insert(pos+1, "N" )
-- don't advance
pos = pos - 1
elseif p == getPhoneIndex("R") and outIndex[pos-1] == getPhoneIndex("T") then
-- RULES FOR PHONEMES BEFORE R
-- T R -> CH R
-- Example: TRACK
-- change T to CH, but don't advance
debugPrint("RULE: T R -> CH R")
outIndex[pos-1] = getPhoneIndex("CH")
pos = pos - 1
elseif p == getPhoneIndex("R") and outIndex[pos-1] == getPhoneIndex("D") then
-- RULES FOR PHONEMES BEFORE R
-- D R -> J R
-- Example: DRY
-- change T to J, but don't advance
debugPrint("RULE: T R -> J R")
outIndex[pos-1] = getPhoneIndex("J")
pos = pos - 1
elseif p == getPhoneIndex("R") and isVowel[outIndex[pos-1]] then
-- RULES FOR PHONEMES BEFORE R
-- <VOWEL> R -> <VOWEL> RX
-- Example: ART
-- change R to RX and advance
debugPrint("RULE: R -> RX")
outIndex[pos] = getPhoneIndex("RX")
elseif p == getPhoneIndex("L") and isVowel[outIndex[pos-1]] then
-- RULE:
-- <VOWEL> L -> <VOWEL> LX
-- Example: ALL
-- change L to LX and advance
debugPrint("RULE: L -> LX")
outIndex[pos] = getPhoneIndex("LX")
elseif p == getPhoneIndex("S") and outIndex[pos-1] == getPhoneIndex("S") then
-- RULE:
-- G S -> G Z
--
-- Can't get to fire -
-- 1. The G -> GX rule intervenes
-- 2. Reciter already replaces GS -> GZ
-- change S to Z and advance
debugPrint("RULE: G S -> G Z")
outIndex[pos] = getPhoneIndex("Z")
elseif p == getPhoneIndex("K") and isBackVowel[outIndex[pos+1]] then
-- RULE:
-- K <BACK VOWEL> -> KX <BACK VOWEL>
-- Example: COW
-- replace K with KX, and advance
debugPrint("RULE: K <BACK VOWEL> -> KX <BACK VOWEL>")
outIndex[pos] = getPhoneIndex("KX")
elseif p == getPhoneIndex("G") and isBackVowel[outIndex[pos+1]] then
-- RULE:
-- G <BACK VOWEL> -> GX <BACK VOWEL>
-- Example: GO
-- replace G with GX, and advance
debugPrint("RULE: G <BACK VOWEL> -> GX <BACK VOWEL>")
outIndex[pos] = getPhoneIndex("GX")
elseif p == getPhoneIndex("S") and isUnvoicedPlosive[outIndex[pos+1]] then
-- RULE:
-- S P -> S B
-- S T -> S D
-- S K -> S G
-- S KX -> S GX
-- Examples: SPY, STY, SKY, SCOWL
-- replace unvoiced plosive with voiced plosive, and advance
if outIndex[pos+1] == getPhoneIndex("P") then
debugPrint("RULE: S P -> S B")
outIndex[pos+1] = getPhoneIndex("B")
elseif outIndex[pos+1] == getPhoneIndex("T") then
debugPrint("RULE: S T -> S D")
outIndex[pos+1] = getPhoneIndex("D")
elseif outIndex[pos+1] == getPhoneIndex("K") then
debugPrint("RULE: S K -> S G")
outIndex[pos+1] = getPhoneIndex("G")
elseif outIndex[pos+1] == getPhoneIndex("KX") then
debugPrint("RULE: S KX -> S KX")
outIndex[pos+1] = getPhoneIndex("GX")
end
elseif p == getPhoneIndex("UW") and isAlveolar[outIndex[pos-1]] then
-- RULE:
-- <ALVEOLAR> UW -> <ALVEOLAR> UX
--
-- Example: NEW, DEW, SUE, ZOO, THOO, TOO
debugPrint("RULE: <ALVEOLAR> UW -> <ALVEOLAR> UX")
outIndex[pos] = getPhoneIndex("UX")
elseif (p == getPhoneIndex("T") or p == getPhoneIndex("D"))
and isVowel[outIndex[pos-1]]
and isVowel[outIndex[pos+1]] then
-- FIXME: This had initially been only if the second vowel is stressed
-- RULE: Soften T or D between a two vowels
-- <VOWEL> T <VOWEL> -> <VOWEL> DX <VOWEL>
-- <VOWEL> D <VOWEL> -> <VOWEL> DX <VOWEL>
-- Example: PARTY, TARDY
-- replace T or D with DX and advance
debugPrint("<VOWEL> T|D <VOWEL> -> <VOWEL> DX <VOWEL>")
outIndex[pos] = getPhoneIndex("DX")
end
end
end
-- Remove duplicate non-nucleus consonants
local function removeDuplicateConsonants()
local at = 1
while at < #outIndex do
if outIndex[at] == outIndex[at+1]
and outLength[at] == 0 then
-- remove from the table
table.remove( outIndex, at )
table.remove( outLength, at )
table.remove( outPitch, at )
-- don't advance, there may be several in a row
else
-- move ahead
at = at + 1
end
end
end