Skip to content

Commit

Permalink
AudioGraphConfig: Add support for reading from a user-specified confi…
Browse files Browse the repository at this point in the history
…g path
  • Loading branch information
ideoforms committed Sep 8, 2024
1 parent afee359 commit d261432
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
15 changes: 15 additions & 0 deletions source/include/signalflow/core/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down Expand Up @@ -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;
Expand Down
26 changes: 24 additions & 2 deletions source/src/core/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,33 @@ std::map<std::string, std::map<std::string, std::string>> 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)
{
Expand Down Expand Up @@ -137,7 +156,10 @@ AudioGraphConfig::AudioGraphConfig()
}
}
}
}

void AudioGraphConfig::parse_env()
{
if (getenv("SIGNALFLOW_SAMPLE_RATE"))
{
this->sample_rate = atoi(getenv("SIGNALFLOW_SAMPLE_RATE"));
Expand Down
1 change: 1 addition & 0 deletions source/src/python/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ void init_python_config(py::module &m)
*-------------------------------------------------------------------------------*/
py::class_<AudioGraphConfig>(m, "AudioGraphConfig", "Configuration options for the AudioGraph")
.def(py::init<>())
.def(py::init<std::string>(), 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)
Expand Down

0 comments on commit d261432

Please sign in to comment.