Add custom event to send loop points to MainFrame.

This commit is contained in:
apoorv569 2021-08-29 19:45:03 +05:30
parent 627cb76950
commit f061fe8a8c
7 changed files with 238 additions and 33 deletions

View File

@ -58,6 +58,7 @@ src = [
'src/Serialize.cpp', 'src/Serialize.cpp',
'src/Tags.cpp', 'src/Tags.cpp',
'src/WaveformViewer.cpp', 'src/WaveformViewer.cpp',
'src/SH_Event.cpp',
] ]

View File

@ -379,7 +379,8 @@ MainFrame::MainFrame()
// Intializing wxTimer // Intializing wxTimer
m_Timer = new wxTimer(this); m_Timer = new wxTimer(this);
m_TopWaveformPanel = new WaveformViewer(this, m_TopPanel, *m_StatusBar, *m_Library, *m_MediaCtrl, *m_Timer, *m_InfoBar, m_ConfigFilepath, m_DatabaseFilepath); m_TopWaveformPanel = new WaveformViewer(this, m_TopPanel, *m_Library, *m_MediaCtrl, *m_Timer, *m_InfoBar,
m_ConfigFilepath, m_DatabaseFilepath);
// Binding events. // Binding events.
Bind(wxEVT_MENU, &MainFrame::OnSelectAddFile, this, MN_AddFile); Bind(wxEVT_MENU, &MainFrame::OnSelectAddFile, this, MN_AddFile);
@ -433,6 +434,9 @@ MainFrame::MainFrame()
Bind(wxEVT_BUTTON, &MainFrame::OnClickAddHive, this, BC_HiveAdd); Bind(wxEVT_BUTTON, &MainFrame::OnClickAddHive, this, BC_HiveAdd);
Bind(wxEVT_BUTTON, &MainFrame::OnClickRemoveHive, this, BC_HiveRemove); Bind(wxEVT_BUTTON, &MainFrame::OnClickRemoveHive, this, BC_HiveRemove);
Bind(SampleHive::SH_EVT_LOOP_POINTS_UPDATED, &MainFrame::OnRecieveLoopPoints, this);
Bind(SampleHive::SH_EVT_STATUSBAR_MESSAGE_UPDATED, &MainFrame::OnRecieveStatusBarStatus, this);
// Adding widgets to their sizers // Adding widgets to their sizers
m_MainSizer->Add(m_MainPanel, 1, wxALL | wxEXPAND, 0); m_MainSizer->Add(m_MainPanel, 1, wxALL | wxEXPAND, 0);
@ -553,8 +557,7 @@ void MainFrame::OnClickSettings(wxCommandEvent& event)
} }
if (settings->IsWaveformColourChanged()) if (settings->IsWaveformColourChanged())
{ {
m_TopWaveformPanel->SetBitmapDirty(true); m_TopWaveformPanel->ResetDC();
m_TopWaveformPanel->Refresh();
} }
break; break;
case wxID_CANCEL: case wxID_CANCEL:
@ -945,8 +948,7 @@ void MainFrame::OnClickPlay(wxCommandEvent& event)
PushStatusText(wxString::Format(_("Now playing: %s"), selection), 1); PushStatusText(wxString::Format(_("Now playing: %s"), selection), 1);
// Update waveform bitmap // Update waveform bitmap
m_TopWaveformPanel->SetBitmapDirty(true); m_TopWaveformPanel->ResetDC();
m_TopWaveformPanel->Refresh();
m_MediaCtrl->Play(); m_MediaCtrl->Play();
@ -1097,8 +1099,7 @@ void MainFrame::OnClickLibrary(wxDataViewEvent& event)
} }
// Update the waveform bitmap // Update the waveform bitmap
m_TopWaveformPanel->SetBitmapDirty(true); m_TopWaveformPanel->ResetDC();
m_TopWaveformPanel->Refresh();
wxString selection = m_Library->GetTextValue(selected_row, 1); wxString selection = m_Library->GetTextValue(selected_row, 1);
@ -2942,4 +2943,36 @@ void MainFrame::OnEnterLoopPoints(wxCommandEvent& event)
wxLogDebug("Loop point text changed"); wxLogDebug("Loop point text changed");
} }
void MainFrame::OnRecieveLoopPoints(SampleHive::SH_LoopPointsEvent& event)
{
wxLogDebug("%s called and recieved loop points", __FUNCTION__);
std::pair<double, double> loop_points = event.GetLoopPoints();
wxLongLong loopA = loop_points.first;
wxLongLong loopB = loop_points.second;
int loopA_min = static_cast<int>((loopA / 60000).GetValue());
int loopA_sec = static_cast<int>(((loopA % 60000) / 1000).GetValue());
int loopB_min = static_cast<int>((loopB / 60000).GetValue());
int loopB_sec = static_cast<int>(((loopB % 60000) / 1000).GetValue());
wxLogDebug(wxString::Format("LoopA: %2i:%02i, LoopB: %2i:%02i",
loopA_min, loopA_sec, loopB_min, loopB_sec));
m_LoopPointAText->SetValue(wxString::Format("%2i:%02i", loopA_min, loopA_sec));
m_LoopPointBText->SetValue(wxString::Format("%2i:%02i", loopB_min, loopB_sec));
// TODO Looping the selected section
wxLogDebug("%s Event processed successfully..", __FUNCTION__);
}
void MainFrame::OnRecieveStatusBarStatus(SampleHive::SH_SetStatusBarMessageEvent& event)
{
std::pair<wxString, int> status = event.GetMessageAndSection();
m_StatusBar->PushStatusText(status.first, status.second);
}
MainFrame::~MainFrame(){} MainFrame::~MainFrame(){}

