Remove unnecessary wxTimer calls.

This commit is contained in:
apoorv569 2021-09-13 04:52:30 +05:30
parent 28cff9a8fe
commit 56d6d86f66
6 changed files with 124 additions and 76 deletions

View File

@ -379,7 +379,7 @@ MainFrame::MainFrame()
// Intializing wxTimer
m_Timer = new wxTimer(this);
m_TopWaveformPanel = new WaveformViewer(this, m_TopPanel, *m_Library, *m_MediaCtrl, *m_Timer, *m_InfoBar,
m_TopWaveformPanel = new WaveformViewer(this, m_TopPanel, *m_Library, *m_MediaCtrl, *m_InfoBar,
m_ConfigFilepath, m_DatabaseFilepath);
// Binding events.
@ -557,7 +557,7 @@ void MainFrame::OnClickSettings(wxCommandEvent& event)
}
if (settings->IsWaveformColourChanged())
{
m_TopWaveformPanel->ResetDC(m_MediaCtrl->GetState() == wxMEDIASTATE_PLAYING);
m_TopWaveformPanel->ResetDC();
}
break;
case wxID_CANCEL:
@ -943,22 +943,10 @@ void MainFrame::OnClickPlay(wxCommandEvent& event)
wxString sample_path = GetFilenamePathAndExtension(selection).Path;
m_MediaCtrl->Load(sample_path);
PushStatusText(wxString::Format(_("Now playing: %s"), selection), 1);
// Update waveform bitmap
// m_TopWaveformPanel->ResetDC();
if (bLoop && m_LoopPointAButton->GetValue() && m_LoopPointAButton->GetValue())
{
m_MediaCtrl->Seek(m_LoopA.GetValue(), wxFromStart);
m_MediaCtrl->Play();
}
if (bLoop && m_LoopPointAButton->GetValue() && m_LoopPointBButton->GetValue())
PlaySample(sample_path.ToStdString(), selection.ToStdString(), true, m_LoopA.ToDouble(), wxFromStart);
else
m_MediaCtrl->Play();
m_Timer->Start(100, wxTIMER_CONTINUOUS);
PlaySample(sample_path.ToStdString(), selection.ToStdString());
}
void MainFrame::OnClickLoop(wxCommandEvent& event)
@ -982,7 +970,9 @@ void MainFrame::OnClickStop(wxCommandEvent& event)
m_MediaCtrl->Stop();
bStopped = true;
m_Timer->Stop();
if (m_Timer->IsRunning())
m_Timer->Stop();
m_SamplePosition->SetLabel("--:--/--:--");
this->SetStatusText(_("Stopped"), 1);
@ -1012,14 +1002,16 @@ void MainFrame::OnMediaFinished(wxMediaEvent& event)
msgDialog.ShowModal();
}
else
{
m_MediaCtrl->Play();
m_Timer->Start(100, wxTIMER_CONTINUOUS);
}
}
else
{
m_Timer->Stop();
if (m_Timer->IsRunning())
{
m_Timer->Stop();
wxLogDebug("TIMER STOPPED");
}
m_SamplePosition->SetLabel("--:--/--:--");
PopStatusText(1);
this->SetStatusText(_("Stopped"), 1);
@ -1028,6 +1020,8 @@ void MainFrame::OnMediaFinished(wxMediaEvent& event)
void MainFrame::UpdateElapsedTime(wxTimerEvent& event)
{
wxLogDebug("TIMER IS RUNNING..");
wxString duration, position;
wxLongLong llLength, llTell;
@ -1044,7 +1038,12 @@ void MainFrame::UpdateElapsedTime(wxTimerEvent& event)
m_SamplePosition->SetLabel(wxString::Format(wxT("%s/%s"), position.c_str(), duration.c_str()));
m_TopControlsPanel->Refresh();
m_TopWaveformPanel->Refresh();
if (bLoop && m_LoopPointAButton->GetValue() && m_LoopPointBButton->GetValue())
if (static_cast<double>(m_MediaCtrl->Tell()) >= m_LoopB.ToDouble())
m_MediaCtrl->Seek(m_LoopA.ToDouble(), wxFromStart);
}
void MainFrame::OnCheckAutoplay(wxCommandEvent& event)
@ -1105,7 +1104,13 @@ void MainFrame::OnClickLibrary(wxDataViewEvent& event)
}
// Update the waveform bitmap
m_TopWaveformPanel->ResetDC(m_MediaCtrl->GetState() == wxMEDIASTATE_PLAYING);
m_TopWaveformPanel->ResetDC();
if (m_Timer->IsRunning())
{
m_Timer->Stop();
wxLogDebug("TIMER STOPPED");
}
wxString selection = m_Library->GetTextValue(selected_row, 1);
@ -1146,21 +1151,12 @@ void MainFrame::OnClickLibrary(wxDataViewEvent& event)
{
ClearLoopPoints();
m_MediaCtrl->Load(sample_path);
if (bAutoplay)
{
PushStatusText(wxString::Format(_("Now playing: %s"), selection), 1);
if (bLoop && m_LoopPointAButton->GetValue() && m_LoopPointAButton->GetValue())
{
m_MediaCtrl->Seek(m_LoopA.GetValue(), wxFromStart);
m_MediaCtrl->Play();
}
if (bLoop && m_LoopPointAButton->GetValue() && m_LoopPointBButton->GetValue())
PlaySample(sample_path.ToStdString(), selection.ToStdString(), true, m_LoopA.ToDouble(), wxFromStart);
else
m_MediaCtrl->Play();
m_Timer->Start(100, wxTIMER_CONTINUOUS);
PlaySample(sample_path.ToStdString(), selection.ToStdString());
}
}
else
@ -2978,10 +2974,6 @@ void MainFrame::OnRecieveLoopPoints(SampleHive::SH_LoopPointsEvent& event)
m_LoopPointAText->SetValue(wxString::Format("%2i:%02i", loopA_min, loopA_sec));
m_LoopPointBText->SetValue(wxString::Format("%2i:%02i", loopB_min, loopB_sec));
if (bLoop && m_LoopPointAButton->GetValue() && m_LoopPointAButton->GetValue())
if (static_cast<double>(m_MediaCtrl->Tell()) >= static_cast<double>(m_LoopB.GetValue()))
m_MediaCtrl->Seek(m_LoopA.GetValue(), wxFromStart);
wxLogDebug("%s Event processed successfully..", __FUNCTION__);
}
@ -3004,4 +2996,25 @@ void MainFrame::ClearLoopPoints()
m_LoopB = 0;
}
void MainFrame::PlaySample(const std::string& filepath, const std::string& sample, bool seek, wxFileOffset where, wxSeekMode mode)
{
wxLogDebug("TIMER STARTING FROM %s", __FUNCTION__);
if (m_MediaCtrl->Load(filepath))
{
if (seek)
m_MediaCtrl->Seek(where, mode);
if (!m_MediaCtrl->Play())
wxLogDebug(_("Error! Cannot play sample."));
PushStatusText(wxString::Format(_("Now playing: %s"), sample), 1);
if (!m_Timer->IsRunning())
m_Timer->Start(20, wxTIMER_CONTINUOUS);
}
else
wxLogDebug(_("Error! Cannot load sample."));
}
MainFrame::~MainFrame(){}

