Reworking creaky voice recognition
[sgc2.git] / ToneProt / ToneRecognition.praat
blob947cd9e76e6a220f1241e54182af8b894ea9d6c0
1 #! praat\r
2 #\r
3 # Construct all tone patterns and look for the one closest to the given one\r
4 #\r
5 \r
6 procedure FreeToneRecognition .pinyin$ .test_word$ .exclude$ .upperRegister .freqRange .durScale .startSyllable\r
7     # Clean up input\r
8     if .pinyin$ <> ""\r
9         .pinyin$ = replace_regex$(.pinyin$, "^\s*(.+)\s*$", "\1", 1)\r
10         .pinyin$ = replace_regex$(.pinyin$, "5", "0", 0)\r
11                 # Missing neutral tones\r
12                 call add_missing_neutral_tones '.pinyin$'\r
13                 .pinyin$ = add_missing_neutral_tones.pinyin$\r
14     endif\r
16     .referenceFrequency = 300\r
17     .frequencyFactor = .referenceFrequency / .upperRegister\r
19     .referenceExt$ = "pitch"\r
20     \r
21     # Bias Z-normalized value of the distance difference between smallest and correct\r
22     .biasDistance = 1.1\r
23     if index_regex(config.strict$, "[^0-9]") <= 0\r
24                 .tmp = 'config.strict$'\r
25                 # Be lenient with longer words\r
26             if .startSyllable > 0\r
27                         .tmp -= 1\r
28             endif\r
29                 if .tmp <= 0\r
30                         .biasDistance = 1.7\r
31                 elsif .tmp = 1\r
32                         .biasDistance = 1.1\r
33                 elsif .tmp = 2\r
34                         .biasDistance = 0.6\r
35                 elsif .tmp = 3\r
36                         .biasDistance = 0.3\r
37                 endif\r
38     endif\r
39     \r
40     # Debugging\r
41     .keepIntermediates = 0\r
42     .debug = 0\r
44     # Generate reference tones\r
45     call toneScript '.pinyin$' '.upperRegister' '.freqRange' '.durScale' Pitch_'.startSyllable'\r
47     # Convert input to Pitch\r
48     if .test_word$ <> "" and .test_word$ <> "REUSEPITCH"\r
49         Read from file... '.test_word$'\r
50         Rename... Source\r
51     endif\r
52     if .test_word$ <> "REUSEPITCH"\r
53         select Sound Source\r
54                 call convert2Pitch 'sgc_ToneProt.minimumPitch' 'sgc_ToneProt.maximumPitch'\r
55                 if index(.pinyin$, "3") > 0\r
56                         select convert2Pitch.object\r
57                         .sourcePitch = Kill octave jumps\r
58                         select convert2Pitch.object\r
59                         Remove\r
60                 else\r
61                         .sourcePitch = convert2Pitch.object\r
62                 endif\r
63                 select .sourcePitch\r
64         Formula... self*'.frequencyFactor'; Normalize Pitch\r
65         Rename... SourcePitch\r
66     endif\r
68     select Pitch SourcePitch\r
70     .countDistance = 0\r
71     .sumDistance = 0\r
72     .sumSqrDistance = 0\r
73     .correctDistance = -1\r
75     .smallestDistance=999999\r
76     sgc_ToneProt.choiceReference$ = "empty"\r
77     select Table ToneList\r
78     .listLength = Get number of rows\r
79     for .i from 1 to .listLength\r
80         select Table ToneList\r
81         .inFile$ = Get value... '.i' Word\r
82         # Broken third tones are still third tones\r
83         # Creaky voice identifies 3rd tone\r
84                 .excludeWord$ = .exclude$\r
85                 if .excludeWord$ = "" and index_regex(toneProt.creakyThree$, "[^\.]")\r
86                         .tmpTones$ = replace_regex$(.inFile$, "[^\d]", "", 0)\r
87                         if index_regex(.tmpTones$, toneProt.creakyThree$) <= 0\r
88                                 .excludeWord$ = .inFile$\r
89                         endif\r
90                 endif\r
92         if (.excludeWord$ = "" or rindex_regex(.inFile$, .excludeWord$) <= 0) and rindex_regex(.inFile$, "[\d]") > 0\r
93             referenceName$ = .inFile$\r
94             select Pitch '.inFile$'\r
95             plus Pitch SourcePitch\r
96             .dtw = noprogress To DTW... 24 10 yes yes no restriction\r
97             Rename... DTW'.inFile$'\r
98             distance = Get distance (weighted)\r
99             # Clean up\r
100             select DTW DTW'.inFile$'\r
101             .countDistance = .countDistance + 1\r
102             .sumDistance = .sumDistance + distance\r
103             .sumSqrDistance = .sumSqrDistance + distance^2\r
104             \r
105                         .inFile$ = replace$(.inFile$, "9", "3", 0)\r
106             if .pinyin$ = .inFile$\r
107                 .correctDistance = distance\r
108             endif\r
110             if .debug > 0\r
111                 # printline 'distance' - '.inFile$'\r
112             endif\r
114             if distance < .smallestDistance\r
115                                 # You cannot have a large range and misidentify it as 00\r
116                                 if not (.freqRange >= 0.5 and index_regex(.inFile$, "0[^0-9]+0") > 0 and index_regex(.test_word$, "0[^0-9]+0") <= 0)\r
117                         .smallestDistance = distance\r
118                         sgc_ToneProt.choiceReference$ = "'.inFile$'"\r
119                 endif\r
120             endif\r
123             if .keepIntermediates = 0\r
124                 Remove\r
125             endif\r
126         endif\r
127     endfor\r
128     \r
129     if .countDistance > 1\r
130         .meanDistance = .sumDistance / .countDistance\r
131         .varDistance = (.sumSqrDistance - .sumDistance^2/.countDistance)/(.countDistance - 1)\r
132         .stdDistance = sqrt(.varDistance)\r
133         .diffDistance = .correctDistance - .smallestDistance\r
134         .zDistance = .diffDistance/.stdDistance\r
136         if .debug > 0\r
137             printline Match: '.pinyin$' <== 'sgc_ToneProt.choiceReference$' small='.smallestDistance' Z='.zDistance'\r
138         endif\r
140         if .zDistance < .biasDistance\r
141             sgc_ToneProt.choiceReference$ = .pinyin$\r
142             .smallestDistance = .correctDistance\r
143         endif\r
144     endif\r
145     \r
147     # Clean up\r
148     for .i from 1 to .listLength\r
149         select Table ToneList\r
150         .inFile$ = Get value... '.i' Word\r
152         if (.exclude$ = "" or rindex_regex(.inFile$, .exclude$) <= 0) and rindex_regex(.inFile$, "[\d]") > 0 \r
154             # Clean up\r
155             select Pitch '.inFile$'\r
156             if .keepIntermediates = 0\r
157                 Remove\r
158             endif\r
159         endif\r
160     endfor\r
162     select Table ToneList\r
163     if .test_word$ <> "" and .test_word$ <> "REUSEPITCH"\r
164         plus Sound Source\r
165     endif\r
166     if .test_word$ <> "" and .test_word$ <> "REUSEPITCH"\r
167         plus Pitch SourcePitch\r
168     endif\r
169     if .keepIntermediates = 0\r
170         Remove\r
171     endif\r
173 endproc\r