feat: add oscillator button

This commit is contained in:
2023-08-08 23:02:25 +04:00
parent 5c485047fb
commit ec01773ab1
4 changed files with 32 additions and 19 deletions

View File

@@ -8,17 +8,18 @@ class Renderer
{ {
private: private:
void DrawMainPanel(const Rectangle& panel_bounds); void DrawMainPanel(const Rectangle& panel_bounds);
void DrawAddOscillatorButton(Synth & synth, SynthGuiState & synthGui, Rectangle panel_bounds);
void DrawOscillatorsPanels(const std::vector<Oscillator*>& oscillators, void DrawOscillatorsPanels(const std::vector<Oscillator*>& oscillators,
const std::vector<OscillatorGuiState*>& guiOscillators, const std::vector<OscillatorGuiState*>& guiOscillators,
const Rectangle& panel_bounds); const Rectangle& panel_bounds);
void DrawOscillatorsShapeInputs(const std::vector<Oscillator*>& oscillators, void DrawOscillatorsShapeInputs(const std::vector<Oscillator*>& oscillators,
const std::vector<OscillatorGuiState*>& guiOscillators); const std::vector<OscillatorGuiState*>& guiOscillators);
void DrawUi(Synth & synth, const SynthGuiState & synthGui); void DrawUi(Synth & synth, SynthGuiState & synthGui);
void DrawSignal(Synth & synth, const SynthGuiState & synthGui); void DrawSignal(Synth & synth, SynthGuiState & synthGui);
public: public:
Renderer(/* args */); Renderer(/* args */);
~Renderer(); ~Renderer();
void Draw(Synth& synth, const SynthGuiState & synthGui); void Draw(Synth& synth, SynthGuiState & synthGui);
}; };

View File

@@ -20,6 +20,7 @@ public:
Synth(/* args */); Synth(/* args */);
~Synth(); ~Synth();
void ProduceNoteSound(Note input); void ProduceNoteSound(Note input);
void AddOscillator();
const std::vector<float> & GetOutSignal() { return m_out_signal; } const std::vector<float> & GetOutSignal() { return m_out_signal; }
const std::vector<Oscillator*>& GetOscillators() { return m_oscillators; } const std::vector<Oscillator*>& GetOscillators() { return m_oscillators; }
}; };

View File

@@ -14,7 +14,7 @@ Renderer::~Renderer()
{ {
} }
void Renderer::Draw(Synth& synth, const SynthGuiState& synthGui) void Renderer::Draw(Synth& synth, SynthGuiState& synthGui)
{ {
BeginDrawing(); BeginDrawing();
@@ -28,7 +28,7 @@ void Renderer::Draw(Synth& synth, const SynthGuiState& synthGui)
EndDrawing(); EndDrawing();
} }
void Renderer::DrawSignal(Synth & synth, const SynthGuiState & synthGui) void Renderer::DrawSignal(Synth & synth, SynthGuiState & synthGui)
{ {
GuiGrid((Rectangle){0, 0, WINDOW_WIDTH, WINDOW_HEIGHT}, "", WINDOW_HEIGHT / 8, 2); GuiGrid((Rectangle){0, 0, WINDOW_WIDTH, WINDOW_HEIGHT}, "", WINDOW_HEIGHT / 8, 2);
auto signal = synth.GetOutSignal(); auto signal = synth.GetOutSignal();
@@ -158,8 +158,10 @@ void Renderer::DrawMainPanel(const Rectangle& panel_bounds)
bool is_shape_dropdown_open = false; bool is_shape_dropdown_open = false;
int shape_index = 0; int shape_index = 0;
GuiPanel(panel_bounds, ""); GuiPanel(panel_bounds, "");
}
/**/ void Renderer::DrawAddOscillatorButton(Synth & synth, SynthGuiState & synthGui, Rectangle panel_bounds)
{
bool click_add_oscillator = GuiButton((Rectangle){ bool click_add_oscillator = GuiButton((Rectangle){
panel_bounds.x + 10, panel_bounds.x + 10,
panel_bounds.y + 10, panel_bounds.y + 10,
@@ -168,24 +170,27 @@ void Renderer::DrawMainPanel(const Rectangle& panel_bounds)
}, "Add Oscillator"); }, "Add Oscillator");
if (click_add_oscillator) if (click_add_oscillator)
{ {
// synth->ui_oscillator_count += 1; synth.AddOscillator();
// // Set defaults: Oscillator* osc = synth.GetOscillators().back();
// UiOscillator *ui_osc = synth->ui_oscillator + (synth->ui_oscillator_count - 1);
// ui_osc->shape = WaveShape_SINE;
// ui_osc->freq = BASE_NOTE_FREQ;
// ui_osc->amplitude_ratio = 0.1f;
// ui_osc->shape_parameter_0 = 0.5f;
}
}
void Renderer::DrawUi(Synth & synth, const SynthGuiState & synthGui) OscillatorGuiState* ui = new OscillatorGuiState {
.freq = osc->GetFreq(),
.waveshape = osc->GetType(),
.volume = osc->GetVolume()
};
synthGui.oscillators.push_back(ui);
}
}
void Renderer::DrawUi(Synth & synth, SynthGuiState & synthGui)
{ {
Rectangle panel_bounds = {.x = 0, .y = 0, .width = OSCILLATOR_PANEL_WIDTH, .height = WINDOW_HEIGHT }; Rectangle panel_bounds = {.x = 0, .y = 0, .width = OSCILLATOR_PANEL_WIDTH, .height = WINDOW_HEIGHT };
DrawMainPanel(panel_bounds); DrawMainPanel(panel_bounds);
DrawAddOscillatorButton(synth, synthGui, panel_bounds);
// Draw Oscillators // Draw Oscillators
std::vector<Oscillator*> oscillators = synth.GetOscillators(); std::vector<Oscillator*> oscillators = synth.GetOscillators();
std::vector<OscillatorGuiState*> guiOscillators = synthGui.oscillators; std::vector<OscillatorGuiState*> guiOscillators = synthGui.oscillators;
DrawOscillatorsPanels(oscillators, guiOscillators, panel_bounds); DrawOscillatorsPanels(oscillators, guiOscillators, panel_bounds);
DrawOscillatorsShapeInputs(oscillators, guiOscillators); DrawOscillatorsShapeInputs(oscillators, guiOscillators);
} }

View File

@@ -5,7 +5,7 @@
Synth::Synth(/* args */) Synth::Synth(/* args */)
{ {
m_oscillators.push_back(new Oscillator(OscillatorType::Sine, 440.f, VOLUME)); AddOscillator();
} }
Synth::~Synth() Synth::~Synth()
@@ -26,8 +26,14 @@ std::vector<float> & Synth::get_note(int semitone, float beats)
return m_adder.SumOscillators(m_oscillators, duration); //todo: add other pipeline steps (e.g ADSR, Filters, FX); return m_adder.SumOscillators(m_oscillators, duration); //todo: add other pipeline steps (e.g ADSR, Filters, FX);
} }
void Synth::ProduceNoteSound(Note input) { void Synth::ProduceNoteSound(Note input)
{
float length = 1.f / input.length; float length = 1.f / input.length;
int semitone_shift = KeyBoard::GetSemitoneShift(input.name); int semitone_shift = KeyBoard::GetSemitoneShift(input.name);
m_out_signal = get_note(semitone_shift, length); m_out_signal = get_note(semitone_shift, length);
} }
void Synth::AddOscillator()
{
m_oscillators.push_back(new Oscillator(OscillatorType::Sine, 440.f, VOLUME));
}