View File

@ -255,6 +255,11 @@ class MainFrame : public wxFrame
void AddSamples(wxArrayString& files);
void OnAutoImportDir(const wxString& pathToDirectory);
// -------------------------------------------------------------------
void PlaySample(const std::string& filepath, const std::string& sample, bool seek = false, wxFileOffset where = NULL, wxSeekMode mode = wxFromStart);
// Recieve custom events
// -------------------------------------------------------------------
void OnRecieveLoopPoints(SampleHive::SH_LoopPointsEvent& event);
void OnRecieveStatusBarStatus(SampleHive::SH_SetStatusBarMessageEvent& event);

View File

@ -53,4 +53,17 @@ namespace SampleHive
}
wxDEFINE_EVENT(SH_EVT_STATUSBAR_MESSAGE_UPDATED, SH_SetStatusBarMessageEvent);
// SH_TimerEvent::SH_TimerEvent(wxEventType eventType, int winId)
// : wxCommandEvent(eventType, winId)
// {
// }
// SH_TimerEvent::~SH_TimerEvent()
// {
// }
// wxDEFINE_EVENT(SH_EVT_TIMER_STATUS_UPDATED, SH_TimerEvent);
}

View File

@ -2,8 +2,7 @@
#include <utility>
// #include "wx/arrstr.h"
#include "wx/event.h"
#include <wx/event.h>
namespace SampleHive
{
@ -75,7 +74,7 @@ namespace SampleHive
virtual wxEvent* Clone() const { return new SH_SetStatusBarMessageEvent(*this); }
public:
std::pair<wxString, int> GetMessageAndSection() const { return {m_Msg, m_Section }; }
std::pair<wxString, int> GetMessageAndSection() const { return { m_Msg, m_Section }; }
void SetMessageAndSection(std::pair<const wxString&, int> status) { m_Msg = status.first; m_Section = status.second; }
private:
@ -84,4 +83,24 @@ namespace SampleHive
};
wxDECLARE_EVENT(SH_EVT_STATUSBAR_MESSAGE_UPDATED, SH_SetStatusBarMessageEvent);
// class SH_TimerEvent : public wxCommandEvent
// {
// public:
// SH_TimerEvent(wxEventType eventType, int winId);
// ~SH_TimerEvent();
// public:
// virtual wxEvent* Clone() const { return new SH_TimerEvent(*this); }
// public:
// std::pair<int, bool> GetSecondsAndMode() const { return { m_Seconds, m_Mode }; }
// void SetSecondsAndMode(std::pair<int, bool> timerStatus) { m_Seconds = timerStatus.first; m_Mode = timerStatus.second; }
// private:
// int m_Seconds;
// bool m_Mode;
// };
// wxDECLARE_EVENT(SH_EVT_TIMER_STATUS_UPDATED, SH_TimerEvent);
}

