Audacity 3.2.0
FreqWindow.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 FreqWindow.cpp
6
7 Dominic Mazzoni
8
9*******************************************************************//****************************************************************//*******************************************************************/
28
29/*
30 Salvo Ventura - November 2006
31 Extended range check for additional FFT windows
32*/
33
34#include "FreqWindow.h"
35
36#include <algorithm>
37
38#include <wx/setup.h> // for wxUSE_* macros
39
40#include <wx/brush.h>
41#include <wx/button.h>
42#include <wx/checkbox.h>
43#include <wx/choice.h>
44#include <wx/dcclient.h>
45#include <wx/dcmemory.h>
46#include <wx/font.h>
47#include <wx/file.h>
48#include <wx/frame.h>
49#include <wx/scrolbar.h>
50#include <wx/slider.h>
51#include <wx/statbmp.h>
52#include <wx/stattext.h>
53#include <wx/statusbr.h>
54
55#include <wx/textctrl.h>
56#include <wx/textfile.h>
57
58#include <wx/wfstream.h>
59#include <wx/txtstrm.h>
60
61#include <math.h>
62
63#include "AColor.h"
64#include "AllThemeResources.h"
65#include "BasicUI.h"
66#include "CommonCommandFlags.h"
67#include "Decibels.h"
68#include "FFT.h"
69#include "PitchName.h"
70#include "Prefs.h"
71#include "Project.h"
72#include "ProjectWindows.h"
73#include "SelectFile.h"
74#include "ShuttleGui.h"
75#include "Theme.h"
76#include "ViewInfo.h"
77
78#include "FileNames.h"
79
80#include "WaveTrack.h"
81
82#include "AudacityMessageBox.h"
83#include "HelpSystem.h"
84#include "widgets/FreqGauge.h"
88#include "widgets/RealFormat.h"
89#include "widgets/RulerPanel.h"
90
91#if wxUSE_ACCESSIBILITY
92#include "WindowAccessible.h"
93#endif
94
95#define FrequencyAnalysisTitle XO("Frequency Analysis")
96
97DEFINE_EVENT_TYPE(EVT_FREQWINDOW_RECALC);
98
99enum {
100 FirstID = 7000,
101
112
113// These specify the minimum plot window width
114
115#define FREQ_WINDOW_WIDTH 480
116#define FREQ_WINDOW_HEIGHT 330
117
118static const char * ZoomIn[] = {
119"16 16 6 1",
120" c None",
121"+ c #1C1C1C",
122"@ c #AEAEAE",
123"# c #F7F7F7",
124"$ c #CFCECC",
125"* c #1C1CA0",
126" ++++ ",
127" @+# @$+@ ",
128" + @** +@ ",
129" +#@ ** #+ ",
130" +@****** +@",
131" + ****** +@",
132" +# ** #+@",
133" + ** +@@",
134" +++# #+@@ ",
135" +++@++++@@ ",
136" +++@@ @@@@ ",
137" +++@@ ",
138" +++@@ ",
139"+++@@ ",
140"@+@@ ",
141" @@ "};
142
143static const char * ZoomOut[] = {
144"16 16 6 1",
145" c None",
146"+ c #1C1C1C",
147"@ c #AEAEAE",
148"# c #F7F7F7",
149"$ c #CFCECC",
150"* c #1C1CA0",
151" ++++ ",
152" @+# $+@ ",
153" + @@ +@ ",
154" +# @ #+ ",
155" +@****** +@",
156" + ****** +@",
157" +# #+@",
158" + +@@",
159" +++# #+@@ ",
160" +++@++++@@ ",
161" +++@@ @@@@ ",
162" +++@@ ",
163" +++@@ ",
164"+++@@ ",
165"@+@@ ",
166" @@ "};
167
168// FrequencyPlotDialog
169
170BEGIN_EVENT_TABLE(FrequencyPlotDialog, wxDialogWrapper)
184 EVT_COMMAND(wxID_ANY, EVT_FREQWINDOW_RECALC, FrequencyPlotDialog::OnRecalc)
186
188 wxWindow* parent, wxWindowID id, AudacityProject& project,
189 const TranslatableString& title, const wxPoint& pos)
192 parent, id, title, pos, wxDefaultSize,
193 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxMAXIMIZE_BOX)
194{
195 SetName();
196
197 mMouseX = 0;
198 mMouseY = 0;
199
200 Populate();
201}
202
204{
205}
206
208{
210
211 TranslatableStrings algChoices{
212 XO("Spectrum") ,
213 XO("Standard Autocorrelation") ,
214 XO("Cuberoot Autocorrelation") ,
215 XO("Enhanced Autocorrelation") ,
216 /* i18n-hint: This is a technical term, derived from the word
217 * "spectrum". Do not translate it unless you are sure you
218 * know the correct technical word in your language. */
219 XO("Cepstrum") ,
220 };
221
222 TranslatableStrings sizeChoices{
223 Verbatim( "128" ) ,
224 Verbatim( "256" ) ,
225 Verbatim( "512" ) ,
226 Verbatim( "1024" ) ,
227 Verbatim( "2048" ) ,
228 Verbatim( "4096" ) ,
229 Verbatim( "8192" ) ,
230 Verbatim( "16384" ) ,
231 Verbatim( "32768" ) ,
232 Verbatim( "65536" ) ,
233 Verbatim( "131072" ) ,
234 };
235
236 TranslatableStrings funcChoices;
237 for (int i = 0, cnt = NumWindowFuncs(); i < cnt; i++)
238 {
239 funcChoices.push_back(
240 /* i18n-hint: This refers to a "window function",
241 * such as Hann or Rectangular, used in the
242 * Frequency analyze dialog box. */
243 XO("%s window").Format( WindowFuncName(i) ) );
244 }
245
246 TranslatableStrings axisChoices{
247 XO("Linear frequency") ,
248 XO("Log frequency") ,
249 };
250
251 mFreqFont = wxFont(fontSize, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
252 mArrowCursor = std::make_unique<wxCursor>(wxCURSOR_ARROW);
253 mCrossCursor = std::make_unique<wxCursor>(wxCURSOR_CROSS);
254
255 long size;
256 // reinterpret one of the verbatim strings above as a number
257 sizeChoices[mSize].MSGID().GET().ToLong(&size);
259
261 if(dBRange < 90.)
262 dBRange = 90.;
263
264 ShuttleGui S(this, eIsCreating);
265
266 S.SetBorder(0);
267
268 S.AddSpace(5);
269
270 S.SetSizerProportion(1);
271 S.StartMultiColumn(3, wxEXPAND);
272 {
273 S.SetStretchyCol(1);
274 S.SetStretchyRow(0);
275
276 // -------------------------------------------------------------------
277 // ROW 1: Freq response panel and sliders for vertical scale
278 // -------------------------------------------------------------------
279
280 S.StartVerticalLay(2);
281 {
283 S.GetParent(), wxID_ANY, wxVERTICAL,
284 wxSize{ 100, 100 }, // Ruler can't handle small sizes
285 RulerPanel::Range{ 0.0, -dBRange },
287 XO("dB"),
289 .LabelEdges(true)
290 .TickColour( theTheme.Colour( clrGraphLabels ) )
291 );
292
293 S.AddSpace(wxDefaultCoord, 1);
294 S.Prop(1)
295 .Position(wxALIGN_RIGHT | wxALIGN_TOP)
296 .AddWindow(vRuler);
297 S.AddSpace(wxDefaultCoord, 1);
298 }
299 S.EndVerticalLay();
300
301 mFreqPlot = safenew FreqPlot(S.GetParent(), wxID_ANY);
302 S.Prop(1)
303 .Position(wxEXPAND)
304 .MinSize( { wxDefaultCoord, FREQ_WINDOW_HEIGHT } )
305 .AddWindow(mFreqPlot);
306
307 S.StartHorizontalLay(wxEXPAND, 0);
308 {
309 S.StartVerticalLay();
310 {
311 mPanScroller = safenew wxScrollBar(S.GetParent(), FreqPanScrollerID,
312 wxDefaultPosition, wxDefaultSize, wxSB_VERTICAL);
313#if wxUSE_ACCESSIBILITY
314 // so that name can be set on a standard control
316#endif
317 S.Prop(1);
318 S
319 .Name(XO("Scroll"))
320 .Position( wxALIGN_LEFT | wxTOP)
321 .AddWindow(mPanScroller);
322 }
323 S.EndVerticalLay();
324
325 S.StartVerticalLay();
326 {
327 wxStaticBitmap *zi = safenew wxStaticBitmap(S.GetParent(), wxID_ANY, wxBitmap(ZoomIn));
328 S.Position(wxALIGN_CENTER)
329 .AddWindow(zi);
330
331 S.AddSpace(5);
332
333 mZoomSlider = safenew wxSliderWrapper(S.GetParent(), FreqZoomSliderID, 100, 1, 100,
334 wxDefaultPosition, wxDefaultSize, wxSL_VERTICAL);
335 S.Prop(1);
336 S
337 .Name(XO("Zoom"))
338 .Position(wxALIGN_CENTER_HORIZONTAL)
339 .AddWindow(mZoomSlider);
340#if wxUSE_ACCESSIBILITY
341 // so that name can be set on a standard control
343#endif
344
345 S.AddSpace(5);
346
347 wxStaticBitmap *zo = safenew wxStaticBitmap(S.GetParent(), wxID_ANY, wxBitmap(ZoomOut));
348 S.Position(wxALIGN_CENTER)
349 .AddWindow(zo);
350 }
351 S.EndVerticalLay();
352
353 S.AddSpace(5, wxDefaultCoord);
354 }
355 S.EndHorizontalLay();
356
357 // -------------------------------------------------------------------
358 // ROW 2: Frequency ruler
359 // -------------------------------------------------------------------
360
361 S.AddSpace(1);
362
363 S.StartHorizontalLay(wxEXPAND, 0);
364 {
366 S.GetParent(), wxID_ANY, wxHORIZONTAL,
367 wxSize{ 100, 100 }, // Ruler can't handle small sizes
368 RulerPanel::Range{ 10, 20000 },
370 XO("Hz"),
372 .Log(true)
373 .Flip(true)
374 .LabelEdges(true)
375 .TickColour( theTheme.Colour( clrGraphLabels ) )
376 );
377
378 S.AddSpace(1, wxDefaultCoord);
379 S.Prop(1)
380 .Position(wxALIGN_LEFT | wxALIGN_TOP)
381 .AddWindow(hRuler);
382 S.AddSpace(1, wxDefaultCoord);
383 }
384 S.EndHorizontalLay();
385
386 S.AddSpace(1);
387
388 // -------------------------------------------------------------------
389 // ROW 3: Spacer
390 // -------------------------------------------------------------------
391
392 S.AddSpace(5);
393 S.AddSpace(5);
394 S.AddSpace(5);
395
396 // -------------------------------------------------------------------
397 // ROW 4: Info
398 // -------------------------------------------------------------------
399
400 S.AddSpace(1);
401
402 S.StartHorizontalLay(wxEXPAND);
403 {
404 S.SetSizerProportion(1);
405 S.StartMultiColumn(6);
406 S.SetStretchyCol(1);
407 S.SetStretchyCol(3);
408 {
409 S.AddPrompt(XXO("Cursor:"));
410
411 mCursorText = S.Style(wxTE_READONLY)
412 .AddTextBox( {}, wxT(""), 10);
413
414 S.AddPrompt(XXO("Peak:"));
415
416 mPeakText = S.Style(wxTE_READONLY)
417 .AddTextBox( {}, wxT(""), 10);
418 S.AddSpace(5);
419
420 mGridOnOff = S.Id(GridOnOffID).AddCheckBox(XXO("&Grids"), mDrawGrid);
421 }
422 S.EndMultiColumn();
423 }
424 S.EndHorizontalLay();
425
426 S.AddSpace(1);
427 }
428 S.EndMultiColumn();
429
430 // -------------------------------------------------------------------
431 // ROW 5: Spacer
432 // -------------------------------------------------------------------
433
434 S.AddSpace(5);
435
436 S.SetBorder(2);
437 S.SetSizerProportion(0);
438 S.StartMultiColumn(9, wxALIGN_CENTER);
439 {
440 // ----------------------------------------------------------------
441 // ROW 6: Algorithm, Size, Export, Replot
442 // ----------------------------------------------------------------
443
444 S.AddSpace(5);
445
446 mAlgChoice = S.Id(FreqAlgChoiceID).Focus()
447 .MinSize( { wxDefaultCoord, wxDefaultCoord } )
448 .AddChoice(XXO("&Algorithm:"), algChoices, mAlg);
449
450 S.AddSpace(5);
451
453 .MinSize( { wxDefaultCoord, wxDefaultCoord } )
454 .AddChoice(XXO("&Size:"), sizeChoices, mSize);
455
456 S.AddSpace(5);
457
458 mExportButton = S.Id(FreqExportButtonID).AddButton(XXO("&Export..."));
459
460 S.AddSpace(5);
461
462 // ----------------------------------------------------------------
463 // ROW 7: Function, Axis, Grids, Close
464 // ----------------------------------------------------------------
465
466 S.AddSpace(5);
467
469 .MinSize( { wxDefaultCoord, wxDefaultCoord } )
470 .AddChoice(XXO("&Function:"), funcChoices, mFunc);
471 mFuncChoice->MoveAfterInTabOrder(mSizeChoice);
472
473 S.AddSpace(5);
474
476 .MinSize( { wxDefaultCoord, wxDefaultCoord } )
477 .AddChoice(XXO("&Axis:"), axisChoices, mAxis);
478 mAxisChoice->MoveAfterInTabOrder(mFuncChoice);
479
480 S.AddSpace(5);
481
482 mReplotButton = S.Id(ReplotButtonID).AddButton(XXO("&Replot..."));
483
484 S.AddSpace(5);
485
486 //mCloseButton = S.Id(wxID_CANCEL).AddButton(XO("&Close"));
487
488 //S.AddSpace(5);
489 }
490 S.EndMultiColumn();
491 S.AddStandardButtons( eHelpButton | eCloseButton );
492
493 // -------------------------------------------------------------------
494 // ROW 8: Spacer
495 // -------------------------------------------------------------------
496
497 S.AddSpace(5);
498
499 mProgress = safenew FreqGauge(S.GetParent(), wxID_ANY); //, wxST_SIZEGRIP);
500 S.Position(wxEXPAND)
501 .AddWindow(mProgress);
502
503 // Log-frequency axis works for spectrum plots only.
505 {
506 mAxis = 0;
507 mAxisChoice->Disable();
508 }
509 mLogAxis = mAxis != 0;
510
511 mCloseButton = static_cast<wxButton*>(FindWindowById( wxID_CANCEL ));
512 mCloseButton->SetDefault();
513
514 Layout();
515 Fit();
516 // Bug 1607:
517 Center();
518
519 SetMinSize(GetSize());
520
521#if defined(__WXGTK__)
522 // This should be rechecked with wx3.
523 //
524 // The scrollbar (focus some reason) doesn't allow tabbing past it
525 // because it can't receive focus. So, convince it otherwise.
526 //
527 // Unfortunately, this still doesn't let you adjust the scrollbar
528 // from the keyboard. Near as I can tell, wxWGTK is capturing the
529 // keyboard input, so the GTK widget doesn't see it, preventing
530 // the normal scroll events from being generated.
531 //
532 // I guess the only way round it would be to handle key actions
533 // ourselves, but we'll leave that for a future date.
534// gtk_widget_set_can_focus(mPanScroller->m_widget, true);
535#endif
536}
537
538void FrequencyPlotDialog::OnGetURL(wxCommandEvent & WXUNUSED(event))
539{
540 // Original help page is back on-line (March 2016), but the manual should be more reliable.
541 // http://www.eramp.com/WCAG_2_audio_contrast_tool_help.htm
542 HelpSystem::ShowHelp(this, L"Plot_Spectrum");
543}
544
546{
547 if (!show)
548 {
549 mFreqPlot->SetCursor(*mArrowCursor);
550 }
551
552 bool shown = IsShown();
553
554 if (show && !shown)
555 {
557 if(dBRange < 90.)
558 dBRange = 90.;
559 if (!GetAudio())
560 return false;
561 // Don't send an event. We need the recalc right away.
562 // so that mAnalyst is valid when we paint.
563 //SendRecalcEvent();
564 Recalc();
565 }
566
567 bool res = wxDialogWrapper::Show(show);
568
569 return res;
570}
571
572void FrequencyPlotDialog::OnSize(wxSizeEvent & WXUNUSED(event))
573{
574 Layout();
575
576 DrawPlot();
577
578 Refresh(true);
579}
580
582{
583 Layout();
584
585 mBitmap.reset();
586
587 mPlotRect = mFreqPlot->GetClientRect();
588
589 mBitmap = std::make_unique<wxBitmap>(mPlotRect.width, mPlotRect.height,24);
590
591 dc.SelectObject(*mBitmap);
592
593 dc.SetBackground(wxBrush(wxColour(254, 254, 254)));// DONT-THEME Mask colour.
594 dc.Clear();
595
596 dc.SetPen(*wxBLACK_PEN);
597 dc.SetBrush(*wxWHITE_BRUSH);
598 dc.DrawRectangle(mPlotRect);
599
600 dc.SetFont(mFreqFont);
601}
602
604{
605 if (!mData || mDataLen < mWindowSize || mAnalyst->GetProcessedSize() == 0) {
606 wxMemoryDC memDC;
607
610
612 hRuler->ruler.SetRange(0, 1);
613
614 DrawBackground(memDC);
615
616 if (mDataLen < mWindowSize) {
617 wxString msg = _("Not enough data selected.");
618 wxSize sz = memDC.GetTextExtent(msg);
619 memDC.DrawText(msg,
620 (mPlotRect.GetWidth() - sz.GetWidth()) / 2,
621 (mPlotRect.GetHeight() - sz.GetHeight()) / 2);
622 }
623
624 memDC.SelectObject(wxNullBitmap);
625
626 mFreqPlot->Refresh();
627
628 Refresh();
629
630 return;
631 }
632
633 float yRange = mYMax - mYMin;
634 float yTotal = yRange * ((float) mZoomSlider->GetValue() / 100.0f);
635
636 int sTotal = yTotal * 100;
637 int sRange = yRange * 100;
638 int sPos = mPanScroller->GetThumbPosition() + ((mPanScroller->GetThumbSize() - sTotal) / 2);
639 mPanScroller->SetScrollbar(sPos, sTotal, sRange, sTotal);
640
641 float yMax = mYMax - ((float)sPos / 100);
642 float yMin = yMax - yTotal;
643
644 // Set up y axis ruler
645
647 vRuler->ruler.SetUnits(XO("dB"));
649 } else {
650 vRuler->ruler.SetUnits({});
652 }
653 int w1, w2, h;
654 vRuler->ruler.GetMaxSize(&w1, &h);
655 vRuler->ruler.SetRange(yMax, yMin); // Note inversion for vertical.
656 vRuler->ruler.GetMaxSize(&w2, &h);
657 if( w1 != w2 ) // Reduces flicker
658 {
659 vRuler->SetMinSize(wxSize(w2,h));
660 Layout();
661 }
662 vRuler->Refresh(false);
663
664 wxMemoryDC memDC;
665 DrawBackground(memDC);
666
667 // Get the plot dimensions
668 //
669 // Must be done after setting the vertical ruler above since the
670 // the width could change.
671 wxRect r = mPlotRect;
672
673 // Set up x axis ruler
674
675 int width = r.width - 2;
676
677 float xMin, xMax, xRatio, xStep;
678
680 xMin = mRate / mWindowSize;
681 xMax = mRate / 2;
682 xRatio = xMax / xMin;
683 if (mLogAxis)
684 {
685 xStep = pow(2.0f, (log(xRatio) / log(2.0f)) / width);
687 }
688 else
689 {
690 xStep = (xMax - xMin) / width;
692 }
693 hRuler->ruler.SetUnits(XO("Hz"));
694 } else {
695 xMin = 0;
696 xMax = mAnalyst->GetProcessedSize() / mRate;
697 xStep = (xMax - xMin) / width;
699 /* i18n-hint: short form of 'seconds'.*/
700 hRuler->ruler.SetUnits(XO("s"));
701 }
702 hRuler->ruler.SetRange(xMin, xMax-xStep);
703 hRuler->Refresh(false);
704
705 // Draw the plot
707 memDC.SetPen(wxPen(theTheme.Colour( clrHzPlot ), 1, wxPENSTYLE_SOLID));
708 else
709 memDC.SetPen(wxPen(theTheme.Colour( clrWavelengthPlot), 1, wxPENSTYLE_SOLID));
710
711 float xPos = xMin;
712
713 for (int i = 0; i < width; i++) {
714 float y;
715
716 if (mLogAxis)
717 y = mAnalyst->GetProcessedValue(xPos, xPos * xStep);
718 else
719 y = mAnalyst->GetProcessedValue(xPos, xPos + xStep);
720
721 float ynorm = (y - yMin) / yTotal;
722
723 int lineheight = (int)(ynorm * (r.height - 1));
724
725 if (lineheight > r.height - 2)
726 lineheight = r.height - 2;
727
728 if (ynorm > 0.0)
729 AColor::Line(memDC, r.x + 1 + i, r.y + r.height - 1 - lineheight,
730 r.x + 1 + i, r.y + r.height - 1);
731
732 if (mLogAxis)
733 xPos *= xStep;
734 else
735 xPos += xStep;
736 }
737
738 // Outline the graph
739 memDC.SetPen(*wxBLACK_PEN);
740 memDC.SetBrush(*wxTRANSPARENT_BRUSH);
741 memDC.DrawRectangle(r);
742
743 if(mDrawGrid)
744 {
745 hRuler->ruler.DrawGrid(memDC, r.height, true, true, 1, 1);
746 vRuler->ruler.DrawGrid(memDC, r.width, true, true, 1, 1);
747 }
748
749 memDC.SelectObject( wxNullBitmap );
750
751 mFreqPlot->Refresh();
752}
753
754
755void FrequencyPlotDialog::PlotMouseEvent(wxMouseEvent & event)
756{
757 if (event.Moving() && (event.m_x != mMouseX || event.m_y != mMouseY)) {
758 mMouseX = event.m_x;
759 mMouseY = event.m_y;
760
761 if (mPlotRect.Contains(mMouseX, mMouseY))
762 mFreqPlot->SetCursor(*mCrossCursor);
763 else
764 mFreqPlot->SetCursor(*mArrowCursor);
765
766 mFreqPlot->Refresh(false);
767 }
768}
769
770void FrequencyPlotDialog::OnPanScroller(wxScrollEvent & WXUNUSED(event))
771{
772 DrawPlot();
773}
774
775void FrequencyPlotDialog::OnZoomSlider(wxCommandEvent & WXUNUSED(event))
776{
777 DrawPlot();
778}
779
780void FrequencyPlotDialog::OnAlgChoice(wxCommandEvent & WXUNUSED(event))
781{
782 mAlg = SpectrumAnalyst::Algorithm(mAlgChoice->GetSelection());
783
784 // Log-frequency axis works for spectrum plots only.
786 mAxisChoice->Enable(true);
787 mLogAxis = mAxisChoice->GetSelection() ? true : false;
788 }
789 else {
790 mAxisChoice->Disable();
791 mLogAxis = false;
792 }
793
795}
796
797void FrequencyPlotDialog::OnSizeChoice(wxCommandEvent & WXUNUSED(event))
798{
799 long windowSize = 0;
800 mSizeChoice->GetStringSelection().ToLong(&windowSize);
801 mWindowSize = windowSize;
802
804}
805
806void FrequencyPlotDialog::OnFuncChoice(wxCommandEvent & WXUNUSED(event))
807{
809}
810
811void FrequencyPlotDialog::OnAxisChoice(wxCommandEvent & WXUNUSED(event))
812{
813 mLogAxis = mAxisChoice->GetSelection() ? true : false;
814 DrawPlot();
815}
816
817void FrequencyPlotDialog::PlotPaint(wxPaintEvent & event)
818{
819 wxPaintDC dc( (wxWindow *) event.GetEventObject() );
820
821 dc.DrawBitmap( *mBitmap, 0, 0, true );
822 // Fix for Bug 1226 "Plot Spectrum freezes... if insufficient samples selected"
823 if (!mData || mDataLen < mWindowSize)
824 return;
825
826 dc.SetFont(mFreqFont);
827
828 wxRect r = mPlotRect;
829
830 int width = r.width - 2;
831
832 float xMin, xMax, xRatio, xStep;
833
835 xMin = mRate / mWindowSize;
836 xMax = mRate / 2;
837 xRatio = xMax / xMin;
838 if (mLogAxis)
839 xStep = pow(2.0f, (log(xRatio) / log(2.0f)) / width);
840 else
841 xStep = (xMax - xMin) / width;
842 } else {
843 xMin = 0;
844 xMax = mAnalyst->GetProcessedSize() / mRate;
845 xStep = (xMax - xMin) / width;
846 }
847
848 float xPos = xMin;
849
850 // Find the peak nearest the cursor and plot it
851 if ( r.Contains(mMouseX, mMouseY) && (mMouseX!=0) && (mMouseX!=r.width-1) ) {
852 if (mLogAxis)
853 xPos = xMin * pow(xStep, mMouseX - (r.x + 1));
854 else
855 xPos = xMin + xStep * (mMouseX - (r.x + 1));
856
857 float bestValue = 0;
858 float bestpeak = mAnalyst->FindPeak(xPos, &bestValue);
859
860 int px;
861 if (mLogAxis)
862 px = (int)(log(bestpeak / xMin) / log(xStep));
863 else
864 px = (int)((bestpeak - xMin) * width / (xMax - xMin));
865
866 dc.SetPen(wxPen(wxColour(255, 32, 32), 1, wxPENSTYLE_SOLID));
867 AColor::Line(dc, r.x + 1 + px, r.y, r.x + 1 + px, r.y + r.height);
868
869 // print out info about the cursor location
870
871 float value;
872
873 if (mLogAxis) {
874 xPos = xMin * pow(xStep, mMouseX - (r.x + 1));
875 value = mAnalyst->GetProcessedValue(xPos, xPos * xStep);
876 } else {
877 xPos = xMin + xStep * (mMouseX - (r.x + 1));
878 value = mAnalyst->GetProcessedValue(xPos, xPos + xStep);
879 }
880
881 TranslatableString cursor;
883
885 auto xp = PitchName_Absolute(FreqToMIDInote(xPos));
886 auto pp = PitchName_Absolute(FreqToMIDInote(bestpeak));
887 /* i18n-hint: The %d's are replaced by numbers, the %s by musical notes, e.g. A#*/
888 cursor = XO("%d Hz (%s) = %d dB")
889 .Format( (int)(xPos + 0.5), xp, (int)(value + 0.5));
890 /* i18n-hint: The %d's are replaced by numbers, the %s by musical notes, e.g. A#*/
891 peak = XO("%d Hz (%s) = %.1f dB")
892 .Format( (int)(bestpeak + 0.5), pp, bestValue );
893 } else if (xPos > 0.0 && bestpeak > 0.0) {
894 auto xp = PitchName_Absolute(FreqToMIDInote(1.0 / xPos));
895 auto pp = PitchName_Absolute(FreqToMIDInote(1.0 / bestpeak));
896 /* i18n-hint: The %d's are replaced by numbers, the %s by musical notes, e.g. A#
897 * the %.4f are numbers, and 'sec' should be an abbreviation for seconds */
898 cursor = XO("%.4f sec (%d Hz) (%s) = %f")
899 .Format( xPos, (int)(1.0 / xPos + 0.5), xp, value );
900 /* i18n-hint: The %d's are replaced by numbers, the %s by musical notes, e.g. A#
901 * the %.4f are numbers, and 'sec' should be an abbreviation for seconds */
902 peak = XO("%.4f sec (%d Hz) (%s) = %.3f")
903 .Format( bestpeak, (int)(1.0 / bestpeak + 0.5), pp, bestValue );
904 }
905 mCursorText->SetValue( cursor.Translation() );
906 mPeakText->SetValue( peak.Translation() );
907 }
908 else {
909 mCursorText->SetValue(wxT(""));
910 mPeakText->SetValue(wxT(""));
911 }
912
913
914 // Outline the graph
915 dc.SetPen(*wxBLACK_PEN);
916 dc.SetBrush(*wxTRANSPARENT_BRUSH);
917 dc.DrawRectangle(r);
918}
919
920void FrequencyPlotDialog::OnCloseWindow(wxCloseEvent & WXUNUSED(event))
921{
922 Show(false);
923}
924
925void FrequencyPlotDialog::OnCloseButton(wxCommandEvent & WXUNUSED(event))
926{
927 gPrefs->Write(wxT("/FrequencyPlotDialog/DrawGrid"), mDrawGrid);
928 gPrefs->Write(wxT("/FrequencyPlotDialog/SizeChoice"), mSizeChoice->GetSelection());
929 gPrefs->Write(wxT("/FrequencyPlotDialog/AlgChoice"), mAlgChoice->GetSelection());
930 gPrefs->Write(wxT("/FrequencyPlotDialog/FuncChoice"), mFuncChoice->GetSelection());
931 gPrefs->Write(wxT("/FrequencyPlotDialog/AxisChoice"), mAxisChoice->GetSelection());
932 gPrefs->Flush();
933 mData.reset();
934 Show(false);
935}
936
938{
939 wxCommandEvent e(EVT_FREQWINDOW_RECALC, wxID_ANY);
940 GetEventHandler()->AddPendingEvent(e);
941}
942
944{
945 if (!mData || mDataLen < mWindowSize) {
946 DrawPlot();
947 return;
948 }
949
951 SpectrumAnalyst::Algorithm(mAlgChoice->GetSelection());
952 int windowFunc = mFuncChoice->GetSelection();
953
954 wxWindow *hadFocus = FindFocus();
955 // In wxMac, the skipped window MUST be a top level window. I'd originally made it
956 // just the mProgress window with the idea of preventing user interaction with the
957 // controls while the plot was being recalculated. This doesn't appear to be necessary
958 // so just use the top level window instead.
959 {
960 std::optional<wxWindowDisabler> blocker;
961 if (IsShown())
962 blocker.emplace(this);
963 wxYieldIfNeeded();
964
965 auto first = true;
966 auto progress = [&](long long num, long long den) {
967 if (first) {
968 mProgress->SetRange(den);
969 first = false;
970 }
971 mProgress->SetValue(num);
972 };
973
974 mAnalyst->Calculate(alg, windowFunc, mWindowSize, mRate,
975 mData.get(), mDataLen,
976 &mYMin, &mYMax, std::move(progress));
977
978 mProgress->Reset();
979 }
980 if (hadFocus) {
981 hadFocus->SetFocus();
982 }
983
984 if (alg == SpectrumAnalyst::Spectrum) {
985 if(mYMin < -dBRange)
986 mYMin = -dBRange;
987 if(mYMax <= -dBRange)
988 mYMax = -dBRange + 10.; // it's all out of range, but show a scale.
989 else
990 mYMax += .5;
991 }
992
993 // Prime the scrollbar
994 mPanScroller->SetScrollbar(0, (mYMax - mYMin) * 100, (mYMax - mYMin) * 100, 1);
995
996 DrawPlot();
997}
998
999void FrequencyPlotDialog::OnExport(wxCommandEvent & WXUNUSED(event))
1000{
1001 wxString fName = _("spectrum.txt");
1002
1003 fName = SelectFile(FileNames::Operation::Export,
1004 XO("Export Spectral Data As:"),
1005 wxEmptyString,
1006 fName,
1007 wxT("txt"),
1009 wxFD_SAVE | wxRESIZE_BORDER,
1010 this);
1011
1012 if (fName.empty())
1013 return;
1014
1015 wxFFileOutputStream ffStream{ fName };
1016 if (!ffStream.IsOk()) {
1017 AudacityMessageBox( XO("Couldn't write to file: %s").Format( fName ) );
1018 return;
1019 }
1020
1021 wxTextOutputStream ss(ffStream);
1022
1023 const int processedSize = mAnalyst->GetProcessedSize();
1024 const float *const processed = mAnalyst->GetProcessed();
1025 if (mAlgChoice->GetSelection() == 0) {
1026 ss
1027 << XO("Frequency (Hz)\tLevel (dB)") << '\n';
1028 for (int i = 1; i < processedSize; i++)
1029 ss
1030 << wxString::Format(wxT("%f\t%f\n"),
1031 i * mRate / mWindowSize, processed[i] );
1032 }
1033 else {
1034 ss
1035 << XO("Lag (seconds)\tFrequency (Hz)\tLevel") << '\n';
1036 for (int i = 1; i < processedSize; i++)
1037 ss
1038 << wxString::Format(wxT("%f\t%f\t%f\n"),
1039 i / mRate, mRate / i, processed[i] );
1040 }
1041}
1042
1043void FrequencyPlotDialog::OnReplot(wxCommandEvent & WXUNUSED(event))
1044{
1046 if(dBRange < 90.)
1047 dBRange = 90.;
1048 GetAudio();
1050}
1051
1052void FrequencyPlotDialog::OnGridOnOff(wxCommandEvent & WXUNUSED(event))
1053{
1054 mDrawGrid = mGridOnOff->IsChecked();
1055
1056 DrawPlot();
1057}
1058
1059void FrequencyPlotDialog::OnRecalc(wxCommandEvent & WXUNUSED(event))
1060{
1061 Recalc();
1062}
1063
1065{
1066 bool shown = IsShown();
1067 if (shown) {
1068 Show(false);
1069 }
1070
1071 auto zoomSlider = mZoomSlider->GetValue();
1072 auto drawGrid = mGridOnOff->GetValue();
1073 auto sizeChoice = mSizeChoice->GetStringSelection();
1074 auto algChoice = mAlgChoice->GetSelection();
1075 auto funcChoice = mFuncChoice->GetSelection();
1076 auto axisChoice = mAxisChoice->GetSelection();
1077
1078 SetSizer(nullptr);
1079 DestroyChildren();
1080
1081 Populate();
1082
1083 mZoomSlider->SetValue(zoomSlider);
1084
1085 mDrawGrid = drawGrid;
1086 mGridOnOff->SetValue(drawGrid);
1087
1088 long windowSize = 0;
1089 sizeChoice.ToLong(&windowSize);
1090 mWindowSize = windowSize;
1091 mSizeChoice->SetStringSelection(sizeChoice);
1092
1093 mAlg = static_cast<SpectrumAnalyst::Algorithm>(algChoice);
1094 mAlgChoice->SetSelection(algChoice);
1095
1096 mFunc = funcChoice;
1097 mFuncChoice->SetSelection(funcChoice);
1098
1099 mAxis = axisChoice;
1100 mAxisChoice->SetSelection(axisChoice);
1101
1102 if (shown) {
1103 Show(true);
1104 }
1105}
1106
1107BEGIN_EVENT_TABLE(FreqPlot, wxWindow)
1108 EVT_ERASE_BACKGROUND(FreqPlot::OnErase)
1109 EVT_PAINT(FreqPlot::OnPaint)
1110 EVT_MOUSE_EVENTS(FreqPlot::OnMouseEvent)
1112
1113FreqPlot::FreqPlot(wxWindow *parent, wxWindowID winid)
1114: wxWindow(parent, winid)
1115{
1116 freqWindow = (FrequencyPlotDialog *) parent;
1117}
1118
1120{
1121 return false;
1122}
1123
1124void FreqPlot::OnErase(wxEraseEvent & WXUNUSED(event))
1125{
1126 // Ignore it to prevent flashing
1127}
1128
1129void FreqPlot::OnPaint(wxPaintEvent & evt)
1130{
1131 freqWindow->PlotPaint(evt);
1132}
1133
1134void FreqPlot::OnMouseEvent(wxMouseEvent & event)
1135{
1136 freqWindow->PlotMouseEvent(event);
1137}
1138
1139// Remaining code hooks this add-on into the application
1140#include "CommandContext.h"
1141#include "CommandManager.h"
1142#include "ProjectWindows.h"
1143
1144namespace {
1145
1146AttachedWindows::RegisteredFactory sFrequencyWindowKey{
1147 []( AudacityProject &parent ) -> wxWeakRef< wxWindow > {
1148 auto &window = GetProjectFrame(parent);
1150 &window, -1, parent, FrequencyAnalysisTitle,
1151 wxPoint{ 150, 150 }
1152 );
1153 }
1154};
1155
1156// Define our extra menu item that invokes that factory
1157void OnPlotSpectrum(const CommandContext &context)
1158{
1159 auto &project = context.project;
1161 auto freqWindow = &GetAttachedWindows(project)
1163
1164 freqWindow->Show(true);
1165 freqWindow->Raise();
1166 freqWindow->SetFocus();
1167}
1168
1169// Register that menu item
1170
1171using namespace MenuRegistry;
1173 Command( wxT("PlotSpectrum"), XXO("Plot Spectrum..."),
1176 wxT("Analyze/Analyzers/Windows")
1177};
1178
1179}
wxT("CloseDown"))
int AudacityMessageBox(const TranslatableString &message, const TranslatableString &caption, long style, wxWindow *parent, int x, int y)
Toolkit-neutral facade for basic user interface services.
END_EVENT_TABLE()
const ReservedCommandFlag & AudioIONotBusyFlag()
const ReservedCommandFlag & TimeSelectedFlag()
const ReservedCommandFlag & WaveTracksSelectedFlag()
IntSetting DecibelScaleCutoff
Negation of this value is the lowest dB level that should be shown in dB scales.
Definition: Decibels.cpp:12
EVT_BUTTON(wxID_NO, DependencyDialog::OnNo) EVT_BUTTON(wxID_YES
int NumWindowFuncs()
Definition: FFT.cpp:327
const TranslatableString WindowFuncName(int whichFunction)
Definition: FFT.cpp:332
#define FrequencyAnalysisTitle
Definition: FreqWindow.cpp:95
static const char * ZoomOut[]
Definition: FreqWindow.cpp:143
#define FREQ_WINDOW_HEIGHT
Definition: FreqWindow.cpp:116
DEFINE_EVENT_TYPE(EVT_FREQWINDOW_RECALC)
@ FreqAxisChoiceID
Definition: FreqWindow.cpp:108
@ FreqFuncChoiceID
Definition: FreqWindow.cpp:107
@ FreqPanScrollerID
Definition: FreqWindow.cpp:103
@ FreqExportButtonID
Definition: FreqWindow.cpp:104
@ ReplotButtonID
Definition: FreqWindow.cpp:109
@ FreqAlgChoiceID
Definition: FreqWindow.cpp:105
@ FreqSizeChoiceID
Definition: FreqWindow.cpp:106
@ FreqZoomSliderID
Definition: FreqWindow.cpp:102
@ GridOnOffID
Definition: FreqWindow.cpp:110
@ FirstID
Definition: FreqWindow.cpp:100
static const char * ZoomIn[]
Definition: FreqWindow.cpp:118
XO("Cut/Copy/Paste")
XXO("&Cut/Copy/Paste Toolbar")
#define _(s)
Definition: Internat.h:73
EVT_COMMAND(wxID_ANY, EVT_FREQUENCYTEXTCTRL_UPDATED, LabelDialog::OnFreqUpdate) LabelDialog
Definition: LabelDialog.cpp:89
#define safenew
Definition: MemoryX.h:10
static const auto title
TranslatableString PitchName_Absolute(const double dMIDInote, const PitchNameChoice choice)
Definition: PitchName.cpp:148
double FreqToMIDInote(const double freq)
Definition: PitchName.cpp:23
audacity::BasicSettings * gPrefs
Definition: Prefs.cpp:68
AUDACITY_DLL_API wxFrame & GetProjectFrame(AudacityProject &project)
Get the top-level window associated with the project (as a wxFrame only, when you do not need to use ...
AUDACITY_DLL_API AttachedWindows & GetAttachedWindows(AudacityProject &project)
accessors for certain important windows associated with each project
FilePath SelectFile(FileNames::Operation op, const TranslatableString &message, const FilePath &default_path, const FilePath &default_filename, const FileExtension &default_extension, const FileTypes &fileTypes, int flags, wxWindow *parent)
Definition: SelectFile.cpp:17
@ eIsCreating
Definition: ShuttleGui.h:37
@ eCloseButton
Definition: ShuttleGui.h:619
@ eHelpButton
Definition: ShuttleGui.h:613
const auto project
THEME_API Theme theTheme
Definition: Theme.cpp:82
#define S(N)
Definition: ToChars.cpp:64
TranslatableString Verbatim(wxString str)
Require calls to the one-argument constructor to go through this distinct global function name.
std::vector< TranslatableString > TranslatableStrings
static void Line(wxDC &dc, wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
Definition: AColor.cpp:194
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
Subclass & Get(const RegisteredFactory &key)
Get reference to an attachment, creating on demand if not present, down-cast it to Subclass.
Definition: ClientData.h:318
CommandContext provides additional information to an 'Apply()' command. It provides the project,...
AudacityProject & project
void RegisterLastAnalyzer(const CommandContext &context)
static CommandManager & Get(AudacityProject &project)
FILES_API const FileType AllFiles
Definition: FileNames.h:70
FILES_API const FileType TextFiles
Definition: FileNames.h:73
Abstract base class used in importing a file.
void SetRange(int range, int bar=12, int gap=3)
Definition: FreqGauge.cpp:27
void Reset()
Definition: FreqGauge.cpp:64
void SetValue(int value)
Definition: FreqGauge.cpp:44
Works with FrequencyPlotDialog to display a spectrum plot of the waveform. This class actually does t...
Definition: FreqWindow.h:36
void OnErase(wxEraseEvent &event)
bool AcceptsFocus() const
void OnMouseEvent(wxMouseEvent &event)
void OnPaint(wxPaintEvent &event)
FrequencyPlotDialog * freqWindow
Definition: FreqWindow.h:49
Displays a spectrum plot of the waveform. Has options for selecting parameters of the plot.
Definition: FreqWindow.h:58
std::unique_ptr< wxCursor > mArrowCursor
Definition: FreqWindow.h:112
std::unique_ptr< wxBitmap > mBitmap
Definition: FreqWindow.h:133
void OnGetURL(wxCommandEvent &event)
Definition: FreqWindow.cpp:538
friend class FreqPlot
Definition: FreqWindow.h:140
void OnAlgChoice(wxCommandEvent &event)
Definition: FreqWindow.cpp:780
void OnPanScroller(wxScrollEvent &event)
Definition: FreqWindow.cpp:770
wxChoice * mAlgChoice
Definition: FreqWindow.h:119
void OnZoomSlider(wxCommandEvent &event)
Definition: FreqWindow.cpp:775
wxChoice * mSizeChoice
Definition: FreqWindow.h:120
wxButton * mExportButton
Definition: FreqWindow.h:116
void OnAxisChoice(wxCommandEvent &event)
Definition: FreqWindow.cpp:811
RulerPanel * hRuler
Definition: FreqWindow.h:104
std::unique_ptr< wxCursor > mCrossCursor
Definition: FreqWindow.h:113
wxButton * mReplotButton
Definition: FreqWindow.h:117
FreqPlot * mFreqPlot
Definition: FreqWindow.h:105
wxSlider * mZoomSlider
Definition: FreqWindow.h:124
void OnRecalc(wxCommandEvent &event)
virtual ~FrequencyPlotDialog()
Definition: FreqWindow.cpp:203
void OnFuncChoice(wxCommandEvent &event)
Definition: FreqWindow.cpp:806
void OnCloseButton(wxCommandEvent &event)
Definition: FreqWindow.cpp:925
void UpdatePrefs() override
wxChoice * mFuncChoice
Definition: FreqWindow.h:121
static const int fontSize
Definition: FreqWindow.h:98
void OnReplot(wxCommandEvent &event)
void OnCloseWindow(wxCloseEvent &event)
Definition: FreqWindow.cpp:920
void OnExport(wxCommandEvent &event)
Definition: FreqWindow.cpp:999
wxTextCtrl * mCursorText
Definition: FreqWindow.h:125
RulerPanel * vRuler
Definition: FreqWindow.h:103
void PlotPaint(wxPaintEvent &event)
Definition: FreqWindow.cpp:817
FreqGauge * mProgress
Definition: FreqWindow.h:106
wxTextCtrl * mPeakText
Definition: FreqWindow.h:126
void OnSize(wxSizeEvent &event)
Definition: FreqWindow.cpp:572
wxButton * mCloseButton
Definition: FreqWindow.h:115
void OnGridOnOff(wxCommandEvent &event)
bool Show(bool show=true) override
Definition: FreqWindow.cpp:545
void OnSizeChoice(wxCommandEvent &event)
Definition: FreqWindow.cpp:797
void DrawBackground(wxMemoryDC &dc)
Definition: FreqWindow.cpp:581
wxChoice * mAxisChoice
Definition: FreqWindow.h:122
wxScrollBar * mPanScroller
Definition: FreqWindow.h:123
void PlotMouseEvent(wxMouseEvent &event)
Definition: FreqWindow.cpp:755
wxCheckBox * mGridOnOff
Definition: FreqWindow.h:118
static void ShowHelp(wxWindow *parent, const FilePath &localFileName, const URLString &remoteURL, bool bModal=false, bool alwaysDefaultBrowser=false)
Definition: HelpSystem.cpp:231
static const LinearDBFormat & Instance()
static const LinearUpdater & Instance()
static const LogarithmicUpdater & Instance()
std::unique_ptr< SpectrumAnalyst > mAnalyst
SpectrumAnalyst::Algorithm mAlg
ArrayOf< float > mData
static const RealFormat & LinearInstance()
Definition: RealFormat.cpp:14
Generates classes whose instances register items at construction.
Definition: Registry.h:388
void DrawGrid(wxDC &dc, int length, bool minor=true, bool major=true, int xOffset=0, int yOffset=0) const
Definition: Ruler.cpp:530
void SetUpdater(const RulerUpdater *pUpdater)
Definition: Ruler.cpp:112
void SetFormat(const RulerFormat *pFormat)
Definition: Ruler.cpp:104
void GetMaxSize(wxCoord *width, wxCoord *height)
Definition: Ruler.cpp:612
void SetUnits(const TranslatableString &units)
Definition: Ruler.cpp:120
void SetRange(double min, double max)
Definition: Ruler.cpp:152
RulerPanel class allows you to work with a Ruler like any other wxWindow.
Definition: RulerPanel.h:19
Ruler ruler
Definition: RulerPanel.h:79
std::pair< double, double > Range
Definition: RulerPanel.h:23
bool Read(T *pVar) const
overload of Read returning a boolean that is true if the value was previously defined *‍/
Definition: Prefs.h:207
Derived from ShuttleGuiBase, an Audacity specific class for shuttling data to and from GUI.
Definition: ShuttleGui.h:640
wxColour & Colour(int iIndex)
Holds a msgid for the translation catalog; may also bind format arguments.
wxString Translation() const
An alternative to using wxWindowAccessible, which in wxWidgets 3.1.1 contained GetParent() which was ...
virtual bool Flush() noexcept=0
virtual bool Write(const wxString &key, bool value)=0
void SetTitle(const TranslatableString &title)
std::unique_ptr< WindowPlacement > FindFocus()
Find the window that is accepting keyboard input, if any.
Definition: BasicUI.h:383
IMPORT_EXPORT_API ExportResult Show(ExportTask exportTask)
constexpr auto Command
Definition: MenuRegistry.h:456
void OnPlotSpectrum(const CommandContext &context)
AttachedWindows::RegisteredFactory sFrequencyWindowKey
Options & LabelEdges(bool l)
Definition: RulerPanel.h:41
Options & Log(bool l)
Definition: RulerPanel.h:35