Debian Codegraph @

foned/FormantGridArea.cpp

raw
1 /* FormantGridArea.cpp
2 *
3 * Copyright (C) 2008-2022,2024,2026 Paul Boersma & David Weenink
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 3 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 "FormantGridArea.h"
20 #include "EditorM.h"
21
22 Thing_implement (FormantGridArea, RealTierArea, 0);
23
24 #include "Prefs_define.h"
25 #include "FormantGridArea_prefs.h"
26 #include "Prefs_install.h"
27 #include "FormantGridArea_prefs.h"
28 #include "Prefs_copyToInstance.h"
29 #include "FormantGridArea_prefs.h"
30
31
32 #pragma mark - FormantGridArea drawing
33
34 void structFormantGridArea :: v_drawInside () {
35 Graphics_setWindow (our graphics(), our startWindow(), our endWindow(), our ymin, our ymax);
36 Graphics_setColour (our graphics(), Melder_RED);
37 //Graphics_line (our graphics(), our startWindow, our formantGridArea -> ycursor, our endWindow, our formantGridArea -> ycursor);
38 Graphics_setTextAlignment (our graphics(), Graphics_RIGHT, Graphics_HALF);
39 //Graphics_text (our graphics(), our startWindow(), our ycursor,
40 // Melder_graphicalHalf (our ycursor));
41 Graphics_setLineWidth (our graphics(), 1.0);
42 Graphics_setColour (our graphics(), Melder_GREY);
43 OrderedOf<structRealTier>* tiers = (
44 our editingBandwidths ?
45 & our formantGrid() -> bandwidths :
46 & our formantGrid() -> formants
47 );
48 for (integer iformant = 1; iformant <= our formantGrid() -> formants.size; iformant ++) if (iformant != our selectedFormant) {
49 RealTier tier = tiers->at [iformant];
50 Melder_assert (Thing_isa (tier, classRealTier));
51 const integer imin = AnyTier_timeToHighIndex (tier->asAnyTier(), our startWindow());
52 const integer imax = AnyTier_timeToLowIndex (tier->asAnyTier(), our endWindow());
53 const integer n = tier -> points.size;
54 if (n == 0) {
55 } else if (imax < imin) {
56 const double yleft = RealTier_getValueAtTime (tier, our startWindow());
57 const double yright = RealTier_getValueAtTime (tier, our endWindow());
58 Graphics_line (our graphics(), our startWindow(), yleft, our endWindow(), yright);
59 } else {
60 for (integer i = imin; i <= imax; i ++) {
61 RealPoint point = tier -> points.at [i];
62 const double t = point -> number, y = point -> value;
63 Graphics_fillCircle_mm (our graphics(), t, y, 2.0);
64 if (i == 1)
65 Graphics_line (our graphics(), our startWindow(), y, t, y);
66 else if (i == imin)
67 Graphics_line (our graphics(), t, y, our startWindow(), RealTier_getValueAtTime (tier, our startWindow()));
68 if (i == n)
69 Graphics_line (our graphics(), t, y, our endWindow(), y);
70 else if (i == imax)
71 Graphics_line (our graphics(), t, y, our endWindow(), RealTier_getValueAtTime (tier, our endWindow()));
72 else {
73 RealPoint pointRight = tier -> points.at [i + 1];
74 Graphics_line (our graphics(), t, y, pointRight -> number, pointRight -> value);
75 }
76 }
77 }
78 }
79 FormantGridArea_Parent :: v_drawInside ();
80 }
81
82
83 #pragma mark - FormantGridArea Modify
84
85 static void menu_cb_removePoints (FormantGridArea me, EDITOR_ARGS) {
86 FunctionArea_save (me, U"Remove point(s)");
87 RealTierArea_removePoints (me);
88 FunctionArea_broadcastDataChanged (me);
89 }
90 static void menu_cb_addPointAtCursor (FormantGridArea me, EDITOR_ARGS) {
91 FunctionArea_save (me, U"Add point");
92 RealTierArea_addPointAtCursor (me);
93 FunctionArea_broadcastDataChanged (me);
94 }
95 static void menu_cb_addPointAt (FormantGridArea me, EDITOR_ARGS) {
96 EDITOR_FORM (U"Add point", nullptr)
97 REAL (time, U"Time (s)", U"0.0")
98 POSITIVE (frequency, U"Frequency (Hz)", U"200.0")
99 EDITOR_OK
100 SET_REAL (time, 0.5 * (my startSelection() + my endSelection()))
101 SET_REAL (frequency, my ycursor)
102 EDITOR_DO
103 Editor_save (my boss(), U"Add point");
104 RealTierArea_addPointAt (me, time, frequency);
105 Editor_broadcastDataChanged (my boss());
106 EDITOR_END
107 }
108 static void menu_cb_setFormantRange (FormantGridArea me, EDITOR_ARGS) {
109 EDITOR_FORM (U"Set formant range", nullptr)
110 REAL (minimumFormant, U"Minimum formant (Hz)", my default_formantFloor())
111 REAL (maximumFormant, U"Maximum formant (Hz)", my default_formantCeiling())
112 EDITOR_OK
113 SET_REAL (minimumFormant, my instancePref_formantFloor())
114 SET_REAL (maximumFormant, my instancePref_formantCeiling())
115 EDITOR_DO
116 my setInstancePref_formantFloor (minimumFormant);
117 my setInstancePref_formantCeiling (maximumFormant);
118 my v_updateScaling (); // BUG: should be viewChanged()
119 FunctionEditor_redraw (my functionEditor());
120 EDITOR_END
121 }
122 static void menu_cb_setBandwidthRange (FormantGridArea me, EDITOR_ARGS) {
123 EDITOR_FORM (U"Set bandwidth range", nullptr)
124 REAL (minimumBandwidth, U"Minimum bandwidth (Hz)", my default_bandwidthFloor())
125 REAL (maximumBandwidth, U"Maximum bandwidth (Hz)", my default_bandwidthCeiling())
126 EDITOR_OK
127 SET_REAL (minimumBandwidth, my instancePref_bandwidthFloor())
128 SET_REAL (maximumBandwidth, my instancePref_bandwidthCeiling())
129 EDITOR_DO
130 my setInstancePref_bandwidthFloor (minimumBandwidth);
131 my setInstancePref_bandwidthCeiling (maximumBandwidth);
132 my v_updateScaling (); // BUG: should be viewChanged()
133 FunctionEditor_redraw (my functionEditor());
134 EDITOR_END
135 }
136 static void menu_cb_showBandwidths (FormantGridArea me, EDITOR_ARGS) {
137 my editingBandwidths = ! my editingBandwidths;
138 GuiMenuItem_check (my d_bandwidthsToggle, my editingBandwidths);
139 Editor_broadcastDataChanged (my boss()); // BUG: the data themselves have not changed, but the view on them has
140 }
141 static void selectFormantOrBandwidth (FormantGridArea me, integer iformant) {
142 const integer numberOfFormants = my formantGrid() -> formants.size;
143 if (iformant > numberOfFormants)
144 Melder_throw (U"Cannot select formant ", iformant, U", because the FormantGrid has only ", numberOfFormants, U" formants.");
145 my selectedFormant = iformant;
146 Editor_dataChanged (my boss(), my boss()); // BUG: the data themselves have not changed, but the view on them has
147 }
148
149 static void menu_cb_selectFirst (FormantGridArea me, EDITOR_ARGS) { selectFormantOrBandwidth (me, 1); }
150 static void menu_cb_selectSecond (FormantGridArea me, EDITOR_ARGS) { selectFormantOrBandwidth (me, 2); }
151 static void menu_cb_selectThird (FormantGridArea me, EDITOR_ARGS) { selectFormantOrBandwidth (me, 3); }
152 static void menu_cb_selectFourth (FormantGridArea me, EDITOR_ARGS) { selectFormantOrBandwidth (me, 4); }
153 static void menu_cb_selectFifth (FormantGridArea me, EDITOR_ARGS) { selectFormantOrBandwidth (me, 5); }
154 static void menu_cb_selectSixth (FormantGridArea me, EDITOR_ARGS) { selectFormantOrBandwidth (me, 6); }
155 static void menu_cb_selectSeventh (FormantGridArea me, EDITOR_ARGS) { selectFormantOrBandwidth (me, 7); }
156 static void menu_cb_selectEighth (FormantGridArea me, EDITOR_ARGS) { selectFormantOrBandwidth (me, 8); }
157 static void menu_cb_selectNinth (FormantGridArea me, EDITOR_ARGS) { selectFormantOrBandwidth (me, 9); }
158 static void menu_cb_selectFormantOrBandwidth (FormantGridArea me, EDITOR_ARGS) {
159 EDITOR_FORM (U"Select formant or bandwidth", nullptr)
160 NATURAL (formantNumber, U"Formant number", U"1")
161 EDITOR_OK
162 SET_INTEGER (formantNumber, my selectedFormant)
163 EDITOR_DO
164 selectFormantOrBandwidth (me, formantNumber);
165 Editor_broadcastDataChanged (my boss()); // BUG: the data themselves have not changed, but the view on them has
166 EDITOR_END
167 }
168
169 static void menu_cb_pitchSettings (FormantGridArea me, EDITOR_ARGS) {
170 EDITOR_FORM (U"Source pitch settings", nullptr)
171 COMMENT (U"These settings apply to the pitch curve")
172 COMMENT (U"that you hear when playing the FormantGrid.")
173 REAL (startTime, U"Start time", my default_source_pitch_tStart ())
174 POSITIVE (startPitch, U"Start pitch (Hz)", my default_source_pitch_f0Start ())
175 REAL (midTime, U"Mid time", my default_source_pitch_tMid ())
176 POSITIVE (midPitch, U"Mid pitch (Hz)", my default_source_pitch_f0Mid ())
177 REAL (endTime, U"End time", my default_source_pitch_tEnd ())
178 POSITIVE (endPitch, U"End pitch (Hz)", my default_source_pitch_f0End ())
179 EDITOR_OK
180 SET_REAL (startTime, my instancePref_source_pitch_tStart())
181 SET_REAL (startPitch, my instancePref_source_pitch_f0Start())
182 SET_REAL (midTime, my instancePref_source_pitch_tMid())
183 SET_REAL (midPitch, my instancePref_source_pitch_f0Mid())
184 SET_REAL (endTime, my instancePref_source_pitch_tEnd())
185 SET_REAL (endPitch, my instancePref_source_pitch_f0End())
186 EDITOR_DO
187 my setInstancePref_source_pitch_tStart (startTime);
188 my setInstancePref_source_pitch_f0Start (startPitch);
189 my setInstancePref_source_pitch_tMid (midTime);
190 my setInstancePref_source_pitch_f0Mid (midPitch);
191 my setInstancePref_source_pitch_tEnd (endTime);
192 my setInstancePref_source_pitch_f0End (endPitch);
193 EDITOR_END
194 }
195
196 void structFormantGridArea :: v_createMenus () {
197 //FormantGridArea_Parent :: v_createMenus ();
198 EditorMenu menu = Editor_addMenu (our functionEditor(), U"Formant", 0);
199 our d_bandwidthsToggle = FunctionAreaMenu_addCommand (menu, U"Show bandwidths", GuiMenu_CHECKBUTTON,
200 menu_cb_showBandwidths, this);
201 FunctionAreaMenu_addCommand (menu, U"Set formant range...", 0,
202 menu_cb_setFormantRange, this);
203 FunctionAreaMenu_addCommand (menu, U"Set bandwidth range...", 0,
204 menu_cb_setBandwidthRange, this);
205 FunctionAreaMenu_addCommand (menu, U"-- select formant --", 0, nullptr, this);
206 FunctionAreaMenu_addCommand (menu, U"Select first", '1', menu_cb_selectFirst, this);
207 FunctionAreaMenu_addCommand (menu, U"Select second", '2', menu_cb_selectSecond, this);
208 FunctionAreaMenu_addCommand (menu, U"Select third", '3', menu_cb_selectThird, this);
209 FunctionAreaMenu_addCommand (menu, U"Select fourth", '4', menu_cb_selectFourth, this);
210 FunctionAreaMenu_addCommand (menu, U"Select fifth", '5', menu_cb_selectFifth, this);
211 FunctionAreaMenu_addCommand (menu, U"Select sixth", '6', menu_cb_selectSixth, this);
212 FunctionAreaMenu_addCommand (menu, U"Select seventh", '7', menu_cb_selectSeventh, this);
213 FunctionAreaMenu_addCommand (menu, U"Select eighth", '8', menu_cb_selectEighth, this);
214 FunctionAreaMenu_addCommand (menu, U"Select ninth", '9', menu_cb_selectNinth, this);
215 FunctionAreaMenu_addCommand (menu, U"Select formant or bandwidth...", 0,
216 menu_cb_selectFormantOrBandwidth, this);
217 menu = Editor_addMenu (our functionEditor(), U"Point", 0);
218 FunctionAreaMenu_addCommand (menu, U"Add point at cursor", 'T',
219 menu_cb_addPointAtCursor, this);
220 FunctionAreaMenu_addCommand (menu, U"Add point at...", 0,
221 menu_cb_addPointAt, this);
222 FunctionAreaMenu_addCommand (menu, U"-- remove point --", 0, nullptr, this);
223 FunctionAreaMenu_addCommand (menu, U"Remove point(s)", GuiMenu_COMMAND_EXTRA | 'T',
224 menu_cb_removePoints, this);
225 if (our v_hasSourceMenu ()) {
226 menu = Editor_addMenu (our functionEditor(), U"Source", 0);
227 FunctionAreaMenu_addCommand (menu, U"Pitch settings...", 0,
228 menu_cb_pitchSettings, this);
229 //FunctionAreaMenu_addCommand (menu, U"Phonation settings...", 0,
230 // menu_cb_phonationSettings, this);
231 }
232 }
233
234 void FormantGridArea_playPart (FormantGridArea me, double startTime, double endTime, Sound_PlayCallback playCallback, Thing playBoss) {
235 FormantGrid_playPart (my formantGrid(), startTime, endTime, my instancePref_play_samplingFrequency(),
236 my instancePref_source_pitch_tStart(), my instancePref_source_pitch_f0Start(),
237 my instancePref_source_pitch_tMid(), my instancePref_source_pitch_f0Mid(),
238 my instancePref_source_pitch_tEnd(), my instancePref_source_pitch_f0End(),
239 my instancePref_source_phonation_adaptFactor(), my instancePref_source_phonation_maximumPeriod(),
240 my instancePref_source_phonation_openPhase(), my instancePref_source_phonation_collisionPhase(),
241 my instancePref_source_phonation_power1(), my instancePref_source_phonation_power2(),
242 playCallback, playBoss);
243 }
244
245 /* End of file FormantGridArea.cpp */