fix: apply format

This commit is contained in:
2023-09-05 03:15:08 +04:00
parent 564955c911
commit de31b73673
11 changed files with 77 additions and 88 deletions

View File

@@ -1,7 +1,7 @@
#pragma once
#include "Effect.h"
#include <cstddef>
#include "Ramp.h"
#include <cstddef>
struct ADSRParameters {
float attack_time; // Attack time in seconds
@@ -16,20 +16,21 @@ class ADSR : public Effect {
private:
ADSRParameters m_parameters;
ADSRState m_state;
Ramp *m_ramp;
Ramp* m_ramp;
void process_sample(float* sample);
bool is_attack_elapsed();
bool is_decay_elapsed();
bool is_release_elapsed();
void recheck_state();
public:
ADSR(/* args */);
ADSR(ADSRParameters param);
~ADSR();
void OnSetNote() override;
void OnUnsetNote() override;
//void RetriggerState() override;
// void RetriggerState() override;
void Process(std::vector<float>& samples) override;
void Reset();
};

View File

@@ -6,13 +6,13 @@
#include <vector>
struct Adder {
static void
SumOscillators(const std::vector<Oscillator*>& oscillators,
std::vector<float>& signal) {
size_t sample_count = STREAM_BUFFER_SIZE;//(size_t)(1.f/FPS * SAMPLE_RATE);
static void SumOscillators(const std::vector<Oscillator*>& oscillators,
std::vector<float>& signal) {
size_t sample_count =
STREAM_BUFFER_SIZE; //(size_t)(1.f/FPS * SAMPLE_RATE);
//std::vector<float>* output = new std::vector<float>();
//output->reserve(sample_count);
// std::vector<float>* output = new std::vector<float>();
// output->reserve(sample_count);
for (size_t i = 0; i < sample_count; i++) {
float sample = 0.0f;

View File

@@ -8,6 +8,6 @@ class Effect {
~Effect(){};
virtual void OnSetNote(){};
virtual void OnUnsetNote(){};
//virtual void RetriggerState(){};
// virtual void RetriggerState(){};
virtual void Process(std::vector<float>& samples){};
};

View File

@@ -1,17 +1,16 @@
#pragma once
class Ramp
{
private:
class Ramp {
private:
float m_level;
float m_sample_rate;
float m_increment;
int m_counter;
public:
public:
Ramp(float starting_level, float sample_rate);
~Ramp();
void RampTo(float value, float time);
float Process();
bool IsCompleted();
};

View File

@@ -8,7 +8,7 @@ class Renderer {
private:
void draw_main_panel(const Rectangle& panel_bounds);
void draw_add_oscillator_button(Synth& synth, SynthGuiState& synth_gui,
Rectangle panel_bounds);
Rectangle panel_bounds);
void draw_oscillators_panels(
const std::vector<Oscillator*>& oscillators,
const std::vector<OscillatorGuiState*>& gui_oscillators,

View File

@@ -2,7 +2,7 @@
#define SAMPLE_RATE 44100.f
#define BPM 120.f
#define BEAT_DURATION 60.f/BPM
#define BEAT_DURATION 60.f / BPM
#define PITCH_STANDARD 440.f
#define VOLUME 0.5f
#define ATTACK_MS 100.f

View File

@@ -1,6 +1,6 @@
#include "ADSR.h"
#include "Settings.h"
#include "Logger.h"
#include "Settings.h"
ADSR::ADSR(/* args */) {
m_parameters.attack_time = 1.f;
@@ -10,13 +10,9 @@ ADSR::ADSR(/* args */) {
m_ramp = new Ramp(0, SAMPLE_RATE);
}
ADSR::ADSR(ADSRParameters param) {
m_parameters = param;
}
ADSR::ADSR(ADSRParameters param) { m_parameters = param; }
ADSR::~ADSR() {
delete m_ramp;
}
ADSR::~ADSR() { delete m_ramp; }
bool ADSR::is_attack_elapsed() {
return m_state == Attack && m_ramp->IsCompleted();
@@ -31,43 +27,39 @@ bool ADSR::is_release_elapsed() {
}
void ADSR::recheck_state() {
switch (m_state)
{
case Attack:
if (is_attack_elapsed()) {
m_state = Decay;
m_ramp->RampTo(m_parameters.sustain_level, m_parameters.decay_time);
}
break;
case Decay:
if (is_decay_elapsed()) {
m_state = Sustain;
}
break;
case Release:
if (is_release_elapsed()) {
m_state = Off;
}
break;
default:
break;
switch (m_state) {
case Attack:
if (is_attack_elapsed()) {
m_state = Decay;
m_ramp->RampTo(m_parameters.sustain_level,
m_parameters.decay_time);
}
break;
case Decay:
if (is_decay_elapsed()) {
m_state = Sustain;
}
break;
case Release:
if (is_release_elapsed()) {
m_state = Off;
}
break;
default:
break;
}
}
void ADSR::process_sample(float* sample) {
if (m_state == Off) {
(*sample) = 0;
}
else if (m_state == Attack) {
} else if (m_state == Attack) {
(*sample) = (*sample) * m_ramp->Process();
}
else if (m_state == Decay) {
} else if (m_state == Decay) {
(*sample) = (*sample) * m_ramp->Process();
}
else if (m_state == Sustain) {
} else if (m_state == Sustain) {
(*sample) = (*sample) * m_parameters.sustain_level;
}
else if (m_state == Release) {
} else if (m_state == Release) {
(*sample) = (*sample) * m_ramp->Process();
}
}
@@ -76,8 +68,7 @@ void ADSR::OnSetNote() {
write_log("Set ADSR\n");
if (m_state == Off) {
m_state = Attack;
}
else if (m_state == Release) {
} else if (m_state == Release) {
m_state = Attack;
};

View File

@@ -86,23 +86,22 @@ bool Application::detect_note_pressed(Note* note) {
}
bool is_note_up() {
return IsKeyReleased(KEY_A) || IsKeyReleased(KEY_B) || IsKeyReleased(KEY_C)
|| IsKeyReleased(KEY_D) || IsKeyReleased(KEY_E) || IsKeyReleased(KEY_F)
|| IsKeyReleased(KEY_G);
return IsKeyReleased(KEY_A) || IsKeyReleased(KEY_B) ||
IsKeyReleased(KEY_C) || IsKeyReleased(KEY_D) ||
IsKeyReleased(KEY_E) || IsKeyReleased(KEY_F) || IsKeyReleased(KEY_G);
}
// Update On Input
void Application::update_on_note_input() {
if (detect_note_pressed(m_current_note)) {
if (!m_synth.GetIsNoteTriggered()){
if (!m_synth.GetIsNoteTriggered()) {
m_synth.TriggerNote((*m_current_note));
}
//m_sound_played_count = 0;
// m_sound_played_count = 0;
write_log("Note played: %s\n", m_current_note->name.c_str());
}
else if (is_note_up() && m_synth.GetIsNoteTriggered()) {
} else if (is_note_up() && m_synth.GetIsNoteTriggered()) {
m_synth.StopSound();
}
// will produce 0 signal if ADSR is in off state
@@ -112,11 +111,14 @@ void Application::update_on_note_input() {
// Play ring-buffered audio
void Application::play_buffered_audio() {
if (IsAudioStreamProcessed(m_synth_stream)) {
//const float audio_frame_start_time = GetTime();
// const float audio_frame_start_time = GetTime();
update_on_note_input();
UpdateAudioStream(m_synth_stream, m_synth.GetOutSignal().data(), STREAM_BUFFER_SIZE);
//const float audio_freme_duration = GetTime() - audio_frame_start_time;
//write_log("Frame time: %.3f%% \n", 100.0f / ((1.0f / audio_freme_duration) / ((float)SAMPLE_RATE/STREAM_BUFFER_SIZE)));
UpdateAudioStream(m_synth_stream, m_synth.GetOutSignal().data(),
STREAM_BUFFER_SIZE);
// const float audio_freme_duration = GetTime() -
// audio_frame_start_time; write_log("Frame time: %.3f%% \n", 100.0f /
// ((1.0f / audio_freme_duration) /
// ((float)SAMPLE_RATE/STREAM_BUFFER_SIZE)));
}
}

View File

@@ -6,13 +6,13 @@ Ramp::Ramp(float starting_level, float sample_rate) {
m_sample_rate = sample_rate;
}
Ramp::~Ramp() {
}
Ramp::~Ramp() {}
void Ramp::RampTo(float val, float time) {
m_increment = (val - m_level) / (m_sample_rate * time);
m_counter = (int)(m_sample_rate * time);
write_log("Ramping from: %.1f to: %.1f by: %lf for: %d\n", m_level, val, m_increment, m_counter);
write_log("Ramping from: %.1f to: %.1f by: %lf for: %d\n", m_level, val,
m_increment, m_counter);
}
float Ramp::Process() {
@@ -24,6 +24,4 @@ float Ramp::Process() {
return m_level;
}
bool Ramp::IsCompleted() {
return m_counter == 0;
}
bool Ramp::IsCompleted() { return m_counter == 0; }

View File

@@ -10,7 +10,6 @@
#include "raygui.h"
#pragma clang diagnostic pop
Renderer::Renderer(/* args */) {
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "SeeSynth - v0.2");
SetTargetFPS(FPS);
@@ -123,23 +122,21 @@ 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;
}
}
}
@@ -149,8 +146,9 @@ void Renderer::draw_main_panel(const Rectangle& panel_bounds) {
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,

View File

@@ -1,9 +1,9 @@
#include "Synth.h"
#include "ADSR.h"
#include "KeyBoard.h"
#include "Logger.h"
#include "OscillatorType.h"
#include "Settings.h"
#include "Logger.h"
Synth::Synth(/* args */) {
AddOscillator();