diff --git a/inc/Application.h b/inc/Application.h index 512956b..475dcf0 100644 --- a/inc/Application.h +++ b/inc/Application.h @@ -18,7 +18,7 @@ class Application { void InitAudio(); void UpdateOnNoteInput(); void PlayBufferedAudio(); - + public: Application(/* args */); ~Application(); diff --git a/src/Application.cpp b/src/Application.cpp index b9ec571..d7e6ae2 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -1,10 +1,11 @@ #include "Application.h" #include "Logger.h" #include "Settings.h" -#include -#include //read file -#include // log10f +#include // log10f +#include // read file +#include // log error #include +#include 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()); - osc->SetVolume(data["Oscillators"][i]["Volume"].template get()); - osc->SetFine(data["Oscillators"][i]["Tune"].template get()); + 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(); + float fine = data["Oscillators"][i]["Tune"].template get(); + float volume = + data["Oscillators"][i]["Volume"].template get(); + + 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 decay = adsr_params["Decay"].template get(); + float sustain = adsr_params["Sustain"].template get(); + float release = adsr_params["Release"].template get(); + + 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()); + m_synth.SetLFOLevel(data["LFO"]["Level"].template get()); + + auto filter_type = + (FilterType)data["Filter"]["Type"].template get(); + 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 filter_res = data["Filter"]["Res"].template get(); + float filter_drive = data["Filter"]["Drive"].template get(); + + 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(), - adsr_params["Decay"].template get(), - adsr_params["Sustain"].template get(), - adsr_params["Release"].template get()); - - auto lfo = m_synth.GetLFO(); - lfo->SetFreq(data["LFO"]["Freq"].template get()); - m_synth.SetLFOLevel(data["LFO"]["Level"].template get()); - - m_synth.SetFilter((FilterType)data["Filter"]["Cutoff"].template get()); - auto filter = m_synth.GetFilter(); - filter->SetParameters(std::log10f(data["Filter"]["Cutoff"].template get()), - data["Filter"]["Res"].template get(), - data["Filter"]["Drive"].template get()); }