diff options
author | Roland Reichwein <mail@reichwein.it> | 2025-01-17 21:02:51 +0100 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2025-01-17 21:02:51 +0100 |
commit | bb56941f6b61529dd770475296d093727c68fdc6 (patch) | |
tree | 065c61ef71a00d0759afcdc0316150db278a7efd | |
parent | 22ed919b2fffa933c8a72763fda2b603a92a18cf (diff) |
Automatically use correct files path (executable path or /media/usb)
-rw-r--r-- | MIDIPlayer.cpp | 9 | ||||
-rw-r--r-- | MIDIPlayer.h | 5 | ||||
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | config.cpp | 26 | ||||
-rw-r--r-- | config.h | 13 | ||||
-rw-r--r-- | midiplay.cpp | 5 |
6 files changed, 55 insertions, 6 deletions
diff --git a/MIDIPlayer.cpp b/MIDIPlayer.cpp index 2c0bc6a..8202850 100644 --- a/MIDIPlayer.cpp +++ b/MIDIPlayer.cpp @@ -1,5 +1,7 @@ #include "MIDIPlayer.h" +#include "config.h" + #include <signal.h> #include <fmt/format.h> @@ -16,12 +18,13 @@ namespace fs = std::filesystem; using namespace std::chrono_literals; namespace { - std::unordered_set<std::string> supported_devices{"AudioBox 22 VSL", "CH345"}; + std::unordered_set<std::string> supported_devices{"AudioBox 22 VSL", "CH345", "M2"}; } -MIDIPlayer::MIDIPlayer(const std::filesystem::path& path): +MIDIPlayer::MIDIPlayer(Config& config): + m_config(config), m_child{}, - m_dir{path}, + m_dir{m_config.get_file_path()}, m_file{} { std::vector<std::string> list = get_filelist(); diff --git a/MIDIPlayer.h b/MIDIPlayer.h index 811bea4..8009029 100644 --- a/MIDIPlayer.h +++ b/MIDIPlayer.h @@ -6,12 +6,14 @@ #include <boost/process.hpp> #include <alsa/asoundlib.h> +#include "config.h" + #include <filesystem> class MIDIPlayer { public: - MIDIPlayer(const std::filesystem::path& path = "."); + MIDIPlayer(Config& config); void start(); @@ -32,6 +34,7 @@ private: void close_seq(void); void iterate_ports(void); + Config& m_config; boost::process::child m_child; std::filesystem::path m_dir; std::string m_file; @@ -2,7 +2,8 @@ TARGET=midiplay SRCS=\ midiplay.cpp \ - MIDIPlayer.cpp + MIDIPlayer.cpp \ + config.cpp \ OBJS=$(SRCS:.cpp=.o) diff --git a/config.cpp b/config.cpp new file mode 100644 index 0000000..beb7f97 --- /dev/null +++ b/config.cpp @@ -0,0 +1,26 @@ +#include "config.h" + +#include <string> +#include <filesystem> + +namespace fs = std::filesystem; + +Config::Config(int argc, char* argv[]) +{ + m_executable_path = fs::path{argv[0]}.parent_path().string(); +} + +std::string Config::get_executable_path() +{ + return m_executable_path; +} + +std::string Config::get_file_path() +{ + // Just a heuristic: If this exists in current dir, then use this path + if (fs::exists(fs::path{m_executable_path} / "magic1.midi")) { + return m_executable_path; + } + return "/media/usb"; +} + diff --git a/config.h b/config.h new file mode 100644 index 0000000..d0520dd --- /dev/null +++ b/config.h @@ -0,0 +1,13 @@ +#pragma once + +#include <string> + +class Config { + public: + Config(int argc, char* argv[]); + std::string get_executable_path(); + std::string get_file_path(); + + private: + std::string m_executable_path; +}; diff --git a/midiplay.cpp b/midiplay.cpp index bc1ad08..36b732e 100644 --- a/midiplay.cpp +++ b/midiplay.cpp @@ -1,5 +1,7 @@ #include "MIDIPlayer.h" +#include "config.h" + #include <stdexcept> #include <string> #include <iostream> @@ -81,7 +83,8 @@ std::string to_xml(const std::vector<std::string>& filelist, const std::string& int main(int argc, char* argv[]) { try { - MIDIPlayer player; + Config config{argc, argv}; + MIDIPlayer player{config}; std::string ok_data{"<data><status>ok</status><message>OK</message></data>"}; std::string error_data{"<data><status>error</status><message>General Error</message></data>"}; |