diff options
-rw-r--r-- | MainLoop.cpp | 4 | ||||
-rw-r--r-- | MainLoop.h | 2 | ||||
-rw-r--r-- | PCM.cpp | 2 | ||||
-rw-r--r-- | config.cpp | 36 | ||||
-rw-r--r-- | config.h | 25 | ||||
-rw-r--r-- | main.cpp | 4 |
6 files changed, 50 insertions, 23 deletions
diff --git a/MainLoop.cpp b/MainLoop.cpp index b1bfff0..8f3e399 100644 --- a/MainLoop.cpp +++ b/MainLoop.cpp @@ -22,8 +22,8 @@ using namespace std::chrono_literals; using namespace std::string_literals; -MainLoop::MainLoop(): - m_config{}, +MainLoop::MainLoop(int argc, char** argv): + m_config{argc, argv}, m_note_click{m_config}, m_clock_click{}, m_internal_click{m_config}, @@ -12,7 +12,7 @@ class MainLoop { public: - MainLoop(); + MainLoop(int argc, char** argv); int run(); private: @@ -5,7 +5,7 @@ PCM::PCM(Config& config): m_phase(1000000) { // prepare the sample - std::string data_s = Reichwein::File::getFile("media/click.s16le"); + std::string data_s = Reichwein::File::getFile(m_config.get_click_data_filename()); m_data.resize(data_s.size() / 2); // src is in bytes memcpy(m_data.data(), data_s.data(), data_s.size()); @@ -2,9 +2,6 @@ #include "log.h" -#include <filesystem> -#include <string> - #include <libreichwein/file.h> #include <libreichwein/stringhelper.h> @@ -14,12 +11,21 @@ using namespace std::string_literals; +namespace fs = std::filesystem; + const char *device = "default"; // playback device std::string config_filename = "click.cfg"; -Config::Config() +fs::path click_data_filename = "media/click.s16le"; + +Config::Config(int argc, char** argv): + m_argv(argc) { + for (int i = 0; i < argc; ++i) { + m_argv[i] = argv[i]; + } + recover(); } @@ -81,7 +87,7 @@ void Config::persist() Reichwein::File::setFile(get_config_filename(), config); } -int Config::get_midi_channel() +int Config::get_midi_channel() const { return m_midi_channel; } @@ -92,7 +98,7 @@ void Config::set_midi_channel(int channel) signal_channel(channel); } -int Config::get_midi_note() +int Config::get_midi_note() const { return m_midi_note; } @@ -103,7 +109,7 @@ void Config::set_midi_note(int note) signal_note(note); } -int Config::get_bpm() +int Config::get_bpm() const { return m_bpm; } @@ -114,7 +120,7 @@ void Config::set_bpm(int bpm) signal_bpm(bpm); } -int Config::get_mode() +int Config::get_mode() const { return m_mode; } @@ -125,7 +131,7 @@ void Config::set_mode(int mode) signal_mode(mode); } -int Config::get_output() +int Config::get_output() const { return m_output; } @@ -136,7 +142,7 @@ void Config::set_output(int output) signal_output(output); } -std::string Config::get_config_filename() +std::string Config::get_config_filename() const { char* envvar = getenv("HOME"); if (envvar == nullptr || !std::filesystem::exists(envvar)) { @@ -145,3 +151,13 @@ std::string Config::get_config_filename() return std::filesystem::path(getenv("HOME")) / ("."s + config_filename); } } + +std::string Config::get_argv(int i) const +{ + return m_argv[i]; +} + +std::filesystem::path Config::get_click_data_filename() const +{ + return fs::path{m_argv[0]}.parent_path() / click_data_filename; +} @@ -3,6 +3,10 @@ #include <boost/signals2.hpp> #include <alsa/asoundlib.h> +#include <filesystem> +#include <string> +#include <vector> + // Defaults static const int CLICK_NOTE = 37; static const int CLICK_CHANNEL = 4; @@ -15,11 +19,12 @@ const int pcm_latency_us = 100000; const int click_latency_frames = 10000; const int default_mode = 0; // 0 = note, 1 = clock, 2 = internal const int default_output = 1; // 0 = off, 1 = on +extern std::filesystem::path click_data_filename; class Config { public: - Config(); + Config(int argc, char** argv); ~Config(); // signals @@ -29,26 +34,32 @@ public: boost::signals2::signal<void(int)> signal_bpm; boost::signals2::signal<void(int)> signal_output; - int get_midi_channel(); + int get_midi_channel() const; void set_midi_channel(int channel); - int get_midi_note(); + int get_midi_note() const; void set_midi_note(int note); - int get_bpm(); + int get_bpm() const; void set_bpm(int bpm); - int get_mode(); + int get_mode() const; void set_mode(int mode); - int get_output(); + int get_output() const; void set_output(int output); void recover(); void persist(); + std::string get_argv(int i) const; + + std::filesystem::path get_click_data_filename() const; + private: - std::string get_config_filename(); + std::string get_config_filename() const; + + std::vector<std::string> m_argv; int m_midi_channel; int m_midi_note; @@ -1,9 +1,9 @@ #include "MainLoop.h" -int main() { +int main(int argc, char** argv) { try { - MainLoop main_loop; + MainLoop main_loop{argc, argv}; return main_loop.run(); } catch (const std::exception& ex) { std::cerr << "Error: " << ex.what() << std::endl; |