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