forked from audacity/audacity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeter.cpp
More file actions
2329 lines (1978 loc) · 67.8 KB
/
Copy pathMeter.cpp
File metadata and controls
2329 lines (1978 loc) · 67.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
Audacity: A Digital Audio Editor
Meter.cpp
Dominic Mazzoni
Vaughan Johnson
2004.06.25 refresh rate limited to 30mS, by ChackoN
*******************************************************************//**
\class MeterPanel
\brief VU Meter, for displaying recording/playback level
This is a bunch of common code that can display many different
forms of VU meters and other displays.
But note that a lot of later code here assumes these are
MeterToolBar meters, e.g., MeterPanel::StartMonitoring,
so these are not as generic/common as originally intended.
*//****************************************************************//**
\class MeterBar
\brief A struct used by MeterPanel to hold the position of one bar.
*//****************************************************************//**
\class MeterUpdateMsg
\brief Message used to update the MeterPanel
*//****************************************************************//**
\class MeterUpdateQueue
\brief Queue of MeterUpdateMsg used to feed the MeterPanel.
*//******************************************************************/
#include "../Audacity.h" // for USE_* macros
#include "Meter.h"
#include <algorithm>
#include <wx/setup.h> // for wxUSE_* macros
#include <wx/wxcrtvararg.h>
#include <wx/app.h>
#include <wx/defs.h>
#include <wx/dialog.h>
#include <wx/dcbuffer.h>
#include <wx/frame.h>
#include <wx/image.h>
#include <wx/intl.h>
#include <wx/menu.h>
#include <wx/settings.h>
#include <wx/textdlg.h>
#include <wx/numdlg.h>
#include <wx/radiobut.h>
#include <wx/tooltip.h>
#include <math.h>
#include "../AudioIO.h"
#include "../AColor.h"
#include "../ImageManipulation.h"
#include "../prefs/GUISettings.h"
#include "../Project.h"
#include "../ProjectAudioManager.h"
#include "../ProjectStatus.h"
#include "../Prefs.h"
#include "../ShuttleGui.h"
#include "../Theme.h"
#include "../AllThemeResources.h"
#include "../widgets/valnum.h"
#if wxUSE_ACCESSIBILITY
#include "WindowAccessible.h"
class MeterAx final : public WindowAccessible
{
public:
MeterAx(wxWindow * window);
virtual ~ MeterAx();
// Performs the default action. childId is 0 (the action for this object)
// or > 0 (the action for a child).
// Return wxACC_NOT_SUPPORTED if there is no default action for this
// window (e.g. an edit control).
wxAccStatus DoDefaultAction(int childId) override;
// Retrieves the address of an IDispatch interface for the specified child.
// All objects must support this property.
wxAccStatus GetChild(int childId, wxAccessible** child) override;
// Gets the number of children.
wxAccStatus GetChildCount(int* childCount) override;
// Gets the default action for this object (0) or > 0 (the action for a child).
// Return wxACC_OK even if there is no action. actionName is the action, or the empty
// string if there is no action.
// The retrieved string describes the action that is performed on an object,
// not what the object does as a result. For example, a toolbar button that prints
// a document has a default action of "Press" rather than "Prints the current document."
wxAccStatus GetDefaultAction(int childId, wxString *actionName) override;
// Returns the description for this object or a child.
wxAccStatus GetDescription(int childId, wxString *description) override;
// Gets the window with the keyboard focus.
// If childId is 0 and child is NULL, no object in
// this subhierarchy has the focus.
// If this object has the focus, child should be 'this'.
wxAccStatus GetFocus(int *childId, wxAccessible **child) override;
// Returns help text for this object or a child, similar to tooltip text.
wxAccStatus GetHelpText(int childId, wxString *helpText) override;
// Returns the keyboard shortcut for this object or child.
// Return e.g. ALT+K
wxAccStatus GetKeyboardShortcut(int childId, wxString *shortcut) override;
// Returns the rectangle for this object (id = 0) or a child element (id > 0).
// rect is in screen coordinates.
wxAccStatus GetLocation(wxRect& rect, int elementId) override;
// Gets the name of the specified object.
wxAccStatus GetName(int childId, wxString *name) override;
// Returns a role constant.
wxAccStatus GetRole(int childId, wxAccRole *role) override;
// Gets a variant representing the selected children
// of this object.
// Acceptable values:
// - a null variant (IsNull() returns TRUE)
// - a list variant (GetType() == wxT("list"))
// - an integer representing the selected child element,
// or 0 if this object is selected (GetType() == wxT("long"))
// - a "void*" pointer to a wxAccessible child object
wxAccStatus GetSelections(wxVariant *selections) override;
// Returns a state constant.
wxAccStatus GetState(int childId, long* state) override;
// Returns a localized string representing the value for the object
// or child.
wxAccStatus GetValue(int childId, wxString* strValue) override;
};
#endif // wxUSE_ACCESSIBILITY
static const long MIN_REFRESH_RATE = 1;
static const long MAX_REFRESH_RATE = 100;
/* Updates to the meter are passed across via meter updates, each contained in
* a MeterUpdateMsg object */
wxString MeterUpdateMsg::toString()
{
wxString output; // somewhere to build up a string in
output = wxString::Format(wxT("Meter update msg: %i channels, %i samples\n"), \
kMaxMeterBars, numFrames);
for (int i = 0; i<kMaxMeterBars; i++)
{ // for each channel of the meters
output += wxString::Format(wxT("%f peak, %f rms "), peak[i], rms[i]);
if (clipping[i])
output += wxString::Format(wxT("clipped "));
else
output += wxString::Format(wxT("no clip "));
output += wxString::Format(wxT("%i head, %i tail\n"), headPeakCount[i], tailPeakCount[i]);
}
return output;
}
wxString MeterUpdateMsg::toStringIfClipped()
{
for (int i = 0; i<kMaxMeterBars; i++)
{
if (clipping[i] || (headPeakCount[i] > 0) || (tailPeakCount[i] > 0))
return toString();
}
return wxT("");
}
//
// The MeterPanel passes itself messages via this queue so that it can
// communicate between the audio thread and the GUI thread.
// This class is as simple as possible in order to be thread-safe
// without needing mutexes.
//
MeterUpdateQueue::MeterUpdateQueue(size_t maxLen):
mBufferSize(maxLen)
{
Clear();
}
// destructor
MeterUpdateQueue::~MeterUpdateQueue()
{
}
void MeterUpdateQueue::Clear()
{
mStart = 0;
mEnd = 0;
}
// Add a message to the end of the queue. Return false if the
// queue was full.
bool MeterUpdateQueue::Put(MeterUpdateMsg &msg)
{
// mStart can be greater than mEnd because it is all mod mBufferSize
wxASSERT( (mEnd + mBufferSize - mStart) >= 0 );
int len = (mEnd + mBufferSize - mStart) % mBufferSize;
// Never completely fill the queue, because then the
// state is ambiguous (mStart==mEnd)
if (len + 1 >= (int)(mBufferSize))
return false;
//wxLogDebug(wxT("Put: %s"), msg.toString());
mBuffer[mEnd] = msg;
mEnd = (mEnd+1)%mBufferSize;
return true;
}
// Get the next message from the start of the queue.
// Return false if the queue was empty.
bool MeterUpdateQueue::Get(MeterUpdateMsg &msg)
{
int len = (mEnd + mBufferSize - mStart) % mBufferSize;
if (len == 0)
return false;
msg = mBuffer[mStart];
mStart = (mStart+1)%mBufferSize;
return true;
}
//
// MeterPanel class
//
#include "../../images/SpeakerMenu.xpm"
#include "../../images/MicMenu.xpm"
// How many pixels between items?
const static int gap = 2;
const static wxChar *PrefStyles[] =
{
wxT("AutomaticStereo"),
wxT("HorizontalStereo"),
wxT("VerticalStereo")
};
enum {
OnMeterUpdateID = 6000,
OnMonitorID,
OnPreferencesID
};
BEGIN_EVENT_TABLE(MeterPanel, MeterPanelBase)
EVT_TIMER(OnMeterUpdateID, MeterPanel::OnMeterUpdate)
EVT_MOUSE_EVENTS(MeterPanel::OnMouse)
EVT_CONTEXT_MENU(MeterPanel::OnContext)
EVT_KEY_DOWN(MeterPanel::OnKeyDown)
EVT_KEY_UP(MeterPanel::OnKeyUp)
EVT_SET_FOCUS(MeterPanel::OnSetFocus)
EVT_KILL_FOCUS(MeterPanel::OnKillFocus)
EVT_ERASE_BACKGROUND(MeterPanel::OnErase)
EVT_PAINT(MeterPanel::OnPaint)
EVT_SIZE(MeterPanel::OnSize)
EVT_MENU(OnMonitorID, MeterPanel::OnMonitor)
EVT_MENU(OnPreferencesID, MeterPanel::OnPreferences)
END_EVENT_TABLE()
IMPLEMENT_CLASS(MeterPanel, wxPanelWrapper)
MeterPanel::MeterPanel(AudacityProject *project,
wxWindow* parent, wxWindowID id,
bool isInput,
const wxPoint& pos /*= wxDefaultPosition*/,
const wxSize& size /*= wxDefaultSize*/,
Style style /*= HorizontalStereo*/,
float fDecayRate /*= 60.0f*/)
: MeterPanelBase(parent, id, pos, size, wxTAB_TRAVERSAL | wxNO_BORDER | wxWANTS_CHARS),
mProject(project),
mQueue(1024),
mWidth(size.x),
mHeight(size.y),
mIsInput(isInput),
mDesiredStyle(style),
mGradient(true),
mDB(true),
mDBRange(ENV_DB_RANGE),
mDecay(true),
mDecayRate(fDecayRate),
mClip(true),
mNumPeakSamplesToClip(3),
mPeakHoldDuration(3),
mT(0),
mRate(0),
mMonitoring(false),
mActive(false),
mNumBars(0),
mLayoutValid(false),
mBitmap{},
mIcon{},
mAccSilent(false)
{
// i18n-hint: Noun (the meter is used for playback or record level monitoring)
SetName( XO("Meter") );
// Suppress warnings about the header file
wxUnusedVar(SpeakerMenu_xpm);
wxUnusedVar(MicMenu_xpm);
wxUnusedVar(PrefStyles);
mStyle = mDesiredStyle;
mIsFocused = false;
#if wxUSE_ACCESSIBILITY
SetAccessible(safenew MeterAx(this));
#endif
// Do this BEFORE UpdatePrefs()!
mRuler.SetFonts(GetFont(), GetFont(), GetFont());
mRuler.SetFlip(mStyle != MixerTrackCluster);
mRuler.SetLabelEdges(true);
//mRuler.SetTickColour( wxColour( 0,0,255 ) );
UpdatePrefs();
wxColour backgroundColour = theTheme.Colour( clrMedium);
mBkgndBrush = wxBrush(backgroundColour, wxBRUSHSTYLE_SOLID);
SetBackgroundColour( backgroundColour );
mPeakPeakPen = wxPen(theTheme.Colour( clrMeterPeak), 1, wxPENSTYLE_SOLID);
mDisabledPen = wxPen(theTheme.Colour( clrMeterDisabledPen), 1, wxPENSTYLE_SOLID);
if (mIsInput) {
wxTheApp->Bind(EVT_AUDIOIO_MONITOR,
&MeterPanel::OnAudioIOStatus,
this);
wxTheApp->Bind(EVT_AUDIOIO_CAPTURE,
&MeterPanel::OnAudioIOStatus,
this);
mPen = wxPen( theTheme.Colour( clrMeterInputPen ), 1, wxPENSTYLE_SOLID);
mBrush = wxBrush( theTheme.Colour( clrMeterInputBrush ), wxBRUSHSTYLE_SOLID);
mRMSBrush = wxBrush( theTheme.Colour( clrMeterInputRMSBrush ), wxBRUSHSTYLE_SOLID);
mClipBrush = wxBrush( theTheme.Colour( clrMeterInputClipBrush ), wxBRUSHSTYLE_SOLID);
// mLightPen = wxPen( theTheme.Colour( clrMeterInputLightPen ), 1, wxSOLID);
// mDarkPen = wxPen( theTheme.Colour( clrMeterInputDarkPen ), 1, wxSOLID);
}
else {
// Register for AudioIO events
wxTheApp->Bind(EVT_AUDIOIO_PLAYBACK,
&MeterPanel::OnAudioIOStatus,
this);
mPen = wxPen( theTheme.Colour( clrMeterOutputPen ), 1, wxPENSTYLE_SOLID);
mBrush = wxBrush( theTheme.Colour( clrMeterOutputBrush ), wxBRUSHSTYLE_SOLID);
mRMSBrush = wxBrush( theTheme.Colour( clrMeterOutputRMSBrush ), wxBRUSHSTYLE_SOLID);
mClipBrush = wxBrush( theTheme.Colour( clrMeterOutputClipBrush ), wxBRUSHSTYLE_SOLID);
// mLightPen = wxPen( theTheme.Colour( clrMeterOutputLightPen ), 1, wxSOLID);
// mDarkPen = wxPen( theTheme.Colour( clrMeterOutputDarkPen ), 1, wxSOLID);
}
// mDisabledBkgndBrush = wxBrush(theTheme.Colour( clrMeterDisabledBrush), wxSOLID);
// No longer show a difference in the background colour when not monitoring.
// We have the tip instead.
mDisabledBkgndBrush = mBkgndBrush;
// MixerTrackCluster style has no menu, so disallows SetStyle, so never needs icon.
if (mStyle != MixerTrackCluster)
{
if(mIsInput)
{
//mIcon = NEW wxBitmap(MicMenuNarrow_xpm);
mIcon = std::make_unique<wxBitmap>(wxBitmap(theTheme.Bitmap(bmpMic)));
}
else
{
//mIcon = NEW wxBitmap(SpeakerMenuNarrow_xpm);
mIcon = std::make_unique<wxBitmap>(wxBitmap(theTheme.Bitmap(bmpSpeaker)));
}
}
mTimer.SetOwner(this, OnMeterUpdateID);
// TODO: Yikes. Hard coded sample rate.
// JKC: I've looked at this, and it's benignish. It just means that the meter
// balistics are right for 44KHz and a bit more frisky than they should be
// for higher sample rates.
Reset(44100.0, true);
}
void MeterPanel::Clear()
{
mQueue.Clear();
}
void MeterPanel::UpdatePrefs()
{
mDBRange = gPrefs->Read(ENV_DB_KEY, ENV_DB_RANGE);
mMeterRefreshRate =
std::max(MIN_REFRESH_RATE, std::min(MAX_REFRESH_RATE,
gPrefs->Read(Key(wxT("RefreshRate")), 30)));
mGradient = gPrefs->Read(Key(wxT("Bars")), wxT("Gradient")) == wxT("Gradient");
mDB = gPrefs->Read(Key(wxT("Type")), wxT("dB")) == wxT("dB");
mMeterDisabled = gPrefs->Read(Key(wxT("Disabled")), (long)0);
if (mDesiredStyle != MixerTrackCluster)
{
wxString style = gPrefs->Read(Key(wxT("Style")));
if (style == wxT("AutomaticStereo"))
{
mDesiredStyle = AutomaticStereo;
}
else if (style == wxT("HorizontalStereo"))
{
mDesiredStyle = HorizontalStereo;
}
else if (style == wxT("VerticalStereo"))
{
mDesiredStyle = VerticalStereo;
}
else
{
mDesiredStyle = AutomaticStereo;
}
}
// Set the desired orientation (resets ruler orientation)
SetActiveStyle(mDesiredStyle);
// Reset to ensure NEW size is retrieved when language changes
mLeftSize = wxSize(0, 0);
mRightSize = wxSize(0, 0);
Reset(mRate, false);
mLayoutValid = false;
Refresh(false);
}
static int MeterPrefsID()
{
static int value = wxNewId();
return value;
}
void MeterPanel::UpdateSelectedPrefs(int id)
{
if (id == MeterPrefsID())
UpdatePrefs();
}
void MeterPanel::OnErase(wxEraseEvent & WXUNUSED(event))
{
// Ignore it to prevent flashing
}
void MeterPanel::OnPaint(wxPaintEvent & WXUNUSED(event))
{
#if defined(__WXMAC__)
auto paintDC = std::make_unique<wxPaintDC>(this);
#else
std::unique_ptr<wxDC> paintDC{ wxAutoBufferedPaintDCFactory(this) };
#endif
wxDC & destDC = *paintDC;
wxColour clrText = theTheme.Colour( clrTrackPanelText );
wxColour clrBoxFill = theTheme.Colour( clrMedium );
if (mLayoutValid == false || (mStyle == MixerTrackCluster ))
{
// Create a NEW one using current size and select into the DC
mBitmap = std::make_unique<wxBitmap>();
mBitmap->Create(mWidth, mHeight, destDC);
wxMemoryDC dc;
dc.SelectObject(*mBitmap);
// Go calculate all of the layout metrics
HandleLayout(dc);
// Start with a clean background
// LLL: Should research USE_AQUA_THEME usefulness...
//#ifndef USE_AQUA_THEME
#ifdef EXPERIMENTAL_THEMING
//if( !mMeterDisabled )
//{
// mBkgndBrush.SetColour( GetParent()->GetBackgroundColour() );
//}
#endif
mBkgndBrush.SetColour( GetBackgroundColour() );
dc.SetPen(*wxTRANSPARENT_PEN);
dc.SetBrush(mBkgndBrush);
dc.DrawRectangle(0, 0, mWidth, mHeight);
//#endif
// MixerTrackCluster style has no icon or L/R labels
if (mStyle != MixerTrackCluster)
{
bool highlight = InIcon();
dc.DrawBitmap( theTheme.Bitmap( highlight ?
bmpHiliteUpButtonSmall : bmpUpButtonSmall ),
mIconRect.GetPosition(), false );
dc.DrawBitmap(*mIcon, mIconRect.GetPosition(), true);
dc.SetFont(GetFont());
dc.SetTextForeground( clrText );
dc.SetTextBackground( clrBoxFill );
dc.DrawText(mLeftText, mLeftTextPos.x, mLeftTextPos.y);
dc.DrawText(mRightText, mRightTextPos.x, mRightTextPos.y);
}
// Setup the colors for the 3 sections of the meter bars
wxColor green(117, 215, 112);
wxColor yellow(255, 255, 0);
wxColor red(255, 0, 0);
// Bug #2473 - (Sort of) Hack to make text on meters more
// visible with darker backgrounds. It would be better to have
// different colors entirely and as part of the theme.
if (GetBackgroundColour().GetLuminance() < 0.25)
{
green = wxColor(117-100, 215-100, 112-100);
yellow = wxColor(255-100, 255-100, 0);
red = wxColor(255-100, 0, 0);
}
else if (GetBackgroundColour().GetLuminance() < 0.50)
{
green = wxColor(117-50, 215-50, 112-50);
yellow = wxColor(255-50, 255-50, 0);
red = wxColor(255-50, 0, 0);
}
// Draw the meter bars at maximum levels
for (unsigned int i = 0; i < mNumBars; i++)
{
// Give it a recessed look
AColor::Bevel(dc, false, mBar[i].b);
// Draw the clip indicator bevel
if (mClip)
{
AColor::Bevel(dc, false, mBar[i].rClip);
}
// Cache bar rect
wxRect r = mBar[i].r;
if (mGradient)
{
// Calculate the size of the two gradiant segments of the meter
double gradw;
double gradh;
if (mDB)
{
gradw = (double) r.GetWidth() / mDBRange * 6.0;
gradh = (double) r.GetHeight() / mDBRange * 6.0;
}
else
{
gradw = (double) r.GetWidth() / 100 * 25;
gradh = (double) r.GetHeight() / 100 * 25;
}
if (mBar[i].vert)
{
// Draw the "critical" segment (starts at top of meter and works down)
r.SetHeight(gradh);
dc.GradientFillLinear(r, red, yellow, wxSOUTH);
// Draw the "warning" segment
r.SetTop(r.GetBottom());
dc.GradientFillLinear(r, yellow, green, wxSOUTH);
// Draw the "safe" segment
r.SetTop(r.GetBottom());
r.SetBottom(mBar[i].r.GetBottom());
dc.SetPen(*wxTRANSPARENT_PEN);
dc.SetBrush(green);
dc.DrawRectangle(r);
}
else
{
// Draw the "safe" segment
r.SetWidth(r.GetWidth() - (int) (gradw + gradw + 0.5));
dc.SetPen(*wxTRANSPARENT_PEN);
dc.SetBrush(green);
dc.DrawRectangle(r);
// Draw the "warning" segment
r.SetLeft(r.GetRight() + 1);
r.SetWidth(floor(gradw));
dc.GradientFillLinear(r, green, yellow);
// Draw the "critical" segment
r.SetLeft(r.GetRight() + 1);
r.SetRight(mBar[i].r.GetRight());
dc.GradientFillLinear(r, yellow, red);
}
#ifdef EXPERIMENTAL_METER_LED_STYLE
if (!mBar[i].vert)
{
wxRect r = mBar[i].r;
wxPen BackgroundPen;
BackgroundPen.SetColour( wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE) );
dc.SetPen( BackgroundPen );
int i;
for(i=0;i<r.width;i++)
{
// 2 pixel spacing between the LEDs
if( (i%7)<2 ){
AColor::Line( dc, i+r.x, r.y, i+r.x, r.y+r.height );
} else {
// The LEDs have triangular ends.
// This code shapes the ends.
int j = abs( (i%7)-4);
AColor::Line( dc, i+r.x, r.y, i+r.x, r.y+j +1);
AColor::Line( dc, i+r.x, r.y+r.height-j, i+r.x, r.y+r.height );
}
}
}
#endif
}
}
mRuler.SetTickColour( clrText );
dc.SetTextForeground( clrText );
// Draw the ruler
#ifndef EXPERIMENTAL_DA
mRuler.Draw(dc);
#endif
// Bitmap created...unselect
dc.SelectObject(wxNullBitmap);
}
// Copy predrawn bitmap to the dest DC
destDC.DrawBitmap(*mBitmap, 0, 0);
// Go draw the meter bars, Left & Right channels using current levels
for (unsigned int i = 0; i < mNumBars; i++)
{
DrawMeterBar(destDC, &mBar[i]);
}
destDC.SetTextForeground( clrText );
#ifndef EXPERIMENTAL_DA
// We can have numbers over the bars, in which case we have to draw them each time.
if (mStyle == HorizontalStereoCompact || mStyle == VerticalStereoCompact)
{
mRuler.SetTickColour( clrText );
// If the text colour is too similar to the meter colour, then we need a background
// for the text. We require a total of at least one full-scale RGB difference.
int d = theTheme.ColourDistance( clrText, theTheme.Colour( clrMeterOutputRMSBrush ) );
if( d < 256 )
{
destDC.SetBackgroundMode( wxSOLID );
destDC.SetTextBackground( clrBoxFill );
}
mRuler.Draw(destDC);
}
#endif
// Let the user know they can click to start monitoring
if( mIsInput && !mActive )
{
destDC.SetFont( GetFont() );
wxArrayStringEx texts{
_("Click to Start Monitoring") ,
_("Click for Monitoring") ,
_("Click to Start") ,
_("Click") ,
};
for( size_t i = 0, cnt = texts.size(); i < cnt; i++ )
{
wxString Text = wxT(" ") + texts[i] + wxT(" ");
wxSize Siz = destDC.GetTextExtent( Text );
Siz.SetWidth( Siz.GetWidth() + gap );
Siz.SetHeight( Siz.GetHeight() + gap );
if( mBar[0].vert)
{
if( Siz.GetWidth() < mBar[0].r.GetHeight() )
{
wxRect r( mBar[1].b.GetLeft() - (int) (Siz.GetHeight() / 2.0) + 0.5,
mBar[0].r.GetTop() + (int) ((mBar[0].r.GetHeight() - Siz.GetWidth()) / 2.0) + 0.5,
Siz.GetHeight(),
Siz.GetWidth() );
destDC.SetBrush( wxBrush( clrBoxFill ) );
destDC.SetPen( *wxWHITE_PEN );
destDC.DrawRectangle( r );
destDC.SetBackgroundMode( wxTRANSPARENT );
r.SetTop( r.GetBottom() + (gap / 2) );
destDC.SetTextForeground( clrText );
destDC.DrawRotatedText( Text, r.GetPosition(), 90 );
break;
}
}
else
{
if( Siz.GetWidth() < mBar[0].r.GetWidth() )
{
wxRect r( mBar[0].r.GetLeft() + (int) ((mBar[0].r.GetWidth() - Siz.GetWidth()) / 2.0) + 0.5,
mBar[1].b.GetTop() - (int) (Siz.GetHeight() / 2.0) + 0.5,
Siz.GetWidth(),
Siz.GetHeight() );
destDC.SetBrush( wxBrush( clrBoxFill ) );
destDC.SetPen( *wxWHITE_PEN );
destDC.DrawRectangle( r );
destDC.SetBackgroundMode( wxTRANSPARENT );
r.SetLeft( r.GetLeft() + (gap / 2) );
r.SetTop( r.GetTop() + (gap / 2));
destDC.SetTextForeground( clrText );
destDC.DrawText( Text, r.GetPosition() );
break;
}
}
}
}
if (mIsFocused)
{
wxRect r = mIconRect;
AColor::DrawFocus(destDC, r.Inflate(1, 1));
}
}
void MeterPanel::OnSize(wxSizeEvent & WXUNUSED(event))
{
GetClientSize(&mWidth, &mHeight);
mLayoutValid = false;
Refresh();
}
bool MeterPanel::InIcon(wxMouseEvent *pEvent) const
{
auto point = pEvent ? pEvent->GetPosition() : ScreenToClient(::wxGetMousePosition());
return mIconRect.Contains(point);
}
void MeterPanel::OnMouse(wxMouseEvent &evt)
{
bool shouldHighlight = InIcon(&evt);
if ((evt.GetEventType() == wxEVT_MOTION || evt.Entering() || evt.Leaving()) &&
(mHighlighted != shouldHighlight)) {
mHighlighted = shouldHighlight;
mLayoutValid = false;
Refresh();
}
if (mStyle == MixerTrackCluster) // MixerTrackCluster style has no menu.
return;
#if wxUSE_TOOLTIPS // Not available in wxX11
if (evt.Leaving()){
ProjectStatus::Get( *mProject ).Set({});
}
else if (evt.Entering()) {
// Display the tooltip in the status bar
wxToolTip * pTip = this->GetToolTip();
if( pTip ) {
auto tipText = Verbatim( pTip->GetTip() );
ProjectStatus::Get( *mProject ).Set(tipText);
}
}
#endif
if (evt.RightDown() ||
(evt.ButtonDown() && InIcon(&evt)))
{
wxMenu menu;
// Note: these should be kept in the same order as the enum
if (mIsInput) {
wxMenuItem *mi;
if (mMonitoring)
mi = menu.Append(OnMonitorID, _("Stop Monitoring"));
else
mi = menu.Append(OnMonitorID, _("Start Monitoring"));
mi->Enable(!mActive || mMonitoring);
}
menu.Append(OnPreferencesID, _("Options..."));
if (evt.RightDown()) {
ShowMenu(evt.GetPosition());
}
else {
ShowMenu(wxPoint(mIconRect.x + 1, mIconRect.y + mIconRect.height + 1));
}
}
else if (evt.LeftDown()) {
if (mIsInput) {
if (mActive && !mMonitoring) {
Reset(mRate, true);
}
else {
StartMonitoring();
}
}
else {
Reset(mRate, true);
}
}
}
void MeterPanel::OnContext(wxContextMenuEvent &evt)
{
#if defined(__WXMSW__)
if (mHadKeyDown)
#endif
if (mStyle != MixerTrackCluster) // MixerTrackCluster style has no menu.
{
ShowMenu(wxPoint(mIconRect.x + 1, mIconRect.y + mIconRect.height + 1));
}
else
{
evt.Skip();
}
#if defined(__WXMSW__)
mHadKeyDown = false;
#endif
}
void MeterPanel::OnKeyDown(wxKeyEvent &evt)
{
switch (evt.GetKeyCode())
{
// These are handled in the OnKeyUp handler because, on Windows at least, the
// key up event will be passed on to the menu if we show it here. This causes
// the default sound to be heard if assigned.
//
// But, again on Windows, when the user selects a menu item, it is handled by
// the menu and the key up event is passed along to our OnKeyUp() handler, so
// we have to ignore it, otherwise we'd just show the menu again.
case WXK_RETURN:
case WXK_NUMPAD_ENTER:
case WXK_WINDOWS_MENU:
case WXK_MENU:
#if defined(__WXMSW__)
mHadKeyDown = true;
#endif
break;
case WXK_RIGHT:
Navigate(wxNavigationKeyEvent::IsForward);
break;
case WXK_LEFT:
Navigate(wxNavigationKeyEvent::IsBackward);
break;
case WXK_TAB:
if (evt.ShiftDown())
Navigate(wxNavigationKeyEvent::IsBackward);
else
Navigate(wxNavigationKeyEvent::IsForward);
break;
default:
evt.Skip();
break;
}
}
void MeterPanel::OnKeyUp(wxKeyEvent &evt)
{
switch (evt.GetKeyCode())
{
case WXK_RETURN:
case WXK_NUMPAD_ENTER:
#if defined(__WXMSW__)
if (mHadKeyDown)
#endif
if (mStyle != MixerTrackCluster) // MixerTrackCluster style has no menu.
{
ShowMenu(wxPoint(mIconRect.x + 1, mIconRect.y + mIconRect.height + 1));
}
#if defined(__WXMSW__)
mHadKeyDown = false;
#endif
break;
default:
evt.Skip();
break;
}
}
void MeterPanel::OnSetFocus(wxFocusEvent & WXUNUSED(evt))
{
mIsFocused = true;
Refresh(false);
}
void MeterPanel::OnKillFocus(wxFocusEvent & WXUNUSED(evt))
{
mIsFocused = false;
Refresh(false);
}
void MeterPanel::SetStyle(Style newStyle)
{
if (mStyle != newStyle && mDesiredStyle == AutomaticStereo)
{
SetActiveStyle(newStyle);
mLayoutValid = false;
Refresh(false);
}
}
void MeterPanel::Reset(double sampleRate, bool resetClipping)
{
mT = 0;
mRate = sampleRate;
for (int j = 0; j < kMaxMeterBars; j++)
{
ResetBar(&mBar[j], resetClipping);
}
// wxTimers seem to be a little unreliable - sometimes they stop for
// no good reason, so this "primes" it every now and then...
mTimer.Stop();
// While it's stopped, empty the queue
mQueue.Clear();
mLayoutValid = false;
mTimer.Start(1000 / mMeterRefreshRate);
Refresh(false);
}
static float floatMax(float a, float b)
{
return a>b? a: b;
}
/* Unused as yet.
static int intmin(int a, int b)
{
return a<b? a: b;
}
*/
static int intmax(int a, int b)
{
return a>b? a: b;
}
static float ClipZeroToOne(float z)
{
if (z > 1.0)
return 1.0;
else if (z < 0.0)
return 0.0;
else
return z;
}
static float ToDB(float v, float range)
{
double db;
if (v > 0)
db = LINEAR_TO_DB(fabs(v));
else
db = -999;
return ClipZeroToOne((db + range) / range);
}
void MeterPanel::UpdateDisplay(unsigned numChannels, int numFrames, float *sampleData)
{
float *sptr = sampleData;
auto num = std::min(numChannels, mNumBars);
MeterUpdateMsg msg;
memset(&msg, 0, sizeof(msg));
msg.numFrames = numFrames;
for(int i=0; i<numFrames; i++) {
for(unsigned int j=0; j<num; j++) {
msg.peak[j] = floatMax(msg.peak[j], fabs(sptr[j]));