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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -86,23 +86,22 @@ bool Application::detect_note_pressed(Note* note) {
} }
bool is_note_up() { bool is_note_up() {
return IsKeyReleased(KEY_A) || IsKeyReleased(KEY_B) || IsKeyReleased(KEY_C) return IsKeyReleased(KEY_A) || IsKeyReleased(KEY_B) ||
|| IsKeyReleased(KEY_D) || IsKeyReleased(KEY_E) || IsKeyReleased(KEY_F) IsKeyReleased(KEY_C) || IsKeyReleased(KEY_D) ||
|| IsKeyReleased(KEY_G); IsKeyReleased(KEY_E) || IsKeyReleased(KEY_F) || IsKeyReleased(KEY_G);
} }
// Update On Input // Update On Input
void Application::update_on_note_input() { void Application::update_on_note_input() {
if (detect_note_pressed(m_current_note)) { if (detect_note_pressed(m_current_note)) {
if (!m_synth.GetIsNoteTriggered()){ if (!m_synth.GetIsNoteTriggered()) {
m_synth.TriggerNote((*m_current_note)); 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()); 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(); m_synth.StopSound();
} }
// will produce 0 signal if ADSR is in off state // will produce 0 signal if ADSR is in off state
@@ -112,11 +111,14 @@ void Application::update_on_note_input() {
// Play ring-buffered audio // Play ring-buffered audio
void Application::play_buffered_audio() { void Application::play_buffered_audio() {
if (IsAudioStreamProcessed(m_synth_stream)) { if (IsAudioStreamProcessed(m_synth_stream)) {
//const float audio_frame_start_time = GetTime(); // const float audio_frame_start_time = GetTime();
update_on_note_input(); update_on_note_input();
UpdateAudioStream(m_synth_stream, m_synth.GetOutSignal().data(), STREAM_BUFFER_SIZE); UpdateAudioStream(m_synth_stream, m_synth.GetOutSignal().data(),
//const float audio_freme_duration = GetTime() - audio_frame_start_time; STREAM_BUFFER_SIZE);
//write_log("Frame time: %.3f%% \n", 100.0f / ((1.0f / audio_freme_duration) / ((float)SAMPLE_RATE/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; m_sample_rate = sample_rate;
} }
Ramp::~Ramp() { Ramp::~Ramp() {}
}
void Ramp::RampTo(float val, float time) { void Ramp::RampTo(float val, float time) {
m_increment = (val - m_level) / (m_sample_rate * time); m_increment = (val - m_level) / (m_sample_rate * time);
m_counter = (int)(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() { float Ramp::Process() {
@@ -24,6 +24,4 @@ float Ramp::Process() {
return m_level; return m_level;
} }
bool Ramp::IsCompleted() { bool Ramp::IsCompleted() { return m_counter == 0; }
return m_counter == 0;
}

View File

@@ -10,7 +10,6 @@
#include "raygui.h" #include "raygui.h"
#pragma clang diagnostic pop #pragma clang diagnostic pop
Renderer::Renderer(/* args */) { Renderer::Renderer(/* args */) {
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "SeeSynth - v0.2"); InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "SeeSynth - v0.2");
SetTargetFPS(FPS); SetTargetFPS(FPS);
@@ -129,17 +128,15 @@ void Renderer::draw_oscillators_panels(
delete_button_rect.y -= el_rect.height + el_spacing; delete_button_rect.y -= el_rect.height + el_spacing;
delete_button_rect.width = 30; delete_button_rect.width = 30;
bool is_delete_button_pressed = GuiButton(delete_button_rect, "X"); bool is_delete_button_pressed = GuiButton(delete_button_rect, "X");
if (is_delete_button_pressed) if (is_delete_button_pressed) {
{ // memmove(
// memmove( // synth->ui_oscillator + ui_osc_i,
// synth->ui_oscillator + ui_osc_i, // synth->ui_oscillator + ui_osc_i + 1,
// synth->ui_oscillator + ui_osc_i + 1, // (synth->ui_oscillator_count - ui_osc_i) *
// (synth->ui_oscillator_count - ui_osc_i) * // sizeof(UiOscillator)
// sizeof(UiOscillator) // );
// ); // synth->ui_oscillator_count -= 1;
// synth->ui_oscillator_count -= 1;
} }
} }
} }
@@ -149,8 +146,9 @@ void Renderer::draw_main_panel(const Rectangle& panel_bounds) {
GuiPanel(panel_bounds, ""); GuiPanel(panel_bounds, "");
} }
void Renderer::draw_add_oscillator_button(Synth& synth, SynthGuiState& synth_gui, void Renderer::draw_add_oscillator_button(Synth& synth,
Rectangle panel_bounds) { SynthGuiState& synth_gui,
Rectangle panel_bounds) {
//clang-format off //clang-format off
bool click_add_oscillator = bool click_add_oscillator =
GuiButton((Rectangle){panel_bounds.x + 10, panel_bounds.y + 10, GuiButton((Rectangle){panel_bounds.x + 10, panel_bounds.y + 10,

View File

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