| 1 | /* SoundArea.cpp |
| 2 | * |
| 3 | * Copyright (C) 2022-2026 Paul Boersma, 2007 Erez Volk (FLAC support) |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "SoundArea.h" |
| 20 | #include "LongSoundArea.h" |
| 21 | #include "../kar/UnicodeData.h" |
| 22 | #include "Sound_and_MixingMatrix.h" |
| 23 | #include "EditorM.h" |
| 24 | |
| 25 | Thing_implement (SoundArea, FunctionArea, 0); |
| 26 | Thing_implement (LongSoundArea, FunctionArea, 0); |
| 27 | |
| 28 | #include "enums_getText.h" |
| 29 | #include "SoundArea_enums.h" |
| 30 | #include "enums_getValue.h" |
| 31 | #include "SoundArea_enums.h" |
| 32 | |
| 33 | #include "Prefs_define.h" |
| 34 | #include "SoundArea_prefs.h" |
| 35 | #include "Prefs_install.h" |
| 36 | #include "SoundArea_prefs.h" |
| 37 | #include "Prefs_copyToInstance.h" |
| 38 | #include "SoundArea_prefs.h" |
| 39 | |
| 40 | |
| 41 | #pragma mark - SoundArea info |
| 42 | |
| 43 | void structSoundArea :: v1_info () { |
| 44 | MelderInfo_writeLine (U"Sound scaling strategy: ", kSoundArea_scalingStrategy_getText (our instancePref_scalingStrategy())); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | #pragma mark - SoundArea drawing |
| 49 | |
| 50 | static void SoundArea_drawCursorFunctionValue (SoundArea me, double yWC, conststring32 yWC_string, conststring32 units) { |
| 51 | Graphics_setColour (my graphics(), DataGui_defaultForegroundColour (me, true)); |
| 52 | Graphics_line (my graphics(), my startWindow(), yWC, 0.99 * my startWindow() + 0.01 * my endWindow(), yWC); |
| 53 | Graphics_fillCircle_mm (my graphics(), 0.5 * (my startSelection() + my endSelection()), yWC, 1.5); |
| 54 | Graphics_setTextAlignment (my graphics(), Graphics_RIGHT, Graphics_HALF); |
| 55 | Graphics_text (my graphics(), my startWindow(), yWC, yWC_string, units); |
| 56 | } |
| 57 | |
| 58 | void structSoundArea :: v_drawInside () { |
| 59 | SoundArea_draw (this); |
| 60 | } |
| 61 | |
| 62 | void SoundArea_draw (SoundArea me) { |
| 63 | Melder_assert (!! my sound() != !! my longSound()); |
| 64 | |
| 65 | if (my longSound()) { |
| 66 | try { |
| 67 | LongSound_haveWindow (my longSound(), my startWindow(), my endWindow()); |
| 68 | } catch (MelderError) { |
| 69 | Melder_clearError (); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | const integer numberOfChannels = my soundOrLongSound() -> ny; |
| 74 | const bool cursorVisible = ( my startSelection() == my endSelection() && |
| 75 | my startSelection() >= my startWindow() && my startSelection() <= my endWindow() ); |
| 76 | Graphics_setColour (my graphics(), Melder_BLACK); |
| 77 | if (my longSound() && my endWindow() - my startWindow() > my longSound() -> bufferLength) { |
| 78 | Graphics_setWindow (my graphics(), 0.0, 1.0, 0.0, 1.0); |
| 79 | Graphics_setColour (my graphics(), Melder_BLACK); |
| 80 | Graphics_setTextAlignment (my graphics(), Graphics_CENTRE, Graphics_BOTTOM); |
| 81 | Graphics_text (my graphics(), 0.5, 0.5, U"(window longer than ", |
| 82 | Melder_graphicalSingle (my longSound() -> bufferLength), U" seconds)"); |
| 83 | Graphics_setTextAlignment (my graphics(), Graphics_CENTRE, Graphics_TOP); |
| 84 | Graphics_text (my graphics(), 0.5, 0.5, U"(zoom in to see the samples)"); |
| 85 | return; |
| 86 | } |
| 87 | bool fits; |
| 88 | try { |
| 89 | fits = ( my sound() ? true : LongSound_haveWindow (my longSound(), my startWindow(), my endWindow()) ); |
| 90 | } catch (MelderError) { |
| 91 | const bool outOfMemory = !! str32str (Melder_getError (), U"memory"); |
| 92 | if (Melder_debug == 9) |
| 93 | Melder_flushError (); |
| 94 | else |
| 95 | Melder_clearError (); |
| 96 | Graphics_setWindow (my graphics(), 0.0, 1.0, 0.0, 1.0); |
| 97 | Graphics_setTextAlignment (my graphics(), Graphics_CENTRE, Graphics_HALF); |
| 98 | Graphics_text (my graphics(), 0.5, 0.5, outOfMemory ? U"(out of memory)" : U"(cannot read sound file)"); |
| 99 | return; |
| 100 | } |
| 101 | if (! fits) { |
| 102 | Graphics_setWindow (my graphics(), 0.0, 1.0, 0.0, 1.0); |
| 103 | Graphics_setTextAlignment (my graphics(), Graphics_CENTRE, Graphics_HALF); |
| 104 | Graphics_text (my graphics(), 0.5, 0.5, U"(window too large; zoom in to see the data)"); |
| 105 | return; |
| 106 | } |
| 107 | integer first, last; |
| 108 | if (Sampled_getWindowSamples (my soundOrLongSound(), my startWindow(), my endWindow(), & first, & last) <= 1) { |
| 109 | Graphics_setWindow (my graphics(), 0.0, 1.0, 0.0, 1.0); |
| 110 | Graphics_setTextAlignment (my graphics(), Graphics_CENTRE, Graphics_HALF); |
| 111 | Graphics_text (my graphics(), 0.5, 0.5, U"(zoom out to see the data)"); |
| 112 | return; |
| 113 | } |
| 114 | const integer numberOfVisibleChannels = Melder_clippedRight (numberOfChannels, 8_integer); |
| 115 | const integer firstVisibleChannel = my channelOffset + 1; |
| 116 | const integer lastVisibleChannel = Melder_clippedRight (my channelOffset + numberOfVisibleChannels, numberOfChannels); |
| 117 | double maximumExtent = 0.0, visibleMinimum = 0.0, visibleMaximum = 0.0; |
| 118 | if (my instancePref_scalingStrategy() == kSoundArea_scalingStrategy::BY_WINDOW) { |
| 119 | if (my longSound()) |
| 120 | LongSound_getWindowExtrema (my longSound(), my startWindow(), my endWindow(), firstVisibleChannel, |
| 121 | & visibleMinimum, & visibleMaximum); |
| 122 | else |
| 123 | Matrix_getWindowExtrema (my sound(), first, last, firstVisibleChannel, firstVisibleChannel, |
| 124 | & visibleMinimum, & visibleMaximum); |
| 125 | for (integer ichan = firstVisibleChannel + 1; ichan <= lastVisibleChannel; ichan ++) { |
| 126 | double visibleChannelMinimum, visibleChannelMaximum; |
| 127 | if (my longSound()) |
| 128 | LongSound_getWindowExtrema (my longSound(), my startWindow(), my endWindow(), ichan, |
| 129 | & visibleChannelMinimum, & visibleChannelMaximum); |
| 130 | else |
| 131 | Matrix_getWindowExtrema (my sound(), first, last, ichan, ichan, & visibleChannelMinimum, & visibleChannelMaximum); |
| 132 | if (visibleChannelMinimum < visibleMinimum) |
| 133 | visibleMinimum = visibleChannelMinimum; |
| 134 | if (visibleChannelMaximum > visibleMaximum) |
| 135 | visibleMaximum = visibleChannelMaximum; |
| 136 | } |
| 137 | maximumExtent = visibleMaximum - visibleMinimum; |
| 138 | } |
| 139 | for (integer ichan = firstVisibleChannel; ichan <= lastVisibleChannel; ichan ++) { |
| 140 | const double cursorFunctionValue = ( my longSound() ? 0.0 : |
| 141 | Vector_getValueAtX (my sound(), 0.5 * (my startSelection() + my endSelection()), ichan, kVector_valueInterpolation :: SINC70) ); |
| 142 | const double ymin = (double) (numberOfVisibleChannels - ichan + my channelOffset) / numberOfVisibleChannels; |
| 143 | const double ymax = (double) (numberOfVisibleChannels + 1 - ichan + my channelOffset) / numberOfVisibleChannels; |
| 144 | Graphics_Viewport vp = Graphics_insetViewport (my graphics(), 0.0, 1.0, ymin, ymax); |
| 145 | bool horizontal = false; |
| 146 | double minimum = -1.0, maximum = +1.0; |
| 147 | if (my instancePref_scalingStrategy() == kSoundArea_scalingStrategy::BY_WHOLE) { |
| 148 | my getGlobalExtrema (& minimum, & maximum); |
| 149 | } else if (my instancePref_scalingStrategy() == kSoundArea_scalingStrategy::BY_WINDOW) { |
| 150 | if (numberOfChannels > 2) { |
| 151 | if (my longSound()) |
| 152 | LongSound_getWindowExtrema (my longSound(), my startWindow(), my endWindow(), ichan, & minimum, & maximum); |
| 153 | else |
| 154 | Matrix_getWindowExtrema (my sound(), first, last, ichan, ichan, & minimum, & maximum); |
| 155 | if (maximumExtent > 0.0) { |
| 156 | const double middle = 0.5 * (minimum + maximum); |
| 157 | minimum = middle - 0.5 * maximumExtent; |
| 158 | maximum = middle + 0.5 * maximumExtent; |
| 159 | } |
| 160 | } else { |
| 161 | minimum = visibleMinimum; |
| 162 | maximum = visibleMaximum; |
| 163 | } |
| 164 | } else if (my instancePref_scalingStrategy() == kSoundArea_scalingStrategy::BY_WINDOW_AND_CHANNEL) { |
| 165 | if (my longSound()) |
| 166 | LongSound_getWindowExtrema (my longSound(), my startWindow(), my endWindow(), ichan, & minimum, & maximum); |
| 167 | else |
| 168 | Matrix_getWindowExtrema (my sound(), first, last, ichan, ichan, & minimum, & maximum); |
| 169 | } else if (my instancePref_scalingStrategy() == kSoundArea_scalingStrategy::FIXED_HEIGHT) { |
| 170 | if (my longSound()) |
| 171 | LongSound_getWindowExtrema (my longSound(), my startWindow(), my endWindow(), ichan, & minimum, & maximum); |
| 172 | else |
| 173 | Matrix_getWindowExtrema (my sound(), first, last, ichan, ichan, & minimum, & maximum); |
| 174 | const double channelExtent = my instancePref_scaling_height(); |
| 175 | const double middle = 0.5 * (minimum + maximum); |
| 176 | minimum = middle - 0.5 * channelExtent; |
| 177 | maximum = middle + 0.5 * channelExtent; |
| 178 | } else if (my instancePref_scalingStrategy() == kSoundArea_scalingStrategy::FIXED_RANGE) { |
| 179 | minimum = my instancePref_scaling_minimum(); |
| 180 | maximum = my instancePref_scaling_maximum(); |
| 181 | } |
| 182 | if (minimum == maximum) { |
| 183 | horizontal = true; |
| 184 | minimum -= 1.0; |
| 185 | maximum += 1.0; |
| 186 | } |
| 187 | Graphics_setWindow (my graphics(), my startWindow(), my endWindow(), minimum, maximum); |
| 188 | Graphics_setColour (my graphics(), DataGui_defaultForegroundColour (me, false)); |
| 189 | if (horizontal) { |
| 190 | Graphics_setTextAlignment (my graphics(), Graphics_RIGHT, Graphics_HALF); |
| 191 | const double mid = 0.5 * (minimum + maximum); |
| 192 | Graphics_text (my graphics(), my startWindow(), mid, Melder_graphicalHalf (mid)); |
| 193 | } else { |
| 194 | if (! cursorVisible || isundef (cursorFunctionValue) || Graphics_dyWCtoMM (my graphics(), cursorFunctionValue - minimum) > 4.0) { |
| 195 | Graphics_setTextAlignment (my graphics(), Graphics_RIGHT, ichan == lastVisibleChannel? Graphics_HALF : Graphics_BOTTOM); |
| 196 | Graphics_text (my graphics(), my startWindow(), minimum, Melder_graphicalHalf (minimum)); |
| 197 | } |
| 198 | if (! cursorVisible || isundef (cursorFunctionValue) || Graphics_dyWCtoMM (my graphics(), maximum - cursorFunctionValue) > 4.0) { |
| 199 | Graphics_setTextAlignment (my graphics(), Graphics_RIGHT, ichan == firstVisibleChannel ? Graphics_HALF : Graphics_TOP); |
| 200 | Graphics_text (my graphics(), my startWindow(), maximum, Melder_graphicalHalf (maximum)); |
| 201 | } |
| 202 | } |
| 203 | if (minimum < 0.0 && maximum > 0.0 && ! horizontal) { |
| 204 | Graphics_setWindow (my graphics(), 0.0, 1.0, minimum, maximum); |
| 205 | if (! cursorVisible || isundef (cursorFunctionValue) || fabs (Graphics_dyWCtoMM (my graphics(), cursorFunctionValue - 0.0)) > 3.5) { |
| 206 | Graphics_setTextAlignment (my graphics(), Graphics_RIGHT, Graphics_HALF); |
| 207 | Graphics_text (my graphics(), 0.0, 0.0, U"0"); |
| 208 | } |
| 209 | Graphics_setLineType (my graphics(), Graphics_DOTTED); |
| 210 | Graphics_line (my graphics(), 0.0, 0.0, 1.0, 0.0); |
| 211 | Graphics_setLineType (my graphics(), Graphics_DRAWN); |
| 212 | } |
| 213 | /* |
| 214 | Garnish the drawing area of each channel. |
| 215 | */ |
| 216 | Graphics_setColour (my graphics(), Melder_BLACK); |
| 217 | if (numberOfChannels > 1) { |
| 218 | Graphics_setTextAlignment (my graphics(), Graphics_LEFT, Graphics_HALF); |
| 219 | conststring32 channelName = my v_getChannelName (ichan); |
| 220 | static MelderString channelLabel; |
| 221 | MelderString_copy (& channelLabel, ( channelName ? U"ch" : U"Ch " ), ichan); |
| 222 | if (channelName) |
| 223 | MelderString_append (& channelLabel, U": ", channelName); |
| 224 | MelderString_append (& channelLabel, U" ", |
| 225 | ( my muteChannels [ichan] ? UNITEXT_SPEAKER_WITH_CANCELLATION_STROKE : UNITEXT_SPEAKER )); |
| 226 | if (ichan > 8 && ichan - my channelOffset == 1) |
| 227 | MelderString_append (& channelLabel, U" " UNITEXT_UPWARDS_ARROW); |
| 228 | else if (numberOfChannels >= 8 && ichan - my channelOffset == 8 && ichan < numberOfChannels) |
| 229 | MelderString_append (& channelLabel, U" " UNITEXT_DOWNWARDS_ARROW); |
| 230 | Graphics_setWindow (my graphics(), 0.0, 1.0, 0.0, 1.0); |
| 231 | Graphics_text (my graphics(), 1.0, 0.5, channelLabel.string); |
| 232 | } |
| 233 | /* |
| 234 | Draw a very thin separator line underneath. |
| 235 | */ |
| 236 | if (ichan - my channelOffset < 8 && ichan - my channelOffset < numberOfVisibleChannels) { |
| 237 | Graphics_setWindow (my graphics(), 0.0, 1.0, 0.0, 1.0); |
| 238 | Graphics_setColour (my graphics(), my editable() ? DataGuiColour_EDITABLE_FRAME : DataGuiColour_NONEDITABLE_FRAME); |
| 239 | Graphics_line (my graphics(), 0.0, 0.0, 1.0, 0.0); |
| 240 | } |
| 241 | /* |
| 242 | Draw the samples. |
| 243 | */ |
| 244 | if (my sound()) { |
| 245 | Graphics_setWindow (my graphics(), my startWindow(), my endWindow(), minimum, maximum); |
| 246 | if (cursorVisible && isdefined (cursorFunctionValue)) |
| 247 | SoundArea_drawCursorFunctionValue (me, cursorFunctionValue, Melder_graphicalHalf (cursorFunctionValue), U""); |
| 248 | Graphics_setColour (my graphics(), DataGui_defaultForegroundColour (me, false)); |
| 249 | Graphics_function (my graphics(), & my sound() -> z [ichan] [0], first, last, |
| 250 | Sampled_indexToX (my sound(), first), Sampled_indexToX (my sound(), last)); |
| 251 | } else { |
| 252 | Graphics_setWindow (my graphics(), my startWindow(), my endWindow(), minimum * 32768, maximum * 32768); |
| 253 | Graphics_setColour (my graphics(), DataGui_defaultForegroundColour (me, false)); |
| 254 | Graphics_function16 (my graphics(), |
| 255 | my longSound() -> buffer.asArgumentToFunctionThatExpectsZeroBasedArray() - my longSound() -> imin * numberOfChannels + (ichan - 1), |
| 256 | numberOfChannels, first, last, Sampled_indexToX (my longSound(), first), Sampled_indexToX (my longSound(), last) |
| 257 | ); |
| 258 | } |
| 259 | Graphics_setColour (my graphics(), Melder_BLACK); |
| 260 | Graphics_resetViewport (my graphics(), vp); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | |
| 265 | #pragma mark - SoundArea tracking |
| 266 | |
| 267 | bool structSoundArea :: v_mouse (GuiDrawingArea_MouseEvent event, double x_world, double localY_fraction) { |
| 268 | if (event -> isClick()) { |
| 269 | const integer numberOfChannels = our soundOrLongSound() -> ny; |
| 270 | if (event -> commandKeyPressed) { |
| 271 | if (numberOfChannels > 1) { |
| 272 | const integer numberOfVisibleChannels = Melder_clippedRight (numberOfChannels, 8_integer); |
| 273 | Melder_assert (numberOfVisibleChannels >= 1); // for Melder_clipped |
| 274 | const integer clickedChannel = our channelOffset + |
| 275 | Melder_clipped (1_integer, Melder_ifloor ((1.0 - localY_fraction) * numberOfVisibleChannels + 1), numberOfVisibleChannels); |
| 276 | const integer firstVisibleChannel = our channelOffset + 1; |
| 277 | const integer lastVisibleChannel = Melder_clippedRight (our channelOffset + numberOfVisibleChannels, numberOfChannels); |
| 278 | if (clickedChannel >= firstVisibleChannel && clickedChannel <= lastVisibleChannel) { |
| 279 | our muteChannels [clickedChannel] = ! our muteChannels [clickedChannel]; |
| 280 | return FunctionEditor_UPDATE_NEEDED; |
| 281 | } |
| 282 | } |
| 283 | } else { |
| 284 | if (numberOfChannels > 8) { |
| 285 | if (x_world >= our endWindow() && localY_fraction > 0.875 && localY_fraction <= 1.000 && our channelOffset > 0) { |
| 286 | our channelOffset -= 8; |
| 287 | return FunctionEditor_UPDATE_NEEDED; |
| 288 | } |
| 289 | if (x_world >= our endWindow() && localY_fraction > 0.000 && localY_fraction <= 0.125 && our channelOffset < numberOfChannels - 8) { |
| 290 | our channelOffset += 8; |
| 291 | return FunctionEditor_UPDATE_NEEDED; |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | return FunctionEditor_defaultMouseInWideDataView (our functionEditor(), event, x_world); |
| 297 | } |
| 298 | |
| 299 | bool SoundArea_mouse (SoundArea me, GuiDrawingArea_MouseEvent event, double x_world, double globalY_fraction) { |
| 300 | const double localY_fraction = my y_fraction_globalToLocal (globalY_fraction); |
| 301 | return my v_mouse (event, x_world, localY_fraction); |
| 302 | } |
| 303 | |
| 304 | |
| 305 | #pragma mark - SoundArea playing |
| 306 | |
| 307 | void SoundArea_play (SoundArea me, double startTime, double endTime) { |
| 308 | const integer numberOfChannels = my soundOrLongSound() -> ny; |
| 309 | integer numberOfMuteChannels = 0; |
| 310 | Melder_assert (my muteChannels.size == numberOfChannels); |
| 311 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) |
| 312 | if (my muteChannels [ichan]) |
| 313 | numberOfMuteChannels ++; |
| 314 | const integer numberOfChannelsToPlay = numberOfChannels - numberOfMuteChannels; |
| 315 | Melder_require (numberOfChannelsToPlay > 0, |
| 316 | U"Please select at least one channel to play."); |
| 317 | if (numberOfMuteChannels > 0) { |
| 318 | autoMixingMatrix thee = MixingMatrix_create (numberOfChannelsToPlay, numberOfChannels); |
| 319 | MixingMatrix_muteAndActivateChannels (thee.get(), my muteChannels.get()); |
| 320 | if (my longSound()) { |
| 321 | autoSound part = LongSound_extractPart (my longSound(), startTime, endTime, true); |
| 322 | Sound_MixingMatrix_playPart (part.get(), thee.get(), startTime, endTime, theFunctionEditor_playCallback, my functionEditor()); |
| 323 | } else { |
| 324 | Sound_MixingMatrix_playPart (my sound(), thee.get(), startTime, endTime, theFunctionEditor_playCallback, my functionEditor()); |
| 325 | } |
| 326 | } else { |
| 327 | if (my longSound()) |
| 328 | LongSound_playPart (my longSound(), startTime, endTime, theFunctionEditor_playCallback, my functionEditor()); |
| 329 | else |
| 330 | Sound_playPart (my sound(), startTime, endTime, theFunctionEditor_playCallback, my functionEditor()); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | |
| 335 | #pragma mark - SoundArea File |
| 336 | |
| 337 | static void do_write (SoundArea me, MelderFile file, int format, int numberOfBitsPerSamplePoint) { |
| 338 | if (my startSelection() >= my endSelection()) |
| 339 | Melder_throw (U"No samples selected."); |
| 340 | if (my longSound()) { |
| 341 | LongSound_savePartAsAudioFile (my longSound(), format, my startSelection(), my endSelection(), |
| 342 | file, numberOfBitsPerSamplePoint); |
| 343 | } else if (my sound()) { |
| 344 | integer first, last; |
| 345 | const integer numberOfSamples = Sampled_getWindowSamples (my sound(), |
| 346 | my startSelection(), my endSelection(), & first, & last); |
| 347 | if (numberOfSamples) { |
| 348 | autoSound save = Sound_create (my sound() -> ny, 0.0, |
| 349 | numberOfSamples * my sound() -> dx, numberOfSamples, my sound() -> dx, 0.5 * my sound() -> dx); |
| 350 | const integer offset = first - 1; |
| 351 | for (integer channel = 1; channel <= my sound() -> ny; channel ++) |
| 352 | for (integer i = first; i <= last; i ++) |
| 353 | save -> z [channel] [i - offset] = my sound() -> z [channel] [i]; |
| 354 | Sound_saveAsAudioFile (save.get(), file, format, numberOfBitsPerSamplePoint); |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | static void menu_cb_WriteWav (SoundArea me, EDITOR_ARGS) { |
| 359 | EDITOR_FORM_SAVE (U"Save selected sound as WAV file", nullptr) |
| 360 | Melder_sprint (defaultName,300, my soundOrLongSound() -> name.get(), U".wav"); |
| 361 | EDITOR_DO_SAVE |
| 362 | do_write (me, file, Melder_WAV, 16); |
| 363 | EDITOR_END |
| 364 | } |
| 365 | static void menu_cb_SaveAs24BitWav (SoundArea me, EDITOR_ARGS) { |
| 366 | EDITOR_FORM_SAVE (U"Save selected sound as 24-bit WAV file", nullptr) |
| 367 | Melder_assert (! my longSound() && my sound()); |
| 368 | Melder_sprint (defaultName,300, my sound() -> name.get(), U".wav"); |
| 369 | EDITOR_DO_SAVE |
| 370 | do_write (me, file, Melder_WAV, 24); |
| 371 | EDITOR_END |
| 372 | } |
| 373 | static void menu_cb_SaveAs32BitWav (SoundArea me, EDITOR_ARGS) { |
| 374 | EDITOR_FORM_SAVE (U"Save selected sound as 32-bit WAV file", nullptr) |
| 375 | Melder_assert (! my longSound() && my sound()); |
| 376 | Melder_sprint (defaultName,300, my sound() -> name.get(), U".wav"); |
| 377 | EDITOR_DO_SAVE |
| 378 | do_write (me, file, Melder_WAV, 32); |
| 379 | EDITOR_END |
| 380 | } |
| 381 | static void menu_cb_WriteAiff (SoundArea me, EDITOR_ARGS) { |
| 382 | EDITOR_FORM_SAVE (U"Save selected sound as AIFF file", nullptr) |
| 383 | Melder_sprint (defaultName,300, my soundOrLongSound() -> name.get(), U".aiff"); |
| 384 | EDITOR_DO_SAVE |
| 385 | do_write (me, file, Melder_AIFF, 16); |
| 386 | EDITOR_END |
| 387 | } |
| 388 | static void menu_cb_WriteAifc (SoundArea me, EDITOR_ARGS) { |
| 389 | EDITOR_FORM_SAVE (U"Save selected sound as AIFC file", nullptr) |
| 390 | Melder_sprint (defaultName,300, my soundOrLongSound() -> name.get(), U".aifc"); |
| 391 | EDITOR_DO_SAVE |
| 392 | do_write (me, file, Melder_AIFC, 16); |
| 393 | EDITOR_END |
| 394 | } |
| 395 | static void menu_cb_WriteNextSun (SoundArea me, EDITOR_ARGS) { |
| 396 | EDITOR_FORM_SAVE (U"Save selected sound as NeXT/Sun file", nullptr) |
| 397 | Melder_sprint (defaultName,300, my soundOrLongSound() -> name.get(), U".au"); |
| 398 | EDITOR_DO_SAVE |
| 399 | do_write (me, file, Melder_NEXT_SUN, 16); |
| 400 | EDITOR_END |
| 401 | } |
| 402 | static void menu_cb_WriteNist (SoundArea me, EDITOR_ARGS) { |
| 403 | EDITOR_FORM_SAVE (U"Save selected sound as NIST file", nullptr) |
| 404 | Melder_sprint (defaultName,300, my soundOrLongSound() -> name.get(), U".nist"); |
| 405 | EDITOR_DO_SAVE |
| 406 | do_write (me, file, Melder_NIST, 16); |
| 407 | EDITOR_END |
| 408 | } |
| 409 | static void menu_cb_WriteFlac (SoundArea me, EDITOR_ARGS) { |
| 410 | EDITOR_FORM_SAVE (U"Save selected sound as FLAC file", nullptr) |
| 411 | Melder_sprint (defaultName,300, my soundOrLongSound() -> name.get(), U".flac"); |
| 412 | EDITOR_DO_SAVE |
| 413 | do_write (me, file, Melder_FLAC, 16); |
| 414 | EDITOR_END |
| 415 | } |
| 416 | void structSoundArea :: v_createMenuItems_save (EditorMenu menu) { |
| 417 | FunctionAreaMenu_addCommand (menu, U"- Save sound to disk:", 0, nullptr, this); |
| 418 | our writeWavButton = FunctionAreaMenu_addCommand (menu, |
| 419 | U"Save selected sound as WAV file... ||" |
| 420 | " Write selected sound to WAV file... ||" |
| 421 | " Write sound selection to WAV file... ||" |
| 422 | " Write selection to WAV file...", |
| 423 | 1, menu_cb_WriteWav, this |
| 424 | ); |
| 425 | if (! Thing_isa (this, classLongSoundArea)) { // BUG: why not for LongSound? |
| 426 | our saveAs24BitWavButton = FunctionAreaMenu_addCommand (menu, |
| 427 | U"Save selected sound as 24-bit WAV file...", |
| 428 | 1, menu_cb_SaveAs24BitWav, this |
| 429 | ); |
| 430 | our saveAs32BitWavButton = FunctionAreaMenu_addCommand (menu, |
| 431 | U"Save selected sound as 32-bit WAV file...", |
| 432 | 1, menu_cb_SaveAs32BitWav, this |
| 433 | ); |
| 434 | } |
| 435 | our writeAiffButton = FunctionAreaMenu_addCommand (menu, |
| 436 | U"Save selected sound as AIFF file... ||" |
| 437 | " Write selected sound to AIFF file... ||" |
| 438 | " Write sound selection to AIFF file... ||" |
| 439 | " Write selection to AIFF file...", |
| 440 | 1, menu_cb_WriteAiff, this |
| 441 | ); |
| 442 | our writeAifcButton = FunctionAreaMenu_addCommand (menu, |
| 443 | U"Save selected sound as AIFC file... ||" |
| 444 | " Write selected sound to AIFC file... ||" |
| 445 | " Write sound selection to AIFC file... ||" |
| 446 | " Write selection to AIFC file...", |
| 447 | 1, menu_cb_WriteAifc, this |
| 448 | ); |
| 449 | our writeNextSunButton = FunctionAreaMenu_addCommand (menu, |
| 450 | U"Save selected sound as NeXT/Sun file... ||" |
| 451 | " Write selected sound to NeXT/Sun file... ||" |
| 452 | " Write sound selection to NeXT/Sun file... ||" |
| 453 | " Write selection to NeXT/Sun file...", |
| 454 | 1, menu_cb_WriteNextSun, this |
| 455 | ); |
| 456 | our writeNistButton = FunctionAreaMenu_addCommand (menu, |
| 457 | U"Save selected sound as NIST file... ||" |
| 458 | " Write selected sound to NIST file... ||" |
| 459 | " Write sound selection to NIST file... ||" |
| 460 | " Write selection to NIST file...", |
| 461 | 1, menu_cb_WriteNist, this |
| 462 | ); |
| 463 | our writeFlacButton = FunctionAreaMenu_addCommand (menu, |
| 464 | U"Save selected sound as FLAC file... ||" |
| 465 | " Write selected sound to FLAC file... ||" |
| 466 | " Write sound selection to FLAC file...", |
| 467 | 1, menu_cb_WriteFlac, this |
| 468 | ); |
| 469 | } |
| 470 | |
| 471 | |
| 472 | #pragma mark - SoundArea Edit |
| 473 | |
| 474 | static void menu_cb_Copy (SoundArea me, EDITOR_ARGS) { |
| 475 | try { |
| 476 | Sound_clipboard = ( my longSound() |
| 477 | ? LongSound_extractPart (my longSound(), my startSelection(), my endSelection(), false) |
| 478 | : Sound_extractPart (my sound(), my startSelection(), my endSelection(), kSound_windowShape::RECTANGULAR, 1.0, false) |
| 479 | ); |
| 480 | } catch (MelderError) { |
| 481 | Melder_throw (U"Sound selection not copied to clipboard."); |
| 482 | } |
| 483 | } |
| 484 | static void menu_cb_Cut (SoundArea me, EDITOR_ARGS) { |
| 485 | Melder_assert (my sound()); |
| 486 | try { |
| 487 | integer first, last; |
| 488 | const integer selectionNumberOfSamples = Sampled_getWindowSamples (my sound(), |
| 489 | my startSelection(), my endSelection(), & first, & last); |
| 490 | const integer oldNumberOfSamples = my sound() -> nx; |
| 491 | const integer newNumberOfSamples = oldNumberOfSamples - selectionNumberOfSamples; |
| 492 | if (newNumberOfSamples < 1) |
| 493 | Melder_throw (U"You cannot cut all of the signal away,\n" |
| 494 | U"because you cannot create a Sound with 0 samples.\n" |
| 495 | U"You could consider using Copy instead." |
| 496 | ); |
| 497 | |
| 498 | if (selectionNumberOfSamples > 0) { |
| 499 | /* |
| 500 | Create without change. |
| 501 | */ |
| 502 | autoSound publish = Sound_create (my sound() -> ny, |
| 503 | 0.0, selectionNumberOfSamples * my sound() -> dx, |
| 504 | selectionNumberOfSamples, my sound() -> dx, 0.5 * my sound() -> dx |
| 505 | ); |
| 506 | for (integer channel = 1; channel <= my sound() -> ny; channel ++) { |
| 507 | integer j = 0; |
| 508 | for (integer i = first; i <= last; i ++) |
| 509 | publish -> z [channel] [++ j] = my sound() -> z [channel] [i]; |
| 510 | } |
| 511 | autoMAT newData = raw_MAT (my sound() -> ny, newNumberOfSamples); |
| 512 | for (integer channel = 1; channel <= my sound() -> ny; channel ++) { |
| 513 | integer j = 0; |
| 514 | for (integer i = 1; i < first; i ++) |
| 515 | newData [channel] [++ j] = my sound() -> z [channel] [i]; |
| 516 | for (integer i = last + 1; i <= oldNumberOfSamples; i ++) |
| 517 | newData [channel] [++ j] = my sound() -> z [channel] [i]; |
| 518 | Melder_assert (j == newData.ncol); |
| 519 | } |
| 520 | FunctionArea_save (me, U"Cut"); |
| 521 | /* |
| 522 | Change without error. |
| 523 | */ |
| 524 | my sound() -> xmin = 0.0; |
| 525 | my sound() -> xmax = newNumberOfSamples * my sound() -> dx; |
| 526 | my sound() -> nx = newNumberOfSamples; |
| 527 | my sound() -> x1 = 0.5 * my sound() -> dx; |
| 528 | my sound() -> z = newData.move(); |
| 529 | Sound_clipboard = publish.move(); |
| 530 | |
| 531 | /* |
| 532 | Start updating the markers of the FunctionEditor, respecting the invariants. |
| 533 | */ |
| 534 | my functionEditor() -> tmin = my sound() -> xmin; |
| 535 | my functionEditor() -> tmax = my sound() -> xmax; |
| 536 | |
| 537 | /* |
| 538 | Collapse the selection, |
| 539 | so that the Cut operation can immediately be undone by a Paste. |
| 540 | The exact position will be half-way in between two samples. |
| 541 | */ |
| 542 | const double cursor = my sound() -> xmin + (first - 1) * my sound() -> dx; |
| 543 | my setSelection (cursor, cursor); |
| 544 | |
| 545 | /* |
| 546 | Update the window. |
| 547 | */ |
| 548 | { |
| 549 | const double t1 = (first - 1) * my sound() -> dx; |
| 550 | const double t2 = last * my sound() -> dx; |
| 551 | const double windowLength = my endWindow() - my startWindow(); // > 0 |
| 552 | if (t1 > my startWindow()) |
| 553 | if (t2 < my endWindow()) |
| 554 | my functionEditor() -> startWindow -= 0.5 * (t2 - t1); |
| 555 | else |
| 556 | (void) 0; |
| 557 | else if (t2 < my endWindow()) |
| 558 | my functionEditor() -> startWindow -= t2 - t1; |
| 559 | else /* Cut overlaps entire window: centre. */ |
| 560 | my functionEditor() -> startWindow = my functionEditor() -> startSelection - 0.5 * windowLength; |
| 561 | my functionEditor() -> endWindow = my startWindow() + windowLength; // first try |
| 562 | if (my endWindow() > my tmax()) { |
| 563 | my functionEditor() -> startWindow -= my endWindow() - my tmax(); // second try |
| 564 | Melder_clipLeft (my tmin(), & my functionEditor() -> startWindow); // third try |
| 565 | my functionEditor() -> endWindow = my tmax(); // second try |
| 566 | } else if (my startWindow() < my tmin()) { |
| 567 | my functionEditor() -> endWindow -= my startWindow() - my tmin(); // second try |
| 568 | Melder_clipRight (& my functionEditor() -> endWindow, my tmax()); // third try |
| 569 | my functionEditor() -> startWindow = my tmin(); // second try |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | /* |
| 574 | Force FunctionEditor to show changes. |
| 575 | */ |
| 576 | FunctionEditor_ungroup (my sound()); |
| 577 | //FunctionEditor_windowMarksChanged (my functionEditor(), true); |
| 578 | FunctionArea_broadcastDataChanged (me); |
| 579 | } else { |
| 580 | Melder_warning (U"No samples selected."); |
| 581 | } |
| 582 | } catch (MelderError) { |
| 583 | Melder_throw (U"Sound selection not cut to clipboard."); |
| 584 | } |
| 585 | } |
| 586 | static void paste (SoundArea me, int where) { |
| 587 | Melder_assert (my sound()); |
| 588 | if (! Sound_clipboard) { |
| 589 | Melder_warning (U"Clipboard is empty; nothing pasted."); |
| 590 | return; |
| 591 | } |
| 592 | Melder_require (Sound_clipboard -> ny == my sound() -> ny, |
| 593 | U"Cannot paste, because\n" |
| 594 | U"the number of channels of the clipboard is not equal to\n" |
| 595 | U"the number of channels of the edited sound." |
| 596 | ); |
| 597 | Melder_require (Sound_clipboard -> dx == my sound() -> dx, |
| 598 | U"Cannot paste, because\n" |
| 599 | U"the sampling frequency of the clipboard is not equal to\n" |
| 600 | U"the sampling frequency of the edited sound." |
| 601 | ); |
| 602 | const integer oldNumberOfSamples = my sound() -> nx; |
| 603 | const integer leftSample = Melder_clipped ( |
| 604 | 0_integer, |
| 605 | Sampled_xToLowIndex (my sound(), where == 3 ? my endSelection() : my startSelection()), |
| 606 | oldNumberOfSamples |
| 607 | ); |
| 608 | const integer rightSample = Melder_clipped ( |
| 609 | 0_integer, |
| 610 | Sampled_xToLowIndex (my sound(), where == 1 ? my startSelection() : my endSelection()), |
| 611 | oldNumberOfSamples |
| 612 | ); |
| 613 | const integer newNumberOfSamples = |
| 614 | oldNumberOfSamples + Sound_clipboard -> nx - (rightSample - leftSample); |
| 615 | /* |
| 616 | Check without change. |
| 617 | */ |
| 618 | autoMAT newData = raw_MAT (my sound() -> ny, newNumberOfSamples); |
| 619 | for (integer channel = 1; channel <= my sound() -> ny; channel ++) { |
| 620 | integer j = 0; |
| 621 | for (integer i = 1; i <= leftSample; i ++) |
| 622 | newData [channel] [++ j] = my sound() -> z [channel] [i]; |
| 623 | for (integer i = 1; i <= Sound_clipboard -> nx; i ++) |
| 624 | newData [channel] [++ j] = Sound_clipboard -> z [channel] [i]; |
| 625 | for (integer i = rightSample + 1; i <= oldNumberOfSamples; i ++) |
| 626 | newData [channel] [++ j] = my sound() -> z [channel] [i]; |
| 627 | Melder_assert (j == newData.ncol); |
| 628 | } |
| 629 | FunctionArea_save (me, where == 1 ? U"Paste before" : where == 2 ? U"Paste over" : U"Paste after"); |
| 630 | /* |
| 631 | Change without error. |
| 632 | */ |
| 633 | my sound() -> xmin = 0.0; |
| 634 | my sound() -> xmax = newNumberOfSamples * my sound() -> dx; |
| 635 | my sound() -> nx = newNumberOfSamples; |
| 636 | my sound() -> x1 = 0.5 * my sound() -> dx; |
| 637 | my sound() -> z = newData.move(); |
| 638 | |
| 639 | /* |
| 640 | Start updating the markers of the FunctionEditor, respecting the invariants. |
| 641 | */ |
| 642 | my functionEditor() -> tmin = my sound() -> xmin; |
| 643 | my functionEditor() -> tmax = my sound() -> xmax; |
| 644 | Melder_clipLeft (my tmin(), & my functionEditor() -> startWindow); |
| 645 | Melder_clipRight (& my functionEditor() -> endWindow, my tmax()); |
| 646 | my setSelection (leftSample * my sound() -> dx, (leftSample + Sound_clipboard -> nx) * my sound() -> dx); |
| 647 | |
| 648 | /* |
| 649 | Force FunctionEditor to show changes. |
| 650 | */ |
| 651 | FunctionEditor_ungroup (my sound()); |
| 652 | FunctionEditor_windowMarksChanged (my functionEditor(), true); |
| 653 | FunctionArea_broadcastDataChanged (me); |
| 654 | } |
| 655 | static void menu_cb_PasteBefore (SoundArea me, EDITOR_ARGS) { |
| 656 | paste (me, 1); |
| 657 | } |
| 658 | static void menu_cb_PasteOver (SoundArea me, EDITOR_ARGS) { |
| 659 | paste (me, 2); |
| 660 | } |
| 661 | static void menu_cb_PasteAfter (SoundArea me, EDITOR_ARGS) { |
| 662 | paste (me, 3); |
| 663 | } |
| 664 | void structSoundArea :: v_createMenuItems_edit (EditorMenu menu) { |
| 665 | FunctionAreaMenu_addCommand (menu, U"-- cut copy paste --", 0, nullptr, this); |
| 666 | const bool weMayUseShortcuts = ! our functionEditor() -> textArea; |
| 667 | if (our editable()) |
| 668 | our cutButton = FunctionAreaMenu_addCommand (menu, U"Cut", 'X' * weMayUseShortcuts, |
| 669 | menu_cb_Cut, this); |
| 670 | our copyButton = FunctionAreaMenu_addCommand (menu, U"Copy selection to Sound clipboard", 'C' * weMayUseShortcuts, |
| 671 | menu_cb_Copy, this); |
| 672 | if (our editable()) { |
| 673 | our pasteBeforeButton = FunctionAreaMenu_addCommand (menu, U"Paste before selection", 0, |
| 674 | menu_cb_PasteBefore, this); |
| 675 | our pasteOverButton = FunctionAreaMenu_addCommand (menu, U"Paste over selection", (GuiMenu_COMMAND_EXTRA | 'V') * weMayUseShortcuts, |
| 676 | menu_cb_PasteOver, this); |
| 677 | our pasteAfterButton = FunctionAreaMenu_addCommand (menu, U"Paste after selection", 'V' * weMayUseShortcuts, |
| 678 | menu_cb_PasteAfter, this); |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | |
| 683 | #pragma mark - SoundArea Modify |
| 684 | |
| 685 | static void menu_cb_SetSelectionToZero (SoundArea me, EDITOR_ARGS) { |
| 686 | Melder_assert (my sound()); |
| 687 | integer first, last; |
| 688 | Sampled_getWindowSamples (my sound(), my startSelection(), my endSelection(), & first, & last); |
| 689 | FunctionArea_save (me, U"Set to zero"); |
| 690 | my sound() -> z.verticalBand (first, last) <<= 0.0; |
| 691 | FunctionArea_broadcastDataChanged (me); |
| 692 | } |
| 693 | static void menu_cb_ReverseSelection (SoundArea me, EDITOR_ARGS) { |
| 694 | Melder_assert (my sound()); |
| 695 | FunctionArea_save (me, U"Reverse selection"); |
| 696 | Sound_reverse (my sound(), my startSelection(), my endSelection()); |
| 697 | FunctionArea_broadcastDataChanged (me); |
| 698 | } |
| 699 | |
| 700 | |
| 701 | #pragma mark - SoundArea Settings |
| 702 | |
| 703 | static void menu_cb_soundMuteChannels (SoundArea me, EDITOR_ARGS) { |
| 704 | EDITOR_FORM (U"Mute channels", nullptr) |
| 705 | NATURAL1VECTOR (channels, U"Channels to mute", WHITESPACE_SEPARATED_, U"2") |
| 706 | EDITOR_OK |
| 707 | EDITOR_DO |
| 708 | const integer numberOfChannels = my soundOrLongSound() -> ny; |
| 709 | Melder_assert (my muteChannels.size == numberOfChannels); |
| 710 | for (integer ichan = 1; ichan <= numberOfChannels; ichan ++) |
| 711 | my muteChannels [ichan] = false; |
| 712 | for (integer ichan = 1; ichan <= channels.size; ichan ++) |
| 713 | if (channels [ichan] >= 1 && channels [ichan] <= numberOfChannels) |
| 714 | my muteChannels [channels [ichan]] = true; |
| 715 | FunctionEditor_redraw (my functionEditor()); |
| 716 | EDITOR_END |
| 717 | } |
| 718 | |
| 719 | |
| 720 | #pragma mark - SoundArea Query |
| 721 | |
| 722 | static void INFO_DATA__SoundInfo (SoundArea me, EDITOR_ARGS) { |
| 723 | INFO_DATA |
| 724 | Melder_assert (me); |
| 725 | if (! Thing_isa (me, classSoundArea)) |
| 726 | Melder_crash (U"Expected a SoundArea but found a ", Thing_className (me)); |
| 727 | Melder_assert (my data()); |
| 728 | Melder_assert (my function()); |
| 729 | Melder_assert (my soundOrLongSound()); |
| 730 | if (! Thing_isa (my soundOrLongSound(), classSampledXY)) |
| 731 | Melder_crash (U"Expected a SoundArea but found a ", Thing_className (my soundOrLongSound())); |
| 732 | Melder_assert (my sound()); |
| 733 | Thing_info (my sound()); |
| 734 | INFO_DATA_END |
| 735 | } |
| 736 | static void INFO_DATA__LongSoundInfo (SoundArea me, EDITOR_ARGS) { |
| 737 | INFO_DATA |
| 738 | Thing_info (my longSound()); |
| 739 | INFO_DATA_END |
| 740 | } |
| 741 | enum { |
| 742 | SoundArea_PART_CURSOR = 1, |
| 743 | SoundArea_PART_SELECTION = 2 |
| 744 | }; |
| 745 | static int makeQueriable (SoundArea me, bool allowCursor, double *tmin, double *tmax) { |
| 746 | if (my startSelection() == my endSelection()) { |
| 747 | if (allowCursor) { |
| 748 | *tmin = *tmax = my startSelection(); |
| 749 | return SoundArea_PART_CURSOR; |
| 750 | } else { |
| 751 | Melder_throw (U"Make a selection first."); |
| 752 | } |
| 753 | } else if (my startSelection() < my startWindow() || my endSelection() > my endWindow()) { |
| 754 | Melder_throw (U"Command ambiguous: a part of the selection (", my startSelection(), U", ", my endSelection(), U") " |
| 755 | U"is outside of the window (", my startWindow(), U", ", my endWindow(), U"). " |
| 756 | U"Either zoom or re-select."); |
| 757 | } |
| 758 | *tmin = my startSelection(); |
| 759 | *tmax = my endSelection(); |
| 760 | return SoundArea_PART_SELECTION; |
| 761 | } |
| 762 | static void INFO_DATA__getAmplitudes (SoundArea me, EDITOR_ARGS) { |
| 763 | INFO_DATA |
| 764 | double tmin, tmax; |
| 765 | const int part = makeQueriable (me, true, & tmin, & tmax); |
| 766 | if (! my sound()) |
| 767 | Melder_throw (U"No Sound object is visible (a LongSound cannot be queried)."); |
| 768 | MelderInfo_open (); |
| 769 | if (part == SoundArea_PART_CURSOR) |
| 770 | for (integer ichan = 1; ichan <= my sound() -> ny; ichan ++) |
| 771 | MelderInfo_writeLine (Vector_getValueAtX (my sound(), 0.5 * (my startSelection() + my endSelection()), ichan, kVector_valueInterpolation :: SINC70), |
| 772 | U" (interpolated amplitude at CURSOR in channel ", ichan, U")"); |
| 773 | else |
| 774 | for (integer ichan = 1; ichan <= my sound() -> ny; ichan ++) |
| 775 | MelderInfo_writeLine (Sampled_getMean (my sound(), my startSelection(), my endSelection(), ichan, 0, true), |
| 776 | U" (mean amplitude in SELECTION in channel ", ichan, U")"); |
| 777 | MelderInfo_close (); |
| 778 | INFO_DATA_END |
| 779 | } |
| 780 | |
| 781 | |
| 782 | #pragma mark - SoundArea View vertical |
| 783 | |
| 784 | static void menu_cb_soundScaling (SoundArea me, EDITOR_ARGS) { |
| 785 | EDITOR_FORM (U"Sound scaling", nullptr) |
| 786 | OPTIONMENU_ENUM (kSoundArea_scalingStrategy, scalingStrategy, |
| 787 | U"Scaling strategy", my default_scalingStrategy()) |
| 788 | COMMENT (U"For \"fixed height\":") |
| 789 | POSITIVE (height, U"Height", my default_scaling_height()) |
| 790 | COMMENT (U"For \"fixed range\":") |
| 791 | REAL (minimum, U"Minimum", my default_scaling_minimum()) |
| 792 | REAL (maximum, U"Maximum", my default_scaling_maximum()) |
| 793 | EDITOR_OK |
| 794 | SET_ENUM (scalingStrategy, kSoundArea_scalingStrategy, my instancePref_scalingStrategy()) |
| 795 | SET_REAL (height, my instancePref_scaling_height()) |
| 796 | SET_REAL (minimum, my instancePref_scaling_minimum()) |
| 797 | SET_REAL (maximum, my instancePref_scaling_maximum()) |
| 798 | EDITOR_DO |
| 799 | my setInstancePref_scalingStrategy (scalingStrategy) ; |
| 800 | my setInstancePref_scaling_height (height); |
| 801 | my setInstancePref_scaling_minimum (minimum); |
| 802 | my setInstancePref_scaling_maximum (maximum); |
| 803 | FunctionEditor_redraw (my functionEditor()); |
| 804 | EDITOR_END |
| 805 | } |
| 806 | |
| 807 | |
| 808 | #pragma mark - SoundArea Select |
| 809 | |
| 810 | static void menu_cb_MoveStartOfSelectionToNearestZeroCrossing (SoundArea me, EDITOR_ARGS) { |
| 811 | const double zero = Sound_getNearestZeroCrossing (my sound(), my startSelection(), 1); // STEREO BUG |
| 812 | if (isdefined (zero)) { |
| 813 | my setSelection (zero, my endSelection()); |
| 814 | Melder_assert (isdefined (my startSelection())); // precondition of FunctionEditor_selectionMarksChanged() |
| 815 | FunctionEditor_selectionMarksChanged (my functionEditor()); |
| 816 | } |
| 817 | } |
| 818 | static void menu_cb_MoveCursorToNearestZeroCrossing (SoundArea me, EDITOR_ARGS) { |
| 819 | const double zero = Sound_getNearestZeroCrossing (my sound(), 0.5 * (my startSelection() + my endSelection()), 1); // STEREO BUG |
| 820 | if (isdefined (zero)) { |
| 821 | my setSelection (zero, zero); |
| 822 | Melder_assert (isdefined (my startSelection())); // precondition of FunctionEditor_selectionMarksChanged() |
| 823 | FunctionEditor_selectionMarksChanged (my functionEditor()); |
| 824 | } |
| 825 | } |
| 826 | static void menu_cb_MoveEndOfSelectionToNearestZeroCrossing (SoundArea me, EDITOR_ARGS) { |
| 827 | const double zero = Sound_getNearestZeroCrossing (my sound(), my endSelection(), 1); // STEREO BUG |
| 828 | if (isdefined (zero)) { |
| 829 | my setSelection (my startSelection(), zero); |
| 830 | Melder_assert (isdefined (my startSelection())); // precondition of FunctionEditor_selectionMarksChanged() |
| 831 | FunctionEditor_selectionMarksChanged (my functionEditor()); |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | |
| 836 | #pragma mark - SoundArea Draw |
| 837 | |
| 838 | static void menu_cb_DrawVisibleSound (SoundArea me, EDITOR_ARGS) { |
| 839 | EDITOR_FORM (U"Draw visible sound", nullptr) |
| 840 | my v_form_pictureWindow (cmd); |
| 841 | COMMENT (U"Sound:") |
| 842 | BOOLEAN (preserveTimes, U"Preserve times", my default_picture_preserveTimes()); |
| 843 | REAL (bottom, U"left Vertical range", my default_picture_bottom()) |
| 844 | REAL (top, U"right Vertical range", my default_picture_top()) |
| 845 | my v_form_pictureMargins (cmd); |
| 846 | my v_form_pictureSelection (cmd); |
| 847 | BOOLEAN (garnish, U"Garnish", my default_picture_garnish()); |
| 848 | EDITOR_OK |
| 849 | my v_ok_pictureWindow (cmd); |
| 850 | SET_BOOLEAN (preserveTimes, my classPref_picture_preserveTimes()) |
| 851 | SET_REAL (bottom, my classPref_picture_bottom()) |
| 852 | SET_REAL (top, my classPref_picture_top()) |
| 853 | my v_ok_pictureMargins (cmd); |
| 854 | my v_ok_pictureSelection (cmd); |
| 855 | SET_BOOLEAN (garnish, my classPref_picture_garnish()) |
| 856 | EDITOR_DO |
| 857 | my v_do_pictureWindow (cmd); |
| 858 | my setClassPref_picture_preserveTimes (preserveTimes); |
| 859 | my setClassPref_picture_bottom (bottom); |
| 860 | my setClassPref_picture_top (top); |
| 861 | my v_do_pictureMargins (cmd); |
| 862 | my v_do_pictureSelection (cmd); |
| 863 | my setClassPref_picture_garnish (garnish); |
| 864 | if (! my soundOrLongSound()) |
| 865 | Melder_throw (U"There is no sound to draw."); |
| 866 | autoSound publish = my longSound() ? |
| 867 | LongSound_extractPart (my longSound(), my startWindow(), my endWindow(), preserveTimes) : |
| 868 | Sound_extractPart (my sound(), my startWindow(), my endWindow(), kSound_windowShape::RECTANGULAR, 1.0, preserveTimes |
| 869 | ); |
| 870 | DataGui_openPraatPicture (me); |
| 871 | Sound_draw (publish.get(), my pictureGraphics(), 0.0, 0.0, bottom, top, garnish, U"Curve"); |
| 872 | FunctionArea_garnishPicture (me); |
| 873 | DataGui_closePraatPicture (me); |
| 874 | EDITOR_END |
| 875 | } |
| 876 | static void menu_cb_DrawSelectedSound (SoundArea me, EDITOR_ARGS) { |
| 877 | EDITOR_FORM (U"Draw selected sound", nullptr) |
| 878 | my v_form_pictureWindow (cmd); |
| 879 | COMMENT (U"Sound:") |
| 880 | BOOLEAN (preserveTimes, U"Preserve times", my default_picture_preserveTimes()); |
| 881 | REAL (bottom, U"left Vertical range", my default_picture_bottom()); |
| 882 | REAL (top, U"right Vertical range", my default_picture_top()); |
| 883 | my v_form_pictureMargins (cmd); |
| 884 | BOOLEAN (garnish, U"Garnish", my default_picture_garnish()); |
| 885 | EDITOR_OK |
| 886 | my v_ok_pictureWindow (cmd); |
| 887 | SET_BOOLEAN (preserveTimes, my classPref_picture_preserveTimes()); |
| 888 | SET_REAL (bottom, my classPref_picture_bottom()); |
| 889 | SET_REAL (top, my classPref_picture_top()); |
| 890 | my v_ok_pictureMargins (cmd); |
| 891 | SET_BOOLEAN (garnish, my classPref_picture_garnish()); |
| 892 | EDITOR_DO |
| 893 | my v_do_pictureWindow (cmd); |
| 894 | my setClassPref_picture_preserveTimes (preserveTimes); |
| 895 | my setClassPref_picture_bottom (bottom); |
| 896 | my setClassPref_picture_top (top); |
| 897 | my v_do_pictureMargins (cmd); |
| 898 | my setClassPref_picture_garnish (garnish); |
| 899 | if (! my soundOrLongSound()) |
| 900 | Melder_throw (U"There is no sound to draw."); |
| 901 | autoSound publish = my longSound() ? |
| 902 | LongSound_extractPart (my longSound(), my startSelection(), my endSelection(), preserveTimes) : |
| 903 | Sound_extractPart (my sound(), my startSelection(), my endSelection(), |
| 904 | kSound_windowShape::RECTANGULAR, 1.0, preserveTimes |
| 905 | ); |
| 906 | DataGui_openPraatPicture (me); |
| 907 | Sound_draw (publish.get(), my pictureGraphics(), 0.0, 0.0, bottom, top, garnish, U"Curve"); |
| 908 | DataGui_closePraatPicture (me); |
| 909 | EDITOR_END |
| 910 | } |
| 911 | |
| 912 | |
| 913 | #pragma mark - SoundArea Extract |
| 914 | |
| 915 | static autoSound do_ExtractSelectedSound (SoundArea me, bool preserveTimes) { |
| 916 | if (my endSelection() <= my startSelection()) |
| 917 | Melder_throw (U"No selection."); |
| 918 | if (my longSound()) |
| 919 | return LongSound_extractPart (my longSound(), my startSelection(), my endSelection(), preserveTimes); |
| 920 | else if (my sound()) |
| 921 | return Sound_extractPart (my sound(), my startSelection(), my endSelection(), |
| 922 | kSound_windowShape::RECTANGULAR, 1.0, preserveTimes); |
| 923 | Melder_crash (U"No Sound or LongSound available."); |
| 924 | return autoSound(); // never reached |
| 925 | } |
| 926 | static void CONVERT_DATA_TO_ONE__ExtractSelectedSound_timeFromZero (SoundArea me, EDITOR_ARGS) { |
| 927 | CONVERT_DATA_TO_ONE |
| 928 | autoSound result = do_ExtractSelectedSound (me, false); |
| 929 | CONVERT_DATA_TO_ONE_END (U"untitled") |
| 930 | } |
| 931 | static void CONVERT_DATA_TO_ONE__ExtractSelectedSound_preserveTimes (SoundArea me, EDITOR_ARGS) { |
| 932 | CONVERT_DATA_TO_ONE |
| 933 | autoSound result = do_ExtractSelectedSound (me, true); |
| 934 | CONVERT_DATA_TO_ONE_END (U"untitled") |
| 935 | } |
| 936 | static void CONVERT_DATA_TO_ONE__ExtractSelectedSound_windowed (SoundArea me, EDITOR_ARGS) { |
| 937 | EDITOR_FORM (U"Extract selected sound (windowed)", U"Extract selected sound (windowed)...") |
| 938 | WORD (name, U"Name", U"slice") |
| 939 | OPTIONMENU_ENUM (kSound_windowShape, windowShape, U"Window shape", my default_extract_windowShape()) |
| 940 | POSITIVE (relativeWidth, U"Relative width", my default_extract_relativeWidth()) |
| 941 | BOOLEAN (preserveTimes, U"Preserve times", my default_extract_preserveTimes()) |
| 942 | EDITOR_OK |
| 943 | SET_ENUM (windowShape, kSound_windowShape, my classPref_extract_windowShape()) |
| 944 | SET_REAL (relativeWidth, my classPref_extract_relativeWidth()) |
| 945 | SET_BOOLEAN (preserveTimes, my classPref_extract_preserveTimes()) |
| 946 | EDITOR_DO |
| 947 | Melder_assert (my sound()); // no LongSound |
| 948 | CONVERT_DATA_TO_ONE |
| 949 | my setClassPref_extract_windowShape (windowShape); |
| 950 | my setClassPref_extract_relativeWidth (relativeWidth); |
| 951 | my setClassPref_extract_preserveTimes (preserveTimes); |
| 952 | autoSound result = Sound_extractPart (my sound(), my startSelection(), my endSelection(), |
| 953 | windowShape, relativeWidth, preserveTimes); |
| 954 | CONVERT_DATA_TO_ONE_END (name) |
| 955 | EDITOR_END |
| 956 | } |
| 957 | static void CONVERT_DATA_TO_ONE__ExtractSelectedSoundForOverlap (SoundArea me, EDITOR_ARGS) { |
| 958 | EDITOR_FORM (U"Extract selected sound for overlap)", nullptr) |
| 959 | WORD (name, U"Name", U"slice") |
| 960 | POSITIVE (overlap, U"Overlap (s)", my default_extract_overlap()) |
| 961 | EDITOR_OK |
| 962 | SET_REAL (overlap, my classPref_extract_overlap()) |
| 963 | EDITOR_DO |
| 964 | Melder_assert (my sound()); // no LongSound |
| 965 | CONVERT_DATA_TO_ONE |
| 966 | my setClassPref_extract_overlap (overlap); |
| 967 | autoSound result = Sound_extractPartForOverlap (my sound(), my startSelection(), my endSelection(), overlap); |
| 968 | CONVERT_DATA_TO_ONE_END (name) |
| 969 | EDITOR_END |
| 970 | } |
| 971 | |
| 972 | |
| 973 | #pragma mark - SoundArea menus |
| 974 | |
| 975 | void structSoundArea :: v_createMenus () { |
| 976 | EditorMenu menu = Editor_addMenu (our functionEditor(), U"Sound", 0); |
| 977 | |
| 978 | FunctionAreaMenu_addCommand (menu, U"Sound scaling...", |
| 979 | 0, menu_cb_soundScaling, this); |
| 980 | FunctionAreaMenu_addCommand (menu, U"Mute channels...", |
| 981 | 0, menu_cb_soundMuteChannels, this); |
| 982 | |
| 983 | if (our editable()) { |
| 984 | FunctionAreaMenu_addCommand (menu, U"- Modify sound:", 0, nullptr, this); |
| 985 | our zeroButton = FunctionAreaMenu_addCommand (menu, U"Set selection to zero", |
| 986 | 1, menu_cb_SetSelectionToZero, this); |
| 987 | our reverseButton = FunctionAreaMenu_addCommand (menu, U"Reverse selection", |
| 988 | 'R' + GuiMenu_DEPTH_1, menu_cb_ReverseSelection, this); |
| 989 | } |
| 990 | |
| 991 | FunctionAreaMenu_addCommand (menu, U"- Query sound:", 0, nullptr, this); |
| 992 | if (Thing_isa (this, classLongSoundArea)) |
| 993 | FunctionAreaMenu_addCommand (menu, U"Info on whole LongSound || LongSound info", |
| 994 | 1, INFO_DATA__LongSoundInfo, this); |
| 995 | else |
| 996 | FunctionAreaMenu_addCommand (menu, U"Info on whole Sound || Sound info", |
| 997 | 1, INFO_DATA__SoundInfo, this); |
| 998 | if (! Thing_isa (this, classLongSoundArea)) { |
| 999 | FunctionAreaMenu_addCommand (menu, U"Get amplitude(s)", |
| 1000 | 1, INFO_DATA__getAmplitudes, this); |
| 1001 | } |
| 1002 | |
| 1003 | if (! Thing_isa (this, classLongSoundArea)) { |
| 1004 | FunctionAreaMenu_addCommand (menu, U"- Select by sound:", 0, nullptr, this); |
| 1005 | FunctionAreaMenu_addCommand (menu, U"Move start of selection to nearest zero crossing || Move begin of selection to nearest zero crossing", |
| 1006 | ',' + GuiMenu_DEPTH_1, menu_cb_MoveStartOfSelectionToNearestZeroCrossing, this); |
| 1007 | FunctionAreaMenu_addCommand (menu, U"Move cursor to nearest zero crossing", |
| 1008 | '0' + GuiMenu_DEPTH_1, menu_cb_MoveCursorToNearestZeroCrossing, this); |
| 1009 | FunctionAreaMenu_addCommand (menu, U"Move end of selection to nearest zero crossing", |
| 1010 | '.' + GuiMenu_DEPTH_1, menu_cb_MoveEndOfSelectionToNearestZeroCrossing, this); |
| 1011 | } |
| 1012 | |
| 1013 | FunctionAreaMenu_addCommand (menu, U"- Draw sound to picture window:", 0, nullptr, this); |
| 1014 | FunctionAreaMenu_addCommand (menu, U"Draw visible sound...", |
| 1015 | 1, menu_cb_DrawVisibleSound, this); |
| 1016 | our drawButton = FunctionAreaMenu_addCommand (menu, U"Draw selected sound...", |
| 1017 | 1, menu_cb_DrawSelectedSound, this); |
| 1018 | |
| 1019 | FunctionAreaMenu_addCommand (menu, U"- Extract sound to objects window:", 0, nullptr, this); |
| 1020 | our publishPreserveButton = FunctionAreaMenu_addCommand (menu, |
| 1021 | U"Extract selected sound (preserve times) ||" |
| 1022 | " Extract sound selection (preserve times) ||" |
| 1023 | " Extract selection (preserve times)", |
| 1024 | 1, CONVERT_DATA_TO_ONE__ExtractSelectedSound_preserveTimes, this |
| 1025 | ); |
| 1026 | our publishButton = FunctionAreaMenu_addCommand (menu, |
| 1027 | U"Extract selected sound (time from 0) ||" |
| 1028 | " Extract sound selection (time from 0) ||" |
| 1029 | " Extract selection (time from 0) ||" |
| 1030 | " Extract selection", |
| 1031 | 1, CONVERT_DATA_TO_ONE__ExtractSelectedSound_timeFromZero, this |
| 1032 | ); |
| 1033 | if (! Thing_isa (this, classLongSoundArea)) { |
| 1034 | our publishWindowButton = FunctionAreaMenu_addCommand (menu, |
| 1035 | U"Extract selected sound (windowed)... ||" |
| 1036 | " Extract windowed sound selection... ||" |
| 1037 | " Extract windowed selection...", |
| 1038 | 1, CONVERT_DATA_TO_ONE__ExtractSelectedSound_windowed, this |
| 1039 | ); |
| 1040 | our publishOverlapButton = FunctionAreaMenu_addCommand (menu, |
| 1041 | U"Extract selected sound for overlap...", |
| 1042 | 1, CONVERT_DATA_TO_ONE__ExtractSelectedSoundForOverlap, this |
| 1043 | ); |
| 1044 | } |
| 1045 | } |
| 1046 | void structSoundArea :: v_updateMenuItems () { |
| 1047 | integer first, last; |
| 1048 | const integer selectedSamples = Sampled_getWindowSamples (our soundOrLongSound(), |
| 1049 | our startSelection(), our endSelection(), & first, & last); |
| 1050 | if (our drawButton) { |
| 1051 | GuiThing_setSensitive (our drawButton, selectedSamples != 0); |
| 1052 | GuiThing_setSensitive (our publishButton, selectedSamples != 0); |
| 1053 | GuiThing_setSensitive (our publishPreserveButton, selectedSamples != 0); |
| 1054 | if (our publishWindowButton) |
| 1055 | GuiThing_setSensitive (our publishWindowButton, selectedSamples != 0); |
| 1056 | if (our publishOverlapButton) |
| 1057 | GuiThing_setSensitive (our publishOverlapButton, selectedSamples != 0); |
| 1058 | } |
| 1059 | GuiThing_setSensitive (our writeWavButton, selectedSamples != 0); |
| 1060 | if (our saveAs24BitWavButton) |
| 1061 | GuiThing_setSensitive (our saveAs24BitWavButton, selectedSamples != 0); |
| 1062 | if (our saveAs32BitWavButton) |
| 1063 | GuiThing_setSensitive (our saveAs32BitWavButton, selectedSamples != 0); |
| 1064 | GuiThing_setSensitive (our writeAiffButton, selectedSamples != 0); |
| 1065 | GuiThing_setSensitive (our writeAifcButton, selectedSamples != 0); |
| 1066 | GuiThing_setSensitive (our writeNextSunButton, selectedSamples != 0); |
| 1067 | GuiThing_setSensitive (our writeNistButton, selectedSamples != 0); |
| 1068 | GuiThing_setSensitive (our writeFlacButton, selectedSamples != 0); |
| 1069 | |
| 1070 | if (our sound()) { |
| 1071 | if (our editable()) |
| 1072 | GuiThing_setSensitive (our cutButton, selectedSamples != 0 && selectedSamples < our sound() -> nx); |
| 1073 | GuiThing_setSensitive (our copyButton, selectedSamples != 0); |
| 1074 | if (our editable()) { |
| 1075 | GuiThing_setSensitive (our zeroButton, selectedSamples != 0); |
| 1076 | GuiThing_setSensitive (our reverseButton, selectedSamples != 0); |
| 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | /* End of file SoundArea.cpp */ |