View File

@ -56,6 +56,7 @@
#include <taglib/tstring.h> #include <taglib/tstring.h>
#include "WaveformViewer.hpp" #include "WaveformViewer.hpp"
#include "SH_Event.hpp"
struct FileInfo struct FileInfo
{ {
@ -252,6 +253,9 @@ class MainFrame : public wxFrame
void AddSamples(wxArrayString& files); void AddSamples(wxArrayString& files);
void OnAutoImportDir(const wxString& pathToDirectory); void OnAutoImportDir(const wxString& pathToDirectory);
void OnRecieveLoopPoints(SampleHive::SH_LoopPointsEvent& event);
void OnRecieveStatusBarStatus(SampleHive::SH_SetStatusBarMessageEvent& event);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
void LoadDatabase(); void LoadDatabase();
void RefreshDatabase(); void RefreshDatabase();

56
src/SH_Event.cpp Normal file
View File

@ -0,0 +1,56 @@
#include "SH_Event.hpp"
namespace SampleHive
{
SH_LoopPointsEvent::SH_LoopPointsEvent(wxEventType eventType, int winId)
: wxCommandEvent(eventType, winId)
{
}
SH_LoopPointsEvent::~SH_LoopPointsEvent()
{
}
wxDEFINE_EVENT(SH_EVT_LOOP_POINTS_UPDATED, SH_LoopPointsEvent);
// SH_AddSampleEvent::SH_AddSampleEvent(wxEventType eventType, int winId)
// : wxCommandEvent(eventType, winId)
// {
// }
// SH_AddSampleEvent::~SH_AddSampleEvent()
// {
// }
// wxDEFINE_EVENT(SH_EVT_STATUS_ADD_SAMPLE, SH_AddSampleEvent);
// SH_MediaEvent::SH_MediaEvent(wxEventType eventType, int winId)
// : wxCommandEvent(eventType, winId)
// {
// }
// SH_MediaEvent::~SH_MediaEvent()
// {
// }
// wxDEFINE_EVENT(SH_EVT_MEDIA_STATUS_UPDATED, SH_MediaEvent);
SH_SetStatusBarMessageEvent::SH_SetStatusBarMessageEvent(wxEventType eventType, int winId)
: wxCommandEvent(eventType, winId)
{
}
SH_SetStatusBarMessageEvent::~SH_SetStatusBarMessageEvent()
{
}
wxDEFINE_EVENT(SH_EVT_STATUSBAR_MESSAGE_UPDATED, SH_SetStatusBarMessageEvent);
}

87
src/SH_Event.hpp Normal file
View File

@ -0,0 +1,87 @@
#pragma once
#include <utility>
// #include "wx/arrstr.h"
#include "wx/event.h"
namespace SampleHive
{
class SH_LoopPointsEvent : public wxCommandEvent
{
public:
SH_LoopPointsEvent(wxEventType eventType, int winId);
~SH_LoopPointsEvent();
public:
virtual wxEvent* Clone() const { return new SH_LoopPointsEvent(*this); }
public:
std::pair<double, double> GetLoopPoints() const { return { m_LoopA, m_LoopB }; };
void SetLoopPoints(std::pair<double&, double&> loopPoints)
{ m_LoopA = loopPoints.first; m_LoopB = loopPoints.second; };
private:
double m_LoopA, m_LoopB;
};
wxDECLARE_EVENT(SH_EVT_LOOP_POINTS_UPDATED, SH_LoopPointsEvent);
// class SH_AddSampleEvent : public wxCommandEvent
// {
// public:
// SH_AddSampleEvent(wxEventType eventType, int winId);
// ~SH_AddSampleEvent();
// public:
// virtual wxEvent* Clone() const { return new SH_AddSampleEvent(*this); }
// public:
// wxArrayString GetArrayString() const { return m_Files; };
// void SetArrayString(const wxArrayString& files) { m_Files = files; };
// private:
// wxArrayString m_Files;
// };
// wxDECLARE_EVENT(SH_EVT_STATUS_ADD_SAMPLE, SH_AddSampleEvent);
// class SH_MediaEvent : public wxCommandEvent
// {
// public:
// SH_MediaEvent(wxEventType eventType, int winId);
// ~SH_MediaEvent();
// public:
// virtual wxEvent* Clone() const { return new SH_MediaEvent(*this); }
// public:
// void SetPath(const wxString& path) { m_Path = path; }
// wxString GetPath() const { return m_Path; }
// private:
// wxString m_Path;
// };
// wxDECLARE_EVENT(SH_EVT_MEDIA_STATUS_UPDATED, SH_MediaEvent);
class SH_SetStatusBarMessageEvent : public wxCommandEvent
{
public:
SH_SetStatusBarMessageEvent(wxEventType eventType, int winId);
~SH_SetStatusBarMessageEvent();
public:
virtual wxEvent* Clone() const { return new SH_SetStatusBarMessageEvent(*this); }
public:
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:
wxString m_Msg;
int m_Section;
};
wxDECLARE_EVENT(SH_EVT_STATUSBAR_MESSAGE_UPDATED, SH_SetStatusBarMessageEvent);
}

View File

@ -36,12 +36,14 @@
#include "SettingsDialog.hpp" #include "SettingsDialog.hpp"
#include "Serialize.hpp" #include "Serialize.hpp"
#include "Tags.hpp" #include "Tags.hpp"
#include "SH_Event.hpp"
WaveformViewer::WaveformViewer(wxWindow* parentFrame, wxWindow* window, wxStatusBar& statusbar, wxDataViewListCtrl& library, wxMediaCtrl& mediaCtrl, WaveformViewer::WaveformViewer(wxWindow* parentFrame, wxWindow* window, wxDataViewListCtrl& library,
wxTimer& timer, wxInfoBar& infoBar, const std::string& configFilepath, const std::string& databaseFilepath) wxMediaCtrl& mediaCtrl, wxTimer& timer, wxInfoBar& infoBar,
const std::string& configFilepath, const std::string& databaseFilepath)
: wxPanel(window, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL | wxNO_BORDER | wxFULL_REPAINT_ON_RESIZE), : 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_ParentFrame(parentFrame), m_Window(window), m_Library(library), m_InfoBar(infoBar), m_MediaCtrl(mediaCtrl),
m_StatusBar(statusbar), m_ConfigFilepath(configFilepath), m_DatabaseFilepath(databaseFilepath) m_Timer(timer), m_ConfigFilepath(configFilepath), m_DatabaseFilepath(databaseFilepath)
{ {
this->SetDoubleBuffered(true); this->SetDoubleBuffered(true);
@ -84,6 +86,7 @@ void WaveformViewer::OnPaint(wxPaintEvent& event)
RenderPlayhead(dc); RenderPlayhead(dc);
// Draw selection range
if (bSelectRange) if (bSelectRange)
{ {
wxRect rect(m_CurrentPoint, m_AnchorPoint); wxRect rect(m_CurrentPoint, m_AnchorPoint);
@ -93,6 +96,7 @@ void WaveformViewer::OnPaint(wxPaintEvent& event)
dc.DrawRectangle(rect); dc.DrawRectangle(rect);
} }
// Draw selected area
if (!bSelectRange && bDrawSelectedArea && !bBitmapDirty) if (!bSelectRange && bDrawSelectedArea && !bBitmapDirty)
{ {
dc.SetPen(wxPen(wxColour(200, 200, 200, 255), 4, wxPENSTYLE_SOLID)); dc.SetPen(wxPen(wxColour(200, 200, 200, 255), 4, wxPENSTYLE_SOLID));
@ -100,6 +104,7 @@ void WaveformViewer::OnPaint(wxPaintEvent& event)
dc.DrawRectangle(wxRect(m_AnchorPoint.x, -2, m_CurrentPoint.x - m_AnchorPoint.x, this->GetSize().GetHeight() + 5)); dc.DrawRectangle(wxRect(m_AnchorPoint.x, -2, m_CurrentPoint.x - m_AnchorPoint.x, this->GetSize().GetHeight() + 5));
bAreaSelected = true; bAreaSelected = true;
SendLoopPoints();
} }
else else
bAreaSelected = false; bAreaSelected = false;
@ -410,19 +415,33 @@ void WaveformViewer::OnMouseLeftButtonUp(wxMouseEvent& event)
SetCursor(wxCURSOR_ARROW); SetCursor(wxCURSOR_ARROW);
m_MediaCtrl.Seek(seek_to, wxFromStart); m_MediaCtrl.Seek(seek_to, wxFromStart);
m_StatusBar.PushStatusText(wxString::Format(_("Now playing: %s"), selected), 1); SendStatusBarStatus(wxString::Format(_("Now playing: %s"), selected), 1);
m_MediaCtrl.Play(); m_MediaCtrl.Play();
} }
} }
WaveformViewer::LoopPoints WaveformViewer::GetLoopPoints() void WaveformViewer::ResetDC()
{ {
bBitmapDirty = true;
bSelectRange = false;
bDrawSelectedArea = false;
Refresh();
}
void WaveformViewer::SendLoopPoints()
{
wxLogDebug("%s Called", __FUNCTION__);
SampleHive::SH_LoopPointsEvent event(SampleHive::SH_EVT_LOOP_POINTS_UPDATED, this->GetId());
event.SetEventObject(this);
Database db(m_InfoBar); Database db(m_InfoBar);
int selected_row = m_Library.GetSelectedRow(); int selected_row = m_Library.GetSelectedRow();
if (selected_row < 0) if (selected_row < 0)
return { 0.0f, 0.0f }; return;
wxString selected = m_Library.GetTextValue(selected_row, 1); wxString selected = m_Library.GetTextValue(selected_row, 1);
std::string path = db.GetSamplePathByFilename(m_DatabaseFilepath, selected.BeforeLast('.').ToStdString()); std::string path = db.GetSamplePathByFilename(m_DatabaseFilepath, selected.BeforeLast('.').ToStdString());
@ -431,15 +450,26 @@ WaveformViewer::LoopPoints WaveformViewer::GetLoopPoints()
int length = tags.GetAudioInfo().length; int length = tags.GetAudioInfo().length;
// double position = m_MediaCtrl.Tell();
int panel_width = this->GetSize().GetWidth(); int panel_width = this->GetSize().GetWidth();
// double line_pos = panel_width * (position / length);
int a = m_AnchorPoint.x, b = m_CurrentPoint.x; int a = m_AnchorPoint.x, b = m_CurrentPoint.x;
double loopA = ((double)a / panel_width) * length; double loopA = ((double)a / panel_width) * length;
double loopB = ((double)b / panel_width) * length; double loopB = ((double)b / panel_width) * length;
return { loopA, loopB }; event.SetLoopPoints({ loopA, loopB });
HandleWindowEvent(event);
wxLogDebug("%s processed event, sending loop points..", __FUNCTION__);
}
void WaveformViewer::SendStatusBarStatus(const wxString& msg, int section)
{
SampleHive::SH_SetStatusBarMessageEvent event(SampleHive::SH_EVT_STATUSBAR_MESSAGE_UPDATED, this->GetId());
event.SetEventObject(this);
event.SetMessageAndSection({ msg, section });
HandleWindowEvent(event);
} }

