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