View File

@ -39,11 +39,11 @@
#include "SH_Event.hpp"
WaveformViewer::WaveformViewer(wxWindow* parentFrame, wxWindow* window, wxDataViewListCtrl& library,
wxMediaCtrl& mediaCtrl, wxTimer& timer, wxInfoBar& infoBar,
wxMediaCtrl& mediaCtrl, wxInfoBar& infoBar,
const std::string& configFilepath, const std::string& databaseFilepath)
: wxPanel(window, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL | wxNO_BORDER | wxFULL_REPAINT_ON_RESIZE),
m_ParentFrame(parentFrame), m_Window(window), m_Library(library), m_InfoBar(infoBar), m_MediaCtrl(mediaCtrl),
m_Timer(timer), m_ConfigFilepath(configFilepath), m_DatabaseFilepath(databaseFilepath)
m_ConfigFilepath(configFilepath), m_DatabaseFilepath(databaseFilepath)
{
this->SetDoubleBuffered(true);
@ -130,8 +130,6 @@ void WaveformViewer::RenderPlayhead(wxDC& dc)
double position = m_MediaCtrl.Tell();
wxLogDebug("Current Sample Position: %f", position);
m_Timer.Start(5, wxTIMER_CONTINUOUS);
int panel_width = this->GetSize().GetWidth();
double line_pos = panel_width * (position / length);
@ -195,39 +193,42 @@ void WaveformViewer::UpdateWaveformBitmap()
wxLogDebug("Calculating Waveform bars RMS..");
float chunk_size = (float)(frames) / (float)display_width;
int number_chunks = static_cast<int>(static_cast<float>(frames) / chunk_size);
int number_of_chunks = static_cast<int>(static_cast<float>(frames) / chunk_size);
// Start with low non-zero value
float normalize = 0.00001;
for (int i=0; i<number_chunks; i++) {
for (int i = 0; i < number_of_chunks; i++)
{
double sum = 0, mono = 0;
int start_point = static_cast<int>(i*chunk_size*channels);
int start_point = static_cast<int>(i * chunk_size * channels);
// Iterate on the chunk, get the square of sum of monos
for (int j=0; j<chunk_size; j++) {
if (channels == 2) {
mono = 0.5f * (sample[start_point+(2*j)] + sample[start_point+(2*j)+1]);
} else {
mono = sample[start_point+j];
}
for (int j = 0; j < chunk_size; j++)
{
if (channels == 2)
mono = 0.5f * (sample[start_point + (2 * j)] + sample[start_point + (2 * j) + 1]);
else
mono = sample[start_point + j];
sum += mono * mono; // Square
}
sum /= chunk_size; // Mean
sum = pow(sum, 0.5); // Root
// We might bleed a bit on the end and get some near infs, dunno
// what is causing astronomically big numbers from sample[]
if ((sum < 200.0) && (sum > normalize)) {
if ((sum < 200.0) && (sum > normalize))
normalize = sum;
}
waveform.push_back(sum);
}
// Actually normalize
for (int i=0; i<waveform.size(); i++) {
for (int i = 0; i < waveform.size(); i++)
waveform[i] /= normalize;
}
// Draw code
wxMemoryDC mdc(m_WaveformBitmap);
@ -243,17 +244,14 @@ void WaveformViewer::UpdateWaveformBitmap()
for (int i = 0; i < waveform.size() - 1; i++)
{
float half_vertical = static_cast<float>(display_height) / 2.0f;
float half_display_height = static_cast<float>(display_height) / 2.0f;
// X is percentage of i relative to waveform.size() multiplied by
// the width, Y is the half height times the value up or down
float X = display_width * ((float)i / waveform.size());
float Y = waveform[i] * half_vertical;
float Y = waveform[i] * half_display_height;
mdc.DrawLine(
X, half_vertical + Y,
X, half_vertical - Y
);
mdc.DrawLine(X, half_display_height + Y, X, half_display_height - Y);
}
wxLogDebug("Done drawing bitmap..");
@ -329,6 +327,8 @@ void WaveformViewer::OnMouseMotion(wxMouseEvent& event)
{
m_CurrentPoint = wxPoint(pos.x , pos.y);
Refresh();
wxLogDebug("CTRL pressed, pressing LMB will draw selection range at %d, %d", pos.x, pos.y);
}
else
@ -425,6 +425,8 @@ void WaveformViewer::OnMouseLeftButtonUp(wxMouseEvent& event)
ReleaseMouse();
SetCursor(wxCURSOR_ARROW);
Refresh();
bSelectRange = false;
if (!bSelectRange)
@ -441,16 +443,13 @@ void WaveformViewer::OnMouseLeftButtonUp(wxMouseEvent& event)
}
}
void WaveformViewer::ResetDC(bool playing)
void WaveformViewer::ResetDC()
{
if (!playing)
{
bBitmapDirty = true;
bSelectRange = false;
bDrawSelectedArea = false;
bBitmapDirty = true;
bSelectRange = false;
bDrawSelectedArea = false;
Refresh();
}
Refresh();
}
void WaveformViewer::SendLoopPoints()

View File

@ -37,7 +37,7 @@ class WaveformViewer : public wxPanel
{
public:
WaveformViewer(wxWindow* parentFrame, wxWindow* window, wxDataViewListCtrl& library,
wxMediaCtrl& mediaCtrl, wxTimer& timer, wxInfoBar& infoBar,
wxMediaCtrl& mediaCtrl, wxInfoBar& infoBar,
const std::string& configFilepath, const std::string& databaseFilepath);
~WaveformViewer();
@ -49,7 +49,6 @@ class WaveformViewer : public wxPanel
wxDataViewListCtrl& m_Library;
wxInfoBar& m_InfoBar;
wxMediaCtrl& m_MediaCtrl;
wxTimer& m_Timer;
const std::string& m_ConfigFilepath;
const std::string& m_DatabaseFilepath;
@ -94,5 +93,5 @@ class WaveformViewer : public wxPanel
public:
// -------------------------------------------------------------------
void ResetDC(bool playing);
void ResetDC();
};