[feature]: ADSR (#15)

closes #14

Reviewed-on: #15
This commit is contained in:
2023-09-06 11:29:46 +03:00
parent 64fa6396bc
commit c50a8335d7
20 changed files with 444 additions and 140 deletions

View File

@@ -2,11 +2,17 @@
#define RAYGUI_IMPLEMENTATION
#include "Logger.h"
#include "Settings.h"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-but-set-variable"
#pragma clang diagnostic ignored "-Wunused-variable"
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#include "raygui.h"
#pragma clang diagnostic pop
Renderer::Renderer(/* args */) {
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "SeeSynth - v0.2");
SetTargetFPS(60);
SetTargetFPS(FPS);
}
Renderer::~Renderer() {}
@@ -70,7 +76,7 @@ void Renderer::draw_oscillators_shape_inputs(
}
}
void Renderer::draw_oscillators_panels(
float Renderer::draw_oscillators_panels(
const std::vector<Oscillator*>& oscillators,
const std::vector<OscillatorGuiState*>& gui_oscillators,
const Rectangle& panel_bounds) {
@@ -105,7 +111,7 @@ void Renderer::draw_oscillators_panels(
// Volume slider
float decibels = (20.f * log10f(osc->GetVolume()));
char amp_slider_label[32];
sprintf(amp_slider_label, "%.1f dB", decibels);
snprintf(amp_slider_label, 7, "%.1f dB", decibels);
decibels =
GuiSlider(el_rect, amp_slider_label, "", decibels, -60.0f, 0.0f);
ui_osc->volume = powf(10.f, decibels * (1.f / 20.f));
@@ -116,34 +122,33 @@ void Renderer::draw_oscillators_panels(
// Defer shape drop-down box.
ui_osc->shape_dropdown_rect = el_rect;
el_rect.y += el_rect.height + el_spacing;
/*
Rectangle delete_button_rect = el_rect;
delete_button_rect.x = osc_panel_x + 5;
delete_button_rect.y -= el_rect.height + el_spacing;
delete_button_rect.width = 30;
bool is_delete_button_pressed = GuiButton(delete_button_rect, "X");
if (is_delete_button_pressed)
{
memmove(
synth->ui_oscillator + ui_osc_i,
synth->ui_oscillator + ui_osc_i + 1,
(synth->ui_oscillator_count - ui_osc_i) *
sizeof(UiOscillator)
);
synth->ui_oscillator_count -= 1;
if (is_delete_button_pressed) {
// memmove(
// synth->ui_oscillator + ui_osc_i,
// synth->ui_oscillator + ui_osc_i + 1,
// (synth->ui_oscillator_count - ui_osc_i) *
// sizeof(UiOscillator)
// );
// synth->ui_oscillator_count -= 1;
}
*/
}
return panel_y_offset;
}
void Renderer::draw_main_panel(const Rectangle& panel_bounds) {
bool is_shape_dropdown_open = false;
int shape_index = 0;
GuiPanel(panel_bounds, "");
}
void Renderer::draw_add_oscillator_button(Synth& synth, SynthGuiState& synth_gui,
Rectangle panel_bounds) {
void Renderer::draw_add_oscillator_button(Synth& synth,
SynthGuiState& synth_gui,
Rectangle panel_bounds) {
//clang-format off
bool click_add_oscillator =
GuiButton((Rectangle){panel_bounds.x + 10, panel_bounds.y + 10,
@@ -174,6 +179,67 @@ void Renderer::draw_ui(Synth& synth, SynthGuiState& synth_gui) {
std::vector<Oscillator*> oscillators = synth.GetOscillators();
std::vector<OscillatorGuiState*> gui_oscillators = synth_gui.oscillators;
draw_oscillators_panels(oscillators, gui_oscillators, panel_bounds);
float panel_y_offset =
draw_oscillators_panels(oscillators, gui_oscillators, panel_bounds);
draw_oscillators_shape_inputs(oscillators, gui_oscillators);
}
draw_adsr_panel(synth.GetADSR(), synth_gui.adsr, panel_bounds,
panel_y_offset);
}
void Renderer::draw_adsr_panel(ADSR* adsr, ADSRGuiState& gui_adsr,
const Rectangle& panel_bounds,
float panel_y_offset) {
// Draw ADSR Panel
const int osc_panel_width = panel_bounds.width - 20;
const int osc_panel_height = 120;
const int osc_panel_x = panel_bounds.x + 10;
const int osc_panel_y = panel_bounds.y + 50 + panel_y_offset;
panel_y_offset += osc_panel_height + 5;
GuiPanel((Rectangle){(float)osc_panel_x, (float)osc_panel_y,
(float)osc_panel_width, (float)osc_panel_height},
"ADSR");
const float slider_padding = 50.f;
const float el_spacing = 5.f;
Rectangle el_rect = {.x = (float)osc_panel_x + slider_padding + 30,
.y = (float)osc_panel_y + 10,
.width = (float)osc_panel_width - (slider_padding * 2),
.height = 25.f};
// Attack slider
float attack = gui_adsr.attack;
char attack_slider_label[32];
snprintf(attack_slider_label, 7, "%.1f s", attack);
attack = GuiSlider(el_rect, attack_slider_label, "", attack, 0.0f, 2.0f);
gui_adsr.attack = attack;
el_rect.y += el_rect.height + el_spacing;
// Decay slider
float decay = gui_adsr.decay;
char decay_slider_label[32];
snprintf(decay_slider_label, 7, "%.1f s", decay);
decay = GuiSlider(el_rect, decay_slider_label, "", decay, 0.0f, 1.0f);
gui_adsr.decay = decay;
el_rect.y += el_rect.height + el_spacing;
// Sustain slider
float sustain = gui_adsr.sustain;
char sustain_slider_label[32];
snprintf(sustain_slider_label, 7, "%.1f u", sustain);
sustain = GuiSlider(el_rect, sustain_slider_label, "", sustain, 0.0f, 1.0f);
gui_adsr.sustain = sustain;
el_rect.y += el_rect.height + el_spacing;
// Release slider
float release = gui_adsr.release;
char release_slider_label[32];
snprintf(release_slider_label, 7, "%.1f s", release);
release = GuiSlider(el_rect, release_slider_label, "", release, 0.0f, 5.0f);
gui_adsr.release = release;
el_rect.y += el_rect.height + el_spacing;
// apply values to real one
adsr->SetParameters(gui_adsr.attack, gui_adsr.decay, gui_adsr.sustain,
gui_adsr.release);
}