Audacity 3.2.0
SelectedRegion.h
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 SelectedRegion.h
6
7 Dominic Mazzoni
8
9*******************************************************************//****************************************************************/
25#ifndef __AUDACITY_SELECTEDREGION__
26#define __AUDACITY_SELECTEDREGION__
27
28#include <wx/defs.h>
29#include <wx/chartype.h> // for wxChar, a typedef
30#include <math.h>
31#include <string_view>
32
33#include "XMLMethodRegistry.h"
34
35class XMLWriter;
37
38class TIME_FREQUENCY_SELECTION_API SelectedRegion {
39
40 // Maintains the invariant: t1() >= t0()
41
42public:
43
44 static const int UndefinedFrequency = -1;
45
47 : mT0(0.0)
48 , mT1(0.0)
49 , mF0(UndefinedFrequency)
50 , mF1(UndefinedFrequency)
51 {}
52
53 SelectedRegion(double t0, double t1)
54 : mT0(t0)
55 , mT1(t1)
56 , mF0(UndefinedFrequency)
57 , mF1(UndefinedFrequency)
58 { ensureOrdering(); }
59
60
61 // LLL: 2014/10/6
62 // Removed "explicit" until we drop OSX PPC support and upgrade to a newer
63 // compiler.
64 //
65 // explicit
67 : mT0(x.mT0)
68 , mT1(x.mT1)
69 , mF0(x.mF0)
70 , mF1(x.mF1)
71 {}
72
74 {
75 if (this != &x) {
76 mT0 = x.mT0;
77 mT1 = x.mT1;
78 mF0 = x.mF0;
79 mF1 = x.mF1;
80 }
81 return *this;
82 }
83
84 // Accessors
85
86 double t0() const { return mT0; }
87 double t1() const { return mT1; }
88 double duration() const { return mT1 - mT0; }
89 bool isPoint() const { return mT1 <= mT0; }
90
91 double f0() const { return mF0; }
92 double f1() const { return mF1; }
93 double fc() const {
94 if (mF0 == UndefinedFrequency ||
95 mF1 == UndefinedFrequency)
96 return UndefinedFrequency;
97 else
98 return sqrt(mF0 * mF1);
99 };
100
101 // Mutators
102 // PRL: to do: more integrity checks
103
104 // Returns true iff the bounds got swapped
105 bool setT0(double t, bool maySwap = true) {
106 mT0 = t;
107 if (maySwap)
108 return ensureOrdering();
109 else {
110 if (mT1 < mT0)
111 mT1 = mT0;
112 return false;
113 }
114 }
115
116 // Returns true iff the bounds got swapped
117 bool setT1(double t, bool maySwap = true) {
118 mT1 = t;
119 if (maySwap)
120 return ensureOrdering();
121 else {
122 if (mT1 < mT0)
123 mT0 = mT1;
124 return false;
125 }
126 }
127
128 // Returns true iff the bounds got swapped
129 bool setTimes(double t0, double t1) {
130 mT0 = t0;
131 mT1 = t1;
132 return ensureOrdering();
133 }
134
135 // Returns true iff the bounds got swapped
136 bool moveT0(double delta, bool maySwap = true) {
137 return setT0(mT0 + delta, maySwap);
138 }
139
140 // Returns true iff the bounds got swapped
141 bool moveT1(double delta, bool maySwap = true) {
142 return setT1(mT1 + delta, maySwap);
143 }
144
145 void move(double delta) {
146 mT0 += delta;
147 mT1 += delta;
148 }
149
150 void collapseToT0() { mT1 = mT0; }
151
152 void collapseToT1() { mT0 = mT1; }
153
154 // Returns true iff the bounds got swapped
155 bool setF0(double f, bool maySwap = true) {
156 if (f < 0)
157 f = UndefinedFrequency;
158 mF0 = f;
159 if (maySwap)
160 return ensureFrequencyOrdering();
161 else {
162 if (mF1 >= 0 && mF1 < mF0)
163 mF1 = mF0;
164 return false;
165 }
166 }
167
168 // Returns true iff the bounds got swapped
169 bool setF1(double f, bool maySwap = true) {
170 if (f < 0)
171 f = UndefinedFrequency;
172 mF1 = f;
173 if (maySwap)
174 return ensureFrequencyOrdering();
175 else {
176 if (mF0 >= 0 && mF1 < mF0)
177 mF0 = mF1;
178 return false;
179 }
180 }
181
182 // Returns true iff the bounds got swapped
183 bool setFrequencies(double f0, double f1)
184 {
185 mF0 = f0;
186 mF1 = f1;
187 return ensureFrequencyOrdering();
188 }
189
190 // Serialization: historically, selections were written to file
191 // in two places (project, and each label) but only as attributes
192 // in the tags, and different names were used in the two places.
193 // For compatibility, continue that, but possibly add attributes
194 // as SelectedRegion is extended. Therefore, this is not an
195 // XMLTagHandler.
196
197 static const char* sDefaultT0Name;
198 static const char* sDefaultT1Name;
199
200 // Serialize, not with tags of its own, but as attributes within a tag.
201 // Don't add more legacy arguments as the structure grows.
202 void WriteXMLAttributes
203 (XMLWriter &xmlFile,
204 const char *legacyT0Name = sDefaultT0Name,
205 const char *legacyT1Name = sDefaultT1Name) const;
206
207 // Return true iff the attribute is recognized.
208 // Don't add more legacy arguments as the structure grows.
209 bool HandleXMLAttribute
210 (const std::string_view &attr, const XMLAttributeValueView &value,
211 const char *legacyT0Name = sDefaultT0Name,
212 const char* legacyT1Name = sDefaultT1Name);
213
214 /*
215 This function encapsulates details of serialization of
216 SelectedRegion. It was serialized with differing attribute values in
217 different contexts, for reasons of history.
218 */
220 Mutators(
221 const char *legacyT0Name, const char* legacyT1Name);
222
224 {
225 if (mT1 < mT0) {
226 const double t = mT1;
227 mT1 = mT0;
228 mT0 = t;
229 return true;
230 }
231 else
232 return false;
233 }
234
235private:
236
238 {
239 if (mF1 < 0)
240 mF1 = UndefinedFrequency;
241 if (mF0 < 0)
242 mF0 = UndefinedFrequency;
243
244 if (mF0 != UndefinedFrequency &&
245 mF1 != UndefinedFrequency &&
246 mF1 < mF0) {
247 const double t = mF1;
248 mF1 = mF0;
249 mF0 = t;
250 return true;
251 }
252 else
253 return false;
254 }
255
256 friend inline bool operator ==
257 (const SelectedRegion &lhs, const SelectedRegion &rhs)
258 {
259 return
260 lhs.mT0 == rhs.mT0
261 && lhs.mT1 == rhs.mT1
262 && lhs.mF0 == rhs.mF0
263 && lhs.mF1 == rhs.mF1
264 ;
265 }
266
267 double mT0;
268 double mT1;
269 double mF0; // low frequency
270 double mF1; // high frequency
271};
272
273inline bool operator != (const SelectedRegion &lhs, const SelectedRegion &rhs)
274{
275 return !(lhs == rhs);
276}
277
278#endif
bool operator!=(const SelectedRegion &lhs, const SelectedRegion &rhs)
Defines a selected portion of a project.
double t1() const
SelectedRegion & operator=(const SelectedRegion &x)
double f0() const
bool moveT0(double delta, bool maySwap=true)
bool setTimes(double t0, double t1)
double fc() const
SelectedRegion(double t0, double t1)
SelectedRegion(const SelectedRegion &x)
bool setF0(double f, bool maySwap=true)
bool setF1(double f, bool maySwap=true)
bool isPoint() const
static const char * sDefaultT1Name
static const char * sDefaultT0Name
double t0() const
double duration() const
bool moveT1(double delta, bool maySwap=true)
bool setFrequencies(double f0, double f1)
bool setT0(double t, bool maySwap=true)
double f1() const
bool setT1(double t, bool maySwap=true)
void move(double delta)
bool ensureFrequencyOrdering()
A view into an attribute value. The class does not take the ownership of the data.
std::vector< std::pair< std::string, Mutator< Substructure > > > Mutators
A helper type alias for a list of mutators, associated with tag strings.
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:25
__finl float_x4 __vecc sqrt(const float_x4 &a)