| 1 |
/* FormantPathArea.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 2020-2023 David Weenink, 2022,2024 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 "FormantPathArea.h" |
| 20 |
#include "EditorM.h" |
| 21 |
|
| 22 |
Thing_implement (FormantPathArea, SoundAnalysisArea, 0); |
| 23 |
Thing_implement (FormantPathArea_without_Sound, FormantPathArea, 0); |
| 24 |
|
| 25 |
#include "Prefs_define.h" |
| 26 |
#include "FormantPathArea_prefs.h" |
| 27 |
#include "Prefs_install.h" |
| 28 |
#include "FormantPathArea_prefs.h" |
| 29 |
#include "Prefs_copyToInstance.h" |
| 30 |
#include "FormantPathArea_prefs.h" |
| 31 |
|
| 32 |
void structFormantPathArea :: v_formantsInfo () const { |
| 33 |
/* Formants flag: */ |
| 34 |
MelderInfo_writeLine (U"Formant show: ", our instancePref_formant_show()); |
| 35 |
/* Formant settings: */ |
| 36 |
if (formantPathAnalysisParametersKnown) { |
| 37 |
MelderInfo_writeLine (U"Formant analysis parameters are known from the last analysis within the editor"); |
| 38 |
MelderInfo_writeLine (U"Formant time step: ", our instancePref_formant_path_timeStep(), U" seconds"); |
| 39 |
MelderInfo_writeLine (U"Formant window length: ", our instancePref_formant_path_windowLength(), U" seconds"); |
| 40 |
MelderInfo_writeLine (U"Formant number of poles: ", Melder_iround (2.0 * our instancePref_formant_path_maximumNumberOfFormants())); |
| 41 |
MelderInfo_writeLine (U"Formant middle ceiling: ", our instancePref_formant_path_middleFormantCeiling(), U" Hertz"); |
| 42 |
MelderInfo_writeLine (U"Formant ceiling step size: ", our instancePref_formant_path_ceilingStepSize()); |
| 43 |
MelderInfo_writeLine (U"Formant number of steps up / down: ", our instancePref_formant_path_numberOfStepsUpDown()); |
| 44 |
MelderInfo_writeLine (U"Formant dynamic range: ", our instancePref_formant_dynamicRange(), U" dB"); |
| 45 |
MelderInfo_writeLine (U"Formant dot size: ", our instancePref_formant_dotSize(), U" mm"); |
| 46 |
/* Advanced formant settings: */ |
| 47 |
MelderInfo_writeLine (U"Formant method: ", kSoundAnalysisArea_formant_analysisMethod_getText (our instancePref_formant_path_method())); |
| 48 |
MelderInfo_writeLine (U"Formant pre-emphasis from: ", our instancePref_formant_path_preEmpasisFrom(), U" Hz"); |
| 49 |
} else { |
| 50 |
FormantPath formantPath = our formantPath(); |
| 51 |
VEC ceilings = formantPath -> ceilings.get(); |
| 52 |
Melder_assert (formantPath -> formantCandidates.size > 0); |
| 53 |
Formant formant = formantPath -> formantCandidates.at [1]; |
| 54 |
MelderInfo_writeLine (U"Formant analysis parameters could only be approximated from the FormantPath"); |
| 55 |
MelderInfo_writeLine (U"Formant time step: ", formantPath -> dx, U" seconds"); |
| 56 |
const double duration = formantPath -> xmax - formantPath-> xmin; |
| 57 |
const double approximateWindowDuration = 0.5 * (duration - formantPath -> nx * formantPath -> dx); // a little bit too small |
| 58 |
MelderInfo_writeLine (U"Formant window length: > ", approximateWindowDuration, U" seconds"); |
| 59 |
const integer numberOfPoles = 2 * formant -> maxnFormants; |
| 60 |
MelderInfo_writeLine (U"Formant number of poles: ", numberOfPoles, U" or ", numberOfPoles + 1); |
| 61 |
const integer middleCeilingIndex = (ceilings.size + 1) / 2; |
| 62 |
const double middleCeiling = ceilings [middleCeilingIndex]; |
| 63 |
MelderInfo_writeLine (U"Formant middle ceiling: ", middleCeiling, U" Hertz"); |
| 64 |
const double stepSize = log (ceilings [middleCeilingIndex + 1] / middleCeiling); |
| 65 |
MelderInfo_writeLine (U"Formant ceiling step size: ", stepSize); |
| 66 |
MelderInfo_writeLine (U"Formant number of steps up / down: ", middleCeilingIndex - 1); |
| 67 |
MelderInfo_writeLine (U"Formant dynamic range: ", our instancePref_formant_dynamicRange(), U" dB"); |
| 68 |
MelderInfo_writeLine (U"Formant dot size: ", our instancePref_formant_dotSize(), U" mm"); |
| 69 |
/* Advanced formant settings: */ |
| 70 |
MelderInfo_writeLine (U"Formant method: unknown"); |
| 71 |
MelderInfo_writeLine (U"Formant pre-emphasis from: unknown"); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/* |
| 76 |
Fast selection of an interval: |
| 77 |
If the mouse click was near a ceiling line in the SoundAnalysisArea we select the whole interval |
| 78 |
at that ceiling frequency. |
| 79 |
*/ |
| 80 |
bool structFormantPathArea :: v_mouse (GuiDrawingArea_MouseEvent event, double x_world, double localY_fraction) { |
| 81 |
const double fmin = our instancePref_spectrogram_viewFrom (); |
| 82 |
const double fmax = our instancePref_spectrogram_viewTo (); |
| 83 |
const double frequencyAtClickPoint = fmin + localY_fraction * (fmax - fmin); |
| 84 |
integer candidate; |
| 85 |
double tmin, tmax; |
| 86 |
FormantPath_getCandidateAtTime (our formantPath(), x_world, & tmin, & tmax, & candidate); |
| 87 |
if (candidate > 0 ) { |
| 88 |
const double ceilingFrequency = our formantPath() -> ceilings [candidate]; |
| 89 |
if (fabs ((ceilingFrequency - frequencyAtClickPoint) / (fmax - fmin)) < 0.02) { |
| 90 |
setSelection (tmin, tmax); |
| 91 |
return true; |
| 92 |
} |
| 93 |
} |
| 94 |
return FunctionEditor_defaultMouseInWideDataView (our functionEditor(), event, x_world); |
| 95 |
} |
| 96 |
|
| 97 |
static void FormantPathArea_drawCeilings (FormantPathArea me, double tmin, double tmax, double fmin, double fmax) { |
| 98 |
TextGrid path = my formantPath() -> path.get(); |
| 99 |
IntervalTier intervalTier = static_cast<IntervalTier> (path -> tiers ->at [1]); |
| 100 |
const integer minIndex = IntervalTier_timeToLowIndex (intervalTier, tmin); |
| 101 |
const integer maxIndex = IntervalTier_timeToHighIndex (intervalTier, tmax); |
| 102 |
Melder_assert (minIndex > 0 && maxIndex > 0); |
| 103 |
Graphics_setWindow (my graphics(), tmin, tmax, fmin, fmax); |
| 104 |
Graphics_setTextAlignment (my graphics(), kGraphics_horizontalAlignment::CENTRE, Graphics_BASELINE); |
| 105 |
Graphics_setColour (my graphics(), Melder_RED); |
| 106 |
Graphics_setLineWidth (my graphics(), 3.0); |
| 107 |
for (integer iint = minIndex; iint <= maxIndex; iint ++) { |
| 108 |
TextInterval textInterval = intervalTier -> intervals.at [iint]; |
| 109 |
conststring32 label = textInterval -> text.get(); |
| 110 |
if (label) { |
| 111 |
const integer index = Melder_atoi (label); |
| 112 |
if (index > 0 && index <= my formantPath() -> ceilings.size) { |
| 113 |
const double ceiling = my formantPath() -> ceilings [index]; |
| 114 |
const double xfrom = std::max (textInterval -> xmin, tmin); |
| 115 |
const double xto = std:: min (textInterval -> xmax, tmax); |
| 116 |
if (ceiling < fmax) { |
| 117 |
Graphics_line (my graphics(), xfrom, ceiling, xto, ceiling); |
| 118 |
Graphics_text (my graphics(), 0.5 * (xfrom + xto), ceiling + 50.0, Melder_fixed (ceiling, 0)); |
| 119 |
} else |
| 120 |
Graphics_line (my graphics(), xfrom, fmax, xto, fmax); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
Graphics_setLineWidth (my graphics(), 1.0); |
| 125 |
} |
| 126 |
|
| 127 |
void structFormantPathArea :: v_draw_analysis_formants () { |
| 128 |
if (our instancePref_formant_show()) { |
| 129 |
Graphics_setColour (our graphics(), Melder_RED); |
| 130 |
Graphics_setSpeckleSize (our graphics(), our instancePref_formant_dotSize()); |
| 131 |
MelderColour oddColour = MelderColour_fromColourName (our instancePref_formant_path_oddColour()); |
| 132 |
MelderColour evenColour = MelderColour_fromColourName (our instancePref_formant_path_evenColour()); |
| 133 |
|
| 134 |
Formant_drawSpeckles_inside (our d_formant.get(), our graphics(), our startWindow(), our endWindow(), |
| 135 |
our instancePref_spectrogram_viewFrom(), our instancePref_spectrogram_viewTo(), |
| 136 |
our instancePref_formant_dynamicRange(), oddColour, evenColour, true |
| 137 |
); |
| 138 |
Graphics_setColour (our graphics(), Melder_PINK); |
| 139 |
FormantPathArea_drawCeilings (this, our startWindow(), our endWindow(), |
| 140 |
our instancePref_spectrogram_viewFrom(), our instancePref_spectrogram_viewTo()); |
| 141 |
Graphics_setColour (our graphics(), Melder_BLACK); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
static void menu_cb_FormantSettings (FormantPathArea me, EDITOR_ARGS) { |
| 147 |
EDITOR_FORM (U"Formant analysis settings...", U"Sound: To FormantPath (burg)...") |
| 148 |
REAL (timeStep, U"Time step (s)", my default_formant_path_timeStep ()) |
| 149 |
POSITIVE (maximumNumberOfFormants, U"Max. number of formants", my default_formant_path_maximumNumberOfFormants ()) |
| 150 |
REAL (middleFormantCeiling, U"Middle formant ceiling (Hz)", my default_formant_path_middleFormantCeiling ()) |
| 151 |
POSITIVE (windowLength, U"Window length (s)", my default_formant_path_windowLength ()) |
| 152 |
POSITIVE (preEmphasisFrom, U"Pre-emphasis from (Hz)", my default_formant_path_preEmpasisFrom ()) |
| 153 |
COMMENT (U"The maximum and minimum ceiling frequencies are determined as:") |
| 154 |
COMMENT (U" middleFormantCeiling * exp(+/- ceilingStepSize * numberOfStepsUpDown).") |
| 155 |
POSITIVE (ceilingStepSize, U"Ceiling step size", my default_formant_path_ceilingStepSize ()) |
| 156 |
NATURAL (numberOfStepsUpDown, U"Number of steps up / down", my default_formant_path_numberOfStepsUpDown ()) |
| 157 |
EDITOR_OK |
| 158 |
SET_REAL (timeStep, my instancePref_formant_path_timeStep ()) |
| 159 |
SET_REAL (maximumNumberOfFormants, my instancePref_formant_path_maximumNumberOfFormants ()) |
| 160 |
SET_REAL (middleFormantCeiling, my instancePref_formant_path_middleFormantCeiling ()) |
| 161 |
SET_REAL (windowLength, my instancePref_formant_path_windowLength ()) |
| 162 |
SET_REAL (preEmphasisFrom, my instancePref_formant_path_preEmpasisFrom ()) |
| 163 |
SET_REAL (ceilingStepSize, my instancePref_formant_path_ceilingStepSize ()) |
| 164 |
SET_INTEGER (numberOfStepsUpDown, my instancePref_formant_path_numberOfStepsUpDown ()) |
| 165 |
EDITOR_DO |
| 166 |
Melder_require (my sound (), |
| 167 |
U"There is no sound to analyze."); |
| 168 |
my setInstancePref_formant_path_timeStep (timeStep); |
| 169 |
my setInstancePref_formant_path_maximumNumberOfFormants (maximumNumberOfFormants); |
| 170 |
my setInstancePref_formant_path_middleFormantCeiling (middleFormantCeiling); |
| 171 |
my setInstancePref_formant_path_windowLength (windowLength); |
| 172 |
my setInstancePref_formant_path_preEmpasisFrom (preEmphasisFrom); |
| 173 |
my setInstancePref_formant_path_ceilingStepSize (ceilingStepSize); |
| 174 |
my setInstancePref_formant_path_numberOfStepsUpDown (numberOfStepsUpDown); |
| 175 |
autoFormantPath result = Sound_to_FormantPath_burg (my sound (), timeStep, maximumNumberOfFormants, middleFormantCeiling, windowLength, |
| 176 |
preEmphasisFrom, ceilingStepSize, numberOfStepsUpDown |
| 177 |
); |
| 178 |
my formantPath() -> nx = result -> nx; |
| 179 |
my formantPath() -> dx = result -> dx; |
| 180 |
my formantPath() -> x1 = result -> x1; |
| 181 |
my formantPath() -> formantCandidates = result -> formantCandidates.move(); |
| 182 |
my formantPath() -> ceilings = result -> ceilings.move(); |
| 183 |
my formantPath() -> path = result -> path.move(); |
| 184 |
my formantPathAnalysisParametersKnown = true; |
| 185 |
my d_formant = FormantPath_extractFormant (my formantPath()); |
| 186 |
FunctionArea_broadcastDataChanged (me); |
| 187 |
EDITOR_END |
| 188 |
} |
| 189 |
|
| 190 |
static void menu_cb_FormantDisplaySettings (FormantPathArea me, EDITOR_ARGS) { |
| 191 |
EDITOR_FORM (U"Formant colour settings", nullptr) |
| 192 |
REAL (dynamicRange, U"Dynamic range (dB)", my default_formant_dynamicRange()) |
| 193 |
POSITIVE (dotSize, U"Dot size (mm)", my default_formant_dotSize()) |
| 194 |
WORD (oddPathColour_string, U"Dots in F1, F3, F5", my default_formant_path_oddColour()) |
| 195 |
WORD (evenPathColour_string, U"Dots in F2, F4", my default_formant_path_evenColour()) |
| 196 |
EDITOR_OK |
| 197 |
SET_REAL (dynamicRange, my instancePref_formant_dynamicRange()) |
| 198 |
SET_REAL (dotSize, my instancePref_formant_dotSize()) |
| 199 |
SET_STRING (oddPathColour_string, my instancePref_formant_path_oddColour()) |
| 200 |
SET_STRING (evenPathColour_string, my instancePref_formant_path_evenColour()) |
| 201 |
EDITOR_DO |
| 202 |
my setInstancePref_formant_dynamicRange (dynamicRange); |
| 203 |
my setInstancePref_formant_dotSize (dotSize); |
| 204 |
my setInstancePref_formant_path_oddColour (oddPathColour_string); |
| 205 |
my setInstancePref_formant_path_evenColour (evenPathColour_string); |
| 206 |
FunctionArea_broadcastDataChanged (me); |
| 207 |
EDITOR_END |
| 208 |
} |
| 209 |
|
| 210 |
static void menu_cb_DrawVisibleFormantContour (FormantPathArea me, EDITOR_ARGS) { |
| 211 |
EDITOR_FORM (U"Draw visible formant contour", nullptr) |
| 212 |
my v_form_pictureWindow (cmd); // BUG: move to area |
| 213 |
my v_form_pictureMargins (cmd); |
| 214 |
my v_form_pictureSelection (cmd); |
| 215 |
BOOLEAN (garnish, U"Garnish", true) |
| 216 |
EDITOR_OK |
| 217 |
my v_ok_pictureWindow (cmd); // BUG: move to area |
| 218 |
my v_ok_pictureMargins (cmd); |
| 219 |
my v_ok_pictureSelection (cmd); |
| 220 |
SET_BOOLEAN (garnish, my instancePref_formant_picture_garnish()) |
| 221 |
EDITOR_DO |
| 222 |
my v_do_pictureWindow (cmd); // BUG: move to area |
| 223 |
my v_do_pictureMargins (cmd); |
| 224 |
my v_do_pictureSelection (cmd); |
| 225 |
my setInstancePref_formant_picture_garnish (garnish); |
| 226 |
if (! my instancePref_formant_show()) |
| 227 |
Melder_throw (U"No formant contour is visible.\nFirst choose \"Show formants\" from the Formants menu."); |
| 228 |
DataGui_openPraatPicture (me); |
| 229 |
Formant_drawSpeckles (my d_formant.get(), my pictureGraphics(), my startWindow(), my endWindow(), |
| 230 |
my instancePref_spectrogram_viewTo(), my instancePref_formant_dynamicRange(), |
| 231 |
garnish |
| 232 |
); |
| 233 |
FunctionArea_garnishPicture (me); |
| 234 |
DataGui_closePraatPicture (me); |
| 235 |
EDITOR_END |
| 236 |
} |
| 237 |
|
| 238 |
static void menu_cb_showFormants (FormantPathArea me, EDITOR_ARGS) { |
| 239 |
my setInstancePref_formant_show (! my instancePref_formant_show()); // toggle |
| 240 |
GuiMenuItem_check (my formantToggle, my instancePref_formant_show()); // in case we're called from a script |
| 241 |
FunctionEditor_redraw (my functionEditor()); |
| 242 |
} |
| 243 |
|
| 244 |
static void INFO_EDITOR__formantListing (FormantPathArea me, EDITOR_ARGS) { |
| 245 |
INFO_EDITOR |
| 246 |
const double startTime = my startSelection(), endTime = my endSelection(); |
| 247 |
MelderInfo_open (); |
| 248 |
MelderInfo_writeLine (U"Time_s F1_Hz F2_Hz F3_Hz F4_Hz"); |
| 249 |
if (startTime == endTime) { |
| 250 |
const double f1 = Formant_getValueAtTime (my d_formant.get(), 1, startTime, kFormant_unit::HERTZ); |
| 251 |
const double f2 = Formant_getValueAtTime (my d_formant.get(), 2, startTime, kFormant_unit::HERTZ); |
| 252 |
const double f3 = Formant_getValueAtTime (my d_formant.get(), 3, startTime, kFormant_unit::HERTZ); |
| 253 |
const double f4 = Formant_getValueAtTime (my d_formant.get(), 4, startTime, kFormant_unit::HERTZ); |
| 254 |
MelderInfo_writeLine (Melder_fixed (startTime, 6), U" ", Melder_fixed (f1, 6), U" ", Melder_fixed (f2, 6), U" ", Melder_fixed (f3, 6), U" ", Melder_fixed (f4, 6)); |
| 255 |
} else { |
| 256 |
integer i1, i2; |
| 257 |
Sampled_getWindowSamples (my d_formant.get(), startTime, endTime, & i1, & i2); |
| 258 |
for (integer i = i1; i <= i2; i ++) { |
| 259 |
const double t = Sampled_indexToX (my d_formant.get(), i); |
| 260 |
const double f1 = Formant_getValueAtTime (my d_formant.get(), 1, t, kFormant_unit::HERTZ); |
| 261 |
const double f2 = Formant_getValueAtTime (my d_formant.get(), 2, t, kFormant_unit::HERTZ); |
| 262 |
const double f3 = Formant_getValueAtTime (my d_formant.get(), 3, t, kFormant_unit::HERTZ); |
| 263 |
const double f4 = Formant_getValueAtTime (my d_formant.get(), 4, t, kFormant_unit::HERTZ); |
| 264 |
MelderInfo_writeLine (Melder_fixed (t, 6), U" ", Melder_fixed (f1, 6), U" ", Melder_fixed (f2, 6), U" ", Melder_fixed (f3, 6), U" ", Melder_fixed (f4, 6)); |
| 265 |
} |
| 266 |
} |
| 267 |
MelderInfo_close (); |
| 268 |
INFO_EDITOR_END |
| 269 |
} |
| 270 |
void structFormantPathArea :: v_createMenuItems_formant (EditorMenu menu) { |
| 271 |
our formantToggle = FunctionAreaMenu_addCommand (menu, U"Show formants", |
| 272 |
GuiMenu_CHECKBUTTON | ( our instancePref_formant_show() ? GuiMenu_TOGGLE_ON : 0 ), |
| 273 |
menu_cb_showFormants, this |
| 274 |
); |
| 275 |
// The following menu item should not be visible if there is no sound |
| 276 |
// djmw: 20230211 I don't know how to test whether a sound is available here |
| 277 |
FunctionAreaMenu_addCommand (menu, U"Formant analysis settings...", 0, |
| 278 |
menu_cb_FormantSettings, this); |
| 279 |
FunctionAreaMenu_addCommand (menu, U"Formant display settings...", 0, |
| 280 |
menu_cb_FormantDisplaySettings, this); |
| 281 |
FunctionAreaMenu_addCommand (menu, U"Draw visible formant contour...", 0, |
| 282 |
menu_cb_DrawVisibleFormantContour, this); |
| 283 |
FunctionAreaMenu_addCommand (menu, U"Formant listing", 0, |
| 284 |
INFO_EDITOR__formantListing, this); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
/* End of file FormantPathArea.cpp */ |