| 1 |
#ifndef _SoundArea_h_ |
| 2 |
#define _SoundArea_h_ |
| 3 |
/* SoundArea.h |
| 4 |
* |
| 5 |
* Copyright (C) 2022,2023 Paul Boersma |
| 6 |
* |
| 7 |
* This code is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or (at |
| 10 |
* your option) any later version. |
| 11 |
* |
| 12 |
* This code is distributed in the hope that it will be useful, but |
| 13 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 |
* See the GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "FunctionArea.h" |
| 22 |
#include "Sound.h" |
| 23 |
#include "LongSound.h" |
| 24 |
#include "SoundArea_enums.h" |
| 25 |
|
| 26 |
/* |
| 27 |
One derived data cache, namely for global extrema. |
| 28 |
TODO: add a second cache, namely for channel extrema (or profile the scrolling speed). |
| 29 |
*/ |
| 30 |
struct SoundArea_GlobalExtremaCache { |
| 31 |
void get (Sound sound, LongSound longSound, double *out_minimum, double *out_maximum) { |
| 32 |
if (! _valid) { |
| 33 |
_compute (sound, longSound); |
| 34 |
_valid = true; |
| 35 |
} |
| 36 |
*out_minimum = _minimum; |
| 37 |
*out_maximum = _maximum; |
| 38 |
} |
| 39 |
void invalidate () { |
| 40 |
_valid = false; |
| 41 |
} |
| 42 |
private: |
| 43 |
bool _valid; |
| 44 |
double _minimum, _maximum; |
| 45 |
void _compute (Sound sound, LongSound longSound) { |
| 46 |
if (sound) { |
| 47 |
Matrix_getWindowExtrema (sound, 1, sound -> nx, 1, sound -> ny, & _minimum, & _maximum); |
| 48 |
} else { |
| 49 |
Melder_assert (longSound); |
| 50 |
_minimum = -1.0; |
| 51 |
_maximum = +1.0; |
| 52 |
} |
| 53 |
} |
| 54 |
}; |
| 55 |
|
| 56 |
Thing_define (SoundArea, FunctionArea) { |
| 57 |
/* |
| 58 |
Accessors. |
| 59 |
*/ |
| 60 |
SampledXY soundOrLongSound() const { return static_cast <SampledXY> (our function()); } |
| 61 |
Sound sound() const { |
| 62 |
return our soundOrLongSound() && Thing_isa (our soundOrLongSound(), classSound) ? (Sound) our soundOrLongSound() : nullptr; |
| 63 |
} |
| 64 |
LongSound longSound() const { |
| 65 |
return our soundOrLongSound() && Thing_isa (our soundOrLongSound(), classLongSound) ? (LongSound) our soundOrLongSound() : nullptr; |
| 66 |
} |
| 67 |
|
| 68 |
/* |
| 69 |
Derived data cache: global extrema. |
| 70 |
*/ |
| 71 |
private: |
| 72 |
SoundArea_GlobalExtremaCache _globalExtremaCache; |
| 73 |
public: |
| 74 |
void getGlobalExtrema (double *out_minimum, double *out_maximum) { |
| 75 |
_globalExtremaCache. get (our sound(), our longSound(), out_minimum, out_maximum); |
| 76 |
} |
| 77 |
/* |
| 78 |
Manage all derived data caches. |
| 79 |
*/ |
| 80 |
protected: |
| 81 |
void v_invalidateAllDerivedDataCaches () override { |
| 82 |
_globalExtremaCache. invalidate (); |
| 83 |
SoundArea_Parent :: v_invalidateAllDerivedDataCaches (); |
| 84 |
} |
| 85 |
|
| 86 |
/* |
| 87 |
Auxiliary data: channelOffset. |
| 88 |
|
| 89 |
For multi-channel sounds, Praat will never show more than 8 channels at a time: |
| 90 |
if there are more than 8 channels, the user can scroll through them, in groups of 8. |
| 91 |
The value of channelOffset is determined as follows: |
| 92 |
- if there are at most 8 channels, channelOffset will always be 0; |
| 93 |
- if there are more than 8 channels, channelOffset will be an integer multiple of 8; |
| 94 |
for instance, if there are 30 channels, channelOffset can have the following values: |
| 95 |
- 0 to show channels 1 throguh 8 (the first 8 channels); |
| 96 |
- 8 to show channels 9 through 16 (the second 8 channels); |
| 97 |
- 16 to show channels 17 through 24 (the third 8 channels); |
| 98 |
- 24 to show channels 25 through 30 (the last 6 channels). |
| 99 |
At initialization, channelOffset stays at zero, i.e. only the first 8 channels are shown. |
| 100 |
*/ |
| 101 |
public: |
| 102 |
integer channelOffset; |
| 103 |
private: |
| 104 |
void _computeChannelOffset () { |
| 105 |
Melder_assert (our soundOrLongSound()); |
| 106 |
Melder_assert (our soundOrLongSound() -> ny > 0); |
| 107 |
Melder_clip (0_integer, & our channelOffset, (our soundOrLongSound() -> ny - 1) / 8 * 8); // works correctly even during initialization (offset will stay 0) |
| 108 |
} |
| 109 |
/* |
| 110 |
Auxiliary data: muteChannels. |
| 111 |
|
| 112 |
At initialization, no channels are muted. |
| 113 |
*/ |
| 114 |
public: |
| 115 |
autoBOOLVEC muteChannels; |
| 116 |
private: |
| 117 |
void _computeMuteChannels () { |
| 118 |
Melder_assert (our soundOrLongSound() && our soundOrLongSound() -> ny > 0); |
| 119 |
if (our muteChannels.size != our soundOrLongSound() -> ny) // condition works correctly even during initialization (when size is still 0, but ny is not) |
| 120 |
our muteChannels = zero_BOOLVEC (our soundOrLongSound() -> ny); |
| 121 |
} |
| 122 |
/* |
| 123 |
Maintain all auxiliary data. |
| 124 |
*/ |
| 125 |
protected: |
| 126 |
void v_computeAuxiliaryData () override { |
| 127 |
SoundArea_Parent :: v_computeAuxiliaryData (); |
| 128 |
_computeChannelOffset (); |
| 129 |
_computeMuteChannels (); |
| 130 |
} |
| 131 |
|
| 132 |
public: |
| 133 |
virtual conststring32 v_getChannelName (integer /* channelNumber */) { return nullptr; } |
| 134 |
|
| 135 |
protected: |
| 136 |
void v_drawInside () |
| 137 |
override; |
| 138 |
public: |
| 139 |
void v1_info () |
| 140 |
override; |
| 141 |
bool v_mouse (GuiDrawingArea_MouseEvent event, double x_world, double localY_fraction) |
| 142 |
override; |
| 143 |
void v_createMenuItems_save (EditorMenu menu) |
| 144 |
override; |
| 145 |
void v_createMenuItems_edit (EditorMenu menu) |
| 146 |
override; |
| 147 |
void v_createMenus () |
| 148 |
override; |
| 149 |
void v_updateMenuItems () |
| 150 |
override; |
| 151 |
GuiMenuItem writeAiffButton, saveAs24BitWavButton, saveAs32BitWavButton, writeAifcButton, writeWavButton, writeNextSunButton, writeNistButton, writeFlacButton; |
| 152 |
GuiMenuItem cutButton, copyButton, pasteBeforeButton, pasteOverButton, pasteAfterButton, zeroButton, reverseButton; |
| 153 |
GuiMenuItem drawButton; |
| 154 |
GuiMenuItem publishButton, publishPreserveButton, publishWindowButton, publishOverlapButton; |
| 155 |
|
| 156 |
#include "SoundArea_prefs.h" |
| 157 |
}; |
| 158 |
|
| 159 |
void SoundArea_draw (SoundArea me); |
| 160 |
|
| 161 |
bool SoundArea_mouse (SoundArea me, GuiDrawingArea_MouseEvent event, double x_world, double globalY_fraction); |
| 162 |
|
| 163 |
void SoundArea_play (SoundArea me, double startTime, double endTime); |
| 164 |
|
| 165 |
DEFINE_FunctionArea_create (SoundArea, Sound) |
| 166 |
|
| 167 |
/* End of file SoundArea.h */ |
| 168 |
#endif |