Adapted pitch extraction to kill octave jumps when 3rd tone is asked. UNTESTED
[sgc2.git] / ToneProt / ToneRecognition.praat
blobf02cb707ec816bfa2bf34fae04754d7e46c6421b
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                 if .tmp = 0\r
26                         .biasDistance = 1.7\r
27                 elsif .tmp = 1\r
28                         .biasDistance = 1.1\r
29                 elsif .tmp = 2\r
30                         .biasDistance = 0.6\r
31                 elsif .tmp = 3\r
32                         .biasDistance = 0.3\r
33                 endif\r
34     endif\r
35     \r
36     # Debugging\r
37     .keepIntermediates = 0\r
38     .debug = 0\r
40     # Generate reference tones\r
41     call toneScript '.pinyin$' '.upperRegister' '.freqRange' '.durScale' Pitch_'.startSyllable'\r
43     # Convert input to Pitch\r
44     if .test_word$ <> "" and .test_word$ <> "REUSEPITCH"\r
45         Read from file... '.test_word$'\r
46         Rename... Source\r
47     endif\r
48     if .test_word$ <> "REUSEPITCH"\r
49         select Sound Source\r
50                 call convert2Pitch 'sgc_ToneProt.minimumPitch' 'sgc_ToneProt.maximumPitch'\r
51                 if index(.pinyin$, "3") > 0\r
52                         select convert2Pitch.object\r
53                         .sourcePitch = Kill octave jumps\r
54                         select convert2Pitch.object\r
55                         Remove\r
56                 else\r
57                         .sourcePitch = convert2Pitch.object\r
58                 endif\r
59                 select .sourcePitch\r
60         Formula... self*'.frequencyFactor'; Normalize Pitch\r
61         Rename... SourcePitch\r
62     endif\r
63     select Pitch SourcePitch\r
65     .countDistance = 0\r
66     .sumDistance = 0\r
67     .sumSqrDistance = 0\r
68     .correctDistance = -1\r
70     .smallestDistance=999999\r
71     sgc_ToneProt.choiceReference$ = "empty"\r
72     select Table ToneList\r
73     .listLength = Get number of rows\r
74     for .i from 1 to .listLength\r
75         select Table ToneList\r
76         .inFile$ = Get value... '.i' Word\r
77         # Broken third tones are still third tones\r
79         if (.exclude$ = "" or rindex_regex(.inFile$, .exclude$) <= 0) and rindex_regex(.inFile$, "[\d]") > 0 \r
80             referenceName$ = .inFile$\r
81             select Pitch '.inFile$'\r
82             plus Pitch SourcePitch\r
83             .dtw = noprogress To DTW... 24 10 yes yes no restriction\r
84             Rename... DTW'.inFile$'\r
85             distance = Get distance (weighted)\r
86             # Clean up\r
87             select DTW DTW'.inFile$'\r
88             .countDistance = .countDistance + 1\r
89             .sumDistance = .sumDistance + distance\r
90             .sumSqrDistance = .sumSqrDistance + distance^2\r
91             \r
92                         .inFile$ = replace$(.inFile$, "9", "3", 0)\r
93             if .pinyin$ = .inFile$\r
94                 .correctDistance = distance\r
95             endif\r
97             if .debug > 0\r
98                 # printline 'distance' - '.inFile$'\r
99             endif\r
101             if distance < .smallestDistance\r
102                 .smallestDistance = distance\r
103                 sgc_ToneProt.choiceReference$ = "'.inFile$'"\r
104             endif\r
107             if .keepIntermediates = 0\r
108                 Remove\r
109             endif\r
110         endif\r
111     endfor\r
112     \r
113     if .countDistance > 1\r
114         .meanDistance = .sumDistance / .countDistance\r
115         .varDistance = (.sumSqrDistance - .sumDistance^2/.countDistance)/(.countDistance - 1)\r
116         .stdDistance = sqrt(.varDistance)\r
117         .diffDistance = .correctDistance - .smallestDistance\r
118         .zDistance = .diffDistance/.stdDistance\r
120         if .debug > 0\r
121             printline Match: '.pinyin$' <== 'sgc_ToneProt.choiceReference$' small='.smallestDistance' Z='.zDistance'\r
122         endif\r
124         if .zDistance < .biasDistance\r
125             sgc_ToneProt.choiceReference$ = .pinyin$\r
126             .smallestDistance = .correctDistance\r
127         endif\r
128     endif\r
129     \r
131     # Clean up\r
132     for .i from 1 to .listLength\r
133         select Table ToneList\r
134         .inFile$ = Get value... '.i' Word\r
136         if (.exclude$ = "" or rindex_regex(.inFile$, .exclude$) <= 0) and rindex_regex(.inFile$, "[\d]") > 0 \r
138             # Clean up\r
139             select Pitch '.inFile$'\r
140             if .keepIntermediates = 0\r
141                 Remove\r
142             endif\r
143         endif\r
144     endfor\r
146     select Table ToneList\r
147     if .test_word$ <> "" and .test_word$ <> "REUSEPITCH"\r
148         plus Sound Source\r
149     endif\r
150     if .test_word$ <> "" and .test_word$ <> "REUSEPITCH"\r
151         plus Pitch SourcePitch\r
152     endif\r
153     if .keepIntermediates = 0\r
154         Remove\r
155     endif\r
157 endproc\r