From d2614321c028eab35e2882ab4944c43f24b01489 Mon Sep 17 00:00:00 2001 From: Daniel Jones Date: Sun, 8 Sep 2024 18:00:44 +0200 Subject: [PATCH] AudioGraphConfig: Add support for reading from a user-specified config path --- source/include/signalflow/core/config.h | 15 ++++++++++++++ source/src/core/config.cpp | 26 +++++++++++++++++++++++-- source/src/python/config.cpp | 1 + 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/source/include/signalflow/core/config.h b/source/include/signalflow/core/config.h index f2fd2330..83980898 100644 --- a/source/include/signalflow/core/config.h +++ b/source/include/signalflow/core/config.h @@ -14,8 +14,20 @@ namespace signalflow class AudioGraphConfig { public: + /**-------------------------------------------------------------------------------- + * Create a new AudioGraphConfig, reading configuration from the default path + * (~/.signalflow/config) + * + *--------------------------------------------------------------------------------*/ AudioGraphConfig(); + /**-------------------------------------------------------------------------------- + * Create a new AudioGraphConfig, reading from a user-specified path. + * If the path does not exist, raises a runtime exception. + * + *--------------------------------------------------------------------------------*/ + AudioGraphConfig(std::string config_path); + /**-------------------------------------------------------------------------------- * Get the preferred sample rate. * To query the actual sample rate, use AudioGraph::get_sample_rate(). @@ -152,6 +164,9 @@ class AudioGraphConfig void print() const; private: + void parse_file(std::ifstream &input); + void parse_env(); + unsigned int sample_rate = 0; unsigned int input_buffer_size = 0; unsigned int output_buffer_size = 0; diff --git a/source/src/core/config.cpp b/source/src/core/config.cpp index 2d408a66..9ec10e00 100644 --- a/source/src/core/config.cpp +++ b/source/src/core/config.cpp @@ -70,14 +70,33 @@ std::map> parse_config(std::istr AudioGraphConfig::AudioGraphConfig() { - const std::string config_path = SIGNALFLOW_USER_DIR + "/config"; + std::string config_path = SIGNALFLOW_USER_DIR + "/config"; + std::ifstream input(config_path); + + // Don't throw an error if the user config file does not exist, + // just continue silently. + if (input.good()) + { + parse_file(input); + } + + parse_env(); +} +AudioGraphConfig::AudioGraphConfig(std::string config_path) +{ std::ifstream input(config_path); if (!input.good()) { - return; + throw std::runtime_error("Config path could not be read: " + config_path); } + parse_file(input); + parse_env(); +} + +void AudioGraphConfig::parse_file(std::ifstream &input) +{ auto sections = parse_config(input); for (auto section_pair : sections) { @@ -137,7 +156,10 @@ AudioGraphConfig::AudioGraphConfig() } } } +} +void AudioGraphConfig::parse_env() +{ if (getenv("SIGNALFLOW_SAMPLE_RATE")) { this->sample_rate = atoi(getenv("SIGNALFLOW_SAMPLE_RATE")); diff --git a/source/src/python/config.cpp b/source/src/python/config.cpp index 1dd5197d..c62aeeff 100644 --- a/source/src/python/config.cpp +++ b/source/src/python/config.cpp @@ -7,6 +7,7 @@ void init_python_config(py::module &m) *-------------------------------------------------------------------------------*/ py::class_(m, "AudioGraphConfig", "Configuration options for the AudioGraph") .def(py::init<>()) + .def(py::init(), R"pbdoc(Read an AudioGraphConfig from a user-specified path)pbdoc") .def("print", &AudioGraphConfig::print, R"pbdoc(Print the AudioGraphConfig to stdout)pbdoc") .def_property("sample_rate", &AudioGraphConfig::get_sample_rate, &AudioGraphConfig::set_sample_rate) .def_property("input_buffer_size", &AudioGraphConfig::get_input_buffer_size, &AudioGraphConfig::set_input_buffer_size)