add config integration for keymap and logger
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
|
||||
#include "ConfigService.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <exception>
|
||||
|
||||
ConfigService::ConfigService(const std::string& filePath) {
|
||||
@@ -30,51 +29,7 @@ bool ConfigService::loadFromFile(const std::string& filePath) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ConfigService::getLoggerConfig(const std::string& id, LoggerConfig* loggerConfig) const {
|
||||
try {
|
||||
const libconfig::Setting& loggers = config_.lookup("Loggers");
|
||||
for (int index = 0; index < loggers.getLength(); ++index) {
|
||||
const libconfig::Setting& loggerSetting = loggers[index];
|
||||
|
||||
std::string loggerId;
|
||||
if (!loggerSetting.lookupValue("Id", loggerId) || loggerId != id) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return parseLoggerConfig(loggerSetting, loggerConfig);
|
||||
}
|
||||
} catch (const libconfig::SettingException& ex) {
|
||||
std::cout << "libconfig setting exception: " << ex.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::string& ConfigService::lastError() const {
|
||||
return lastError_;
|
||||
}
|
||||
|
||||
bool ConfigService::parseLoggerConfig(const libconfig::Setting& loggerSetting, LoggerConfig* loggerConfig) const {
|
||||
|
||||
if (!loggerSetting.lookupValue("Id", loggerConfig->id)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
const libconfig::Setting& flags = loggerSetting.lookup("FlagsEnabled");
|
||||
for (int index = 0; index < flags.getLength(); ++index) {
|
||||
loggerConfig->flagsEnabled.push_back(static_cast<const char*>(flags[index]));
|
||||
}
|
||||
} catch (const libconfig::SettingException&) {
|
||||
loggerConfig->flagsEnabled.clear();
|
||||
}
|
||||
|
||||
loggerSetting.lookupValue("ShowTime", loggerConfig->showTime);
|
||||
loggerSetting.lookupValue("ShowSourceTrace", loggerConfig->showSourceTrace);
|
||||
loggerSetting.lookupValue("CoutEnabled", loggerConfig->coutEnabled);
|
||||
loggerSetting.lookupValue("FileEnabled", loggerConfig->fileEnabled);
|
||||
loggerSetting.lookupValue("FilePath", loggerConfig->filePath);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -6,6 +6,8 @@
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <unordered_map>
|
||||
|
||||
struct LoggerConfig {
|
||||
std::string id;
|
||||
@@ -15,6 +17,30 @@ struct LoggerConfig {
|
||||
bool coutEnabled = false;
|
||||
bool fileEnabled = false;
|
||||
std::string filePath;
|
||||
|
||||
bool parseConfig(const libconfig::Setting& setting) {
|
||||
|
||||
if (!setting.lookupValue("Id", id)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
const libconfig::Setting& flags = setting.lookup("FlagsEnabled");
|
||||
for (int index = 0; index < flags.getLength(); ++index) {
|
||||
flagsEnabled.push_back(static_cast<const char*>(flags[index]));
|
||||
}
|
||||
} catch (const libconfig::SettingException&) {
|
||||
flagsEnabled.clear();
|
||||
}
|
||||
|
||||
setting.lookupValue("ShowTime", showTime);
|
||||
setting.lookupValue("ShowSourceTrace", showSourceTrace);
|
||||
setting.lookupValue("CoutEnabled", coutEnabled);
|
||||
setting.lookupValue("FileEnabled", fileEnabled);
|
||||
setting.lookupValue("FilePath", filePath);
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
struct AudioConfig {
|
||||
@@ -27,6 +53,40 @@ struct AudioConfig {
|
||||
int32_t notesPerOctave;
|
||||
};
|
||||
|
||||
struct KeymapConfig {
|
||||
std::string id;
|
||||
std::unordered_map<int32_t, uint8_t> keymap;
|
||||
|
||||
bool parseConfig(const libconfig::Setting& setting) {
|
||||
|
||||
if (!setting.lookupValue("Id", id)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
const libconfig::Setting& keymapGroup = setting.lookup("Keymap");
|
||||
const libconfig::Setting& notesGroup = setting.lookup("Notes");
|
||||
const libconfig::Setting& keysGroup = setting.lookup("Keys");
|
||||
|
||||
for(size_t i = 0; i < keymapGroup.getLength(); i++) {
|
||||
libconfig::Setting& item = keymapGroup[i];
|
||||
std::string key = item.getName();
|
||||
std::string note = "";
|
||||
if(item.getType() == libconfig::Setting::TypeString) note = std::string(item.c_str());
|
||||
|
||||
int8_t noteId = static_cast<int>(notesGroup[note]) % INT8_MAX;
|
||||
int32_t keyId = static_cast<int>(keysGroup[key]) % INT8_MAX;
|
||||
keymap.emplace(keyId, noteId);
|
||||
}
|
||||
|
||||
} catch (const libconfig::SettingException&) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
class ConfigService {
|
||||
|
||||
public:
|
||||
@@ -36,11 +96,31 @@ public:
|
||||
|
||||
bool loadFromFile(const std::string& filePath);
|
||||
|
||||
bool getLoggerConfig(const std::string& id, LoggerConfig* loggerConfig) const;
|
||||
template<typename T>
|
||||
bool getConfig(const std::string& type, const std::string& id, T* config) const {
|
||||
try {
|
||||
const libconfig::Setting& configs = config_.lookup(type);
|
||||
for (int index = 0; index < configs.getLength(); ++index) {
|
||||
const libconfig::Setting& configSetting = configs[index];
|
||||
|
||||
std::string configId;
|
||||
if (!configSetting.lookupValue("Id", configId) || configId != id) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return config->parseConfig(configSetting);
|
||||
}
|
||||
} catch (const libconfig::SettingException& ex) {
|
||||
std::cout << "libconfig setting exception: " << ex.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::string& lastError() const;
|
||||
|
||||
private:
|
||||
bool parseLoggerConfig(const libconfig::Setting& loggerSetting, LoggerConfig* loggerConfig) const;
|
||||
|
||||
libconfig::Config config_;
|
||||
std::string lastError_;
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace fs = std::filesystem;
|
||||
|
||||
LoggerService::LoggerService(ConfigService* config, const std::string& loggerId) {
|
||||
|
||||
if(!(config->getLoggerConfig(loggerId, &configuration_))) {
|
||||
if(!(config->getConfig<LoggerConfig>("Logger", loggerId, &configuration_))) {
|
||||
std::cout << "Failed to get logger configuration fom config service" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "LoggerService.hpp"
|
||||
#include "ConfigService.hpp"
|
||||
#include "synth/AudioEngine.hpp"
|
||||
#include "synth/KeyboardController.hpp"
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
|
||||
@@ -33,6 +34,8 @@ int main(int argc, char* argv[]) {
|
||||
// create app objects
|
||||
ConfigService config = ConfigService("config/sonobulus.cfg");
|
||||
LoggerService logger = LoggerService(&config, "Engine");
|
||||
NoteQueue queue = NoteQueue();
|
||||
KeyboardController keyboard = KeyboardController(&config, &logger, &queue);
|
||||
|
||||
// audio synthesizer doohickey
|
||||
AudioEngine audioEngine = AudioEngine(&logger);
|
||||
|
||||
@@ -5,37 +5,14 @@
|
||||
// #include <yaml-cpp/yaml.h>
|
||||
#include <filesystem>
|
||||
|
||||
KeyboardController::KeyboardController(NoteQueue& queue, ConfigService* config) : queue_(queue), config_(config) {
|
||||
KeyboardController::KeyboardController(ConfigService* config, LoggerService* logger, NoteQueue* queue) : config_(config), logger_(logger), queue_(queue) {
|
||||
|
||||
// load keymap from config file
|
||||
std::string filepath = "config/keymap.yaml";
|
||||
filepath = std::filesystem::absolute(filepath).string();
|
||||
// YAML::Node file;
|
||||
try {
|
||||
// file = YAML::LoadFile(filepath);
|
||||
} catch(const std::exception& e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
// load keymap from config service
|
||||
if(!(config->getConfig<KeymapConfig>("KeyboardController", "Main", &configuration_))) {
|
||||
logger_->log("Keyboard", LogFlag::Error, "Failed to get logger configuration fom config service");
|
||||
return;
|
||||
}
|
||||
|
||||
// YAML::Node keymapNode = file["keymap"]; // node for string to string mappings
|
||||
// YAML::Node notesNode = file["notes"]; // string to midi int mappings
|
||||
// YAML::Node keysNode = file["keys"]; // string to qt key id mappings
|
||||
|
||||
// for each element in the keymap
|
||||
// for (const auto& entry : keymapNode) {
|
||||
|
||||
// std::string keyString = entry.first.as<std::string>();
|
||||
// std::string noteString = entry.second.as<std::string>();
|
||||
|
||||
// // match the strings to ints
|
||||
// uint8_t noteValue = notesNode[noteString].as<uint8_t>();
|
||||
// uint32_t keyValue = keysNode[keyString].as<uint32_t>();
|
||||
|
||||
// // insert into map
|
||||
// keymap_.emplace(keyValue, noteValue);
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
void KeyboardController::handleKeyPress(QKeyEvent* e) {
|
||||
@@ -44,7 +21,7 @@ void KeyboardController::handleKeyPress(QKeyEvent* e) {
|
||||
auto it = keymap_.find(e->key());
|
||||
if (it == keymap_.end()) return;
|
||||
|
||||
queue_.push({
|
||||
queue_->push({
|
||||
NoteEventType::NoteOn,
|
||||
it->second,
|
||||
0.8f,
|
||||
@@ -58,7 +35,7 @@ void KeyboardController::handleKeyRelease(QKeyEvent* e) {
|
||||
auto it = keymap_.find(e->key());
|
||||
if (it == keymap_.end()) return;
|
||||
|
||||
queue_.push({
|
||||
queue_->push({
|
||||
NoteEventType::NoteOff,
|
||||
it->second,
|
||||
0.8f,
|
||||
|
||||
@@ -7,12 +7,13 @@
|
||||
|
||||
#include "NoteQueue.hpp"
|
||||
#include "ConfigService.hpp"
|
||||
#include "LoggerService.hpp"
|
||||
|
||||
// The keyboardcontroller handles user inputs from a keyboard and maps them to note events
|
||||
class KeyboardController {
|
||||
|
||||
public:
|
||||
explicit KeyboardController(NoteQueue& queue, ConfigService* config);
|
||||
explicit KeyboardController(ConfigService* config, LoggerService* logger, NoteQueue* queue);
|
||||
~KeyboardController() = default;
|
||||
|
||||
void handleKeyPress(QKeyEvent* e);
|
||||
@@ -20,10 +21,13 @@ public:
|
||||
|
||||
private:
|
||||
|
||||
NoteQueue& queue_;
|
||||
NoteQueue* queue_;
|
||||
ConfigService* config_;
|
||||
LoggerService* logger_;
|
||||
|
||||
// keymap is key -> midi note id
|
||||
std::unordered_map<int32_t, uint8_t> keymap_;
|
||||
|
||||
KeymapConfig configuration_;
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user