forked from Sneeds-Feed-and-Seed/sneedacity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFreqWindow.cpp
1246 lines (1003 loc) · 33.7 KB
/
FreqWindow.cpp
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
/**********************************************************************
Sneedacity: A Digital Audio Editor
FreqWindow.cpp
Dominic Mazzoni
*******************************************************************//**
\class FrequencyPlotDialog
\brief Displays a spectrum plot of the waveform. Has options for
selecting parameters of the plot.
Has a feature that finds peaks and reports their value as you move
the mouse around.
*//****************************************************************//**
\class FreqPlot
\brief Works with FrequencyPlotDialog to dsplay a spectrum plot of the waveform.
This class actually does the graph display.
Has a feature that finds peaks and reports their value as you move
the mouse around.
*//*******************************************************************/
/*
Salvo Ventura - November 2006
Extended range check for additional FFT windows
*/
#include "FreqWindow.h"
#include <algorithm>
#include <wx/setup.h> // for wxUSE_* macros
#include <wx/brush.h>
#include <wx/button.h>
#include <wx/checkbox.h>
#include <wx/choice.h>
#include <wx/dcclient.h>
#include <wx/dcmemory.h>
#include <wx/font.h>
#include <wx/image.h>
#include <wx/file.h>
#include <wx/intl.h>
#include <wx/scrolbar.h>
#include <wx/sizer.h>
#include <wx/slider.h>
#include <wx/statbmp.h>
#include <wx/stattext.h>
#include <wx/statusbr.h>
#include <wx/textctrl.h>
#include <wx/textfile.h>
#include <wx/wfstream.h>
#include <wx/txtstrm.h>
#include <math.h>
#include "ShuttleGui.h"
#include "AColor.h"
#include "CommonCommandFlags.h"
#include "FFT.h"
#include "PitchName.h"
#include "prefs/GUISettings.h"
#include "Prefs.h"
#include "Project.h"
#include "ProjectWindow.h"
#include "Theme.h"
#include "ViewInfo.h"
#include "AllThemeResources.h"
#include "FileNames.h"
#include "WaveTrack.h"
#include "./widgets/HelpSystem.h"
#include "widgets/SneedacityMessageBox.h"
#include "widgets/Ruler.h"
#if wxUSE_ACCESSIBILITY
#include "widgets/WindowAccessible.h"
#endif
#define FrequencyAnalysisTitle XO("Frequency Analysis")
DEFINE_EVENT_TYPE(EVT_FREQWINDOW_RECALC);
enum {
FirstID = 7000,
FreqZoomSliderID,
FreqPanScrollerID,
FreqExportButtonID,
FreqAlgChoiceID,
FreqSizeChoiceID,
FreqFuncChoiceID,
FreqAxisChoiceID,
ReplotButtonID,
GridOnOffID
};
// These specify the minimum plot window width
#define FREQ_WINDOW_WIDTH 480
#define FREQ_WINDOW_HEIGHT 330
static const char * ZoomIn[] = {
"16 16 6 1",
" c None",
"+ c #1C1C1C",
"@ c #AEAEAE",
"# c #F7F7F7",
"$ c #CFCECC",
"* c #1C1CA0",
" ++++ ",
" @+# @$+@ ",
" + @** +@ ",
" +#@ ** #+ ",
" +@****** +@",
" + ****** +@",
" +# ** #+@",
" + ** +@@",
" +++# #+@@ ",
" +++@++++@@ ",
" +++@@ @@@@ ",
" +++@@ ",
" +++@@ ",
"+++@@ ",
"@+@@ ",
" @@ "};
static const char * ZoomOut[] = {
"16 16 6 1",
" c None",
"+ c #1C1C1C",
"@ c #AEAEAE",
"# c #F7F7F7",
"$ c #CFCECC",
"* c #1C1CA0",
" ++++ ",
" @+# $+@ ",
" + @@ +@ ",
" +# @ #+ ",
" +@****** +@",
" + ****** +@",
" +# #+@",
" + +@@",
" +++# #+@@ ",
" +++@++++@@ ",
" +++@@ @@@@ ",
" +++@@ ",
" +++@@ ",
"+++@@ ",
"@+@@ ",
" @@ "};
// FrequencyPlotDialog
BEGIN_EVENT_TABLE(FrequencyPlotDialog, wxDialogWrapper)
EVT_CLOSE(FrequencyPlotDialog::OnCloseWindow)
EVT_SIZE(FrequencyPlotDialog::OnSize)
EVT_SLIDER(FreqZoomSliderID, FrequencyPlotDialog::OnZoomSlider)
EVT_COMMAND_SCROLL(FreqPanScrollerID, FrequencyPlotDialog::OnPanScroller)
EVT_CHOICE(FreqAlgChoiceID, FrequencyPlotDialog::OnAlgChoice)
EVT_CHOICE(FreqSizeChoiceID, FrequencyPlotDialog::OnSizeChoice)
EVT_CHOICE(FreqFuncChoiceID, FrequencyPlotDialog::OnFuncChoice)
EVT_CHOICE(FreqAxisChoiceID, FrequencyPlotDialog::OnAxisChoice)
EVT_BUTTON(FreqExportButtonID, FrequencyPlotDialog::OnExport)
EVT_BUTTON(ReplotButtonID, FrequencyPlotDialog::OnReplot)
EVT_BUTTON(wxID_CANCEL, FrequencyPlotDialog::OnCloseButton)
EVT_BUTTON(wxID_HELP, FrequencyPlotDialog::OnGetURL)
EVT_CHECKBOX(GridOnOffID, FrequencyPlotDialog::OnGridOnOff)
EVT_COMMAND(wxID_ANY, EVT_FREQWINDOW_RECALC, FrequencyPlotDialog::OnRecalc)
END_EVENT_TABLE()
FrequencyPlotDialog::FrequencyPlotDialog(wxWindow * parent, wxWindowID id,
SneedacityProject &project,
const TranslatableString & title,
const wxPoint & pos)
: wxDialogWrapper(parent, id, title, pos, wxDefaultSize,
wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxMAXIMIZE_BOX),
mAnalyst(std::make_unique<SpectrumAnalyst>())
, mProject{ &project }
{
SetName();
mMouseX = 0;
mMouseY = 0;
mRate = 0;
mDataLen = 0;
gPrefs->Read(wxT("/FrequencyPlotDialog/DrawGrid"), &mDrawGrid, true);
gPrefs->Read(wxT("/FrequencyPlotDialog/SizeChoice"), &mSize, 3);
int alg;
gPrefs->Read(wxT("/FrequencyPlotDialog/AlgChoice"), &alg, 0);
mAlg = static_cast<SpectrumAnalyst::Algorithm>(alg);
gPrefs->Read(wxT("/FrequencyPlotDialog/FuncChoice"), &mFunc, 3);
gPrefs->Read(wxT("/FrequencyPlotDialog/AxisChoice"), &mAxis, 1);
Populate();
}
FrequencyPlotDialog::~FrequencyPlotDialog()
{
}
void FrequencyPlotDialog::Populate()
{
SetTitle(FrequencyAnalysisTitle);
TranslatableStrings algChoices{
XO("Spectrum") ,
XO("Standard Autocorrelation") ,
XO("Cuberoot Autocorrelation") ,
XO("Enhanced Autocorrelation") ,
/* i18n-hint: This is a technical term, derived from the word
* "spectrum". Do not translate it unless you are sure you
* know the correct technical word in your language. */
XO("Cepstrum") ,
};
TranslatableStrings sizeChoices{
Verbatim( "128" ) ,
Verbatim( "256" ) ,
Verbatim( "512" ) ,
Verbatim( "1024" ) ,
Verbatim( "2048" ) ,
Verbatim( "4096" ) ,
Verbatim( "8192" ) ,
Verbatim( "16384" ) ,
Verbatim( "32768" ) ,
Verbatim( "65536" ) ,
};
TranslatableStrings funcChoices;
for (int i = 0, cnt = NumWindowFuncs(); i < cnt; i++)
{
funcChoices.push_back(
/* i18n-hint: This refers to a "window function",
* such as Hann or Rectangular, used in the
* Frequency analyze dialog box. */
XO("%s window").Format( WindowFuncName(i) ) );
}
TranslatableStrings axisChoices{
XO("Linear frequency") ,
XO("Log frequency") ,
};
mFreqFont = wxFont(fontSize, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
mArrowCursor = std::make_unique<wxCursor>(wxCURSOR_ARROW);
mCrossCursor = std::make_unique<wxCursor>(wxCURSOR_CROSS);
long size;
// reinterpret one of the verbatim strings above as a number
sizeChoices[mSize].MSGID().GET().ToLong(&size);
mWindowSize = size;
gPrefs->Read(ENV_DB_KEY, &dBRange, ENV_DB_RANGE);
if(dBRange < 90.)
dBRange = 90.;
ShuttleGui S(this, eIsCreating);
S.SetBorder(0);
S.AddSpace(5);
S.SetSizerProportion(1);
S.StartMultiColumn(3, wxEXPAND);
{
S.SetStretchyCol(1);
S.SetStretchyRow(0);
// -------------------------------------------------------------------
// ROW 1: Freq response panel and sliders for vertical scale
// -------------------------------------------------------------------
S.StartVerticalLay(2);
{
vRuler = safenew RulerPanel(
S.GetParent(), wxID_ANY, wxVERTICAL,
wxSize{ 100, 100 }, // Ruler can't handle small sizes
RulerPanel::Range{ 0.0, -dBRange },
Ruler::LinearDBFormat,
XO("dB"),
RulerPanel::Options{}
.LabelEdges(true)
.TickColour( theTheme.Colour( clrGraphLabels ) )
);
S.AddSpace(wxDefaultCoord, 1);
S.Prop(1)
.Position(wxALIGN_RIGHT | wxALIGN_TOP)
.AddWindow(vRuler);
S.AddSpace(wxDefaultCoord, 1);
}
S.EndVerticalLay();
mFreqPlot = safenew FreqPlot(S.GetParent(), wxID_ANY);
S.Prop(1)
.Position(wxEXPAND)
.MinSize( { wxDefaultCoord, FREQ_WINDOW_HEIGHT } )
.AddWindow(mFreqPlot);
S.StartHorizontalLay(wxEXPAND, 0);
{
S.StartVerticalLay();
{
mPanScroller = safenew wxScrollBar(S.GetParent(), FreqPanScrollerID,
wxDefaultPosition, wxDefaultSize, wxSB_VERTICAL);
#if wxUSE_ACCESSIBILITY
// so that name can be set on a standard control
mPanScroller->SetAccessible(safenew WindowAccessible(mPanScroller));
#endif
S.Prop(1);
S
.Name(XO("Scroll"))
.Position( wxALIGN_LEFT | wxTOP)
.AddWindow(mPanScroller);
}
S.EndVerticalLay();
S.StartVerticalLay();
{
wxStaticBitmap *zi = safenew wxStaticBitmap(S.GetParent(), wxID_ANY, wxBitmap(ZoomIn));
S.Position(wxALIGN_CENTER)
.AddWindow(zi);
S.AddSpace(5);
mZoomSlider = safenew wxSliderWrapper(S.GetParent(), FreqZoomSliderID, 100, 1, 100,
wxDefaultPosition, wxDefaultSize, wxSL_VERTICAL);
S.Prop(1);
S
.Name(XO("Zoom"))
.Position(wxALIGN_CENTER_HORIZONTAL)
.AddWindow(mZoomSlider);
#if wxUSE_ACCESSIBILITY
// so that name can be set on a standard control
mZoomSlider->SetAccessible(safenew WindowAccessible(mZoomSlider));
#endif
S.AddSpace(5);
wxStaticBitmap *zo = safenew wxStaticBitmap(S.GetParent(), wxID_ANY, wxBitmap(ZoomOut));
S.Position(wxALIGN_CENTER)
.AddWindow(zo);
}
S.EndVerticalLay();
S.AddSpace(5, wxDefaultCoord);
}
S.EndHorizontalLay();
// -------------------------------------------------------------------
// ROW 2: Frequency ruler
// -------------------------------------------------------------------
S.AddSpace(1);
S.StartHorizontalLay(wxEXPAND, 0);
{
hRuler = safenew RulerPanel(
S.GetParent(), wxID_ANY, wxHORIZONTAL,
wxSize{ 100, 100 }, // Ruler can't handle small sizes
RulerPanel::Range{ 10, 20000 },
Ruler::RealFormat,
XO("Hz"),
RulerPanel::Options{}
.Log(true)
.Flip(true)
.LabelEdges(true)
.TickColour( theTheme.Colour( clrGraphLabels ) )
);
S.AddSpace(1, wxDefaultCoord);
S.Prop(1)
.Position(wxALIGN_LEFT | wxALIGN_TOP)
.AddWindow(hRuler);
S.AddSpace(1, wxDefaultCoord);
}
S.EndHorizontalLay();
S.AddSpace(1);
// -------------------------------------------------------------------
// ROW 3: Spacer
// -------------------------------------------------------------------
S.AddSpace(5);
S.AddSpace(5);
S.AddSpace(5);
// -------------------------------------------------------------------
// ROW 4: Info
// -------------------------------------------------------------------
S.AddSpace(1);
S.StartHorizontalLay(wxEXPAND);
{
S.SetSizerProportion(1);
S.StartMultiColumn(6);
S.SetStretchyCol(1);
S.SetStretchyCol(3);
{
S.AddPrompt(XXO("Cursor:"));
mCursorText = S.Style(wxTE_READONLY)
.AddTextBox( {}, wxT(""), 10);
S.AddPrompt(XXO("Peak:"));
mPeakText = S.Style(wxTE_READONLY)
.AddTextBox( {}, wxT(""), 10);
S.AddSpace(5);
mGridOnOff = S.Id(GridOnOffID).AddCheckBox(XXO("&Grids"), mDrawGrid);
}
S.EndMultiColumn();
}
S.EndHorizontalLay();
S.AddSpace(1);
}
S.EndMultiColumn();
// -------------------------------------------------------------------
// ROW 5: Spacer
// -------------------------------------------------------------------
S.AddSpace(5);
S.SetBorder(2);
S.SetSizerProportion(0);
S.StartMultiColumn(9, wxALIGN_CENTER);
{
// ----------------------------------------------------------------
// ROW 6: Algorithm, Size, Export, Replot
// ----------------------------------------------------------------
S.AddSpace(5);
mAlgChoice = S.Id(FreqAlgChoiceID).Focus()
.MinSize( { wxDefaultCoord, wxDefaultCoord } )
.AddChoice(XXO("&Algorithm:"), algChoices, mAlg);
S.AddSpace(5);
mSizeChoice = S.Id(FreqSizeChoiceID)
.MinSize( { wxDefaultCoord, wxDefaultCoord } )
.AddChoice(XXO("&Size:"), sizeChoices, mSize);
S.AddSpace(5);
mExportButton = S.Id(FreqExportButtonID).AddButton(XXO("&Export..."));
S.AddSpace(5);
// ----------------------------------------------------------------
// ROW 7: Function, Axix, Grids, Close
// ----------------------------------------------------------------
S.AddSpace(5);
mFuncChoice = S.Id(FreqFuncChoiceID)
.MinSize( { wxDefaultCoord, wxDefaultCoord } )
.AddChoice(XXO("&Function:"), funcChoices, mFunc);
mFuncChoice->MoveAfterInTabOrder(mSizeChoice);
S.AddSpace(5);
mAxisChoice = S.Id(FreqAxisChoiceID)
.MinSize( { wxDefaultCoord, wxDefaultCoord } )
.AddChoice(XXO("&Axis:"), axisChoices, mAxis);
mAxisChoice->MoveAfterInTabOrder(mFuncChoice);
S.AddSpace(5);
mReplotButton = S.Id(ReplotButtonID).AddButton(XXO("&Replot..."));
S.AddSpace(5);
//mCloseButton = S.Id(wxID_CANCEL).AddButton(XO("&Close"));
//S.AddSpace(5);
}
S.EndMultiColumn();
S.AddStandardButtons( eHelpButton | eCloseButton );
// -------------------------------------------------------------------
// ROW 8: Spacer
// -------------------------------------------------------------------
S.AddSpace(5);
mProgress = safenew FreqGauge(S.GetParent(), wxID_ANY); //, wxST_SIZEGRIP);
S.Position(wxEXPAND)
.AddWindow(mProgress);
// Log-frequency axis works for spectrum plots only.
if (mAlg != SpectrumAnalyst::Spectrum)
{
mAxis = 0;
mAxisChoice->Disable();
}
mLogAxis = mAxis != 0;
mCloseButton = static_cast<wxButton*>(FindWindowById( wxID_CANCEL ));
mCloseButton->SetDefault();
Layout();
Fit();
// Bug 1607:
Center();
SetMinSize(GetSize());
#if defined(__WXGTK__)
// This should be rechecked with wx3.
//
// The scrollbar (focus some reason) doesn't allow tabbing past it
// because it can't receive focus. So, convince it otherwise.
//
// Unfortunately, this still doesn't let you adjust the scrollbar
// from the keyboard. Near as I can tell, wxWGTK is capturing the
// keyboard input, so the GTK widget doesn't see it, preventing
// the normal scroll events from being generated.
//
// I guess the only way round it would be to handle key actions
// ourselves, but we'll leave that for a future date.
// gtk_widget_set_can_focus(mPanScroller->m_widget, true);
#endif
}
void FrequencyPlotDialog::OnGetURL(wxCommandEvent & WXUNUSED(event))
{
// Original help page is back on-line (March 2016), but the manual should be more reliable.
// http://www.eramp.com/WCAG_2_audio_contrast_tool_help.htm
HelpSystem::ShowHelp(this, L"Plot Spectrum");
}
bool FrequencyPlotDialog::Show(bool show)
{
if (!show)
{
mFreqPlot->SetCursor(*mArrowCursor);
}
bool shown = IsShown();
if (show && !shown)
{
gPrefs->Read(ENV_DB_KEY, &dBRange, ENV_DB_RANGE);
if(dBRange < 90.)
dBRange = 90.;
GetAudio();
// Don't send an event. We need the recalc right away.
// so that mAnalyst is valid when we paint.
//SendRecalcEvent();
Recalc();
}
bool res = wxDialogWrapper::Show(show);
return res;
}
void FrequencyPlotDialog::GetAudio()
{
mData.reset();
mDataLen = 0;
int selcount = 0;
bool warning = false;
for (auto track : TrackList::Get( *mProject ).Selected< const WaveTrack >()) {
auto &selectedRegion = ViewInfo::Get( *mProject ).selectedRegion;
if (selcount==0) {
mRate = track->GetRate();
auto start = track->TimeToLongSamples(selectedRegion.t0());
auto end = track->TimeToLongSamples(selectedRegion.t1());
auto dataLen = end - start;
if (dataLen > 10485760) {
warning = true;
mDataLen = 10485760;
}
else
// dataLen is not more than 10 * 2 ^ 20
mDataLen = dataLen.as_size_t();
mData = Floats{ mDataLen };
// Don't allow throw for bad reads
track->GetFloats(mData.get(), start, mDataLen,
fillZero, false);
}
else {
if (track->GetRate() != mRate) {
SneedacityMessageBox(
XO(
"To plot the spectrum, all selected tracks must be the same sample rate.") );
mData.reset();
mDataLen = 0;
return;
}
auto start = track->TimeToLongSamples(selectedRegion.t0());
Floats buffer2{ mDataLen };
// Again, stop exceptions
track->GetFloats(buffer2.get(), start, mDataLen,
fillZero, false);
for (size_t i = 0; i < mDataLen; i++)
mData[i] += buffer2[i];
}
selcount++;
}
if (selcount == 0)
return;
if (warning) {
auto msg = XO(
"Too much audio was selected. Only the first %.1f seconds of audio will be analyzed.")
.Format(mDataLen / mRate);
SneedacityMessageBox( msg );
}
}
void FrequencyPlotDialog::OnSize(wxSizeEvent & WXUNUSED(event))
{
Layout();
DrawPlot();
Refresh(true);
}
void FrequencyPlotDialog::DrawBackground(wxMemoryDC & dc)
{
Layout();
mBitmap.reset();
mPlotRect = mFreqPlot->GetClientRect();
mBitmap = std::make_unique<wxBitmap>(mPlotRect.width, mPlotRect.height,24);
dc.SelectObject(*mBitmap);
dc.SetBackground(wxBrush(wxColour(254, 254, 254)));// DONT-THEME Mask colour.
dc.Clear();
dc.SetPen(*wxBLACK_PEN);
dc.SetBrush(*wxWHITE_BRUSH);
dc.DrawRectangle(mPlotRect);
dc.SetFont(mFreqFont);
}
void FrequencyPlotDialog::DrawPlot()
{
if (!mData || mDataLen < mWindowSize || mAnalyst->GetProcessedSize() == 0) {
wxMemoryDC memDC;
vRuler->ruler.SetLog(false);
vRuler->ruler.SetRange(0.0, -dBRange);
hRuler->ruler.SetLog(false);
hRuler->ruler.SetRange(0, 1);
DrawBackground(memDC);
if (mDataLen < mWindowSize) {
wxString msg = _("Not enough data selected.");
wxSize sz = memDC.GetTextExtent(msg);
memDC.DrawText(msg,
(mPlotRect.GetWidth() - sz.GetWidth()) / 2,
(mPlotRect.GetHeight() - sz.GetHeight()) / 2);
}
memDC.SelectObject(wxNullBitmap);
mFreqPlot->Refresh();
Refresh();
return;
}
float yRange = mYMax - mYMin;
float yTotal = yRange * ((float) mZoomSlider->GetValue() / 100.0f);
int sTotal = yTotal * 100;
int sRange = yRange * 100;
int sPos = mPanScroller->GetThumbPosition() + ((mPanScroller->GetThumbSize() - sTotal) / 2);
mPanScroller->SetScrollbar(sPos, sTotal, sRange, sTotal);
float yMax = mYMax - ((float)sPos / 100);
float yMin = yMax - yTotal;
// Set up y axis ruler
if (mAlg == SpectrumAnalyst::Spectrum) {
vRuler->ruler.SetUnits(XO("dB"));
vRuler->ruler.SetFormat(Ruler::LinearDBFormat);
} else {
vRuler->ruler.SetUnits({});
vRuler->ruler.SetFormat(Ruler::RealFormat);
}
int w1, w2, h;
vRuler->ruler.GetMaxSize(&w1, &h);
vRuler->ruler.SetRange(yMax, yMin); // Note inversion for vertical.
vRuler->ruler.GetMaxSize(&w2, &h);
if( w1 != w2 ) // Reduces flicker
{
vRuler->SetMinSize(wxSize(w2,h));
Layout();
}
vRuler->Refresh(false);
wxMemoryDC memDC;
DrawBackground(memDC);
// Get the plot dimensions
//
// Must be done after setting the vertical ruler above since the
// the width could change.
wxRect r = mPlotRect;
// Set up x axis ruler
int width = r.width - 2;
float xMin, xMax, xRatio, xStep;
if (mAlg == SpectrumAnalyst::Spectrum) {
xMin = mRate / mWindowSize;
xMax = mRate / 2;
xRatio = xMax / xMin;
if (mLogAxis)
{
xStep = pow(2.0f, (log(xRatio) / log(2.0f)) / width);
hRuler->ruler.SetLog(true);
}
else
{
xStep = (xMax - xMin) / width;
hRuler->ruler.SetLog(false);
}
hRuler->ruler.SetUnits(XO("Hz"));
} else {
xMin = 0;
xMax = mAnalyst->GetProcessedSize() / mRate;
xStep = (xMax - xMin) / width;
hRuler->ruler.SetLog(false);
/* i18n-hint: short form of 'seconds'.*/
hRuler->ruler.SetUnits(XO("s"));
}
hRuler->ruler.SetRange(xMin, xMax-xStep);
hRuler->Refresh(false);
// Draw the plot
if (mAlg == SpectrumAnalyst::Spectrum)
memDC.SetPen(wxPen(theTheme.Colour( clrHzPlot ), 1, wxPENSTYLE_SOLID));
else
memDC.SetPen(wxPen(theTheme.Colour( clrWavelengthPlot), 1, wxPENSTYLE_SOLID));
float xPos = xMin;
for (int i = 0; i < width; i++) {
float y;
if (mLogAxis)
y = mAnalyst->GetProcessedValue(xPos, xPos * xStep);
else
y = mAnalyst->GetProcessedValue(xPos, xPos + xStep);
float ynorm = (y - yMin) / yTotal;
int lineheight = (int)(ynorm * (r.height - 1));
if (lineheight > r.height - 2)
lineheight = r.height - 2;
if (ynorm > 0.0)
AColor::Line(memDC, r.x + 1 + i, r.y + r.height - 1 - lineheight,
r.x + 1 + i, r.y + r.height - 1);
if (mLogAxis)
xPos *= xStep;
else
xPos += xStep;
}
// Outline the graph
memDC.SetPen(*wxBLACK_PEN);
memDC.SetBrush(*wxTRANSPARENT_BRUSH);
memDC.DrawRectangle(r);
if(mDrawGrid)
{
hRuler->ruler.DrawGrid(memDC, r.height, true, true, 1, 1);
vRuler->ruler.DrawGrid(memDC, r.width, true, true, 1, 1);
}
memDC.SelectObject( wxNullBitmap );
mFreqPlot->Refresh();
}
void FrequencyPlotDialog::PlotMouseEvent(wxMouseEvent & event)
{
if (event.Moving() && (event.m_x != mMouseX || event.m_y != mMouseY)) {
mMouseX = event.m_x;
mMouseY = event.m_y;
if (mPlotRect.Contains(mMouseX, mMouseY))
mFreqPlot->SetCursor(*mCrossCursor);
else
mFreqPlot->SetCursor(*mArrowCursor);
mFreqPlot->Refresh(false);
}
}
void FrequencyPlotDialog::OnPanScroller(wxScrollEvent & WXUNUSED(event))
{
DrawPlot();
}
void FrequencyPlotDialog::OnZoomSlider(wxCommandEvent & WXUNUSED(event))
{
DrawPlot();
}
void FrequencyPlotDialog::OnAlgChoice(wxCommandEvent & WXUNUSED(event))
{
mAlg = SpectrumAnalyst::Algorithm(mAlgChoice->GetSelection());
// Log-frequency axis works for spectrum plots only.
if (mAlg == SpectrumAnalyst::Spectrum) {
mAxisChoice->Enable(true);
mLogAxis = mAxisChoice->GetSelection() ? true : false;
}
else {
mAxisChoice->Disable();
mLogAxis = false;
}
SendRecalcEvent();
}
void FrequencyPlotDialog::OnSizeChoice(wxCommandEvent & WXUNUSED(event))
{
long windowSize = 0;
mSizeChoice->GetStringSelection().ToLong(&windowSize);
mWindowSize = windowSize;
SendRecalcEvent();
}
void FrequencyPlotDialog::OnFuncChoice(wxCommandEvent & WXUNUSED(event))
{
SendRecalcEvent();
}
void FrequencyPlotDialog::OnAxisChoice(wxCommandEvent & WXUNUSED(event))
{
mLogAxis = mAxisChoice->GetSelection() ? true : false;
DrawPlot();
}
void FrequencyPlotDialog::PlotPaint(wxPaintEvent & event)
{
wxPaintDC dc( (wxWindow *) event.GetEventObject() );
dc.DrawBitmap( *mBitmap, 0, 0, true );
// Fix for Bug 1226 "Plot Spectrum freezes... if insufficient samples selected"
if (!mData || mDataLen < mWindowSize)
return;
dc.SetFont(mFreqFont);
wxRect r = mPlotRect;
int width = r.width - 2;
float xMin, xMax, xRatio, xStep;
if (mAlg == SpectrumAnalyst::Spectrum) {
xMin = mRate / mWindowSize;
xMax = mRate / 2;
xRatio = xMax / xMin;
if (mLogAxis)
xStep = pow(2.0f, (log(xRatio) / log(2.0f)) / width);
else
xStep = (xMax - xMin) / width;
} else {
xMin = 0;
xMax = mAnalyst->GetProcessedSize() / mRate;
xStep = (xMax - xMin) / width;
}
float xPos = xMin;
// Find the peak nearest the cursor and plot it
if ( r.Contains(mMouseX, mMouseY) & (mMouseX!=0) & (mMouseX!=r.width-1) ) {
if (mLogAxis)
xPos = xMin * pow(xStep, mMouseX - (r.x + 1));
else
xPos = xMin + xStep * (mMouseX - (r.x + 1));
float bestValue = 0;
float bestpeak = mAnalyst->FindPeak(xPos, &bestValue);
int px;
if (mLogAxis)
px = (int)(log(bestpeak / xMin) / log(xStep));
else
px = (int)((bestpeak - xMin) * width / (xMax - xMin));
dc.SetPen(wxPen(wxColour(160,160,160), 1, wxPENSTYLE_SOLID));
AColor::Line(dc, r.x + 1 + px, r.y, r.x + 1 + px, r.y + r.height);
// print out info about the cursor location
float value;
if (mLogAxis) {
xPos = xMin * pow(xStep, mMouseX - (r.x + 1));
value = mAnalyst->GetProcessedValue(xPos, xPos * xStep);
} else {
xPos = xMin + xStep * (mMouseX - (r.x + 1));
value = mAnalyst->GetProcessedValue(xPos, xPos + xStep);
}
TranslatableString cursor;
TranslatableString peak;
if (mAlg == SpectrumAnalyst::Spectrum) {
auto xp = PitchName_Absolute(FreqToMIDInote(xPos));
auto pp = PitchName_Absolute(FreqToMIDInote(bestpeak));
/* i18n-hint: The %d's are replaced by numbers, the %s by musical notes, e.g. A#*/
cursor = XO("%d Hz (%s) = %d dB")
.Format( (int)(xPos + 0.5), xp, (int)(value + 0.5));
/* i18n-hint: The %d's are replaced by numbers, the %s by musical notes, e.g. A#*/
peak = XO("%d Hz (%s) = %.1f dB")
.Format( (int)(bestpeak + 0.5), pp, bestValue );
} else if (xPos > 0.0 && bestpeak > 0.0) {
auto xp = PitchName_Absolute(FreqToMIDInote(1.0 / xPos));
auto pp = PitchName_Absolute(FreqToMIDInote(1.0 / bestpeak));
/* i18n-hint: The %d's are replaced by numbers, the %s by musical notes, e.g. A#
* the %.4f are numbers, and 'sec' should be an abbreviation for seconds */
cursor = XO("%.4f sec (%d Hz) (%s) = %f")
.Format( xPos, (int)(1.0 / xPos + 0.5), xp, value );
/* i18n-hint: The %d's are replaced by numbers, the %s by musical notes, e.g. A#
* the %.4f are numbers, and 'sec' should be an abbreviation for seconds */
peak = XO("%.4f sec (%d Hz) (%s) = %.3f")
.Format( bestpeak, (int)(1.0 / bestpeak + 0.5), pp, bestValue );
}
mCursorText->SetValue( cursor.Translation() );
mPeakText->SetValue( peak.Translation() );
}
else {
mCursorText->SetValue(wxT(""));
mPeakText->SetValue(wxT(""));
}
// Outline the graph
dc.SetPen(*wxBLACK_PEN);
dc.SetBrush(*wxTRANSPARENT_BRUSH);
dc.DrawRectangle(r);
}
void FrequencyPlotDialog::OnCloseWindow(wxCloseEvent & WXUNUSED(event))
{
Show(false);
}
void FrequencyPlotDialog::OnCloseButton(wxCommandEvent & WXUNUSED(event))
{
gPrefs->Write(wxT("/FrequencyPlotDialog/DrawGrid"), mDrawGrid);
gPrefs->Write(wxT("/FrequencyPlotDialog/SizeChoice"), mSizeChoice->GetSelection());
gPrefs->Write(wxT("/FrequencyPlotDialog/AlgChoice"), mAlgChoice->GetSelection());
gPrefs->Write(wxT("/FrequencyPlotDialog/FuncChoice"), mFuncChoice->GetSelection());
gPrefs->Write(wxT("/FrequencyPlotDialog/AxisChoice"), mAxisChoice->GetSelection());
gPrefs->Flush();
Show(false);