New attempt to creacky voice detection in third tones
[sgc2.git] / ToneProt / ToneRecognition.praat
bloba33b3df98debb4abb7a6178207ee2dd075ecba92
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 reduces 3rd tone distances\r
85         if (.exclude$ = "" or rindex_regex(.inFile$, .exclude$) <= 0) and rindex_regex(.inFile$, "[\d]") > 0 \r
86             referenceName$ = .inFile$\r
87             select Pitch '.inFile$'\r
88             plus Pitch SourcePitch\r
89             .dtw = noprogress To DTW... 24 10 yes yes no restriction\r
90             Rename... DTW'.inFile$'\r
91             distance = Get distance (weighted)\r
92             # Clean up\r
93             select DTW DTW'.inFile$'\r
94             .countDistance = .countDistance + 1\r
95             .sumDistance = .sumDistance + distance\r
96             .sumSqrDistance = .sumSqrDistance + distance^2\r
97             \r
98                         .inFile$ = replace$(.inFile$, "9", "3", 0)\r
99             if .pinyin$ = .inFile$\r
100                 .correctDistance = distance\r
101             endif\r
103             if .debug > 0\r
104                 # printline 'distance' - '.inFile$'\r
105             endif\r
107             if distance < .smallestDistance\r
108                                 # You cannot have a large range and misidentify it as 00\r
109                                 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
110                         .smallestDistance = distance\r
111                         sgc_ToneProt.choiceReference$ = "'.inFile$'"\r
112                 endif\r
113             endif\r
116             if .keepIntermediates = 0\r
117                 Remove\r
118             endif\r
119         endif\r
120     endfor\r
121     \r
122     if .countDistance > 1\r
123         .meanDistance = .sumDistance / .countDistance\r
124         .varDistance = (.sumSqrDistance - .sumDistance^2/.countDistance)/(.countDistance - 1)\r
125         .stdDistance = sqrt(.varDistance)\r
126         .diffDistance = .correctDistance - .smallestDistance\r
127         .zDistance = .diffDistance/.stdDistance\r
129         if .debug > 0\r
130             printline Match: '.pinyin$' <== 'sgc_ToneProt.choiceReference$' small='.smallestDistance' Z='.zDistance'\r
131         endif\r
133         if .zDistance < .biasDistance\r
134             sgc_ToneProt.choiceReference$ = .pinyin$\r
135             .smallestDistance = .correctDistance\r
136         endif\r
137     endif\r
138     \r
140     # Clean up\r
141     for .i from 1 to .listLength\r
142         select Table ToneList\r
143         .inFile$ = Get value... '.i' Word\r
145         if (.exclude$ = "" or rindex_regex(.inFile$, .exclude$) <= 0) and rindex_regex(.inFile$, "[\d]") > 0 \r
147             # Clean up\r
148             select Pitch '.inFile$'\r
149             if .keepIntermediates = 0\r
150                 Remove\r
151             endif\r
152         endif\r
153     endfor\r
155     select Table ToneList\r
156     if .test_word$ <> "" and .test_word$ <> "REUSEPITCH"\r
157         plus Sound Source\r
158     endif\r
159     if .test_word$ <> "" and .test_word$ <> "REUSEPITCH"\r
160         plus Pitch SourcePitch\r
161     endif\r
162     if .keepIntermediates = 0\r
163         Remove\r
164     endif\r
166 endproc\r