make basic audio engine parameters configurable

This commit is contained in:
2026-01-24 14:07:38 -06:00
parent a4ea6490a4
commit 69a507d57b
10 changed files with 78 additions and 56 deletions

View File

@@ -11,39 +11,34 @@ namespace fs = std::filesystem;
ConfigInterface::ConfigInterface() {
std::string audioConfig = configRoot + "/" + filename;
std::cout << "CWD: " << fs::current_path() << std::endl;
std::cout << "Audio config: " << fs::absolute(audioConfig).string() << std::endl;
// opening the file myself because YAML::LoadFile didn't seem to work on windows
std::ifstream file(fs::absolute(audioConfig).string());
if(file.good()) {
std::cout << "File exists" << std::endl;
} else {
std::cout << "File does not exist" << std::endl;
}
//YAML::Node config;
/*
try {
YAML::Node config = YAML::Load("{sampleRate: 44100}");
std::cout << "Loaded config file" << std::endl;
std::cout << "Type enum: " << static_cast<int>(config.Type()) << std::endl;
if(config["sampleRate"]) {
//int sampleRate = config["sampleRate"].as<int>();
//std::cout << sampleRate << std::endl;
} else {
std::cout << "Key does not exist" << std::endl;
}
} catch(const std::exception& e) {
std::cerr << e.what() << '\n';
}
*/
YAML::Node n = YAML::Load("{sampleRate: 44100}");
std::cout << n["sampleRate"].as<int>() << "\n";
//std::cout << "Config constructor" << std::endl;
}
int ConfigInterface::getValue(ConfigFile file, std::string key, int defaultVal) {
// assemble filepath
std::string filepath = configRoot + "/" + filePaths[static_cast<int>(file)];
filepath = fs::absolute(filepath).string();
// attempt to open file
YAML::Node config;
try {
YAML::Node config = YAML::LoadFile(filepath);
// read key if it exists
if(config[key]) {
return config[key].as<int>(defaultVal);
} else {
return -1; // key does not exist
}
} catch(const std::exception& e) {
std::cerr << e.what() << std::endl;
return -1;
}
// unreachable
return -1;
}

View File

@@ -2,6 +2,17 @@
#pragma once
#include <string>
#include <vector>
enum class ConfigFile {
Audio = 0
// other files here
};
// might have a config file for specifying paths to other config files instead of this
const std::vector<std::string> filePaths = {
"audio.yaml"
};
class ConfigInterface {
@@ -10,9 +21,10 @@ public:
ConfigInterface();
~ConfigInterface() = default;
int getValue(ConfigFile file, std::string key, int defaultVal);
private:
const std::string configRoot = "config";
const std::string filename = "audio.yaml";
};

View File

@@ -9,9 +9,7 @@
int main(int argc, char *argv[]) {
// std::cout << "Main()" << std::endl;
ConfigInterface config = ConfigInterface();
QApplication app(argc, argv);
MainWindow window; // entry point goes to MainWindow::MainWindow()

View File

@@ -3,14 +3,11 @@
#include <iostream>
AudioEngine::AudioEngine() : synth_(params_) {
AudioEngine::AudioEngine(ConfigInterface* config) : synth_(params_), config_(config) {
if(audio_.getDeviceCount() < 1) {
throw std::runtime_error("No audio devices found");
}
// TODO: get audio configurations
synth_.setSampleRate(sampleRate_);
synth_.setScopeBuffer(&scope_);
}
@@ -21,6 +18,13 @@ AudioEngine::~AudioEngine() {
bool AudioEngine::start() {
// get config values
sampleRate_ = config_->getValue(ConfigFile::Audio, "sampleRate", sampleRate_);
bufferFrames_ = config_->getValue(ConfigFile::Audio, "bufferSize", bufferFrames_);
channels_ = config_->getValue(ConfigFile::Audio, "channels", channels_);
synth_.setSampleRate(sampleRate_);
// initialize the audio engine
RtAudio::StreamParameters params;
params.deviceId = audio_.getDefaultOutputDevice();

View File

@@ -7,6 +7,7 @@
#include "Synth.h"
#include "../KeyboardController.h"
#include "../ConfigInterface.h"
#if defined(_WIN32)
#define AUDIO_API RtAudio::WINDOWS_WASAPI
@@ -17,7 +18,7 @@
class AudioEngine {
public:
AudioEngine();
AudioEngine(ConfigInterface* config);
~AudioEngine();
// starts the audio stream. returns true on success and false on failure
@@ -43,6 +44,7 @@ private:
NoteQueue noteQueue_; // stores note events for passing between threads
Synth synth_; // generates audio
ScopeBuffer scope_ { 1024 }; // stores audio samples for visualization
ConfigInterface* config_; // access to config files
RtAudio audio_{AUDIO_API}; // audio device
// TODO: id like a yml config file or something for these

View File

@@ -11,11 +11,12 @@ void ScopeBuffer::push(float sample) {
buffer_[w % buffer_.size()] = sample;
}
// TODO: needs a mutex to prevent flickering
// TODO: needs a mutex/spinlock to prevent flickering
// outputs value from the scope buffer, called by the scope widget
void ScopeBuffer::read(std::vector<float>& out) const {
while(!spinLock_) { int x = 1 + 1; }
// yeah this didn't work, maybe it needs to be atomic or something
//while(!spinLock_) { int x = 1 + 1; }
size_t w = writeIndex_.load(std::memory_order_relaxed);
for (size_t i = 0; i < out.size(); i++) {

View File

@@ -8,7 +8,7 @@
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui_(new Ui::MainWindow),
audio_(new AudioEngine()),
audio_(new AudioEngine(&config_)),
keyboard_(audio_->noteQueue()),
midi_(audio_->noteQueue()) {

View File

@@ -6,6 +6,7 @@
#include "../synth/AudioEngine.h"
#include "../MidiController.h"
#include "../ConfigInterface.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
@@ -32,5 +33,6 @@ private:
AudioEngine* audio_ = nullptr;
KeyboardController keyboard_;
MidiController midi_;
ConfigInterface config_;
};