wip: apply gui states

This commit is contained in:
2024-01-19 01:55:51 +07:00
parent b996685fe6
commit 19c90b0434
2 changed files with 64 additions and 29 deletions

View File

@@ -18,7 +18,7 @@ class Application {
void InitAudio();
void UpdateOnNoteInput();
void PlayBufferedAudio();
public:
Application(/* args */);
~Application();

View File

@@ -1,10 +1,11 @@
#include "Application.h"
#include "Logger.h"
#include "Settings.h"
#include <string>
#include <fstream> //read file
#include <cmath> // log10f
#include <cmath> // log10f
#include <fstream> // read file
#include <iostream> // log error
#include <nlohmann/json.hpp>
#include <string>
using json = nlohmann::json;
Application::Application(/* args */) {
@@ -98,7 +99,7 @@ void Application::UpdateOnNoteInput() {
if (!m_synth.GetIsNoteTriggered()) {
m_synth.Trigger((*m_current_note));
}
//write_log("Note played: %s\n", m_current_note->name.c_str());
// write_log("Note played: %s\n", m_current_note->name.c_str());
} else if (is_note_up() && m_synth.GetIsNoteTriggered()) {
m_synth.Release();
}
@@ -125,30 +126,64 @@ void Application::Run() {
void Application::ParsePatch(std::string file_path) {
std::ifstream f(file_path);
json data = json::parse(f);
if (!f.is_open()) {
std::cerr << "[ERR] failed to open " << file_path << '\n';
} else {
json data = json::parse(f);
auto oscillators = m_synth.GetOscillators();
for (int i = 0; i < oscillators.size(); i++) {
auto osc = oscillators[i];
osc->SetType(data["Oscillators"][i]["Wave"].template get<std::string>());
osc->SetVolume(data["Oscillators"][i]["Volume"].template get<float>());
osc->SetFine(data["Oscillators"][i]["Tune"].template get<float>());
auto oscillators = m_synth.GetOscillators();
for (int i = 0; i < oscillators.size(); i++) {
auto osc = oscillators[i];
auto gui_osc = m_synth_gui_state.oscillators[i];
std::string type =
data["Oscillators"][i]["Wave"].template get<std::string>();
float fine = data["Oscillators"][i]["Tune"].template get<float>();
float volume =
data["Oscillators"][i]["Volume"].template get<float>();
osc->SetType(type);
osc->SetFine(fine);
osc->SetVolume(volume);
gui_osc->waveshape = osc->GetType();
gui_osc->fine = fine;
gui_osc->volume = volume;
}
auto adsr = m_synth.GetADSR();
auto gui_adsr = m_synth_gui_state.adsr;
auto adsr_params = data["ADSR"];
float attack = adsr_params["Attack"].template get<float>();
float decay = adsr_params["Decay"].template get<float>();
float sustain = adsr_params["Sustain"].template get<float>();
float release = adsr_params["Release"].template get<float>();
adsr->SetParameters(attack, decay, sustain, release);
gui_adsr.attack = attack;
gui_adsr.decay = decay;
gui_adsr.sustain = sustain;
gui_adsr.release = release;
auto lfo = m_synth.GetLFO();
lfo->SetFreq(data["LFO"]["Freq"].template get<float>());
m_synth.SetLFOLevel(data["LFO"]["Level"].template get<float>());
auto filter_type =
(FilterType)data["Filter"]["Type"].template get<int>();
m_synth.SetFilter(filter_type);
auto filter = m_synth.GetFilter();
auto gui_filter = m_synth_gui_state.filter;
float filter_freq =
std::log10f(data["Filter"]["Cutoff"].template get<float>());
float filter_res = data["Filter"]["Res"].template get<float>();
float filter_drive = data["Filter"]["Drive"].template get<float>();
filter->SetParameters(filter_freq, filter_res, filter_drive);
gui_filter.freq = filter_freq;
gui_filter.type = filter_type;
}
auto adsr = m_synth.GetADSR();
auto adsr_params = data["ADSR"];
adsr->SetParameters(adsr_params["Attack"].template get<float>(),
adsr_params["Decay"].template get<float>(),
adsr_params["Sustain"].template get<float>(),
adsr_params["Release"].template get<float>());
auto lfo = m_synth.GetLFO();
lfo->SetFreq(data["LFO"]["Freq"].template get<float>());
m_synth.SetLFOLevel(data["LFO"]["Level"].template get<float>());
m_synth.SetFilter((FilterType)data["Filter"]["Cutoff"].template get<int>());
auto filter = m_synth.GetFilter();
filter->SetParameters(std::log10f(data["Filter"]["Cutoff"].template get<float>()),
data["Filter"]["Res"].template get<float>(),
data["Filter"]["Drive"].template get<float>());
}