| 1 | /* VowelEditor.cpp |
| 2 | * |
| 3 | * Copyright (C) 2008-2023 David Weenink, 2015-2023 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. See the GNU |
| 13 | * 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 | /* |
| 20 | trajectory --> path ???? |
| 21 | The main part of the VowelEditor is a drawing area. |
| 22 | In this drawing area a cursor can be moved around by a mouse. |
| 23 | The position of the cursor is related to the F1 and F2 frequencies. |
| 24 | On_mouse_down the position of the cursor is sampled (Not at fixed intervals!). |
| 25 | This results in a series of (x,y) values that will be transformed to (F1,F2) values in Hertz. |
| 26 | The corresponding sound is made audible after the mouse is released. |
| 27 | |
| 28 | The user graphics area is the F1-F2 plane: here the origin is at the top-right with log(F2) on the |
| 29 | horizontal axis and log(F1) on the vertical axis (i.e. log (F1) top-down, log(F2) right-to-left) |
| 30 | F1, F2 are always evaluated to hertz values; |
| 31 | On the contrary the Graphics_window has it origin at the bottom left (0,0) and the top-right is at (1,1). |
| 32 | We use a transformation such that log(fmin) -> 1 and log(fmax)-> 0 |
| 33 | Transformations XY <=> F1F2: VowelEditor_getXYFromF1F2(...) and VowelEditor_getF1F2FromXY(...) |
| 34 | For x direction F2 from right to left |
| 35 | 1 = a * log(f2min) +b |
| 36 | 0 = a * log(f2max) +b |
| 37 | x' = a (log(f2)-log(f2max)) |
| 38 | |
| 39 | 1 = a * log(f1min) +b |
| 40 | 0 = a * log(f1max) +b |
| 41 | y' = a (log(f1)-log(f1max)) |
| 42 | TO DO: |
| 43 | Make sound-follows-mouse real time! |
| 44 | */ |
| 45 | |
| 46 | #include "FormantGrid.h" |
| 47 | #include "KlattGrid.h" |
| 48 | #include "PitchTier_to_Sound.h" |
| 49 | #include "Polygon.h" |
| 50 | #include "Preferences.h" |
| 51 | #include "TableOfReal_extensions.h" |
| 52 | #include "Table_extensions.h" |
| 53 | #include "VowelEditor.h" // this includes windows.h |
| 54 | #include "EditorM.h" // include this *after* including windows.h (it redefines BOOLEAN) |
| 55 | #include "machine.h" |
| 56 | #include <time.h> |
| 57 | |
| 58 | #if defined (macintosh) |
| 59 | #include <sys/time.h> |
| 60 | #elif defined (linux) |
| 61 | #include <sys/time.h> |
| 62 | #include <signal.h> |
| 63 | #endif |
| 64 | |
| 65 | #include "enums_getText.h" |
| 66 | #include "VowelEditor_enums.h" |
| 67 | #include "enums_getValue.h" |
| 68 | #include "VowelEditor_enums.h" |
| 69 | |
| 70 | Thing_implement (VowelEditor, Editor, 0); |
| 71 | |
| 72 | #include "Prefs_define.h" |
| 73 | #include "VowelEditor_prefs.h" |
| 74 | #include "Prefs_install.h" |
| 75 | #include "VowelEditor_prefs.h" |
| 76 | #include "Prefs_copyToInstance.h" |
| 77 | #include "VowelEditor_prefs.h" |
| 78 | |
| 79 | #define STATUS_INFO (3*Gui_LABEL_HEIGHT) |
| 80 | #define MARGIN_RIGHT 10 |
| 81 | #define MARGIN_LEFT 50 |
| 82 | #define MARGIN_TOP 50 |
| 83 | #define MARGIN_BOTTOM (100+STATUS_INFO) |
| 84 | |
| 85 | #pragma mark - class TrajectoryPointTier |
| 86 | |
| 87 | Thing_implement (TrajectoryPoint, AnyPoint, 0); |
| 88 | |
| 89 | static autoTrajectoryPoint TrajectoryPoint_create (double time, double f1, double f2, MelderColour colour) { |
| 90 | autoTrajectoryPoint thee = Thing_new (TrajectoryPoint); |
| 91 | thy number = time; |
| 92 | thy f1 = f1; |
| 93 | thy f2 = f2; |
| 94 | thy colour = colour; |
| 95 | return thee; |
| 96 | } |
| 97 | |
| 98 | Thing_implement (Trajectory, Function, 0); |
| 99 | |
| 100 | static autoTrajectory Trajectory_create (double duration) { |
| 101 | try { |
| 102 | autoTrajectory me = Thing_new (Trajectory); |
| 103 | Function_init (me.get(), 0.0, duration); |
| 104 | return me; |
| 105 | } catch (MelderError) { |
| 106 | Melder_throw (U"Trajectory not created."); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | static void Trajectory_addPoint (Trajectory me, double time, double f1, double f2, MelderColour colour) { |
| 111 | try { |
| 112 | autoTrajectoryPoint point = TrajectoryPoint_create (time, f1, f2, colour); |
| 113 | my xmax = std::max (my xmax, time); |
| 114 | my points. addItem_move (point.move()); |
| 115 | } catch (MelderError) { |
| 116 | Melder_throw (me, U" no point added."); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | static void Trajectory_newDuration (Trajectory me, double newDuration) { |
| 121 | if (newDuration != my xmax) { |
| 122 | const double multiplier = newDuration / my xmax; |
| 123 | for (integer ipoint = 1; ipoint <= my points.size; ipoint ++) { |
| 124 | TrajectoryPoint point = my points.at [ipoint]; |
| 125 | point -> number *= multiplier; |
| 126 | } |
| 127 | my xmax = newDuration; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | static void Trajectory_reverse (Trajectory me) { |
| 132 | const double duration = my xmax; |
| 133 | const integer np = my points.size, np_2 = np / 2; |
| 134 | |
| 135 | for (integer ipoint = 1; ipoint <= np_2; ipoint ++) { |
| 136 | std::swap (my points.at [ipoint], my points.at [np - ipoint + 1]); |
| 137 | TrajectoryPoint point = my points.at [ipoint]; |
| 138 | point -> number = duration - point -> number; |
| 139 | point = my points.at [np - ipoint + 1]; |
| 140 | point -> number = duration - point -> number; |
| 141 | } |
| 142 | if (np % 2 == 1) { |
| 143 | TrajectoryPoint point = my points.at [np_2 + 1]; |
| 144 | point -> number = duration - point -> number; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | static void Trajectory_shift_semitones (Trajectory me, double f1_st, double f2_st) { |
| 149 | for (integer ipoint = 1; ipoint <= my points.size; ipoint ++) { |
| 150 | TrajectoryPoint point = my points.at [ipoint]; |
| 151 | double f1 = point -> f1, f2 = point -> f2; |
| 152 | f1 *= pow (2.0, f1_st / 12.0); |
| 153 | point -> f1 = f1; |
| 154 | f2 *= pow (2.0, f2_st / 12.0); |
| 155 | point -> f2 = f2; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | static void Trajectory_setColour (Trajectory me, double startTime, double endTime, MelderColour colour) { |
| 160 | Melder_require (Function_intersectRangeWithDomain (me, & startTime, & endTime), |
| 161 | U"The time interval is not part of the trajectory."); |
| 162 | /* |
| 163 | If a segment runs from p1 to p2 then its colour is detemined by p1. |
| 164 | */ |
| 165 | integer startIndex = AnyTier_timeToHighIndex (my asAnyTier (), startTime); |
| 166 | integer endIndex = AnyTier_timeToLowIndex (my asAnyTier (), endTime); |
| 167 | |
| 168 | for (integer ipoint = startIndex; ipoint <= endIndex; ipoint ++) { |
| 169 | TrajectoryPoint point = my points.at [ipoint]; |
| 170 | point -> colour = colour; |
| 171 | } |
| 172 | /* |
| 173 | We just add the points to the tier. |
| 174 | If the startTime and endTime are too far away from a trajectory point, |
| 175 | as could happen if a trajectory was created by the 'New trajectory' and/or |
| 176 | 'Extend trajectory' menu items, then we have to include a new trajectory point. |
| 177 | The (time,f1,f2) numbers are obtained by linear interpolation, where we |
| 178 | use that two points in a trajectory never bear the same time. |
| 179 | |
| 180 | We also have to guard against rounding down in the forms. For example an endTime |
| 181 | of 0.4632200000000007 might be shown in the form as '0.46322'. |
| 182 | */ |
| 183 | TrajectoryPoint p2, p1 = my points.at [endIndex]; |
| 184 | if (p1 -> number == endTime) { |
| 185 | if (endIndex < my points.size) { |
| 186 | p2 = my points.at [endIndex + 1]; |
| 187 | p1 -> colour = p2 -> colour; |
| 188 | } |
| 189 | } else { |
| 190 | p2 = my points.at [endIndex + 1]; |
| 191 | double t = (endTime - p1 -> number) / (p2 -> number - p1 -> number); |
| 192 | double f2 = p1 -> f2 + (p2 -> f2 - p1 -> f2) * t; |
| 193 | double f1 = p1 -> f1 + (p2 -> f1 - p1 -> f1) * t; |
| 194 | Trajectory_addPoint (me, endTime, f1, f2, ( startIndex > endIndex ? p1 -> colour : p2 -> colour )); |
| 195 | } |
| 196 | p2 = my points.at [startIndex]; |
| 197 | if (p2 -> number == startTime) { |
| 198 | if (startIndex > 1) { |
| 199 | p1 = my points.at [startIndex - 1]; |
| 200 | p2 -> colour = colour; |
| 201 | } |
| 202 | } else { |
| 203 | p1 = my points.at [startIndex - 1]; |
| 204 | double t = (startTime - p1 -> number) / (p2 -> number - p1 -> number); |
| 205 | double f2 = p1 -> f2 + (p2 -> f2 - p1 -> f2) * t; |
| 206 | double f1 = p1 -> f1 + (p2 -> f1 - p1 -> f1) * t; |
| 207 | Trajectory_addPoint (me, startTime, f1, f2, colour); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | #pragma mark - class Vowel |
| 212 | |
| 213 | static bool isValidVowelMarksTableFile (MelderFile file, autoTable *out_marks) { |
| 214 | if (! MelderFile_exists (file)) |
| 215 | return false; |
| 216 | try { |
| 217 | autoDaata data = Data_readFromFile (file); |
| 218 | if (! Thing_isa (data.get(), classTable)) |
| 219 | return false; |
| 220 | autoTable marks = data.static_cast_move <structTable>(); |
| 221 | /* |
| 222 | Require columns Vowel F1 and F2 to be present in the Table |
| 223 | */ |
| 224 | if (! (Table_columnNameToNumber_e (marks.get(), U"Vowel") |
| 225 | && Table_columnNameToNumber_e (marks.get(), U"F1") |
| 226 | && Table_columnNameToNumber_e (marks.get(), U"F2"))) |
| 227 | return false; |
| 228 | if (out_marks) |
| 229 | *out_marks = marks.move(); |
| 230 | return true; |
| 231 | } catch (MelderError) { |
| 232 | Melder_clearError (); |
| 233 | return false; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | static void VowelEditor_create_twoFormantSchwa (VowelEditor me) { |
| 238 | try { |
| 239 | my trajectory = Trajectory_create (my instancePref_trajectory_minimumDuration()); |
| 240 | MelderColour colour = MelderColour_fromColourNameOrRGBString (my instancePref_trajectory_colour()); |
| 241 | Trajectory_addPoint (my trajectory.get(), 0.0, 500.0, 1500.0, colour); |
| 242 | Trajectory_addPoint (my trajectory.get(), my instancePref_trajectory_minimumDuration(), 500.0, 1500.0, colour); |
| 243 | } catch (MelderError) { |
| 244 | Melder_throw (U"Schwa Vowel not created"); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | static inline double VowelEditor_getXFromF2 (VowelEditor me, double f2) { |
| 249 | return log (f2 / my instancePref_window_f2max()) / log (my instancePref_window_f2min() / my instancePref_window_f2max()); |
| 250 | } |
| 251 | |
| 252 | static inline double VowelEditor_getYFromF1 (VowelEditor me, double f1) { |
| 253 | return log (f1 / my instancePref_window_f1max()) / log (my instancePref_window_f1min() / my instancePref_window_f1max()); |
| 254 | } |
| 255 | |
| 256 | static void VowelEditor_getXYFromF1F2 (VowelEditor me, double f1, double f2, double *x, double *y) { |
| 257 | *y = VowelEditor_getYFromF1 (me, f1); |
| 258 | *x = VowelEditor_getXFromF2 (me, f2); |
| 259 | } |
| 260 | |
| 261 | static void VowelEditor_getF1F2FromXY (VowelEditor me, double x, double y, double *f1, double *f2) { |
| 262 | *f2 = my instancePref_window_f2min() * pow (my instancePref_window_f2max() / my instancePref_window_f2min(), 1.0 - x); |
| 263 | *f1 = my instancePref_window_f1min() * pow (my instancePref_window_f1max() / my instancePref_window_f1min(), 1.0 - y); |
| 264 | } |
| 265 | |
| 266 | static double getRealFromTextWidget (GuiText me) { |
| 267 | double value = undefined; |
| 268 | autostring32 dirty = GuiText_getString (me); |
| 269 | try { |
| 270 | Interpreter_numericExpression (nullptr, dirty.get(), & value); |
| 271 | } catch (MelderError) { |
| 272 | Melder_clearError (); |
| 273 | value = undefined; |
| 274 | } |
| 275 | return value; |
| 276 | } |
| 277 | |
| 278 | static void clipF1F2 (VowelEditor me, double *f1, double *f2) { |
| 279 | Melder_clip (my instancePref_window_f1min(), f1, my instancePref_window_f1max()); |
| 280 | Melder_clip (my instancePref_window_f2min(), f2, my instancePref_window_f2max()); |
| 281 | } |
| 282 | |
| 283 | static void VowelEditor_updateFromF0StartAndSlopeTextWidgets (VowelEditor me) { |
| 284 | double f0 = getRealFromTextWidget (my f0TextField); |
| 285 | Melder_clip (my instancePref_f0_minimum(), & f0, my instancePref_f0_maximum()); |
| 286 | GuiText_setString (my f0TextField, Melder_double (f0)); |
| 287 | my setInstancePref_f0_start (f0); |
| 288 | double slope = getRealFromTextWidget (my f0SlopeTextField); |
| 289 | if (isundef (slope)) |
| 290 | slope = 0.0; |
| 291 | my setInstancePref_f0_slope (slope); |
| 292 | GuiText_setString (my f0SlopeTextField, Melder_double (slope)); |
| 293 | } |
| 294 | |
| 295 | static void VowelEditor_updateFromExtendDurationTextWidget (VowelEditor me) { |
| 296 | double extend = getRealFromTextWidget (my extendTextField); |
| 297 | if (isundef (extend) || extend <= my instancePref_trajectory_minimumDuration()) |
| 298 | extend = my instancePref_trajectory_minimumDuration(); |
| 299 | GuiText_setString (my extendTextField, Melder_double (extend)); |
| 300 | my setInstancePref_trajectory_extendDuration (extend); |
| 301 | } |
| 302 | |
| 303 | static double VowelEditor_updateFromDurationTextWidget (VowelEditor me) { |
| 304 | double duration = getRealFromTextWidget (my durationTextField); |
| 305 | if (isundef (duration) || duration < my instancePref_trajectory_minimumDuration()) |
| 306 | duration = my instancePref_trajectory_minimumDuration(); |
| 307 | my setInstancePref_trajectory_duration (duration); |
| 308 | GuiText_setString (my durationTextField, Melder_fixed (duration, 6)); |
| 309 | Trajectory_newDuration (my trajectory.get(), duration); |
| 310 | return duration; |
| 311 | } |
| 312 | |
| 313 | static void Sound_fadeIn (Sound me, double duration, bool fromFirstNonZeroSample) { |
| 314 | integer istart = 1, numberOfSamples = Melder_ifloor (duration / my dx); // ppgb: waarom afronden naar beneden? |
| 315 | if (numberOfSamples < 2) |
| 316 | return; |
| 317 | if (fromFirstNonZeroSample) { |
| 318 | /* |
| 319 | If the first part of the sound is very low level we put sample values to zero and |
| 320 | start windowing from the position where the amplitude is above the minimum level. |
| 321 | WARNING: this part is special for the artificial vowels because |
| 322 | 1. They have no offset |
| 323 | 2. They are already scaled to a maximum amplitude of 0.99 |
| 324 | 3. For 16 bit precision |
| 325 | */ |
| 326 | const double zmin = 0.5 / pow (2.0, 16.0); |
| 327 | while (fabs (my z [1] [istart]) < zmin && istart < my nx) { |
| 328 | my z [1] [istart] = 0.0; // To make sure |
| 329 | istart ++; |
| 330 | } |
| 331 | } |
| 332 | Melder_clipRight (& numberOfSamples, my nx - istart + 1); |
| 333 | for (integer i = 1; i <= numberOfSamples; i ++) { |
| 334 | const double phase = NUMpi * (i - 1) / (numberOfSamples - 1); |
| 335 | my z [1] [istart + i - 1] *= 0.5 * (1.0 - cos (phase)); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | static void Sound_fadeOut (Sound me, double duration) { |
| 340 | integer numberOfSamples = Melder_ifloor (duration / my dx); |
| 341 | Melder_clipRight (& numberOfSamples, my nx); |
| 342 | if (numberOfSamples < 2) |
| 343 | return; |
| 344 | const integer istart = my nx - numberOfSamples; |
| 345 | /* |
| 346 | Only one channel. |
| 347 | */ |
| 348 | for (integer i = 1; i <= numberOfSamples; i ++) { |
| 349 | const double phase = NUMpi * (i - 1) / (numberOfSamples - 1); |
| 350 | my z [1] [istart + i] *= 0.5 * (1.0 + cos (phase)); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | static double VowelEditor_getF0AtTime (VowelEditor me, double time) { |
| 355 | double f0 = my instancePref_f0_start() * pow (2.0, my instancePref_f0_slope() * time); |
| 356 | Melder_clip (my instancePref_f0_minimum(), & f0, my instancePref_f0_maximum()); |
| 357 | return f0; |
| 358 | } |
| 359 | |
| 360 | static void VowelEditor_updateTrajectorySpecification (VowelEditor me) { |
| 361 | /* |
| 362 | Always update; GuiObject text might have changed. |
| 363 | */ |
| 364 | VowelEditor_updateFromDurationTextWidget (me); |
| 365 | VowelEditor_updateFromF0StartAndSlopeTextWidgets (me); |
| 366 | } |
| 367 | |
| 368 | static autoFormantGrid VowelEditor_to_FormantGrid (VowelEditor me) { |
| 369 | try { |
| 370 | autoFormantGrid thee = FormantGrid_createEmpty (my trajectory -> xmin, my trajectory -> xmax, my instancePref_synthesis_numberOfFormants()); |
| 371 | for (integer ipoint = 1; ipoint <= my trajectory -> points.size; ipoint ++) { |
| 372 | const TrajectoryPoint tp = my trajectory -> points.at [ipoint]; |
| 373 | const double time = tp -> number; |
| 374 | FormantGrid_addFormantPoint (thee.get(), 1, time, tp -> f1); |
| 375 | FormantGrid_addBandwidthPoint (thee.get(), 1, time, tp -> f1 / my instancePref_synthesis_q1()); |
| 376 | if (my instancePref_synthesis_numberOfFormants() < 2) |
| 377 | continue; |
| 378 | FormantGrid_addFormantPoint (thee.get(), 2, time, tp -> f2); |
| 379 | FormantGrid_addBandwidthPoint (thee.get(), 2, time, tp -> f2 / my instancePref_synthesis_q2()); |
| 380 | for (integer ifor = 1; ifor <= my instancePref_synthesis_numberOfFormants() - 2; ifor ++) { |
| 381 | if (my instancePref_synthesis_numberOfFormants() < 2 + ifor) |
| 382 | break; |
| 383 | FormantGrid_addFormantPoint (thee.get(), 2 + ifor, time, my extraFrequencyBandwidthPairs [2 * ifor - 1]); |
| 384 | FormantGrid_addBandwidthPoint (thee.get(), 2 + ifor, time, my extraFrequencyBandwidthPairs [2 * ifor]); |
| 385 | } |
| 386 | } |
| 387 | return thee; |
| 388 | } catch (MelderError) { |
| 389 | Melder_throw (U"No FormantGrid created."); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | static autoPitchTier VowelEditor_to_PitchTier (VowelEditor me) { |
| 394 | try { |
| 395 | autoPitchTier thee = PitchTier_create (my trajectory -> xmin, my trajectory -> xmax); |
| 396 | for (integer ipoint = 1; ipoint <= my trajectory -> points.size; ipoint ++) { |
| 397 | const TrajectoryPoint point = my trajectory -> points.at [ipoint]; |
| 398 | const double f0 = VowelEditor_getF0AtTime (me, point -> number); |
| 399 | RealTier_addPoint (thee.get(), point -> number, f0); |
| 400 | } |
| 401 | return thee; |
| 402 | } catch (MelderError) { |
| 403 | Melder_throw (U"PitchTier not created."); |
| 404 | } |
| 405 | } |
| 406 | static autoSound VowelEditor_createTargetSound (VowelEditor me) { |
| 407 | try { |
| 408 | VowelEditor_updateTrajectorySpecification (me); // update pitch and duration |
| 409 | autoFormantGrid formantGrid = VowelEditor_to_FormantGrid (me); |
| 410 | autoPitchTier pitchTier = VowelEditor_to_PitchTier (me); |
| 411 | autoSound thee = PitchTier_to_Sound_pulseTrain (pitchTier.get(), my instancePref_synthesis_samplingFrequency(), 0.7, 0.05, 30, false); |
| 412 | Sound_FormantGrid_filter_inplace (thee.get(), formantGrid.get()); |
| 413 | Vector_scale (thee.get(), 0.99); |
| 414 | Sound_fadeIn (thee.get(), 0.005, true); |
| 415 | Sound_fadeOut (thee.get(), 0.005); |
| 416 | return thee; |
| 417 | } catch (MelderError) { |
| 418 | Melder_throw (U"Target Sound not created."); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | /* Precondition: trajectory points are all different */ |
| 423 | static void VowelEditor_drawF1F2Trajectory (VowelEditor me, Graphics g) { |
| 424 | if (my trajectory -> points.size < 2) |
| 425 | return; |
| 426 | |
| 427 | const int savedLineType = Graphics_inqLineType (g); |
| 428 | const double savedLineWidth = Graphics_inqLineWidth (g); |
| 429 | const MelderColour savedColour = Graphics_inqColour (g); |
| 430 | |
| 431 | Graphics_setInner (g); |
| 432 | Graphics_setWindow (g, 0.0, 1.0, 0.0, 1.0); |
| 433 | Graphics_setLineType (g, Graphics_DRAWN); |
| 434 | if (my trajectory -> xmax - my trajectory -> xmin < 0.005) // too short to hear? |
| 435 | Graphics_setColour (g, Melder_RED); |
| 436 | |
| 437 | auto getx = [=](double f) { return log (f / my instancePref_window_f2max()) / log (my instancePref_window_f2min() / my instancePref_window_f2max()); }; |
| 438 | auto gety = [=](double f) { return log (f / my instancePref_window_f1max()) / log (my instancePref_window_f1min() / my instancePref_window_f1max()); }; |
| 439 | |
| 440 | const TrajectoryPoint firstPoint = my trajectory -> points.at [1]; |
| 441 | double x1 = getx (firstPoint -> f2); |
| 442 | double y1 = gety (firstPoint -> f1); |
| 443 | double t1 = firstPoint -> number; |
| 444 | MelderColour colour = firstPoint -> colour; // first point determines colour of segment |
| 445 | Graphics_setColour (g, colour); |
| 446 | integer imark = 1; |
| 447 | for (integer it = 2; it <= my trajectory -> points.size; it ++) { |
| 448 | const TrajectoryPoint point = my trajectory -> points.at [it]; |
| 449 | const double x2 = getx (point -> f2); |
| 450 | const double y2 = gety (point -> f1); |
| 451 | const double t2 = point -> number; |
| 452 | Graphics_setLineWidth (g, my instancePref_trajectory_lineWidth()); |
| 453 | Graphics_line (g, x1, y1, x2, y2); |
| 454 | /* |
| 455 | Draw line orthogonal to the trajectory at regular points. |
| 456 | */ |
| 457 | double markTime; |
| 458 | while ((markTime = my instancePref_trajectory_markEvery() * imark) < t2) { |
| 459 | /* |
| 460 | The parametrization of the line running from (x1,y1) to (x2,y2) is |
| 461 | (1) (x1, y1) + s * (x2 - x1, y2 - y1), where 0 <= s <= 1; |
| 462 | The mark position (xm,ym) is at s = (markTime - t1) / (t2 - t1). |
| 463 | The line perpendicular to this line can be parametrized as |
| 464 | (2) (xm, ym) + v * (-(y2 - y1), x2 - x1). |
| 465 | We need the startpoint, M1, and endpoint, M2, of the mark line. These are parametrized as |
| 466 | (xl1,yl1) = (xm,ym) + V * (y1 - y2, x2 - x1) and (xl2,yl2) = (xm , ym) - V * (y1 - y2, x2 - x1). |
| 467 | The length of the vector mark line constrains the value of V. |
| 468 | */ |
| 469 | const double s = (markTime - t1) / (t2 - t1); |
| 470 | const double dx = x2 - x1, dy = y2 - y1; |
| 471 | const double xm = x1 + s * dx; |
| 472 | const double ym = y1 + s * dy; |
| 473 | const double d = hypot (dx, dy); |
| 474 | const double v = my instancePref_trajectory_markLength() / (2.0 * d); // d > 0 |
| 475 | const double xl1 = xm - v * dy, yl1 = ym + v * dx; |
| 476 | const double xl2 = xm + v * dy, yl2 = ym - v * dx; |
| 477 | Graphics_setLineWidth (g, 1); |
| 478 | Graphics_line (g, xl1, yl1, xl2, yl2); |
| 479 | imark ++; |
| 480 | } |
| 481 | x1 = x2; |
| 482 | y1 = y2; |
| 483 | t1 = t2; |
| 484 | if (! MelderColour_equal (colour, point -> colour)) { |
| 485 | Graphics_setColour (g, point -> colour); |
| 486 | colour = point -> colour; |
| 487 | } |
| 488 | } |
| 489 | /* |
| 490 | Arrow at end. |
| 491 | */ |
| 492 | { |
| 493 | const integer n = my trajectory -> points.size; |
| 494 | const double savedArrowSize = Graphics_inqArrowSize (g), arrowSize = 1.0; |
| 495 | double resolution = Graphics_getResolution (g); |
| 496 | Graphics_setArrowSize (g, arrowSize); |
| 497 | integer it = 0; |
| 498 | const TrajectoryPoint lastPoint = my trajectory -> points.at [n]; |
| 499 | TrajectoryPoint point; |
| 500 | Melder_assert (n >= 2); // otherwise `point` will stay undefined |
| 501 | while (++ it <= n - 1) { |
| 502 | point = my trajectory -> points.at [n - it]; |
| 503 | const double dx = resolution * (getx (lastPoint -> f2) - getx (point -> f2)); |
| 504 | const double dy = resolution * (gety (lastPoint -> f1) - gety (point -> f1)); |
| 505 | const double d2 = dx * dx + dy * dy; |
| 506 | if (sqrt (d2) > arrowSize) |
| 507 | break; |
| 508 | } |
| 509 | Graphics_arrow (g, getx (point -> f2), gety (point -> f1), getx (lastPoint -> f2), gety (lastPoint -> f1)); |
| 510 | Graphics_setArrowSize (g, savedArrowSize); |
| 511 | } |
| 512 | Graphics_unsetInner (g); |
| 513 | Graphics_setLineType (g, savedLineType); |
| 514 | Graphics_setLineWidth (g, savedLineWidth); |
| 515 | Graphics_setColour (g, savedColour); |
| 516 | } |
| 517 | |
| 518 | static void Table_addColumnIfNotExists_size (Table me, double size) { |
| 519 | const integer sizeColumn = Table_columnNameToNumber_0 (me, U"Size"); |
| 520 | if (sizeColumn == 0) { |
| 521 | Table_appendColumn (me, U"Size"); |
| 522 | for (integer irow = 1; irow <= my rows.size; irow ++) |
| 523 | Table_setNumericValue (me, irow, my numberOfColumns, size); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | static void Table_addColumnIfNotExists_colour (Table me, conststring32 colour) { |
| 528 | const integer colourColumn = Table_columnNameToNumber_0 (me, U"Colour"); |
| 529 | if (colourColumn == 0) { |
| 530 | Table_appendColumn (me, U"Colour"); |
| 531 | for (integer irow = 1; irow <= my rows.size; irow ++) |
| 532 | Table_setStringValue (me, irow, my numberOfColumns, colour); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | static void VowelEditor_getVowelMarksFromFile (VowelEditor me) { |
| 537 | autoTable marks; |
| 538 | structMelderFile file { }; |
| 539 | Melder_pathToFile (my instancePref_marks_fileName(), & file); |
| 540 | if (! isValidVowelMarksTableFile (& file, & marks)) |
| 541 | return; |
| 542 | Table_addColumnIfNotExists_size (marks.get(), my instancePref_marks_fontSize()); |
| 543 | my marks = marks.move(); |
| 544 | } |
| 545 | |
| 546 | static void VowelEditor_getMarks (VowelEditor me) { |
| 547 | autoTable te; |
| 548 | if (my instancePref_marks_dataSet() == kVowelEditor_marksDataSet::AMERICAN_ENGLISH) { |
| 549 | const autoTable thee = Table_create_petersonBarney1952 (); |
| 550 | te = Table_extractRowsWhereColumn_string (thee.get(), 1, kMelder_string::EQUAL_TO, |
| 551 | ( my instancePref_marks_speakerType() == kVowelEditor_speakerType::MAN ? U"m" : |
| 552 | my instancePref_marks_speakerType() == kVowelEditor_speakerType::WOMAN ? U"w" : |
| 553 | my instancePref_marks_speakerType() == kVowelEditor_speakerType::CHILD ? U"c": U"m" )); |
| 554 | } else if (my instancePref_marks_dataSet() == kVowelEditor_marksDataSet::DUTCH) { |
| 555 | if (my instancePref_marks_speakerType() == kVowelEditor_speakerType::CHILD) { |
| 556 | const autoTable thee = Table_create_weenink1983 (); |
| 557 | te = Table_extractRowsWhereColumn_string (thee.get(), 1, kMelder_string::EQUAL_TO, U"c"); |
| 558 | } |
| 559 | else { // male + female from Pols van Nierop |
| 560 | const autoTable thee = Table_create_polsVanNierop1973 (); |
| 561 | te = Table_extractRowsWhereColumn_string (thee.get(), 1, kMelder_string::EQUAL_TO, |
| 562 | ( my instancePref_marks_speakerType() == kVowelEditor_speakerType::MAN ? U"m" : my instancePref_marks_speakerType() == kVowelEditor_speakerType::WOMAN ? U"f" : U"f" )); |
| 563 | } |
| 564 | } else if (my instancePref_marks_dataSet() == kVowelEditor_marksDataSet::NONE) { |
| 565 | my marks. reset(); |
| 566 | return; |
| 567 | } else { // other |
| 568 | VowelEditor_getVowelMarksFromFile (me); |
| 569 | return; |
| 570 | } |
| 571 | |
| 572 | /* mutable move */ autoTable newMarks = Table_collapseRows (te.get(), |
| 573 | autoSTRVEC ({ U"IPA" }).get(), |
| 574 | autoSTRVEC ({}).get(), |
| 575 | autoSTRVEC ({ U"F1", U"F2" }).get(), |
| 576 | autoSTRVEC ({}).get(), |
| 577 | autoSTRVEC ({}).get(), |
| 578 | autoSTRVEC ({}).get() |
| 579 | ); |
| 580 | const integer col_ipa = Table_columnNameToNumber_0 (newMarks.get(), U"IPA"); |
| 581 | Table_renameColumn_e (newMarks.get(), col_ipa, U"Vowel"); |
| 582 | |
| 583 | Table_addColumnIfNotExists_size (newMarks.get(), my instancePref_marks_fontSize()); |
| 584 | Table_addColumnIfNotExists_colour (newMarks.get(), my instancePref_marks_colour()); |
| 585 | my marks = newMarks.move(); |
| 586 | } |
| 587 | |
| 588 | static void VowelEditor_drawBackground (VowelEditor me, Graphics g) { |
| 589 | Graphics_setInner (g); |
| 590 | Graphics_setWindow (g, 0.0, 1.0, 0.0, 1.0); |
| 591 | Graphics_setGrey (g, 0.0); |
| 592 | Graphics_setLineType (g, Graphics_DRAWN); |
| 593 | Graphics_setLineWidth (g, 1.0); |
| 594 | Graphics_setGrey (g, 0.5); |
| 595 | const double fontSize = Graphics_inqFontSize (g); |
| 596 | /* |
| 597 | Draw the marks. |
| 598 | */ |
| 599 | if (my marks) { |
| 600 | const integer col_vowel = Table_columnNameToNumber_e (my marks.get(), U"Vowel"); |
| 601 | const integer col_f1 = Table_columnNameToNumber_e (my marks.get(), U"F1"); |
| 602 | const integer col_f2 = Table_columnNameToNumber_e (my marks.get(), U"F2"); |
| 603 | const integer col_size = Table_columnNameToNumber_0 (my marks.get(), U"Size"); |
| 604 | const integer col_colour = Table_columnNameToNumber_0 (my marks.get(), U"Colour"); |
| 605 | for (integer irow = 1; irow <= my marks -> rows.size; irow ++) { |
| 606 | const conststring32 label = Table_getStringValue_a (my marks.get(), irow, col_vowel); |
| 607 | const double f1 = Table_getNumericValue_a (my marks.get(), irow, col_f1); |
| 608 | const double f2 = Table_getNumericValue_a (my marks.get(), irow, col_f2); |
| 609 | if (f1 >= my instancePref_window_f1min() && f1 <= my instancePref_window_f1max() && f2 >= my instancePref_window_f2min() && f2 <= my instancePref_window_f2max()) { |
| 610 | double x1, y1; |
| 611 | VowelEditor_getXYFromF1F2 (me, f1, f2, & x1, & y1); |
| 612 | double size = my instancePref_marks_fontSize(); |
| 613 | if (col_size != 0) |
| 614 | size = Table_getNumericValue_a (my marks.get(), irow, col_size); |
| 615 | if (col_colour != 0) { |
| 616 | conststring32 colourString = Table_getStringValue_a (my marks.get(), irow, col_colour); |
| 617 | MelderColour colour = MelderColour_fromColourNameOrNumberStringOrRGBString (colourString); |
| 618 | if (! colour. valid()) |
| 619 | colour = MelderColour_fromColourName (my instancePref_marks_colour()); |
| 620 | Graphics_setColour (g, colour); |
| 621 | } |
| 622 | Graphics_setFontSize (g, size); |
| 623 | Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::CENTRE, Graphics_HALF); |
| 624 | Graphics_text (g, x1, y1, label); |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | Graphics_setFontSize (g, fontSize); |
| 629 | Graphics_setColour (g, Melder_BLACK); |
| 630 | /* |
| 631 | Draw the line F1=F2. |
| 632 | */ |
| 633 | double xl1, yl1, xl2, yl2; |
| 634 | VowelEditor_getXYFromF1F2 (me, my instancePref_window_f2min(), my instancePref_window_f2min(), & xl1, & yl1); |
| 635 | if (yl1 >= 0.0 && yl1 <= 1.0) { |
| 636 | VowelEditor_getXYFromF1F2 (me, my instancePref_window_f1max(), my instancePref_window_f1max(), & xl2, & yl2); |
| 637 | if (xl2 >= 0.0 && xl2 <= 1.0) { |
| 638 | double x [] = { xl1, xl2, 1.0 }; |
| 639 | double y [] = { yl1, yl2, 0.0 }; |
| 640 | Graphics_setGrey (g, 0.6); |
| 641 | Graphics_fillArea (g, 3, x , y); |
| 642 | Graphics_setColour (g, Melder_BLACK); |
| 643 | Graphics_line (g, xl1, yl1, xl2, yl2); |
| 644 | } |
| 645 | } |
| 646 | /* |
| 647 | Draw the horizontal grid lines. |
| 648 | */ |
| 649 | if (my instancePref_grid_df1() < my instancePref_window_f1max() - my instancePref_window_f1min()) { |
| 650 | integer iline = Melder_iroundDown ((my instancePref_window_f1min() + my instancePref_grid_df1()) / my instancePref_grid_df1()); |
| 651 | Graphics_setGrey (g, 0.5); |
| 652 | Graphics_setLineType (g, Graphics_DOTTED); |
| 653 | double f1_gridLine, xg1, yg1, xg2, yg2; |
| 654 | while ((f1_gridLine = iline * my instancePref_grid_df1()) < my instancePref_window_f1max()) { |
| 655 | if (f1_gridLine > my instancePref_window_f1min()) { |
| 656 | VowelEditor_getXYFromF1F2 (me, f1_gridLine, my instancePref_window_f2min(), & xg1, & yg1); |
| 657 | VowelEditor_getXYFromF1F2 (me, f1_gridLine, my instancePref_window_f2max(), & xg2, & yg2); |
| 658 | Graphics_line (g, xg1, yg1, xg2, yg2); |
| 659 | } |
| 660 | iline ++; |
| 661 | } |
| 662 | Graphics_setLineType (g, Graphics_DRAWN); |
| 663 | Graphics_setColour (g, Melder_BLACK); |
| 664 | } |
| 665 | /* |
| 666 | Draw the vertical grid lines. |
| 667 | */ |
| 668 | if (my instancePref_grid_df2() < my instancePref_window_f2max() - my instancePref_window_f2min()) { |
| 669 | integer iline = Melder_iroundDown ((my instancePref_window_f2min() + my instancePref_grid_df2()) / my instancePref_grid_df2()); |
| 670 | Graphics_setGrey (g, 0.5); |
| 671 | Graphics_setLineType (g, Graphics_DOTTED); |
| 672 | double f2_gridLine, xg1, yg1, xg2, yg2; |
| 673 | while ((f2_gridLine = iline * my instancePref_grid_df2()) < my instancePref_window_f2max()) { // vert line |
| 674 | if (f2_gridLine > my instancePref_window_f2min()) { |
| 675 | VowelEditor_getXYFromF1F2 (me, my instancePref_window_f1min(), f2_gridLine, & xg1, & yg1); |
| 676 | VowelEditor_getXYFromF1F2 (me, my instancePref_window_f1max(), f2_gridLine, & xg2, & yg2); |
| 677 | Graphics_line (g, xg1, yg1, xg2, yg2); |
| 678 | } |
| 679 | iline ++; |
| 680 | } |
| 681 | Graphics_setLineType (g, Graphics_DRAWN); |
| 682 | Graphics_setColour (g, Melder_BLACK); |
| 683 | } |
| 684 | Graphics_setLineWidth (g, 2.0); |
| 685 | Graphics_rectangle (g, 0.0, 1.0, 0.0, 1.0); |
| 686 | Graphics_unsetInner (g); |
| 687 | Graphics_setGrey (g, 0.0); // black |
| 688 | Graphics_markLeft (g, 0.0, false, true, false, Melder_double (my instancePref_window_f1max())); |
| 689 | Graphics_markLeft (g, 1.0, false, true, false, Melder_double (my instancePref_window_f1min())); |
| 690 | Graphics_markTop (g, 0.0, false, true, false, Melder_double (my instancePref_window_f2max())); |
| 691 | Graphics_markTop (g, 1.0, false, true, false, Melder_double (my instancePref_window_f2min())); |
| 692 | } |
| 693 | |
| 694 | #pragma mark - menu methods |
| 695 | |
| 696 | static void updateInfoLabels (VowelEditor me) { |
| 697 | const double startF0 = VowelEditor_getF0AtTime (me, my trajectory -> xmin); |
| 698 | const double endF0 = VowelEditor_getF0AtTime (me, my trajectory -> xmax); |
| 699 | const TrajectoryPoint startPoint = my trajectory -> points.at [1]; |
| 700 | const TrajectoryPoint endPoint = my trajectory -> points.at [my trajectory -> points.size]; |
| 701 | MelderString statusInfo; |
| 702 | if (! my graphics) |
| 703 | return; // could be the case in the very beginning |
| 704 | MelderString_append (& statusInfo, U"Start (F1,F2,f0) = (", Melder_fixed (startPoint -> f1, 1), U", ", |
| 705 | Melder_fixed (startPoint -> f2, 1), U", ", Melder_fixed (startF0, 1), U")"); |
| 706 | GuiLabel_setText (my startInfo, statusInfo.string); |
| 707 | MelderString_empty (& statusInfo); |
| 708 | MelderString_append (& statusInfo, U"End (F1,F2,f0) = (", Melder_fixed (endPoint -> f1, 1), U", ", |
| 709 | Melder_fixed (endPoint -> f2, 1), U", ", Melder_fixed (endF0, 1), U")"); |
| 710 | GuiLabel_setText (my endInfo, statusInfo.string); |
| 711 | } |
| 712 | |
| 713 | static void menu_cb_help (VowelEditor /* me */, EDITOR_ARGS) { |
| 714 | HELP (U"VowelEditor") |
| 715 | } |
| 716 | |
| 717 | static void menu_cb_trajectoryInfo (VowelEditor me, EDITOR_ARGS) { |
| 718 | INFO_EDITOR |
| 719 | MelderInfo_open (); |
| 720 | MelderInfo_writeLine (U"Trajectory info:"); |
| 721 | MelderInfo_writeLine (U"Number of points: ", my trajectory -> points.size); |
| 722 | MelderInfo_writeLine (U"Start time: ", my trajectory -> xmin, U" s"); |
| 723 | TrajectoryPoint p1 = my trajectory -> points.at [1]; |
| 724 | MelderInfo_writeLine (U" F1: ", p1 -> f1, U" Hz"); |
| 725 | MelderInfo_writeLine (U" F2: ", p1 -> f2, U" Hz"); |
| 726 | MelderInfo_writeLine (U"End time: ", my trajectory -> xmax, U" s"); |
| 727 | TrajectoryPoint p2 = my trajectory -> points.at [my trajectory -> points.size]; |
| 728 | MelderInfo_writeLine (U" F1: ", p2 -> f1, U" Hz"); |
| 729 | MelderInfo_writeLine (U" F2: ", p2 -> f2, U" Hz"); |
| 730 | MelderInfo_close (); |
| 731 | INFO_EDITOR_END |
| 732 | } |
| 733 | |
| 734 | static void menu_cb_settings (VowelEditor me, EDITOR_ARGS) { |
| 735 | EDITOR_FORM (U"Settings", nullptr); |
| 736 | BOOLEAN (soundFollowsMouse, U"Sound follows mouse", my default_soundFollowsMouse()) |
| 737 | COMMENT (U"F1 and F2 frequencies are specified by the trajectory.") |
| 738 | COMMENT (U"The bandwidths of a formant can be specified by its Q-value") |
| 739 | COMMENT (U"which defines the sharpness of the peak: Q = frequency / bandwidth. ") |
| 740 | POSITIVE (q1, U"F1 sharpness", my default_synthesis_q1 ()) |
| 741 | POSITIVE (q2, U"F2 sharpness", my default_synthesis_q2 ()) |
| 742 | COMMENT (U"You can define extra fixed formants for the synthesis by supplying") |
| 743 | COMMENT (U"formant frequency bandwidth pairs.") |
| 744 | TEXTFIELD (extraFrequencyBandwidthPairs_string, U"Frequency–bandwidth pairs", my default_synthesis_extraFBPairs(), 3) |
| 745 | COMMENT (U"The total number of formants used for synthesis") |
| 746 | NATURAL (numberOfFormants, U"Number of formants for synthesis", my default_synthesis_numberOfFormants ()) |
| 747 | EDITOR_OK |
| 748 | SET_BOOLEAN (soundFollowsMouse, my instancePref_soundFollowsMouse()) |
| 749 | SET_REAL (q1, my instancePref_synthesis_q1()) |
| 750 | SET_REAL (q2, my instancePref_synthesis_q2()) |
| 751 | SET_STRING (extraFrequencyBandwidthPairs_string, my instancePref_synthesis_extraFBPairs()) |
| 752 | SET_INTEGER (numberOfFormants, my instancePref_synthesis_numberOfFormants()) |
| 753 | EDITOR_DO |
| 754 | my setInstancePref_soundFollowsMouse (soundFollowsMouse); |
| 755 | my setInstancePref_synthesis_q1 (q1); |
| 756 | my setInstancePref_synthesis_q2 (q2); |
| 757 | autoVEC extraFrequencyBandwidthPairs = splitByWhitespace_VEC (extraFrequencyBandwidthPairs_string); |
| 758 | |
| 759 | Melder_require (extraFrequencyBandwidthPairs.size % 2 == 0, |
| 760 | U"There should be an even number of values in the “Frequencies and bandwidths pairs” list."); |
| 761 | /* |
| 762 | All items should be positive numbers and frequencies should be lower than the Nyquist. |
| 763 | Bandwidths should be greater than zero. |
| 764 | */ |
| 765 | for (integer item = 1; item <= extraFrequencyBandwidthPairs.size; item ++) { |
| 766 | Melder_require (extraFrequencyBandwidthPairs [item] > 0.0, |
| 767 | U"All frequency values and bandwidth values should be positive."); |
| 768 | if (item % 2 == 1) |
| 769 | Melder_require (extraFrequencyBandwidthPairs [item] < 0.5 * my instancePref_synthesis_samplingFrequency(), |
| 770 | U"All formant frequencies should be below the Nyquist frequency (", |
| 771 | 0.5 * my instancePref_synthesis_samplingFrequency(), U" Hz."); |
| 772 | } |
| 773 | const integer numberOfPairs = extraFrequencyBandwidthPairs.size / 2; |
| 774 | Melder_require (numberOfFormants <= numberOfPairs + 2, |
| 775 | U"The “Number of formants for synthesis” should not exceed 2 plus the number of extra frequency–bandwidth pairs " |
| 776 | U"(i.e. 2+", numberOfPairs, |
| 777 | U"). Either lower the number of formants for synthesis, or specify more frequency–bandwidth pairs."); |
| 778 | /* |
| 779 | Formants and bandwidths are valid. It is safe to copy them. |
| 780 | */ |
| 781 | my setInstancePref_synthesis_extraFBPairs (extraFrequencyBandwidthPairs_string); |
| 782 | my setInstancePref_synthesis_numberOfFormants (numberOfFormants); |
| 783 | my extraFrequencyBandwidthPairs = extraFrequencyBandwidthPairs.move(); |
| 784 | EDITOR_END |
| 785 | } |
| 786 | |
| 787 | static void menu_cb_ranges_f1f2 (VowelEditor me, EDITOR_ARGS) { |
| 788 | EDITOR_FORM (U"F1 (vert) and F2 (hor) view ranges", nullptr); |
| 789 | POSITIVE (f1min, U"left F1 range (Hz)", my default_window_f1min()) |
| 790 | POSITIVE (f1max, U"right F1 range (Hz)", my default_window_f1max()) |
| 791 | POSITIVE (f2min, U"left F2 range (Hz)", my default_window_f2min()) |
| 792 | POSITIVE (f2max, U"right F2 range (Hz)", my default_window_f2max()) |
| 793 | EDITOR_OK |
| 794 | SET_REAL (f1min, my instancePref_window_f1min()) |
| 795 | SET_REAL (f1max, my instancePref_window_f1max()) |
| 796 | SET_REAL (f2min, my instancePref_window_f2min()) |
| 797 | SET_REAL (f2max, my instancePref_window_f2max()) |
| 798 | EDITOR_DO |
| 799 | my setInstancePref_window_f1min (f1min); |
| 800 | my setInstancePref_window_f1max (f1max); |
| 801 | my setInstancePref_window_f2min (f2min); |
| 802 | my setInstancePref_window_f2max (f2max); |
| 803 | Graphics_updateWs (my graphics.get()); |
| 804 | EDITOR_END |
| 805 | } |
| 806 | |
| 807 | static void CREATE_ONE__publishSound (VowelEditor me, EDITOR_ARGS) { |
| 808 | CREATE_ONE |
| 809 | autoSound result = VowelEditor_createTargetSound (me); |
| 810 | CREATE_ONE_END (U"untitled") |
| 811 | } |
| 812 | |
| 813 | static void CREATE_ONE__Extract_FormantGrid (VowelEditor me, EDITOR_ARGS) { |
| 814 | CREATE_ONE |
| 815 | VowelEditor_updateTrajectorySpecification (me); |
| 816 | autoFormantGrid result = VowelEditor_to_FormantGrid (me); |
| 817 | CREATE_ONE_END (U"untitled") |
| 818 | } |
| 819 | |
| 820 | static void CREATE_ONE__Extract_KlattGrid (VowelEditor me, EDITOR_ARGS) { |
| 821 | CREATE_ONE |
| 822 | VowelEditor_updateTrajectorySpecification (me); |
| 823 | autoFormantGrid fg = VowelEditor_to_FormantGrid (me); |
| 824 | autoKlattGrid result = KlattGrid_create (fg -> xmin, fg -> xmax, fg -> formants.size, 0, 0, 0, 0, 0, 0); |
| 825 | KlattGrid_addVoicingAmplitudePoint (result.get(), fg -> xmin, 90.0); |
| 826 | autoPitchTier pitchTier = VowelEditor_to_PitchTier (me); |
| 827 | KlattGrid_replacePitchTier (result.get(), pitchTier.get()); |
| 828 | KlattGrid_replaceFormantGrid (result.get(), kKlattGridFormantType::ORAL, fg.get()); |
| 829 | CREATE_ONE_END (U"untitled") |
| 830 | } |
| 831 | |
| 832 | static void CREATE_ONE__Extract_PitchTier (VowelEditor me, EDITOR_ARGS) { |
| 833 | CREATE_ONE |
| 834 | VowelEditor_updateTrajectorySpecification (me); |
| 835 | autoPitchTier result = VowelEditor_to_PitchTier (me); |
| 836 | CREATE_ONE_END (U"untitled") |
| 837 | } |
| 838 | |
| 839 | static void CREATE_ONE__Extract_TrajectoryAsTable (VowelEditor me, EDITOR_ARGS) { |
| 840 | CREATE_ONE |
| 841 | VowelEditor_updateTrajectorySpecification (me); |
| 842 | const conststring32 columnNames [] = { U"Time", U"F1", U"F2", U"Colour" }; |
| 843 | autoTable result = Table_createWithColumnNames (my trajectory -> points.size, ARRAY_TO_STRVEC (columnNames)); |
| 844 | for (integer ipoint = 1; ipoint <= my trajectory -> points.size; ipoint ++) { |
| 845 | TrajectoryPoint point = my trajectory -> points.at [ipoint]; |
| 846 | Table_setNumericValue (result.get(), ipoint, 1, point -> number); |
| 847 | Table_setNumericValue (result.get(), ipoint, 2, point -> f1); |
| 848 | Table_setNumericValue (result.get(), ipoint, 3, point -> f2); |
| 849 | Table_setStringValue (result.get(), ipoint, 4, MelderColour_namePrettyOrNull (point -> colour)); |
| 850 | } |
| 851 | CREATE_ONE_END (U"untitled") |
| 852 | } |
| 853 | |
| 854 | static void menu_cb_drawTrajectory (VowelEditor me, EDITOR_ARGS) { |
| 855 | EDITOR_FORM (U"Draw trajectory", nullptr) |
| 856 | my v_form_pictureWindow (cmd); |
| 857 | BOOLEAN (garnish, U"Garnish", true) |
| 858 | EDITOR_OK |
| 859 | my v_ok_pictureWindow (cmd); |
| 860 | EDITOR_DO |
| 861 | my v_do_pictureWindow (cmd); |
| 862 | DataGui_openPraatPicture (me); |
| 863 | if (garnish) |
| 864 | VowelEditor_drawBackground (me, my pictureGraphics()); |
| 865 | VowelEditor_drawF1F2Trajectory (me, my pictureGraphics()); |
| 866 | DataGui_closePraatPicture (me); |
| 867 | EDITOR_END |
| 868 | } |
| 869 | |
| 870 | static void menu_cb_showOneVowelMark (VowelEditor me, EDITOR_ARGS) { |
| 871 | EDITOR_FORM (U"Show one vowel mark", nullptr); |
| 872 | WORD (mark, U"Mark", U"u") |
| 873 | POSITIVE (f1, U"F1 (Hz)", U"300.0") |
| 874 | POSITIVE (f2, U"F2 (Hz)", U"600.0") |
| 875 | POSITIVE (fontSize, U"Font size", U"14.0") |
| 876 | COLOUR (colour, U"Colour", U"Green") |
| 877 | EDITOR_OK |
| 878 | EDITOR_DO |
| 879 | Melder_require (f1 >= my instancePref_window_f1min() && f1 <= my instancePref_window_f1max(), |
| 880 | U"The first formant should be in the range from ", my instancePref_window_f1min(), U" to ", my instancePref_window_f1max(), U" Hz."); |
| 881 | Melder_require (f2 >= my instancePref_window_f2min() && f2 <= my instancePref_window_f1max(), |
| 882 | U"The second formant should be in the range from ", my instancePref_window_f2min(), U" to ", my instancePref_window_f2max(), U" Hz."); |
| 883 | if (! my marks) { |
| 884 | const conststring32 columnNames [] = { U"IPA", U"F1", U"F2", U"Size", U"Colour" }; |
| 885 | my marks = Table_createWithColumnNames (0, ARRAY_TO_STRVEC (columnNames)); |
| 886 | } |
| 887 | Table_appendRow (my marks.get()); |
| 888 | const integer irow = my marks -> rows.size; |
| 889 | Table_setStringValue (my marks.get(), irow, 1, mark); |
| 890 | Table_setNumericValue (my marks.get(), irow, 2, f1); |
| 891 | Table_setNumericValue (my marks.get(), irow, 3, f2); |
| 892 | Table_setNumericValue (my marks.get(), irow, 4, fontSize); |
| 893 | Table_setStringValue (my marks.get(), irow, 5, MelderColour_name (colour)); |
| 894 | Graphics_updateWs (my graphics.get()); |
| 895 | EDITOR_END |
| 896 | } |
| 897 | |
| 898 | static void menu_cb_vowelMarks (VowelEditor me, EDITOR_ARGS) { |
| 899 | EDITOR_FORM (U"Show vowel marks", nullptr); |
| 900 | OPTIONMENU_ENUM (kVowelEditor_marksDataSet, dataSet, U"Data set", my default_marks_dataSet ()) |
| 901 | OPTIONMENU_ENUM (kVowelEditor_speakerType, speaker, U"Speaker", my default_marks_speakerType ()) |
| 902 | POSITIVE (fontSize, U"Font size (points)", my default_marks_fontSize ()) |
| 903 | WORD (colour_string, U"Colour", my default_marks_colour ()); |
| 904 | EDITOR_OK |
| 905 | SET_ENUM (dataSet, kVowelEditor_marksDataSet, my instancePref_marks_dataSet()) |
| 906 | SET_ENUM (speaker, kVowelEditor_speakerType, my instancePref_marks_speakerType()) |
| 907 | SET_REAL (fontSize, my instancePref_marks_fontSize()) |
| 908 | SET_STRING (colour_string, my instancePref_trajectory_colour()) |
| 909 | EDITOR_DO |
| 910 | my setInstancePref_marks_dataSet (dataSet); |
| 911 | my setInstancePref_marks_speakerType (speaker); |
| 912 | my setInstancePref_marks_fontSize (fontSize); |
| 913 | my setInstancePref_marks_colour (colour_string); |
| 914 | VowelEditor_getMarks (me); |
| 915 | Graphics_updateWs (my graphics.get()); |
| 916 | EDITOR_END |
| 917 | } |
| 918 | |
| 919 | static void menu_cb_vowelMarksFromTableFile (VowelEditor me, EDITOR_ARGS) { |
| 920 | EDITOR_FORM_READ (U"VowelEditor: Show vowel marks from Table file", U"VowelEditor: Show vowel marks from Table file..."); |
| 921 | EDITOR_DO_READ |
| 922 | Melder_require (isValidVowelMarksTableFile (file, nullptr), |
| 923 | U"File ", file, U" does not contain valid Table data."); |
| 924 | my setInstancePref_marks_fileName (MelderFile_peekPath (file)); |
| 925 | my setInstancePref_marks_speakerType (kVowelEditor_speakerType::UNKNOWN); |
| 926 | my setInstancePref_marks_dataSet (kVowelEditor_marksDataSet::OTHER); |
| 927 | VowelEditor_getVowelMarksFromFile (me); |
| 928 | Graphics_updateWs (my graphics.get()); |
| 929 | EDITOR_END |
| 930 | } |
| 931 | |
| 932 | static void menu_cb_setF0 (VowelEditor me, EDITOR_ARGS) { |
| 933 | EDITOR_FORM (U"Set f0", nullptr); |
| 934 | POSITIVE (f0Start, U"Start f0 (Hz)", my default_f0_start()) |
| 935 | REAL (f0Slope, U"Slope (oct/s)", my default_f0_slope()) |
| 936 | EDITOR_OK |
| 937 | SET_REAL (f0Start, my instancePref_f0_start()) |
| 938 | SET_REAL (f0Slope, my instancePref_f0_slope()) |
| 939 | EDITOR_DO |
| 940 | Melder_clip (my instancePref_f0_minimum(), & f0Start, my instancePref_f0_maximum()); |
| 941 | my setInstancePref_f0_start (f0Start); |
| 942 | my setInstancePref_f0_slope (f0Slope); |
| 943 | GuiText_setString (my f0TextField, Melder_double (f0Start)); |
| 944 | GuiText_setString (my f0SlopeTextField, Melder_double (f0Slope)); |
| 945 | EDITOR_END |
| 946 | } |
| 947 | |
| 948 | static void menu_cb_setF3F4 (VowelEditor me, EDITOR_ARGS) { // deprecated 20200404 |
| 949 | EDITOR_FORM (U"Set F3 & F4", nullptr); |
| 950 | POSITIVE (f3, U"F3 (Hz)", U"2500.0") |
| 951 | POSITIVE (b3, U"B3 (Hz)", U"250.0") |
| 952 | POSITIVE (f4, U"F4 (Hz)", U"3500.0") |
| 953 | POSITIVE (b4, U"B4 (Hz)", U"350.0") |
| 954 | EDITOR_OK |
| 955 | EDITOR_DO |
| 956 | Melder_require (f3 < f4, |
| 957 | U"F4 should be larger than F3."); |
| 958 | my setInstancePref_synthesis_numberOfFormants (4); |
| 959 | my extraFrequencyBandwidthPairs [1] = f3; |
| 960 | my extraFrequencyBandwidthPairs [2] = b3; |
| 961 | my extraFrequencyBandwidthPairs [3] = f4; |
| 962 | my extraFrequencyBandwidthPairs [4] = b4; |
| 963 | EDITOR_END |
| 964 | } |
| 965 | |
| 966 | static void menu_cb_reverseTrajectory (VowelEditor me, EDITOR_ARGS) { |
| 967 | Trajectory_reverse (my trajectory.get()); |
| 968 | updateInfoLabels (me); |
| 969 | Graphics_updateWs (my graphics.get()); |
| 970 | } |
| 971 | |
| 972 | static void menu_cb_newTrajectory (VowelEditor me, EDITOR_ARGS) { |
| 973 | EDITOR_FORM (U"New Trajectory", nullptr); |
| 974 | POSITIVE (startF1, U"Start F1 (Hz)", U"700.0") |
| 975 | POSITIVE (startF2, U"Start F2 (Hz)", U"1200.0") |
| 976 | POSITIVE (endF1, U"End F1 (Hz)", U"350.0") |
| 977 | POSITIVE (endF2, U"End F2 (Hz)", U"800.0") |
| 978 | POSITIVE (newDuration, U"Duration (s)", my default_trajectory_newDuration()) |
| 979 | WORD (colour_string, U"Colour", my default_trajectory_colour()); |
| 980 | EDITOR_OK |
| 981 | SET_REAL (newDuration, my instancePref_trajectory_newDuration()) |
| 982 | SET_STRING (colour_string, my instancePref_trajectory_colour()) // TODO SET_COLOUR |
| 983 | EDITOR_DO |
| 984 | clipF1F2 (me, & startF1, & startF2); |
| 985 | MelderColour colour = MelderColour_fromColourNameOrRGBString (colour_string); |
| 986 | my trajectory = Trajectory_create (newDuration); |
| 987 | Trajectory_addPoint (my trajectory.get(), 0.0, startF1, startF2, colour); |
| 988 | clipF1F2 (me, & endF1, & endF2); |
| 989 | Trajectory_addPoint (my trajectory.get(), newDuration, endF1, endF2, colour); |
| 990 | GuiText_setString (my durationTextField, Melder_fixed (newDuration, 6)); |
| 991 | my setInstancePref_trajectory_newDuration (newDuration); |
| 992 | my setInstancePref_trajectory_colour (colour_string); |
| 993 | updateInfoLabels (me); |
| 994 | Graphics_updateWs (my graphics.get()); |
| 995 | EDITOR_END |
| 996 | } |
| 997 | |
| 998 | static void menu_cb_extendTrajectory (VowelEditor me, EDITOR_ARGS) { |
| 999 | EDITOR_FORM (U"Extend Trajectory", nullptr); |
| 1000 | POSITIVE (toF1, U"To F1 (Hz)", U"500.0") |
| 1001 | POSITIVE (toF2, U"To F2 (Hz)", U"1500.0") |
| 1002 | POSITIVE (extendDuration, U"Extend duration (s)", my default_trajectory_extendDuration()) |
| 1003 | WORD (colour_string, U"Colour", my default_trajectory_colour()); |
| 1004 | EDITOR_OK |
| 1005 | SET_REAL (extendDuration, my instancePref_trajectory_extendDuration()) |
| 1006 | SET_STRING (colour_string, my instancePref_trajectory_colour()) // TODO SET_COLOUR |
| 1007 | EDITOR_DO |
| 1008 | MelderColour colour = MelderColour_fromColourNameOrRGBString (colour_string); |
| 1009 | TrajectoryPoint startPoint = my trajectory -> points.at [my trajectory -> points.size]; |
| 1010 | startPoint -> colour = colour; |
| 1011 | const double startTime = startPoint -> number; |
| 1012 | const double endTime = startTime + extendDuration; |
| 1013 | clipF1F2 (me, & toF1, & toF2); |
| 1014 | Trajectory_addPoint (my trajectory.get(), endTime, toF1, toF2, colour); |
| 1015 | GuiText_setString (my durationTextField, Melder_fixed (endTime, 6)); |
| 1016 | GuiText_setString (my extendTextField, Melder_fixed (extendDuration, 6)); |
| 1017 | my setInstancePref_trajectory_extendDuration (extendDuration); |
| 1018 | my setInstancePref_trajectory_duration (endTime); // TODO: justify why this is a preference |
| 1019 | my setInstancePref_trajectory_colour (colour_string); |
| 1020 | updateInfoLabels (me); |
| 1021 | Graphics_updateWs (my graphics.get()); |
| 1022 | EDITOR_END |
| 1023 | } |
| 1024 | |
| 1025 | static void menu_cb_modifyTrajectoryDuration (VowelEditor me, EDITOR_ARGS) { |
| 1026 | EDITOR_FORM (U"Modify duration", nullptr); |
| 1027 | POSITIVE (newDuration, U"New duration (s)", my default_trajectory_duration()) |
| 1028 | EDITOR_OK |
| 1029 | SET_REAL (newDuration, my instancePref_trajectory_duration()) |
| 1030 | EDITOR_DO |
| 1031 | Melder_require (newDuration > my instancePref_trajectory_minimumDuration(), |
| 1032 | U"The duration should be larger than ", my instancePref_trajectory_minimumDuration(), U" s."); |
| 1033 | my setInstancePref_trajectory_duration (newDuration); |
| 1034 | Trajectory_newDuration (my trajectory.get(), newDuration); |
| 1035 | GuiText_setString (my durationTextField, Melder_fixed (newDuration, 6)); |
| 1036 | EDITOR_END |
| 1037 | } |
| 1038 | |
| 1039 | static void menu_cb_shiftTrajectory (VowelEditor me, EDITOR_ARGS) { |
| 1040 | EDITOR_FORM (U"Shift trajectory", nullptr); |
| 1041 | REAL (f1_st, U"F1 (semitones)", U"0.5") |
| 1042 | REAL (f2_st, U"F2 (semitones)", U"0.5") |
| 1043 | EDITOR_OK |
| 1044 | EDITOR_DO |
| 1045 | Trajectory_shift_semitones (my trajectory.get(), f1_st, f2_st); |
| 1046 | updateInfoLabels (me); |
| 1047 | Graphics_updateWs (my graphics.get()); |
| 1048 | EDITOR_END |
| 1049 | } |
| 1050 | |
| 1051 | static void menu_cb_trajectoryTimeMarksEvery (VowelEditor me, EDITOR_ARGS) { // deprecated 20200404 |
| 1052 | EDITOR_FORM (U"Trajectory time marks every", nullptr); |
| 1053 | POSITIVE (distance, U"Distance (s)", my default_trajectory_markEvery()) |
| 1054 | EDITOR_OK |
| 1055 | SET_REAL (distance, my instancePref_trajectory_markEvery()) |
| 1056 | EDITOR_DO |
| 1057 | my setInstancePref_trajectory_markEvery (distance); |
| 1058 | Graphics_updateWs (my graphics.get()); |
| 1059 | EDITOR_END |
| 1060 | } |
| 1061 | |
| 1062 | static void menu_cb_trajectory_colour (VowelEditor me, EDITOR_ARGS) { |
| 1063 | EDITOR_FORM (U"Trajectory colour settings", nullptr); |
| 1064 | REAL (startTime, U"left Time range (s)", U"0.0") |
| 1065 | REAL (endTime, U"right Time range (s)", my default_trajectory_duration()) |
| 1066 | WORD (colour_string, U"Colour", my default_trajectory_colour()) |
| 1067 | EDITOR_OK |
| 1068 | SET_REAL (endTime, my instancePref_trajectory_duration()) |
| 1069 | SET_STRING (colour_string, my instancePref_trajectory_colour()) // TODO SET_COLOUR |
| 1070 | EDITOR_DO |
| 1071 | my setInstancePref_trajectory_colour (colour_string); |
| 1072 | Trajectory_setColour (my trajectory.get(), startTime, endTime, MelderColour_fromColourNameOrRGBString(colour_string)); |
| 1073 | Graphics_updateWs (my graphics.get()); |
| 1074 | EDITOR_END |
| 1075 | |
| 1076 | } |
| 1077 | |
| 1078 | #pragma mark - button methods |
| 1079 | |
| 1080 | static void playTrajectory (VowelEditor me) { |
| 1081 | autoSound thee = VowelEditor_createTargetSound (me); |
| 1082 | Sound_play (thee.get(), nullptr, nullptr); |
| 1083 | } |
| 1084 | |
| 1085 | static void gui_button_cb_play (VowelEditor me, GuiButtonEvent /* event */) { |
| 1086 | playTrajectory (me); |
| 1087 | } |
| 1088 | |
| 1089 | static void gui_button_cb_publish (VowelEditor me, GuiButtonEvent /* event */) { |
| 1090 | autoSound publish = VowelEditor_createTargetSound (me); |
| 1091 | Editor_broadcastPublication (me, publish.move()); |
| 1092 | } |
| 1093 | |
| 1094 | static void gui_button_cb_reverse (VowelEditor me, GuiButtonEvent /* event */) { |
| 1095 | Trajectory_reverse (my trajectory.get()); |
| 1096 | playTrajectory (me); |
| 1097 | updateInfoLabels (me); |
| 1098 | Graphics_updateWs (my graphics.get()); |
| 1099 | } |
| 1100 | |
| 1101 | static void gui_drawingarea_cb_expose (VowelEditor me, GuiDrawingArea_ExposeEvent /* event */) { |
| 1102 | Melder_assert (me); |
| 1103 | Melder_assert (my trajectory); |
| 1104 | if (! my graphics) |
| 1105 | return; // could be the case in the very beginning |
| 1106 | |
| 1107 | Graphics_setGrey (my graphics.get(), 0.9); |
| 1108 | Graphics_fillRectangle (my graphics.get(), 0.0, 1.0, 0.0, 1.0); |
| 1109 | Graphics_setInner (my graphics.get()); |
| 1110 | Graphics_setWindow (my graphics.get(), 0.0, 1.0, 0.0, 1.0); |
| 1111 | Graphics_setGrey (my graphics.get(), 1.0); |
| 1112 | Graphics_fillRectangle (my graphics.get(), 0.0, 1.0, 0.0, 1.0); |
| 1113 | Graphics_unsetInner (my graphics.get()); |
| 1114 | Graphics_setGrey (my graphics.get(), 0.0); |
| 1115 | |
| 1116 | VowelEditor_drawBackground (me, my graphics.get()); |
| 1117 | Melder_assert (me); |
| 1118 | Melder_assert (my trajectory); |
| 1119 | VowelEditor_drawF1F2Trajectory (me, my graphics.get()); |
| 1120 | } |
| 1121 | |
| 1122 | static void gui_drawingarea_cb_resize (VowelEditor me, GuiDrawingArea_ResizeEvent event) { |
| 1123 | Melder_assert (me); |
| 1124 | if (! my graphics) |
| 1125 | return; // could be the case in the very beginning |
| 1126 | my width = event -> width; |
| 1127 | my height = event -> height; |
| 1128 | Graphics_setWsViewport (my graphics.get(), 0.0, my width, 0.0, my height); |
| 1129 | Graphics_setWsWindow (my graphics.get(), 0.0, my width, 0.0, my height); |
| 1130 | Graphics_setViewport (my graphics.get(), 0.0, my width, 0.0, my height); |
| 1131 | Graphics_updateWs (my graphics.get()); |
| 1132 | /* |
| 1133 | Save the current shell size as the user's preference. |
| 1134 | */ |
| 1135 | my setClassPref_shellWidth (GuiShell_getShellWidth (my windowForm)); |
| 1136 | my setClassPref_shellHeight (GuiShell_getShellHeight (my windowForm)); |
| 1137 | } |
| 1138 | |
| 1139 | // shift key always extends what already is. |
| 1140 | // Special case : !soundFollowsMouse. The first click just defines the vowel's first f1f2-position, |
| 1141 | static void gui_drawingarea_cb_mouse (VowelEditor me, GuiDrawingArea_MouseEvent event) { |
| 1142 | Graphics_setInner (my graphics.get()); |
| 1143 | double mouseX, mouseY; |
| 1144 | Graphics_DCtoWC (my graphics.get(), event -> x, event -> y, & mouseX, & mouseY); |
| 1145 | Melder_clip (0.0, & mouseX, 1.0); |
| 1146 | Melder_clip (0.0, & mouseY, 1.0); |
| 1147 | double f1, f2; |
| 1148 | VowelEditor_getF1F2FromXY (me, mouseX, mouseY, & f1, & f2); |
| 1149 | MelderColour colour = MelderColour_fromColourNameOrRGBString (my instancePref_trajectory_colour()); |
| 1150 | if (event -> isClick()) { |
| 1151 | my anchorTime = Melder_clock (); |
| 1152 | if (event -> shiftKeyPressed) { |
| 1153 | VowelEditor_updateFromExtendDurationTextWidget (me); |
| 1154 | const double duration = my dt = my trajectory -> xmax + my instancePref_trajectory_extendDuration(); |
| 1155 | Trajectory_addPoint (my trajectory.get(), duration, f1, f2, colour); |
| 1156 | GuiText_setString (my durationTextField, Melder_double (duration)); |
| 1157 | } else { |
| 1158 | const double duration = my dt = 0.0; |
| 1159 | my trajectory = Trajectory_create (my instancePref_trajectory_minimumDuration()); |
| 1160 | Trajectory_addPoint (my trajectory.get(), duration, f1, f2, colour); |
| 1161 | GuiText_setString (my durationTextField, Melder_double (duration)); |
| 1162 | if (! my instancePref_soundFollowsMouse()) |
| 1163 | Trajectory_addPoint (my trajectory.get(), my instancePref_trajectory_minimumDuration(), f1, f2, colour); |
| 1164 | } |
| 1165 | my previousX = mouseX; |
| 1166 | my previousY = mouseY; |
| 1167 | } else { |
| 1168 | double duration = Melder_clock () - my anchorTime + my dt; |
| 1169 | if (mouseX != my previousX || mouseY != my previousY) { |
| 1170 | Trajectory_addPoint (my trajectory.get(), duration, f1, f2, colour); |
| 1171 | GuiText_setString (my durationTextField, Melder_fixed (duration, 6)); |
| 1172 | my previousX = mouseX; |
| 1173 | my previousY = mouseY; |
| 1174 | } |
| 1175 | if (event -> isDrop()) { |
| 1176 | if (my trajectory -> points.size == 1) { |
| 1177 | /* |
| 1178 | Add a point with a slightly modified second formant because successive points should not have equal f1 and f2 values. |
| 1179 | */ |
| 1180 | Melder_clipLeft (my instancePref_trajectory_minimumDuration(), & duration); |
| 1181 | GuiText_setString (my durationTextField, Melder_fixed (duration, 6)); |
| 1182 | Trajectory_addPoint (my trajectory.get(), duration, f1, 1.00001 * f2, colour); // points have to be different |
| 1183 | } |
| 1184 | autoSound sound = VowelEditor_createTargetSound (me); |
| 1185 | Sound_play (sound.get(), nullptr, nullptr); |
| 1186 | } |
| 1187 | } |
| 1188 | Graphics_unsetInner (my graphics.get()); |
| 1189 | updateInfoLabels (me); |
| 1190 | Graphics_updateWs (my graphics.get()); |
| 1191 | } |
| 1192 | |
| 1193 | static void updateWidgets (void *void_me) { |
| 1194 | iam (VowelEditor); |
| 1195 | (void) me; |
| 1196 | } |
| 1197 | |
| 1198 | void structVowelEditor :: v_createMenus () { |
| 1199 | VowelEditor_Parent :: v_createMenus (); |
| 1200 | |
| 1201 | Editor_addCommand (this, U"File", U"Settings...", 0, menu_cb_settings); |
| 1202 | Editor_addCommand (this, U"File", U"-- publish data --", 0, nullptr); |
| 1203 | Editor_addCommand (this, U"File", U"Publish Sound", 0, |
| 1204 | CREATE_ONE__publishSound); |
| 1205 | Editor_addCommand (this, U"File", U"Extract KlattGrid", 0, |
| 1206 | CREATE_ONE__Extract_KlattGrid); |
| 1207 | Editor_addCommand (this, U"File", U"Extract FormantGrid", 0, |
| 1208 | CREATE_ONE__Extract_FormantGrid); |
| 1209 | Editor_addCommand (this, U"File", U"Extract PitchTier", 0, |
| 1210 | CREATE_ONE__Extract_PitchTier); |
| 1211 | Editor_addCommand (this, U"File", U"Extract Trajectory as Table", 0, |
| 1212 | CREATE_ONE__Extract_TrajectoryAsTable); |
| 1213 | Editor_addCommand (this, U"File", U"-- drawing --", 0, nullptr); |
| 1214 | Editor_addCommand (this, U"File", U"Draw trajectory...", 0, menu_cb_drawTrajectory); |
| 1215 | Editor_addCommand (this, U"File", U"-- scripting --", 0, nullptr); |
| 1216 | |
| 1217 | Editor_addCommand (this, U"Edit", U"-- f0 --", 0, nullptr); |
| 1218 | Editor_addCommand (this, U"Edit", U"Set f0...", 0, menu_cb_setF0); |
| 1219 | Editor_addCommand (this, U"Edit", U"Set F3 & F4...", GuiMenu_HIDDEN, menu_cb_setF3F4); |
| 1220 | Editor_addCommand (this, U"Edit", U"-- trajectory commands --", 0, nullptr); |
| 1221 | Editor_addCommand (this, U"Edit", U"Reverse trajectory", 0, menu_cb_reverseTrajectory); |
| 1222 | Editor_addCommand (this, U"Edit", U"Modify trajectory duration...", 0, menu_cb_modifyTrajectoryDuration); |
| 1223 | Editor_addCommand (this, U"Edit", U"New trajectory...", 0, menu_cb_newTrajectory); |
| 1224 | Editor_addCommand (this, U"Edit", U"Extend trajectory...", 0, menu_cb_extendTrajectory); |
| 1225 | Editor_addCommand (this, U"Edit", U"Shift trajectory...", 0, menu_cb_shiftTrajectory); |
| 1226 | |
| 1227 | Editor_addMenu (this, U"Query", 0); |
| 1228 | Editor_addCommand (this, U"Query", U"Trajectory info", 0, menu_cb_trajectoryInfo); |
| 1229 | |
| 1230 | Editor_addMenu (this, U"View", 0); |
| 1231 | Editor_addCommand (this, U"View", U"F1 & F2 range...", 0, menu_cb_ranges_f1f2); |
| 1232 | Editor_addCommand (this, U"View", U"--show vowel marks--", 0, nullptr); |
| 1233 | Editor_addCommand (this, U"View", U"Show one vowel mark...", GuiMenu_HIDDEN, menu_cb_showOneVowelMark); |
| 1234 | Editor_addCommand (this, U"View", U"Vowel marks...", GuiMenu_HIDDEN, menu_cb_vowelMarks); |
| 1235 | Editor_addCommand (this, U"View", U"Vowel marks from fixed set...", 0, menu_cb_vowelMarks); |
| 1236 | Editor_addCommand (this, U"View", U"Show vowel marks from Table file...", 0, menu_cb_vowelMarksFromTableFile); |
| 1237 | Editor_addCommand (this, U"View", U"--show trajectory settings--", 0, nullptr); |
| 1238 | Editor_addCommand (this, U"View", U"Trajectory colour...", 0, menu_cb_trajectory_colour); |
| 1239 | Editor_addCommand (this, U"View", U"Trajectory time marks every...", 0, menu_cb_trajectoryTimeMarksEvery); |
| 1240 | } |
| 1241 | |
| 1242 | void structVowelEditor :: v_createMenuItems_help (EditorMenu menu) { |
| 1243 | VowelEditor_Parent :: v_createMenuItems_help (menu); |
| 1244 | EditorMenu_addCommand (menu, U"VowelEditor help", '?', menu_cb_help); |
| 1245 | } |
| 1246 | |
| 1247 | void structVowelEditor :: v_createChildren () |
| 1248 | { |
| 1249 | const int button_width = 90, text_width = 110, status_info_width = 400; |
| 1250 | int top, bottom, bottom_widgets_top, bottom_widgets_bottom, bottom_widgets_halfway; |
| 1251 | /* |
| 1252 | Three buttons on a row: Play, Reverse, Publish |
| 1253 | */ |
| 1254 | int left = 10, right = left + button_width; |
| 1255 | bottom_widgets_top = top = -MARGIN_BOTTOM + 10; |
| 1256 | bottom_widgets_bottom = bottom = -STATUS_INFO - Gui_LABEL_HEIGHT; |
| 1257 | playButton = GuiButton_createShown (our windowForm, left, right, top, bottom, U"Play", gui_button_cb_play, this, 0); |
| 1258 | left = right + 10; |
| 1259 | right = left + button_width; |
| 1260 | reverseButton = GuiButton_createShown (our windowForm, left, right, top, bottom, U"Reverse", gui_button_cb_reverse, this, 0); |
| 1261 | left = right + 10; |
| 1262 | right = left + button_width; |
| 1263 | publishButton = GuiButton_createShown (our windowForm, left, right, top, bottom, U"Publish", gui_button_cb_publish, this, 0); |
| 1264 | /* |
| 1265 | Four Text widgets with the label on top: Duration, Extend, f0, Slope. |
| 1266 | Make the f0 slope button 10 wider to accommodate the text |
| 1267 | We wil not use a callback from a Text widget. It will get called multiple times during the editing |
| 1268 | of the text. Better to have all editing done and then query the widget for its value! |
| 1269 | */ |
| 1270 | left = right + 10; |
| 1271 | right = left + text_width; |
| 1272 | bottom_widgets_halfway = bottom = (top + bottom) / 2; |
| 1273 | top = bottom_widgets_top; |
| 1274 | GuiLabel_createShown (our windowForm, left, right, top , bottom, U"Duration (s):", 0); |
| 1275 | top = bottom; |
| 1276 | bottom = bottom_widgets_bottom; |
| 1277 | durationTextField = GuiText_createShown (our windowForm, left, right, top, bottom, 0); |
| 1278 | |
| 1279 | left = right + 10; |
| 1280 | right = left + text_width; |
| 1281 | top = bottom_widgets_top; |
| 1282 | bottom = bottom_widgets_halfway; |
| 1283 | GuiLabel_createShown (our windowForm, left, right, top, bottom, U"Extend (s):", 0); |
| 1284 | top = bottom; |
| 1285 | bottom = bottom_widgets_bottom; |
| 1286 | extendTextField = GuiText_createShown (our windowForm, left, right, top, bottom, 0); |
| 1287 | |
| 1288 | left = right + 10; |
| 1289 | right = left + text_width; |
| 1290 | top = bottom_widgets_top; |
| 1291 | bottom = bottom_widgets_halfway; |
| 1292 | GuiLabel_createShown (our windowForm, left, right, top, bottom, U"Start f0 (Hz):", 0); |
| 1293 | top = bottom; |
| 1294 | bottom = bottom_widgets_bottom; |
| 1295 | f0TextField = GuiText_createShown (our windowForm, left, right, top, bottom, 0); |
| 1296 | |
| 1297 | left = right + 10; |
| 1298 | right = left + text_width + 10; |
| 1299 | top = bottom_widgets_top; |
| 1300 | bottom = bottom_widgets_halfway; |
| 1301 | GuiLabel_createShown (our windowForm, left, right, top, bottom, U"Slope f0 (oct/s):", 0); |
| 1302 | top = bottom; |
| 1303 | bottom = bottom_widgets_bottom; |
| 1304 | f0SlopeTextField = GuiText_createShown (our windowForm, left, right, top, bottom, 0); |
| 1305 | /* |
| 1306 | The status startInfo and endInfo widget at the bottom: |
| 1307 | */ |
| 1308 | bottom = - (STATUS_INFO - Gui_LABEL_HEIGHT) / 2; |
| 1309 | top = bottom - Gui_LABEL_HEIGHT; |
| 1310 | left = 10; //MARGIN_LEFT; |
| 1311 | right = left + status_info_width; |
| 1312 | startInfo = GuiLabel_createShown (our windowForm, left, right, top, bottom, U"", 0); |
| 1313 | |
| 1314 | left = right; |
| 1315 | right = left + status_info_width; |
| 1316 | endInfo = GuiLabel_createShown (our windowForm, left, right, top, bottom, U"", 0); |
| 1317 | /* |
| 1318 | Create drawing area. |
| 1319 | Approximately square because for our defaults: f1min=200, f1max=1000 and f2min = 500, f2mx = 2500, |
| 1320 | log distances are equal (log (1000/200) == log (2500/500) ). |
| 1321 | */ |
| 1322 | drawingArea = GuiDrawingArea_createShown (our windowForm, 0, 0, Machine_getMenuBarBottom (), -MARGIN_BOTTOM, |
| 1323 | gui_drawingarea_cb_expose, gui_drawingarea_cb_mouse, // TODO: mouse-dragged and mouse-up events |
| 1324 | nullptr, gui_drawingarea_cb_resize, nullptr, this, 0 |
| 1325 | ); |
| 1326 | our width = GuiControl_getWidth (drawingArea); |
| 1327 | our height = GuiControl_getHeight (drawingArea); |
| 1328 | } |
| 1329 | |
| 1330 | void structVowelEditor :: v9_repairPreferences () { |
| 1331 | if (! (our instancePref_window_f1min() < our instancePref_window_f1max())) { // NaN-safe test |
| 1332 | our setInstancePref_window_f1min (Melder_atof (our default_window_f1min())); |
| 1333 | our setInstancePref_window_f1max (Melder_atof (our default_window_f1max())); |
| 1334 | } |
| 1335 | if (! (our instancePref_window_f2min() < our instancePref_window_f2max())) { // NaN-safe test |
| 1336 | our setInstancePref_window_f2min (Melder_atof (our default_window_f2min())); |
| 1337 | our setInstancePref_window_f2max (Melder_atof (our default_window_f2max())); |
| 1338 | } |
| 1339 | if (! (our instancePref_marks_fontSize() > 0.0)) // NaN-safe test |
| 1340 | our setInstancePref_marks_fontSize (Melder_atof (our default_marks_fontSize())); |
| 1341 | if (our instancePref_marks_dataSet() == kVowelEditor_marksDataSet::OTHER) { |
| 1342 | if (Melder_equ (our instancePref_marks_fileName(), U"")) { |
| 1343 | Melder_warning (U"Although your prederences indicate that you want to read marks from a file, no file was " |
| 1344 | "specified. Default marks will be used instead."); |
| 1345 | our setInstancePref_marks_dataSet (our default_marks_dataSet()); |
| 1346 | our setInstancePref_marks_speakerType (our default_marks_speakerType()); |
| 1347 | } else { |
| 1348 | structMelderFile file { }; |
| 1349 | Melder_pathToFile (our instancePref_marks_fileName(), & file); |
| 1350 | if (! isValidVowelMarksTableFile (& file, nullptr)) { |
| 1351 | Melder_warning (U"The file '", our instancePref_marks_fileName(), U"' which was specified in your preferences " |
| 1352 | "is not a valid Table file. Default marks will be used instead."); |
| 1353 | our setInstancePref_marks_dataSet (our default_marks_dataSet()); |
| 1354 | our setInstancePref_marks_speakerType (our default_marks_speakerType()); |
| 1355 | our setInstancePref_marks_fileName (U""); |
| 1356 | } |
| 1357 | } |
| 1358 | } |
| 1359 | if (! (our instancePref_synthesis_samplingFrequency() > 0.0)) // NaN-safe test |
| 1360 | our setInstancePref_synthesis_samplingFrequency (Melder_atof (our default_synthesis_samplingFrequency())); |
| 1361 | if (! (our instancePref_trajectory_minimumDuration() > 0.0)) // NaN-safe test |
| 1362 | our setInstancePref_trajectory_minimumDuration (Melder_atof (our default_trajectory_minimumDuration())); |
| 1363 | if (! (our instancePref_trajectory_extendDuration() > 0.0 && our instancePref_trajectory_markEvery() > 0.0)) { // NaN-safe test |
| 1364 | our setInstancePref_trajectory_extendDuration (Melder_atof (our default_trajectory_extendDuration())); |
| 1365 | our setInstancePref_trajectory_markEvery (Melder_atof (our default_trajectory_markEvery())); |
| 1366 | } |
| 1367 | if (! (our instancePref_f0_start() > 0.0)) // NaN-safe test |
| 1368 | our setInstancePref_f0_start (Melder_atof (our default_f0_start())); |
| 1369 | if (! (our instancePref_f0_slope() > 0.0)) // NaN-safe test |
| 1370 | our setInstancePref_f0_slope (Melder_atof (our default_f0_slope())); |
| 1371 | if (! (our instancePref_f0_minimum() > 0.0 && our instancePref_f0_maximum() > 0.0)) { // NaN-safe test |
| 1372 | our setInstancePref_f0_minimum (Melder_atof (our default_f0_minimum())); |
| 1373 | our setInstancePref_f0_maximum (Melder_atof (our default_f0_maximum())); |
| 1374 | } |
| 1375 | if (! (our instancePref_grid_df1() > 0.0)) // NaN-safe test |
| 1376 | our setInstancePref_grid_df1 (Melder_atof (our default_grid_df1())); |
| 1377 | if (! (our instancePref_grid_df2() > 0.0)) // NaN-safe test |
| 1378 | our setInstancePref_grid_df2 (Melder_atof (our default_grid_df2())); |
| 1379 | VowelEditor_Parent :: v9_repairPreferences (); |
| 1380 | } |
| 1381 | |
| 1382 | autoVowelEditor VowelEditor_create (conststring32 title) { |
| 1383 | try { |
| 1384 | trace (U"enter"); |
| 1385 | autoVowelEditor me = Thing_new (VowelEditor); |
| 1386 | Melder_assert (me.get()); |
| 1387 | Editor_init (me.get(), 0, 0, 0, 0, title, nullptr); |
| 1388 | #if motif |
| 1389 | Melder_assert (XtWindow (my drawingArea -> d_widget)); |
| 1390 | #endif |
| 1391 | my graphics = Graphics_create_xmdrawingarea (my drawingArea); |
| 1392 | Melder_assert (my graphics); |
| 1393 | Graphics_setFontSize (my graphics.get(), 12.0); |
| 1394 | |
| 1395 | VowelEditor_getMarks (me.get()); |
| 1396 | if (my instancePref_synthesis_numberOfFormants() <= 0) |
| 1397 | my setInstancePref_synthesis_numberOfFormants (Melder_atoi (my default_synthesis_numberOfFormants ())); |
| 1398 | if (my instancePref_synthesis_q1() <= 0 || my instancePref_synthesis_q2() <= 0) { |
| 1399 | my setInstancePref_synthesis_q1 (Melder_atof (my default_synthesis_q1())); |
| 1400 | my setInstancePref_synthesis_q2 (Melder_atof (my default_synthesis_q2())); |
| 1401 | } |
| 1402 | my extraFrequencyBandwidthPairs = splitByWhitespace_VEC (my instancePref_synthesis_extraFBPairs()); |
| 1403 | if (my extraFrequencyBandwidthPairs.size < 2) { |
| 1404 | my setInstancePref_synthesis_extraFBPairs (my default_synthesis_extraFBPairs ()); |
| 1405 | my extraFrequencyBandwidthPairs = splitByWhitespace_VEC (my instancePref_synthesis_extraFBPairs()); |
| 1406 | } else if (my extraFrequencyBandwidthPairs.size < 4) { |
| 1407 | const double f3 = my extraFrequencyBandwidthPairs [1]; |
| 1408 | const double b3 = my extraFrequencyBandwidthPairs [2]; |
| 1409 | const double f4 = f3 + 1000.0; |
| 1410 | const double b4 = b3 + 100.0; |
| 1411 | my setInstancePref_synthesis_extraFBPairs (Melder_cat (f3, U" ", b3, U" ", f4, U" ", b4)); |
| 1412 | my extraFrequencyBandwidthPairs = splitByWhitespace_VEC (my instancePref_synthesis_extraFBPairs()); |
| 1413 | } |
| 1414 | Melder_assert (my extraFrequencyBandwidthPairs.size >= 4); // for deprecated Set F3 & F4 |
| 1415 | //my p_soundFollowsMouse = true; // no real preference yet // ppgb 20220504: what does this mean? |
| 1416 | VowelEditor_create_twoFormantSchwa (me.get()); |
| 1417 | GuiText_setString (my f0TextField, Melder_double (my instancePref_f0_start())); |
| 1418 | GuiText_setString (my f0SlopeTextField, Melder_double (my instancePref_f0_slope())); |
| 1419 | GuiText_setString (my durationTextField, U"0.2"); // source has been created |
| 1420 | GuiText_setString (my extendTextField, Melder_double (my instancePref_trajectory_extendDuration())); |
| 1421 | updateWidgets (me.get()); |
| 1422 | updateInfoLabels (me.get()); |
| 1423 | trace (U"exit"); |
| 1424 | return me; |
| 1425 | } catch (MelderError) { |
| 1426 | Melder_throw (U"VowelEditor not created."); |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | /* End of file VowelEditor.cpp */ |