#include "config.h" #include "log.h" #include #include #include #include const char *device = "default"; // playback device std::string config_filename = "click.cfg"; Config::Config() { recover(); } Config::~Config() { persist(); } void Config::recover() { try { // presets / defaults m_midi_channel = CLICK_CHANNEL; m_midi_note = CLICK_NOTE; m_bpm = default_bpm; m_mode = default_mode; m_output = default_output; std::string config = Reichwein::File::getFile(config_filename); std::vector lines = Reichwein::Stringhelper::split(config, "\n"); for (const auto& i: lines) { if (i.starts_with("midi_channel=")) { m_midi_channel = stoul(i.substr(13)); signal_channel(m_midi_channel); } if (i.starts_with("midi_note=")) { m_midi_note = stoul(i.substr(10)); signal_note(m_midi_note); } if (i.starts_with("bpm=")) { m_bpm = stoul(i.substr(4)); signal_bpm(m_bpm); } if (i.starts_with("mode=")) { m_mode = stoul(i.substr(5)); signal_mode(m_mode); } if (i.starts_with("output=")) { m_output = stoul(i.substr(7)); signal_output(m_output); } } } catch (const std::exception& ex) { log_cout << "Config not found. Setting config to defaults." << std::endl; } } void Config::persist() { std::string config = fmt::format("midi_channel={}\n", m_midi_channel) + fmt::format("midi_note={}\n", m_midi_note) + fmt::format("bpm={}\n", m_bpm) + fmt::format("mode={}\n", m_mode) + fmt::format("output={}\n", m_output); Reichwein::File::setFile(config_filename, config); } int Config::get_midi_channel() { return m_midi_channel; } void Config::set_midi_channel(int channel) { m_midi_channel = channel; signal_channel(channel); } int Config::get_midi_note() { return m_midi_note; } void Config::set_midi_note(int note) { m_midi_note = note; signal_note(note); } int Config::get_bpm() { return m_bpm; } void Config::set_bpm(int bpm) { m_bpm = bpm; signal_bpm(bpm); } int Config::get_mode() { return m_mode; } void Config::set_mode(int mode) { m_mode = mode; signal_mode(mode); } int Config::get_output() { return m_output; } void Config::set_output(int output) { m_output = output; signal_output(output); }