Debian Codegraph @

foned/SpectrumArea.cpp

raw
1 /* SpectrumArea.cpp
2 *
3 * Copyright (C) 1992-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.
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 "SpectrumArea.h"
20 #include "EditorM.h"
21
22 Thing_implement (SpectrumArea, FunctionArea, 0);
23
24 #include "Prefs_define.h"
25 #include "SpectrumArea_prefs.h"
26 #include "Prefs_install.h"
27 #include "SpectrumArea_prefs.h"
28 #include "Prefs_copyToInstance.h"
29 #include "SpectrumArea_prefs.h"
30
31
32 #pragma mark - SpectrumArea helpers
33
34 inline static autoSpectrum Spectrum_band (Spectrum me, double fmin, double fmax) {
35 autoSpectrum band = Data_copy (me);
36 double *re = & band -> z [1] [0], *im = & band -> z [2] [0];
37 const integer imin = Sampled_xToLowIndex (band.get(), fmin);
38 const integer imax = Sampled_xToHighIndex (band.get(), fmax);
39 for (integer i = 1; i <= imin; i ++)
40 re [i] = 0.0, im [i] = 0.0;
41 for (integer i = imax; i <= band -> nx; i ++)
42 re [i] = 0.0, im [i] = 0.0;
43 return band;
44 }
45 inline static autoSound Spectrum_to_Sound_part (Spectrum me, double fmin, double fmax) {
46 autoSpectrum band = Spectrum_band (me, fmin, fmax);
47 autoSound sound = Spectrum_to_Sound (band.get());
48 return sound;
49 }
50
51
52 #pragma mark - SpectrumArea info
53
54 void structSpectrumArea :: v1_info () {
55 MelderInfo_writeLine (U"Dynamic range: ", our instancePref_dynamicRange(), U" dB");
56 MelderInfo_writeLine (U"Cursor height: ", our cursorHeight, U" dB");
57 MelderInfo_writeLine (U"Minimum: ", our minimum(), U" dB");
58 MelderInfo_writeLine (U"Maximum: ", our maximum(), U" dB");
59 }
60
61
62 #pragma mark - SpectrumArea drawing
63
64 void structSpectrumArea :: v_drawInside () {
65 Spectrum_drawInside (our spectrum(), our graphics(), our startWindow(), our endWindow(),
66 our minimum(), our maximum());
67 Graphics_setColour (our graphics(), Melder_BLUE);
68 FunctionArea_drawRightMark (this, our maximum(),
69 Melder_fixed (our maximum(), 1), U" dB", Graphics_HALF);
70 FunctionArea_drawRightMark (this, our minimum(),
71 Melder_fixed (our minimum(), 1), U" dB", Graphics_HALF);
72 if (our cursorHeight > our minimum() && our cursorHeight < our maximum())
73 FunctionEditor_drawHorizontalHair (our functionEditor(), our cursorHeight,
74 Melder_fixed (our cursorHeight, 1), U" dB");
75 }
76
77
78 #pragma mark - SpectrumArea tracking
79
80 bool structSpectrumArea :: v_mouse (GuiDrawingArea_MouseEvent event, double x_world, double localY_fraction) {
81 our cursorHeight = our minimum() + localY_fraction * (our maximum() - our minimum());
82 return FunctionEditor_defaultMouseInWideDataView (our functionEditor(), event, x_world) || true; // BUG:
83 }
84
85
86 #pragma mark - SpectrumArea View
87
88 static void menu_cb_setDynamicRange (SpectrumArea me, EDITOR_ARGS) {
89 EDITOR_FORM (U"Set dynamic range", nullptr)
90 POSITIVE (dynamicRange, U"Dynamic range (dB)", my default_dynamicRange())
91 EDITOR_OK
92 SET_REAL (dynamicRange, my instancePref_dynamicRange())
93 EDITOR_DO
94 my setInstancePref_dynamicRange (dynamicRange);
95 my updateRange ();
96 FunctionEditor_redraw (my functionEditor());
97 EDITOR_END
98 }
99
100
101 #pragma mark - SpectrumArea Modify
102
103 static void menu_cb_passBand (SpectrumArea me, EDITOR_ARGS) {
104 EDITOR_FORM (U"Filter (pass Hann band)", U"Spectrum: Filter (pass Hann band)...");
105 REAL (bandSmoothing, U"Band smoothing (Hz)", my default_bandSmoothing())
106 EDITOR_OK
107 SET_REAL (bandSmoothing, my instancePref_bandSmoothing())
108 EDITOR_DO
109 my setInstancePref_bandSmoothing (bandSmoothing);
110 Melder_require (my endSelection() > my startSelection(),
111 U"To apply a band-pass filter, first make a selection.");
112
113 FunctionArea_save (me, U"Pass band");
114
115 Spectrum_passHannBand (my spectrum(), my startSelection(), my endSelection(), my instancePref_bandSmoothing());
116
117 FunctionArea_broadcastDataChanged (me);
118 EDITOR_END
119 }
120 static void menu_cb_stopBand (SpectrumArea me, EDITOR_ARGS) {
121 EDITOR_FORM (U"Filter (stop Hann band)", nullptr)
122 REAL (bandSmoothing, U"Band smoothing (Hz)", my default_bandSmoothing())
123 EDITOR_OK
124 SET_REAL (bandSmoothing, my instancePref_bandSmoothing())
125 EDITOR_DO
126 my setInstancePref_bandSmoothing (bandSmoothing);
127 Melder_require (my endSelection() > my startSelection(),
128 U"To apply a band-stop filter, first make a selection.");
129
130 FunctionArea_save (me, U"Stop band");
131
132 Spectrum_stopHannBand (my spectrum(), my startSelection(), my endSelection(), my instancePref_bandSmoothing());
133
134 FunctionArea_broadcastDataChanged (me);
135 EDITOR_END
136 }
137
138
139 #pragma mark - SpectrumArea Select
140
141 static void menu_cb_moveCursorToPeak (SpectrumArea me, EDITOR_ARGS) {
142 MelderPoint peak = Spectrum_getNearestMaximum (my spectrum(), 0.5 * (my startSelection() + my endSelection()));
143 my setSelection (peak. x, peak. x);
144 my cursorHeight = peak. y;
145 Melder_assert (isdefined (my startSelection())); // precondition of FunctionEditor_selectionMarksChanged()
146 FunctionEditor_selectionMarksChanged (my functionEditor());
147 }
148
149
150 #pragma mark - SpectrumArea Extract
151
152 static void CONVERT_DATA_TO_ONE__PublishBand (SpectrumArea me, EDITOR_ARGS) {
153 CONVERT_DATA_TO_ONE
154 autoSpectrum result = Spectrum_band (my spectrum(), my startSelection(), my endSelection());
155 CONVERT_DATA_TO_ONE_END (U"untitled")
156 }
157 static void CONVERT_DATA_TO_ONE__PublishSound (SpectrumArea me, EDITOR_ARGS) {
158 CONVERT_DATA_TO_ONE
159 autoSound result = Spectrum_to_Sound_part (my spectrum(), my startSelection(), my endSelection());
160 CONVERT_DATA_TO_ONE_END (U"untitled")
161 }
162
163
164 #pragma mark - SpectrumArea all menus?
165
166 void structSpectrumArea :: v_createMenus () {
167 EditorMenu menu = Editor_addMenu (our functionEditor(), U"Spectrum", 0);
168
169 FunctionAreaMenu_addCommand (menu, U"Power density range:", 0, nullptr, this);
170 FunctionAreaMenu_addCommand (menu, U"Set dynamic range...", 1,
171 menu_cb_setDynamicRange, this);
172
173 FunctionAreaMenu_addCommand (menu, U"- Modify spectrum:", 0, nullptr, this);
174 FunctionAreaMenu_addCommand (menu, U"Pass band...", 1,
175 menu_cb_passBand, this);
176 FunctionAreaMenu_addCommand (menu, U"Stop band...", 1,
177 menu_cb_stopBand, this);
178
179 FunctionAreaMenu_addCommand (menu, U"- Select by spectrum:", 0, nullptr, this);
180 FunctionAreaMenu_addCommand (menu, U"Move cursor to nearest peak", 'K' | GuiMenu_DEPTH_1,
181 menu_cb_moveCursorToPeak, this);
182
183 FunctionAreaMenu_addCommand (menu, U"- Extract spectrum:", 0, nullptr, this);
184 our publishBandButton = FunctionAreaMenu_addCommand (menu, U"Publish band", 1,
185 CONVERT_DATA_TO_ONE__PublishBand, this);
186 our publishSoundButton = FunctionAreaMenu_addCommand (menu, U"Publish band-filtered sound", 1,
187 CONVERT_DATA_TO_ONE__PublishSound, this);
188 }
189
190
191 #pragma mark - SpectrumArea functions
192
193 void SpectrumArea_play (SpectrumArea me, double fromFrequency, double toFrequency) {
194 autoSound sound = Spectrum_to_Sound_part (my spectrum(), fromFrequency, toFrequency);
195 Sound_play (sound.get(), nullptr, nullptr);
196 }
197
198 /* End of file SpectrumArea.cpp */