View File

@ -36,17 +36,11 @@
class WaveformViewer : public wxPanel class WaveformViewer : public wxPanel
{ {
public: public:
WaveformViewer(wxWindow* parentFrame, wxWindow* window, wxStatusBar& statusbar, wxDataViewListCtrl& library, wxMediaCtrl& mediaCtrl, WaveformViewer(wxWindow* parentFrame, wxWindow* window, wxDataViewListCtrl& library,
wxTimer& timer, wxInfoBar& infoBar, const std::string& configFilepath, const std::string& databaseFilepath); wxMediaCtrl& mediaCtrl, wxTimer& timer, wxInfoBar& infoBar,
const std::string& configFilepath, const std::string& databaseFilepath);
~WaveformViewer(); ~WaveformViewer();
private:
// -------------------------------------------------------------------
struct LoopPoints
{
double A, B;
};
private: private:
// ------------------------------------------------------------------- // -------------------------------------------------------------------
wxWindow* m_ParentFrame; wxWindow* m_ParentFrame;
@ -56,7 +50,6 @@ class WaveformViewer : public wxPanel
wxInfoBar& m_InfoBar; wxInfoBar& m_InfoBar;
wxMediaCtrl& m_MediaCtrl; wxMediaCtrl& m_MediaCtrl;
wxTimer& m_Timer; wxTimer& m_Timer;
wxStatusBar& m_StatusBar;
const std::string& m_ConfigFilepath; const std::string& m_ConfigFilepath;
const std::string& m_DatabaseFilepath; const std::string& m_DatabaseFilepath;
@ -94,11 +87,12 @@ class WaveformViewer : public wxPanel
void OnControlKeyUp(wxKeyEvent& event); void OnControlKeyUp(wxKeyEvent& event);
void OnControlKeyDown(wxKeyEvent& event); void OnControlKeyDown(wxKeyEvent& event);
public: // -------------------------------------------------------------------
LoopPoints GetLoopPoints(); // Send custom events
void SendLoopPoints();
void SendStatusBarStatus(const wxString& msg, int section);
public: public:
inline bool IsBitmapDirty() { return bBitmapDirty; } // -------------------------------------------------------------------
inline void SetBitmapDirty(bool dirty) { bBitmapDirty = dirty; } void ResetDC();
inline bool IsAreaSelected() { return bAreaSelected; }
}; };