Debian Codegraph @

foned/PitchArea.cpp

raw symbols SCIP
1 /* PitchArea.cpp
2 *
3 * Copyright (C) 1992-2012,2014-2022 Paul Boersma
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "PitchArea.h"
20 #include "Pitch_to_Sound.h"
21 #include "EditorM.h"
22
23 Thing_implement (PitchArea, FunctionArea, 0);
24
25 #define HEIGHT_UNV 3.0
26 #define HEIGHT_INTENS 6.0
27 #define RADIUS 2.5
28
29
30 #pragma mark - PitchArea Query selection
31
32 static void QUERY_DATA_FOR_REAL__getPitch (PitchArea me, EDITOR_ARGS) {
33 QUERY_DATA_FOR_REAL
34 const double result = ( my startSelection() == my endSelection()
35 ? Pitch_getValueAtTime (my pitch(), my startSelection(), kPitch_unit::HERTZ, 1)
36 : Pitch_getMean (my pitch(), my startSelection(), my endSelection(), kPitch_unit::HERTZ)
37 );
38 QUERY_DATA_FOR_REAL_END (U" Hz")
39 }
40
41
42 #pragma mark - PitchArea Modify whole
43
44 static void menu_cb_setCeiling (PitchArea me, EDITOR_ARGS) {
45 EDITOR_FORM (U"Change ceiling", nullptr)
46 POSITIVE (ceiling, U"Ceiling (Hz)", U"600.0")
47 EDITOR_OK
48 SET_REAL (ceiling, my pitch() -> ceiling)
49 EDITOR_DO
50 MODIFY_DATA (U"Change ceiling")
51 Pitch_setCeiling (my pitch(), ceiling);
52 MODIFY_DATA_END
53 EDITOR_END
54 }
55
56 static void menu_cb_pathFinder (PitchArea me, EDITOR_ARGS) {
57 EDITOR_FORM (U"Path finder", nullptr)
58 REAL (silenceThreshold, U"Silence threshold", U"0.03")
59 REAL (voicingThreshold, U"Voicing threshold", U"0.45")
60 REAL (octaveCost, U"Octave cost", U"0.01")
61 REAL (octaveJumpCost, U"Octave-jump cost", U"0.35")
62 REAL (voicedUnvoicedCost, U"Voiced/unvoiced cost", U"0.14")
63 POSITIVE (ceiling, U"Ceiling (Hz)", U"600.0")
64 BOOLEAN (pullFormants, U"Pull formants", false)
65 EDITOR_OK
66 SET_REAL (ceiling, my pitch() -> ceiling)
67 EDITOR_DO
68 MODIFY_DATA (U"Path finder")
69 Pitch_pathFinder (my pitch(), silenceThreshold, voicingThreshold,
70 octaveCost, octaveJumpCost, voicedUnvoicedCost, ceiling, pullFormants);
71 MODIFY_DATA_END
72 EDITOR_END
73 }
74
75
76 #pragma mark - PitchArea Modify selection
77
78 static void menu_cb_octaveUp (PitchArea me, EDITOR_ARGS) {
79 MODIFY_DATA (U"Octave up")
80 Pitch_step (my pitch(), 2.0, 0.1, my startSelection(), my endSelection());
81 MODIFY_DATA_END
82 }
83
84 static void menu_cb_fifthUp (PitchArea me, EDITOR_ARGS) {
85 MODIFY_DATA (U"Fifth up")
86 Pitch_step (my pitch(), 1.5, 0.1, my startSelection(), my endSelection());
87 MODIFY_DATA_END
88 }
89
90 static void menu_cb_fifthDown (PitchArea me, EDITOR_ARGS) {
91 MODIFY_DATA (U"Fifth down")
92 Pitch_step (my pitch(), 1 / 1.5, 0.1, my startSelection(), my endSelection());
93 MODIFY_DATA_END
94 }
95
96 static void menu_cb_octaveDown (PitchArea me, EDITOR_ARGS) {
97 MODIFY_DATA (U"Octave down")
98 Pitch_step (my pitch(), 0.5, 0.1, my startSelection(), my endSelection());
99 MODIFY_DATA_END
100 }
101
102 static void menu_cb_voiceless (PitchArea me, EDITOR_ARGS) {
103 const integer ileft = Melder_clippedLeft (1_integer, Sampled_xToHighIndex (my pitch(), my startSelection()));
104 const integer iright = Melder_clippedRight (Sampled_xToLowIndex (my pitch(), my endSelection()), my pitch() -> nx);
105 MODIFY_DATA (U"Unvoice")
106 for (integer iframe = ileft; iframe <= iright; iframe ++) {
107 const Pitch_Frame frame = & my pitch() -> frames [iframe];
108 for (integer cand = 1; cand <= frame -> nCandidates; cand ++)
109 if (frame -> candidates [cand]. frequency == 0.0)
110 std::swap (frame -> candidates [1], frame -> candidates [cand]);
111 }
112 MODIFY_DATA_END
113 }
114
115
116 #pragma mark - PitchArea all menus
117
118 void structPitchArea :: v_createMenus () {
119 PitchArea_Parent :: v_createMenus ();
120
121 EditorMenu menu = Editor_addMenu (our functionEditor(), U"Pitch", 0);
122
123 FunctionAreaMenu_addCommand (menu, U"- Query selected part of pitch:", 0, nullptr, this);
124 FunctionAreaMenu_addCommand (menu, U"Get pitch", GuiMenu_F5,
125 QUERY_DATA_FOR_REAL__getPitch, this);
126
127 FunctionAreaMenu_addCommand (menu, U"- Modify whole pitch:", 0, nullptr, this);
128 FunctionAreaMenu_addCommand (menu, U"Change ceiling...", 0,
129 menu_cb_setCeiling, this);
130 FunctionAreaMenu_addCommand (menu, U"Path finder...", 0,
131 menu_cb_pathFinder, this);
132 FunctionAreaMenu_addCommand (menu, U"- Modify selected part of pitch:", 0, nullptr, this);
133 FunctionAreaMenu_addCommand (menu, U"Unvoice", 0,
134 menu_cb_voiceless, this);
135 FunctionAreaMenu_addCommand (menu, U"-- up and down --", 0, nullptr, this);
136 FunctionAreaMenu_addCommand (menu, U"Octave up", 0,
137 menu_cb_octaveUp, this);
138 FunctionAreaMenu_addCommand (menu, U"Fifth up", 0,
139 menu_cb_fifthUp, this);
140 FunctionAreaMenu_addCommand (menu, U"Fifth down", 0,
141 menu_cb_fifthDown, this);
142 FunctionAreaMenu_addCommand (menu, U"Octave down", 0,
143 menu_cb_octaveDown, this);
144 }
145
146
147 #pragma mark - PitchArea drawing
148
149 void structPitchArea :: v_drawInside () {
150 const double dyUnv = Graphics_dyMMtoWC (our graphics(), HEIGHT_UNV);
151 const double dyIntens = Graphics_dyMMtoWC (our graphics(), HEIGHT_INTENS);
152
153 integer it1, it2;
154 Sampled_getWindowSamples (our pitch(), our startWindow(), our endWindow(), & it1, & it2);
155
156 /*
157 Show pitch.
158 */
159 {
160 const double df =
161 our pitch() -> ceiling > 10000.0 ? 2000.0 :
162 our pitch() -> ceiling > 5000.0 ? 1000.0 :
163 our pitch() -> ceiling > 2000.0 ? 500.0 :
164 our pitch() -> ceiling > 800.0 ? 200.0 :
165 our pitch() -> ceiling > 400.0 ? 100.0 :
166 50.0
167 ;
168 Graphics_Viewport previous;
169 previous = Graphics_insetViewport (our graphics(), 0.0, 1.0, dyUnv, 1.0 - dyIntens);
170 Graphics_setWindow (our graphics(), our startWindow(), our endWindow(), 0.0, our pitch() -> ceiling);
171 const double radius = Graphics_dxMMtoWC (our graphics(), RADIUS);
172
173 /* Horizontal hair at current pitch. */
174
175 if (our startSelection() == our endSelection() && our startSelection() >= our startWindow() && our startSelection() <= our endWindow()) {
176 const double frequency = Pitch_getValueAtTime (our pitch(), our startSelection(), kPitch_unit::HERTZ, Pitch_LINEAR);
177 if (isdefined (frequency)) {
178 Graphics_setColour (our graphics(), Melder_RED);
179 Graphics_line (our graphics(), our startWindow() - radius, frequency, our endWindow(), frequency);
180 Graphics_setTextAlignment (our graphics(), Graphics_RIGHT, Graphics_HALF);
181 Graphics_text (our graphics(), our startWindow() - radius, frequency, Melder_fixed (frequency, 2));
182 }
183 }
184
185 /* Horizontal scaling lines. */
186
187 Graphics_setColour (our graphics(), Melder_BLUE);
188 Graphics_setLineType (our graphics(), Graphics_DOTTED);
189 Graphics_setTextAlignment (our graphics(), Graphics_LEFT, Graphics_HALF);
190 for (double frequency = df; frequency <= our pitch() -> ceiling; frequency += df) {
191 Graphics_line (our graphics(), our startWindow(), frequency, our endWindow(), frequency);
192 Graphics_text (our graphics(), our endWindow() + 0.5 * radius, frequency, frequency, U" Hz");
193 }
194 Graphics_setLineType (our graphics(), Graphics_DRAWN);
195
196 /*
197 Show candidates.
198 */
199 for (integer it = it1; it <= it2; it ++) {
200 const Pitch_Frame frame = & our pitch() -> frames [it];
201 const double time = Sampled_indexToX (our pitch(), it);
202 double frequency = frame -> candidates [1]. frequency;
203 if (Pitch_util_frequencyIsVoiced (frequency, our pitch() -> ceiling)) {
204 Graphics_setColour (our graphics(), Melder_MAGENTA);
205 Graphics_fillCircle_mm (our graphics(), time, frequency, RADIUS * 2.0);
206 }
207 Graphics_setColour (our graphics(), Melder_BLACK);
208 Graphics_setTextAlignment (our graphics(), Graphics_CENTRE, Graphics_HALF);
209 for (integer icand = 1; icand <= frame -> nCandidates; icand ++) {
210 frequency = frame -> candidates [icand]. frequency;
211 if (Pitch_util_frequencyIsVoiced (frequency, our pitch() -> ceiling)) {
212 const integer strength = Melder_clippedRight (Melder_iround (10.0 * frame -> candidates [icand]. strength),
213 9_integer); // map 0.0-1.0 to 0-9
214 Graphics_text (our graphics(), time, frequency, strength);
215 }
216 }
217 }
218 Graphics_resetViewport (our graphics(), previous);
219 }
220
221 /*
222 Show intensity.
223 */
224 {
225 Graphics_Viewport previous = Graphics_insetViewport (our graphics(), 0.0, 1.0, 1.0 - dyIntens, 1.0);
226 Graphics_setWindow (our graphics(), our startWindow(), our endWindow(), 0.0, 1.0);
227 Graphics_setColour (our graphics(), Melder_BLACK);
228 Graphics_setTextAlignment (our graphics(), Graphics_RIGHT, Graphics_HALF);
229 Graphics_text (our graphics(), our startWindow(), 0.5, U"intens");
230 Graphics_setTextAlignment (our graphics(), Graphics_LEFT, Graphics_HALF);
231 Graphics_text (our graphics(), our endWindow(), 0.5, U"intens");
232 Graphics_setTextAlignment (our graphics(), Graphics_CENTRE, Graphics_HALF);
233 for (integer it = it1; it <= it2; it ++) {
234 const Pitch_Frame frame = & our pitch() -> frames [it];
235 const double time = Sampled_indexToX (our pitch(), it);
236 const integer strength = Melder_clippedRight (Melder_iround (10.0 * frame -> intensity + 0.5),
237 9_integer); // map 0.0-1.0 to 1-9
238 Graphics_text (our graphics(), time, 0.5, strength);
239 }
240 Graphics_resetViewport (our graphics(), previous);
241 }
242
243 if (it1 > 1)
244 it1 -= 1;
245 if (it2 < our pitch() -> nx)
246 it2 += 1;
247
248 /*
249 Show voicelessness.
250 */
251 {
252 Graphics_Viewport previous = Graphics_insetViewport (our graphics(), 0.0, 1.0, 0.0, dyUnv);
253 Graphics_setColour (our graphics(), Melder_BLUE);
254 Graphics_line (our graphics(), our startWindow(), 1.0, our endWindow(), 1.0);
255 Graphics_setTextAlignment (our graphics(), Graphics_RIGHT, Graphics_HALF);
256 Graphics_text (our graphics(), our startWindow(), 0.5, U"Unv");
257 Graphics_setTextAlignment (our graphics(), Graphics_LEFT, Graphics_HALF);
258 Graphics_text (our graphics(), our endWindow(), 0.5, U"Unv");
259 for (integer it = it1; it <= it2; it ++) {
260 const Pitch_Frame frame = & our pitch() -> frames [it];
261 const double time = Sampled_indexToX (our pitch(), it);
262 double tleft = time - 0.5 * our pitch() -> dx, tright = time + 0.5 * our pitch() -> dx;
263 double frequency = frame -> candidates [1]. frequency;
264 if (Pitch_util_frequencyIsVoiced (frequency, our pitch() -> ceiling) || tright <= our startWindow() || tleft >= our endWindow())
265 continue;
266 Melder_clipLeft (our startWindow(), & tleft);
267 Melder_clipRight (& tright, our endWindow());
268 Graphics_fillRectangle (our graphics(), tleft, tright, 0.0, 1.0);
269 }
270 Graphics_setColour (our graphics(), Melder_BLACK);
271 Graphics_resetViewport (our graphics(), previous);
272 }
273 }
274
275 bool structPitchArea :: v_mouse (GuiDrawingArea_MouseEvent event, double x_world, double localY_fraction) {
276 if (! event -> isClick())
277 return FunctionEditor_defaultMouseInWideDataView (our functionEditor(), event, x_world); // move cursor or drag selection
278 const double dyUnv = Graphics_dyMMtoWC (our graphics(), HEIGHT_UNV);
279 const double dyIntens = Graphics_dyMMtoWC (our graphics(), HEIGHT_INTENS);
280 const double clickedFrequency = (localY_fraction - dyUnv) / (1.0 - dyIntens - dyUnv) * our pitch() -> ceiling;
281 double minimumDf = 1e30;
282 integer bestCandidate = -1;
283
284 integer ibestFrame = Sampled_xToNearestIndex (our pitch(), x_world);
285 Melder_clip (1_integer, & ibestFrame, our pitch() -> nx);
286 const Pitch_Frame bestFrame = & our pitch() -> frames [ibestFrame];
287
288 const double tmid = Sampled_indexToX (our pitch(), ibestFrame);
289 for (integer icand = 1; icand <= bestFrame -> nCandidates; icand ++) {
290 const Pitch_Candidate candidate = & bestFrame -> candidates [icand];
291 const double df = clickedFrequency - candidate -> frequency;
292 if (fabs (df) < minimumDf) {
293 minimumDf = fabs (df);
294 bestCandidate = icand;
295 }
296 }
297 if (bestCandidate != -1) {
298 const double bestFrequency = bestFrame -> candidates [bestCandidate]. frequency;
299 const double distanceWC = (clickedFrequency - bestFrequency) / our pitch() -> ceiling * (1.0 - dyIntens - dyUnv);
300 const double dx_mm = Graphics_dxWCtoMM (our graphics(), x_world - tmid), dy_mm = Graphics_dyWCtoMM (our graphics(), distanceWC);
301 if (bestFrequency < our pitch() -> ceiling && // above ceiling: ignore
302 ((bestFrequency <= 0.0 && fabs (x_world - tmid) <= 0.5 * our pitch() -> dx && clickedFrequency <= 0.0) || // voiceless: click within frame
303 (bestFrequency > 0.0 && dx_mm * dx_mm + dy_mm * dy_mm <= RADIUS * RADIUS))) // voiced: click within circle
304 {
305 Editor_save (our boss(), U"Change path");
306 std::swap (bestFrame -> candidates [1], bestFrame -> candidates [bestCandidate]);
307 Editor_broadcastDataChanged (our boss());
308 our setSelection (tmid, tmid); // cursor will snap to candidate
309 return FunctionEditor_UPDATE_NEEDED;
310 } else {
311 return FunctionEditor_defaultMouseInWideDataView (our functionEditor(), event, x_world); // move cursor or drag selection
312 }
313 }
314 return FunctionEditor_defaultMouseInWideDataView (our functionEditor(), event, x_world); // move cursor or drag selection
315 }
316
317 /* End of file PitchArea.cpp */