diff --git a/src/GUI/MainFrame.cpp b/src/GUI/MainFrame.cpp index f6a14fe..5eaec42 100644 --- a/src/GUI/MainFrame.cpp +++ b/src/GUI/MainFrame.cpp @@ -150,7 +150,7 @@ cMainFrame::cMainFrame() m_pTimer = new wxTimer(this); m_pTransportControls = new cTransportControls(m_pTopPanel, *m_pMediaCtrl); - m_pWaveformViewer = new cWaveformViewer(m_pTopPanel, *m_pMediaCtrl, *m_pDatabase); + m_pWaveformViewer = new cWaveformViewer(m_pTopPanel, *m_pMediaCtrl); // Binding events. Bind(wxEVT_MENU, &cMainFrame::OnSelectAddFile, this, SampleHive::ID::MN_AddFile); @@ -227,7 +227,7 @@ void cMainFrame::OnMediaFinished(wxMediaEvent& event) if (m_pTimer->IsRunning()) { m_pTimer->Stop(); - SH_LOG_DEBUG("TIMER STOPPED"); + SH_LOG_DEBUG("Stopping timer."); } m_pTransportControls->SetSamplePositionText("--:--/--:--"); @@ -238,8 +238,6 @@ void cMainFrame::OnMediaFinished(wxMediaEvent& event) void cMainFrame::UpdateElapsedTime(wxTimerEvent& event) { - SH_LOG_DEBUG("TIMER IS RUNNING.."); - wxString duration, position; wxLongLong llLength, llTell; @@ -869,9 +867,7 @@ void cMainFrame::OnRecieveCallFunctionPlay(SampleHive::cCallFunctionEvent& event wxString selection = event.GetSlection(); bool checkAutoplay = event.GetAutoplayValue(); - wxString sample_path = serializer.DeserializeShowFileExtension() ? - m_pDatabase->GetSamplePathByFilename(selection.BeforeLast('.').ToStdString()) : - m_pDatabase->GetSamplePathByFilename(selection.ToStdString()); + wxString sample_path = SampleHive::cUtils::Get().GetFilenamePathAndExtension(selection).Path; if (checkAutoplay) { @@ -931,7 +927,7 @@ void cMainFrame::PlaySample(const std::string& filepath, const std::string& samp if (!m_pTimer->IsRunning()) { - SH_LOG_INFO("Starting timer.."); + SH_LOG_DEBUG("Starting timer."); m_pTimer->Start(20, wxTIMER_CONTINUOUS); } } diff --git a/src/GUI/WaveformViewer.cpp b/src/GUI/WaveformViewer.cpp index d9807b0..58a7656 100644 --- a/src/GUI/WaveformViewer.cpp +++ b/src/GUI/WaveformViewer.cpp @@ -22,6 +22,7 @@ #include "Utility/HiveData.hpp" #include "Utility/Log.hpp" #include "Utility/Paths.hpp" +#include "Utility/Utils.hpp" #include "Utility/Serialize.hpp" #include "Utility/Event.hpp" #include "Utility/Signal.hpp" @@ -41,9 +42,9 @@ #include #include -cWaveformViewer::cWaveformViewer(wxWindow* window, wxMediaCtrl& mediaCtrl, cDatabase& database) +cWaveformViewer::cWaveformViewer(wxWindow* window, wxMediaCtrl& mediaCtrl) : wxPanel(window, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL | wxNO_BORDER | wxFULL_REPAINT_ON_RESIZE), - m_Window(window), m_Database(database), m_MediaCtrl(mediaCtrl) + m_Window(window), m_MediaCtrl(mediaCtrl) { this->SetDoubleBuffered(true); @@ -125,21 +126,17 @@ void cWaveformViewer::RenderPlayhead(wxDC& dc) return; wxString selected = SampleHive::cHiveData::Get().GetListCtrlTextValue(selected_row, 1); - std::string path = m_Database.GetSamplePathByFilename(selected.BeforeLast('.').ToStdString()); + std::string path = SampleHive::cUtils::Get().GetFilenamePathAndExtension(selected).Path.ToStdString(); SampleHive::cTags tags(path); int length = tags.GetAudioInfo().length; - SH_LOG_DEBUG("Sample length: {}", length); double position = m_MediaCtrl.Tell(); - SH_LOG_DEBUG("Current Sample Position: {}", position); int panel_width = this->GetSize().GetWidth(); double line_pos = panel_width * (position / length); - SH_LOG_DEBUG("Drawing playhead at: {}", line_pos); - m_PlayheadColour = wxColor(255, 0, 0, 255); // Draw the triangle @@ -165,15 +162,7 @@ void cWaveformViewer::UpdateWaveformBitmap() wxString selection = SampleHive::cHiveData::Get().GetListCtrlTextValue(selected_row, 1); - wxString filepath_with_extension = m_Database.GetSamplePathByFilename(selection.BeforeLast('.').ToStdString()); - wxString filepath_without_extension = m_Database.GetSamplePathByFilename(selection.ToStdString()); - - std::string extension = serializer.DeserializeShowFileExtension() ? - m_Database.GetSampleFileExtension(selection.ToStdString()) : - m_Database.GetSampleFileExtension(selection.BeforeLast('.').ToStdString()); - - wxString path = selection.Contains(wxString::Format(".%s", extension)) ? - filepath_with_extension : filepath_without_extension; + wxString path = SampleHive::cUtils::Get().GetFilenamePathAndExtension(selection).Path; SndfileHandle snd_file(path.ToStdString().c_str()); @@ -314,7 +303,7 @@ void cWaveformViewer::OnMouseMotion(wxMouseEvent& event) return; wxString selected = SampleHive::cHiveData::Get().GetListCtrlTextValue(selected_row, 1); - std::string path = m_Database.GetSamplePathByFilename(selected.BeforeLast('.').ToStdString()); + std::string path = SampleHive::cUtils::Get().GetFilenamePathAndExtension(selected).Path.ToStdString(); SampleHive::cTags tags(path); @@ -354,7 +343,7 @@ void cWaveformViewer::OnMouseLeftButtonDown(wxMouseEvent& event) return; wxString selected = SampleHive::cHiveData::Get().GetListCtrlTextValue(selected_row, 1); - std::string path = m_Database.GetSamplePathByFilename(selected.BeforeLast('.').ToStdString()); + std::string path = SampleHive::cUtils::Get().GetFilenamePathAndExtension(selected).Path.ToStdString(); SampleHive::cTags tags(path); @@ -403,7 +392,7 @@ void cWaveformViewer::OnMouseLeftButtonUp(wxMouseEvent& event) return; wxString selected = SampleHive::cHiveData::Get().GetListCtrlTextValue(selected_row, 1); - std::string path = m_Database.GetSamplePathByFilename(selected.BeforeLast('.').ToStdString()); + std::string path = SampleHive::cUtils::Get().GetFilenamePathAndExtension(selected).Path.ToStdString(); SampleHive::cTags tags(path); @@ -468,7 +457,7 @@ std::pair cWaveformViewer::CalculateLoopPoints() return { 0.0, 0.0 }; wxString selected = SampleHive::cHiveData::Get().GetListCtrlTextValue(selected_row, 1); - std::string path = m_Database.GetSamplePathByFilename(selected.BeforeLast('.').ToStdString()); + std::string path = SampleHive::cUtils::Get().GetFilenamePathAndExtension(selected).Path.ToStdString(); SampleHive::cTags tags(path); diff --git a/src/GUI/WaveformViewer.hpp b/src/GUI/WaveformViewer.hpp index cf1e90f..8a88354 100644 --- a/src/GUI/WaveformViewer.hpp +++ b/src/GUI/WaveformViewer.hpp @@ -20,8 +20,6 @@ #pragma once -#include "Database/Database.hpp" - #include #include #include @@ -35,7 +33,7 @@ class cWaveformViewer : public wxPanel { public: - cWaveformViewer(wxWindow* window, wxMediaCtrl& mediaCtrl, cDatabase& database); + cWaveformViewer(wxWindow* window, wxMediaCtrl& mediaCtrl); ~cWaveformViewer(); private: @@ -45,7 +43,6 @@ class cWaveformViewer : public wxPanel wxBoxSizer* m_Sizer; // ------------------------------------------------------------------- - cDatabase& m_Database; wxMediaCtrl& m_MediaCtrl; private: diff --git a/src/Utility/Utils.cpp b/src/Utility/Utils.cpp index 6e51bcc..299403f 100644 --- a/src/Utility/Utils.cpp +++ b/src/Utility/Utils.cpp @@ -34,26 +34,15 @@ namespace SampleHive { SampleHive::cUtils::FileInfo SampleHive::cUtils::GetFilenamePathAndExtension(const wxString& selected, - bool checkExtension, bool doGetFilename) const + bool checkExtension, bool doGetFilename) { - SampleHive::cSerializer serializer; - cDatabase db; - wxString path; std::string extension, filename; - wxString filename_with_extension = db.GetSamplePathByFilename(selected.BeforeLast('.').ToStdString()); - wxString filename_without_extension = db.GetSamplePathByFilename(selected.ToStdString()); + path = GetSamplePath(selected); if (checkExtension) - { - extension = serializer.DeserializeShowFileExtension() ? - db.GetSampleFileExtension(selected.ToStdString()) : - db.GetSampleFileExtension(selected.BeforeLast('.').ToStdString()); - } - - path = selected.Contains(wxString::Format(".%s", extension)) ? - filename_with_extension : filename_without_extension; + extension = path.AfterLast('/').AfterLast('.').ToStdString(); if (doGetFilename) filename = path.AfterLast('/').BeforeLast('.').ToStdString(); @@ -222,4 +211,25 @@ namespace SampleHive { SH_LOG_DEBUG("Done Importing Samples"); } + std::string cUtils::GetSamplePath(const wxString& name) + { + SampleHive::cSerializer serializer; + cDatabase db; + + wxString sample_path; + + if (m_PathCache.find(name.ToStdString()) != m_PathCache.end()) + return m_PathCache[name.ToStdString()]; + else + { + sample_path = serializer.DeserializeShowFileExtension() ? + db.GetSamplePathByFilename(name.BeforeLast('.').ToStdString()) : + db.GetSamplePathByFilename(name.ToStdString()); + + m_PathCache[name.ToStdString()] = sample_path; + } + + return sample_path.ToStdString(); + } + } diff --git a/src/Utility/Utils.hpp b/src/Utility/Utils.hpp index 082894b..e457c6c 100644 --- a/src/Utility/Utils.hpp +++ b/src/Utility/Utils.hpp @@ -22,6 +22,8 @@ #include "wx/string.h" #include "wx/window.h" +#include + namespace SampleHive { class cUtils @@ -30,10 +32,12 @@ namespace SampleHive { cUtils() = default; public: + // ------------------------------------------------------------------- cUtils(const cUtils&) = delete; cUtils& operator=(const cUtils) = delete; public: + // ------------------------------------------------------------------- static cUtils& Get() { static cUtils s_cUtils; @@ -41,6 +45,7 @@ namespace SampleHive { } public: + // ------------------------------------------------------------------- struct FileInfo { wxString Path; @@ -51,9 +56,17 @@ namespace SampleHive { // ------------------------------------------------------------------- cUtils::FileInfo GetFilenamePathAndExtension(const wxString& selected, bool checkExtension = true, - bool doGetFilename = true) const; + bool doGetFilename = true); void AddSamples(wxArrayString& files, wxWindow* parent); void OnAutoImportDir(const wxString& pathToDirectory, wxWindow* parent); + + private: + // ------------------------------------------------------------------- + std::string GetSamplePath(const wxString& name); + + private: + // ------------------------------------------------------------------- + std::unordered_map m_PathCache; }; }