Audacity  2.2.0
Meter.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  Meter.h
6 
7  Dominic Mazzoni
8 
9  VU Meter, for displaying recording/playback level
10 
11  This is a bunch of common code that can display many different
12  forms of VU meters and other displays.
13 
14 **********************************************************************/
15 
16 #ifndef __AUDACITY_METER__
17 #define __AUDACITY_METER__
18 
19 #include <wx/defs.h>
20 #include <wx/timer.h>
21 
22 #include "../SampleFormat.h"
23 #include "Ruler.h"
24 
25 // Event used to notify all meters of preference changes
26 DECLARE_EXPORTED_EVENT_TYPE(AUDACITY_DLL_API, EVT_METER_PREFERENCES_CHANGED, -1);
27 
28 // Increase this when we add support for multichannel meters
29 // (most of the code is already there)
30 const int kMaxMeterBars = 2;
31 
32 struct MeterBar {
33  bool vert;
34  wxRect b; // Bevel around bar
35  wxRect r; // True bar drawing area
36  float peak;
37  float rms;
38  float peakHold;
39  double peakHoldTime;
40  wxRect rClip;
41  bool clipping;
42  bool isclipping; //ANSWER-ME: What's the diff between these bools?! "clipping" vs "isclipping" is not clear.
44  float peakPeakHold;
45 };
46 
48 {
49  public:
50  int numFrames;
56 
57  /* neither constructor nor destructor do anything */
60  /* for debugging purposes, printing the values out is really handy */
62  wxString toString();
64  wxString toStringIfClipped();
65 };
66 
67 // Thread-safe queue of update messages
69 {
70  public:
71  explicit MeterUpdateQueue(size_t maxLen);
73 
74  bool Put(MeterUpdateMsg &msg);
75  bool Get(MeterUpdateMsg &msg);
76 
77  void Clear();
78 
79  private:
80  int mStart;
81  int mEnd;
82  size_t mBufferSize;
84 };
85 
86 class MeterAx;
87 
88 class Meter final : public wxPanelWrapper
89 {
90  DECLARE_DYNAMIC_CLASS(Meter)
91 
92  public:
93  // These should be kept in the same order as they appear
94  // in the menu
95  enum Style {
99  MixerTrackCluster, // Doesn't show menu, icon, or L/R labels, but otherwise like VerticalStereo.
101  VerticalStereoCompact, // Narrower.
102  };
103 
104 
106  wxWindow* parent, wxWindowID id,
107  bool isInput,
108  const wxPoint& pos = wxDefaultPosition,
109  const wxSize& size = wxDefaultSize,
110  Style style = HorizontalStereo,
111  float fDecayRate = 60.0f);
112 
113  ~Meter();
114 
115  bool AcceptsFocus() const override { return s_AcceptsFocus; }
116  bool AcceptsFocusFromKeyboard() const override { return true; }
117 
118  void SetFocusFromKbd() override;
119 
120  void UpdatePrefs();
121  void Clear();
122 
123  Style GetStyle() const { return mStyle; }
124  Style GetDesiredStyle() const { return mDesiredStyle; }
125  void SetStyle(Style newStyle);
126 
132  void Reset(double sampleRate, bool resetClipping);
133 
156  void UpdateDisplay(unsigned numChannels,
157  int numFrames, float *sampleData);
158 
159  // Vaughan, 2010-11-29: This not currently used. See comments in MixerTrackCluster::UpdateMeter().
160  //void UpdateDisplay(int numChannels, int numFrames,
161  // // Need to make these double-indexed max and min arrays if we handle more than 2 channels.
162  // float* maxLeft, float* rmsLeft,
163  // float* maxRight, float* rmsRight,
164  // const size_t kSampleCount);
165 
171  bool IsMeterDisabled() const;
172 
173  float GetMaxPeak() const;
174 
175  bool IsClipping() const;
176 
177  void StartMonitoring();
178  void StopMonitoring();
179 
180  // These exist solely for the purpose of resetting the toolbars
181  struct State{ bool mSaved, mMonitoring, mActive; };
182  State SaveState();
183  void RestoreState(const State &state);
184 
185  private:
186  static bool s_AcceptsFocus;
187  struct Resetter { void operator () (bool *p) const { if(p) *p = false; } };
188  using TempAllowFocus = std::unique_ptr<bool, Resetter>;
189 
190  public:
192 
193  private:
194  //
195  // Event handlers
196  //
197  void OnErase(wxEraseEvent &evt);
198  void OnPaint(wxPaintEvent &evt);
199  void OnSize(wxSizeEvent &evt);
200  bool InIcon(wxMouseEvent *pEvent = nullptr) const;
201  void OnMouse(wxMouseEvent &evt);
202  void OnKeyDown(wxKeyEvent &evt);
203  void OnKeyUp(wxKeyEvent &evt);
204  void OnContext(wxContextMenuEvent &evt);
205  void OnSetFocus(wxFocusEvent &evt);
206  void OnKillFocus(wxFocusEvent &evt);
207 
208  void OnAudioIOStatus(wxCommandEvent &evt);
209 
210  void OnMeterUpdate(wxTimerEvent &evt);
211 
212  void HandleLayout(wxDC &dc);
213  void SetActiveStyle(Style style);
214  void SetBarAndClip(int iBar, bool vert);
215  void DrawMeterBar(wxDC &dc, MeterBar *meterBar);
216  void ResetBar(MeterBar *bar, bool resetClipping);
217  void RepaintBarsNow();
218  wxFont GetFont() const;
219 
220  //
221  // Pop-up menu
222  //
223  void ShowMenu(const wxPoint & pos);
224  void OnMonitor(wxCommandEvent &evt);
225  void OnPreferences(wxCommandEvent &evt);
226  void OnMeterPrefsUpdated(wxCommandEvent &evt);
227 
228  wxString Key(const wxString & key) const;
229 
232  wxTimer mTimer;
233 
234  int mWidth;
235  int mHeight;
236 
239 
240  bool mIsInput;
241 
244  bool mGradient;
245  bool mDB;
246  int mDBRange;
247  bool mDecay;
248  float mDecayRate; // dB/sec
249  bool mClip;
252  double mT;
253  double mRate;
255  long mMeterDisabled; //is used as a bool, needs long for easy gPrefs...
256 
258 
259  bool mActive;
260 
261  unsigned mNumBars;
263 
265 
266  std::unique_ptr<wxBitmap> mBitmap;
267  wxRect mIconRect;
268  wxPoint mLeftTextPos;
269  wxPoint mRightTextPos;
270  wxSize mLeftSize;
271  wxSize mRightSize;
272  std::unique_ptr<wxBitmap> mIcon;
273  wxPen mPen;
276  wxBrush mBrush;
277  wxBrush mRMSBrush;
278  wxBrush mClipBrush;
279  wxBrush mBkgndBrush;
282  wxString mLeftText;
283  wxString mRightText;
284 
286  wxRect mFocusRect;
287 #if defined(__WXMSW__)
288  bool mHadKeyDown;
289 #endif
290 
292 
293  friend class MeterAx;
294 
295  bool mHighlighted {};
296 
297  DECLARE_EVENT_TABLE()
298 };
299 
300 #if wxUSE_ACCESSIBILITY
301 
302 class MeterAx final : public wxWindowAccessible
303 {
304 public:
305  MeterAx(wxWindow * window);
306 
307  virtual ~ MeterAx();
308 
309  // Performs the default action. childId is 0 (the action for this object)
310  // or > 0 (the action for a child).
311  // Return wxACC_NOT_SUPPORTED if there is no default action for this
312  // window (e.g. an edit control).
313  wxAccStatus DoDefaultAction(int childId) override;
314 
315  // Retrieves the address of an IDispatch interface for the specified child.
316  // All objects must support this property.
317  wxAccStatus GetChild(int childId, wxAccessible** child) override;
318 
319  // Gets the number of children.
320  wxAccStatus GetChildCount(int* childCount) override;
321 
322  // Gets the default action for this object (0) or > 0 (the action for a child).
323  // Return wxACC_OK even if there is no action. actionName is the action, or the empty
324  // string if there is no action.
325  // The retrieved string describes the action that is performed on an object,
326  // not what the object does as a result. For example, a toolbar button that prints
327  // a document has a default action of "Press" rather than "Prints the current document."
328  wxAccStatus GetDefaultAction(int childId, wxString *actionName) override;
329 
330  // Returns the description for this object or a child.
331  wxAccStatus GetDescription(int childId, wxString *description) override;
332 
333  // Gets the window with the keyboard focus.
334  // If childId is 0 and child is NULL, no object in
335  // this subhierarchy has the focus.
336  // If this object has the focus, child should be 'this'.
337  wxAccStatus GetFocus(int *childId, wxAccessible **child) override;
338 
339  // Returns help text for this object or a child, similar to tooltip text.
340  wxAccStatus GetHelpText(int childId, wxString *helpText) override;
341 
342  // Returns the keyboard shortcut for this object or child.
343  // Return e.g. ALT+K
344  wxAccStatus GetKeyboardShortcut(int childId, wxString *shortcut) override;
345 
346  // Returns the rectangle for this object (id = 0) or a child element (id > 0).
347  // rect is in screen coordinates.
348  wxAccStatus GetLocation(wxRect& rect, int elementId) override;
349 
350  // Gets the name of the specified object.
351  wxAccStatus GetName(int childId, wxString *name) override;
352 
353  // Returns a role constant.
354  wxAccStatus GetRole(int childId, wxAccRole *role) override;
355 
356  // Gets a variant representing the selected children
357  // of this object.
358  // Acceptable values:
359  // - a null variant (IsNull() returns TRUE)
360  // - a list variant (GetType() == wxT("list"))
361  // - an integer representing the selected child element,
362  // or 0 if this object is selected (GetType() == wxT("long"))
363  // - a "void*" pointer to a wxAccessible child object
364  wxAccStatus GetSelections(wxVariant *selections) override;
365 
366  // Returns a state constant.
367  wxAccStatus GetState(int childId, long* state) override;
368 
369  // Returns a localized string representing the value for the object
370  // or child.
371  wxAccStatus GetValue(int childId, wxString* strValue) override;
372 
373 };
374 
375 #endif // wxUSE_ACCESSIBILITY
376 
377 #endif // __AUDACITY_METER__
MeterBar mBar[kMaxMeterBars]
Definition: Meter.h:262
bool mIsInput
Definition: Meter.h:240
double mRate
Definition: Meter.h:253
void ResetBar(MeterBar *bar, bool resetClipping)
Definition: Meter.cpp:1119
bool mMonitoring
Definition: Meter.h:181
bool mActive
Definition: Meter.h:181
float rms[kMaxMeterBars]
Definition: Meter.h:52
void StartMonitoring()
Definition: Meter.cpp:1814
void OnSize(wxSizeEvent &evt)
Definition: Meter.cpp:681
size_t mBufferSize
Definition: Meter.h:82
bool mActive
Definition: Meter.h:259
int mNumPeakSamplesToClip
Definition: Meter.h:250
Definition: Meter.h:181
void UpdatePrefs()
Definition: Meter.cpp:377
bool mAccSilent
Definition: Meter.h:291
wxSize mLeftSize
Definition: Meter.h:270
int mRulerHeight
Definition: Meter.h:238
wxString mLeftText
Definition: Meter.h:282
wxRect mIconRect
Definition: Meter.h:267
wxRect r
Definition: Meter.h:35
static TempAllowFocus TemporarilyAllowFocus()
Definition: Meter.cpp:2081
wxString mRightText
Definition: Meter.h:283
wxBrush mRMSBrush
Definition: Meter.h:277
wxRect b
Definition: Meter.h:34
Definition: Meter.h:101
int mEnd
Definition: Meter.h:81
long mMeterRefreshRate
Definition: Meter.h:254
void OnKillFocus(wxFocusEvent &evt)
Definition: Meter.cpp:844
VU Meter, for displaying recording/playback level.
Definition: Meter.h:88
bool mSaved
Definition: Meter.h:181
float mDecayRate
Definition: Meter.h:248
void OnMeterPrefsUpdated(wxCommandEvent &evt)
Definition: Meter.cpp:1924
void operator()(bool *p) const
Definition: Meter.h:187
int mStart
Definition: Meter.h:80
friend class MeterAx
Definition: Meter.h:293
void OnKeyUp(wxKeyEvent &evt)
Definition: Meter.cpp:815
bool AcceptsFocusFromKeyboard() const override
Definition: Meter.h:116
bool mIsFocused
Definition: Meter.h:285
void OnAudioIOStatus(wxCommandEvent &evt)
Definition: Meter.cpp:1841
void Clear()
Definition: Meter.cpp:337
Style mStyle
Definition: Meter.h:242
int tailPeakCount[kMaxMeterBars]
Definition: Meter.h:55
void HandleLayout(wxDC &dc)
Definition: Meter.cpp:1226
bool mGradient
Definition: Meter.h:244
void OnErase(wxEraseEvent &evt)
Definition: Meter.cpp:421
Definition: Meter.h:98
Definition: wxPanelWrapper.h:19
int mDBRange
Definition: Meter.h:246
void StopMonitoring()
Definition: Meter.cpp:1834
bool clipping[kMaxMeterBars]
Definition: Meter.h:53
bool mClip
Definition: Meter.h:249
bool vert
Definition: Meter.h:33
Style GetDesiredStyle() const
Definition: Meter.h:124
void RestoreState(const State &state)
Definition: Meter.cpp:1870
wxBrush mDisabledBkgndBrush
Definition: Meter.h:280
bool Get(MeterUpdateMsg &msg)
Definition: Meter.cpp:156
bool Put(MeterUpdateMsg &msg)
Definition: Meter.cpp:135
void OnMouse(wxMouseEvent &evt)
Definition: Meter.cpp:694
bool mDB
Definition: Meter.h:245
wxPen mPeakPeakPen
Definition: Meter.h:275
MeterUpdateQueue(size_t maxLen)
Definition: Meter.cpp:116
Style GetStyle() const
Definition: Meter.h:123
wxPen mDisabledPen
Definition: Meter.h:274
int mRulerWidth
Definition: Meter.h:237
AudacityProject provides the main window, with tools and tracks contained within it.
Definition: Project.h:161
wxRect rClip
Definition: Meter.h:40
bool mDecay
Definition: Meter.h:247
float rms
Definition: Meter.h:37
int tailPeakCount
Definition: Meter.h:43
void SetActiveStyle(Style style)
Definition: Meter.cpp:1142
bool mHighlighted
Definition: Meter.h:295
double mT
Definition: Meter.h:252
bool mMonitoring
Definition: Meter.h:257
int mWidth
Definition: Meter.h:234
std::unique_ptr< bool, Resetter > TempAllowFocus
Definition: Meter.h:188
Queue of MeterUpdateMsg used to feed the Meter.
Definition: Meter.h:68
void OnMeterUpdate(wxTimerEvent &evt)
Definition: Meter.cpp:1003
Definition: Meter.h:99
Definition: Meter.h:100
void OnKeyDown(wxKeyEvent &evt)
Definition: Meter.cpp:778
int mHeight
Definition: Meter.h:235
void OnSetFocus(wxFocusEvent &evt)
Definition: Meter.cpp:838
float peak[kMaxMeterBars]
Definition: Meter.h:51
wxPoint mLeftTextPos
Definition: Meter.h:268
bool isclipping
Definition: Meter.h:42
~MeterUpdateQueue()
Definition: Meter.cpp:123
Ruler mRuler
Definition: Meter.h:281
bool InIcon(wxMouseEvent *pEvent=nullptr) const
Definition: Meter.cpp:688
State SaveState()
Definition: Meter.cpp:1865
wxString toStringIfClipped()
Only print meter updates if clipping may be happening.
Definition: Meter.cpp:99
~Meter()
Definition: Meter.cpp:342
double peakHoldTime
Definition: Meter.h:39
bool AcceptsFocus() const override
Definition: Meter.h:115
long mMeterDisabled
Definition: Meter.h:255
void ShowMenu(const wxPoint &pos)
Definition: Meter.cpp:1887
void OnContext(wxContextMenuEvent &evt)
Definition: Meter.cpp:759
float peakPeakHold
Definition: Meter.h:44
wxPen mPen
Definition: Meter.h:273
AudacityProject * mProject
Definition: Meter.h:230
void OnPreferences(wxCommandEvent &evt)
Definition: Meter.cpp:1933
bool mLayoutValid
Definition: Meter.h:264
void UpdateDisplay(unsigned numChannels, int numFrames, float *sampleData)
Update the meters with a block of audio data.
Definition: Meter.cpp:922
bool IsClipping() const
Definition: Meter.cpp:1134
std::unique_ptr< wxBitmap > mIcon
Definition: Meter.h:272
float peakHold
Definition: Meter.h:38
Style mDesiredStyle
Definition: Meter.h:243
MeterUpdateMsg()
Definition: Meter.h:58
Message used to update the Meter.
Definition: Meter.h:47
const int kMaxMeterBars
Definition: Meter.h:30
int numFrames
Definition: Meter.h:50
wxBrush mBrush
Definition: Meter.h:276
wxTimer mTimer
Definition: Meter.h:232
float GetMaxPeak() const
Definition: Meter.cpp:1099
MeterUpdateQueue mQueue
Definition: Meter.h:231
wxString toString()
Print out all the values in the meter update message.
Definition: Meter.cpp:82
const wxChar * name
Definition: Distortion.cpp:94
Used to display a Ruler.
Definition: Ruler.h:32
int headPeakCount[kMaxMeterBars]
Definition: Meter.h:54
bool clipping
Definition: Meter.h:41
float peak
Definition: Meter.h:36
std::unique_ptr< wxBitmap > mBitmap
Definition: Meter.h:266
wxString Key(const wxString &key) const
Definition: Meter.cpp:2064
wxBrush mClipBrush
Definition: Meter.h:278
Meter(AudacityProject *, wxWindow *parent, wxWindowID id, bool isInput, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, Style style=HorizontalStereo, float fDecayRate=60.0f)
Definition: Meter.cpp:212
Definition: Meter.h:96
wxBrush mBkgndBrush
Definition: Meter.h:279
unsigned mNumBars
Definition: Meter.h:261
void OnMonitor(wxCommandEvent &evt)
Definition: Meter.cpp:1919
void OnPaint(wxPaintEvent &evt)
Definition: Meter.cpp:426
DECLARE_EXPORTED_EVENT_TYPE(AUDACITY_DLL_API, EVT_METER_PREFERENCES_CHANGED,-1)
static bool s_AcceptsFocus
Definition: Meter.h:186
Definition: Meter.h:97
void DrawMeterBar(wxDC &dc, MeterBar *meterBar)
Definition: Meter.cpp:1549
void SetStyle(Style newStyle)
Definition: Meter.cpp:850
wxSize mRightSize
Definition: Meter.h:271
Style
Definition: Meter.h:95
wxFont GetFont() const
Definition: Meter.cpp:1109
wxPoint mRightTextPos
Definition: Meter.h:269
void SetBarAndClip(int iBar, bool vert)
Definition: Meter.cpp:1183
double mPeakHoldDuration
Definition: Meter.h:251
void Reset(double sampleRate, bool resetClipping)
This method is thread-safe! Feel free to call from a different thread (like from an audio I/O callbac...
Definition: Meter.cpp:862
ArrayOf< MeterUpdateMsg > mBuffer
Definition: Meter.h:83
void RepaintBarsNow()
Definition: Meter.cpp:1532
void SetFocusFromKbd() override
Definition: Meter.cpp:2089
void Clear()
Definition: Meter.cpp:127
bool IsMeterDisabled() const
Find out if the level meter is disabled or not.
Definition: Meter.cpp:1809
Definition: Meter.h:187
~MeterUpdateMsg()
Definition: Meter.h:59
A struct used by Meter to hold the position of one bar.
Definition: Meter.h:32
wxRect mFocusRect
Definition: Meter.h:286