[refactor]: formatting

This commit is contained in:
2023-08-08 23:24:26 +04:00
parent a445fc44b3
commit 268103d7da
14 changed files with 495 additions and 384 deletions

View File

@@ -1,24 +1,22 @@
#pragma once
#include <vector>
#include "Oscillator.h"
#include "Settings.h"
#include <numeric>
#include <vector>
struct Adder
{
static std::vector<float> & SumOscillators(const std::vector<Oscillator*> & oscillators, float duration)
{
struct Adder {
static std::vector<float>&
SumOscillators(const std::vector<Oscillator*>& oscillators,
float duration) {
size_t sample_count = (size_t)(duration * SAMPLE_RATE);
std::vector<float>* output = new std::vector<float>();
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;
for (Oscillator* osc : oscillators)
{
for (Oscillator* osc : oscillators) {
sample += osc->GenerateSample(duration);
}

View File

@@ -1,14 +1,13 @@
#pragma once
#include "Note.h"
#include "Synth.h"
#include "raylib.h"
#include "RingBuffer.h"
#include "Renderer.h"
#include "RingBuffer.h"
#include "Synth.h"
#include "SynthGuiState.h"
#include "raylib.h"
class Application
{
private:
class Application {
private:
Synth m_synth;
SynthGuiState m_synth_gui_state;
RingBuffer<float>* m_ring_buffer;
@@ -24,9 +23,8 @@ private:
void play_buffered_audio();
void fill_audio_buffer();
public:
public:
Application(/* args */);
~Application();
void Run();
};

View File

@@ -1,22 +1,23 @@
#pragma once
#include "Settings.h"
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstring>
#include <string>
class KeyBoard
{
private:
class KeyBoard {
private:
/* data */
static int get_semitone_shift_internal(const char* root_note, char* target_note) {
const char* pitch_classes[12] =
{ "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
static int get_semitone_shift_internal(const char* root_note,
char* target_note) {
const char* pitch_classes[12] = {"C", "C#", "D", "D#", "E", "F",
"F#", "G", "G#", "A", "A#", "B"};
// Extract the note number and pitch class for the root note
int root_note_num = (int)root_note[strlen(root_note) - 1] - '0';
char* root_pitch_class_str = (char*)malloc((strlen(root_note) - 1) * sizeof(char));
char* root_pitch_class_str =
(char*)malloc((strlen(root_note) - 1) * sizeof(char));
strncpy(root_pitch_class_str, root_note, strlen(root_note) - 1);
int root_pitch_class = -1;
@@ -50,11 +51,12 @@ private:
// Calculate the semitone shift using the formula
return (target_note_num - root_note_num) * 12 +
(target_pitch_class - root_pitch_class);
(target_pitch_class - root_pitch_class);
}
public:
public:
KeyBoard(/* args */);
~KeyBoard();
~KeyBoard();
static float GetHzBySemitone(int semitone) {
return PITCH_STANDARD * powf(powf(2.f, (1.f / 12.f)), semitone);
@@ -63,18 +65,14 @@ public:
static int GetSemitoneShift(const std::string& target_note) {
char* target_note_cstr = new char[target_note.length() + 1];
strcpy(target_note_cstr, target_note.c_str());
int result = get_semitone_shift_internal("A4", target_note_cstr);
delete[] target_note_cstr;
return result;
}
};
KeyBoard::KeyBoard(/* args */)
{
}
KeyBoard::KeyBoard(/* args */) {}
KeyBoard::~KeyBoard()
{
}
KeyBoard::~KeyBoard() {}

View File

@@ -1,40 +1,35 @@
#pragma once
#include<vector>
#include "OscillatorType.h"
#include <vector>
class Oscillator {
private:
OscillatorType m_osc;
float m_freq;
float m_volume;
float m_phase;
float m_phase_dt;
float (Oscillator::*m_osc_function)(void);
float (Oscillator::*m_dt_function)(float freq);
void sine_osc_phase_incr();
void saw_osc_phase_incr();
float calc_saw_phase_delta(float freq);
float calc_sine_phase_delta(float freq);
float sawosc();
float triangleosc();
float squareosc();
float sign(float v);
float sineosc();
class Oscillator
{
//typedef float (Oscillator::*OscFunction)(void);
//typedef float (Oscillator::*DtFunction)(float);
private:
OscillatorType m_osc;
float m_freq;
float m_volume;
float m_phase;
float m_phase_dt;
//значение типа "float (Oscillator::*)()" нельзя присвоить сущности типа "float (*)()"
float (Oscillator::*m_osc_function)(void);
float (Oscillator::*m_dt_function)(float freq);
void sine_osc_phase_incr();
void saw_osc_phase_incr();
float calc_saw_phase_delta(float freq);
float calc_sine_phase_delta(float freq);
float sawosc();
float triangleosc();
float squareosc();
float sign(float v);
float sineosc();
public:
Oscillator(OscillatorType osc, float freq, float volume);
~Oscillator();
OscillatorType GetType() { return m_osc; }
void SetType(OscillatorType osc);
float GetVolume() { return m_volume; }
void SetVolume(float volume) { m_volume = volume; }
float GetFreq() { return m_freq; }
void SetFreq(float freq);
void Reset();
float GenerateSample(float duration);
public:
Oscillator(OscillatorType osc, float freq, float volume);
~Oscillator();
OscillatorType GetType() { return m_osc; }
void SetType(OscillatorType osc);
float GetVolume() { return m_volume; }
void SetVolume(float volume) { m_volume = volume; }
float GetFreq() { return m_freq; }
void SetFreq(float freq);
void Reset();
float GenerateSample(float duration);
};

View File

@@ -1,7 +1,2 @@
#pragma once
typedef enum {
Sine,
Triangle,
Saw,
Square
} OscillatorType;
typedef enum { Sine, Triangle, Saw, Square } OscillatorType;

View File

@@ -1,27 +1,26 @@
#pragma once
#include "Synth.h"
#include "SynthGuiState.h"
#include <vector>
#include "raylib.h"
#include <vector>
class Renderer
{
private:
class Renderer {
private:
void DrawMainPanel(const Rectangle& panel_bounds);
void DrawAddOscillatorButton(Synth & synth, SynthGuiState & synthGui, Rectangle panel_bounds);
void DrawOscillatorsPanels(const std::vector<Oscillator*>& oscillators,
void DrawAddOscillatorButton(Synth& synth, SynthGuiState& synthGui,
Rectangle panel_bounds);
void DrawOscillatorsPanels(
const std::vector<Oscillator*>& oscillators,
const std::vector<OscillatorGuiState*>& guiOscillators,
const Rectangle& panel_bounds);
void DrawOscillatorsShapeInputs(const std::vector<Oscillator*>& oscillators,
void DrawOscillatorsShapeInputs(
const std::vector<Oscillator*>& oscillators,
const std::vector<OscillatorGuiState*>& guiOscillators);
void DrawUi(Synth & synth, SynthGuiState & synthGui);
void DrawSignal(Synth & synth, SynthGuiState & synthGui);
public:
void DrawUi(Synth& synth, SynthGuiState& synthGui);
void DrawSignal(Synth& synth, SynthGuiState& synthGui);
public:
Renderer(/* args */);
~Renderer();
void Draw(Synth& synth, SynthGuiState & synthGui);
void Draw(Synth& synth, SynthGuiState& synthGui);
};

View File

@@ -1,10 +1,8 @@
#pragma once
#include <cstddef>
#include "Logger.h"
template <typename T>
class RingBuffer
{
private:
#include <cstddef>
template <typename T> class RingBuffer {
private:
T* m_items; /* data */
std::size_t m_head;
std::size_t m_tail;
@@ -13,7 +11,8 @@ private:
std::size_t m_size;
void advance_pointer();
void retreat_pointer();
public:
public:
RingBuffer(std::size_t size);
~RingBuffer();
bool IsFull() { return m_is_full; }
@@ -26,8 +25,7 @@ public:
void Print();
};
template <typename T> RingBuffer<T>::RingBuffer(std::size_t size)
{
template <typename T> RingBuffer<T>::RingBuffer(std::size_t size) {
m_items = new T[size];
m_head = 0;
m_tail = 0;
@@ -36,50 +34,42 @@ template <typename T> RingBuffer<T>::RingBuffer(std::size_t size)
m_size = size;
}
template <typename T> RingBuffer<T>::~RingBuffer()
{
delete[] m_items;
}
template <typename T> RingBuffer<T>::~RingBuffer() { delete[] m_items; }
template <typename T> void RingBuffer<T>::Reset()
{
template <typename T> void RingBuffer<T>::Reset() {
m_head = 0;
m_tail = 0;
m_is_full = 0;
}
template <typename T> void RingBuffer<T>::advance_pointer()
{
template <typename T> void RingBuffer<T>::advance_pointer() {
if (m_is_full) {
m_tail++;
if (m_tail == m_size) {
m_tail = 0;
}
}
if (m_tail == m_size) {
m_tail = 0;
}
}
m_head++;
if (m_head == m_size) {
m_head = 0;
}
if (m_head == m_size) {
m_head = 0;
}
std::size_t p_is_full = m_head == m_tail ? 1 : 0;
m_is_full = p_is_full;
m_is_full = p_is_full;
}
template <typename T> void RingBuffer<T>::retreat_pointer()
{
m_is_full = 0;
template <typename T> void RingBuffer<T>::retreat_pointer() {
m_is_full = 0;
m_tail++;
if (m_tail == m_size) {
m_tail = 0;
}
if (m_tail == m_size) {
m_tail = 0;
}
}
template <typename T> void RingBuffer<T>::Write(T* data, std::size_t count)
{
template <typename T> void RingBuffer<T>::Write(T* data, std::size_t count) {
if (m_is_full || m_head + count > m_size) {
write_log("[WARN] Trying to overfill the ring buffer: \n\tIsFull:%d\n\tHead:%zu\n\tCount:%zu\n\t",
m_is_full,
m_head,
count);
write_log("[WARN] Trying to overfill the ring buffer: "
"\n\tIsFull:%d\n\tHead:%zu\n\tCount:%zu\n\t",
m_is_full, m_head, count);
return;
}
m_is_empty = 0;
@@ -88,11 +78,10 @@ template <typename T> void RingBuffer<T>::Write(T* data, std::size_t count)
m_items[m_head] = data[i];
advance_pointer();
}
//m_is_empty = m_is_full && (m_head == m_tail);
// m_is_empty = m_is_full && (m_head == m_tail);
}
template <typename T> bool RingBuffer<T>::Read(T* output, std::size_t count)
{
template <typename T> bool RingBuffer<T>::Read(T* output, std::size_t count) {
if (m_is_empty) {
write_log("[WARN] Trying to read empty buffer");
return 0;
@@ -106,26 +95,21 @@ template <typename T> bool RingBuffer<T>::Read(T* output, std::size_t count)
return 1;
}
template <typename T> std::size_t RingBuffer<T>::GetSize()
{
size_t p_size = m_size;
if(!m_is_full) {
if(m_head >= m_tail) {
p_size = (m_head - m_tail);
}
else {
p_size = (m_size + m_head - m_tail);
}
}
template <typename T> std::size_t RingBuffer<T>::GetSize() {
size_t p_size = m_size;
if (!m_is_full) {
if (m_head >= m_tail) {
p_size = (m_head - m_tail);
} else {
p_size = (m_size + m_head - m_tail);
}
}
return p_size;
return p_size;
}
template <typename T> void RingBuffer<T>::Print()
{
write_log("[INFO] The ring buffer: \n\tIsFull:%d\n\tIsEmpty:%d\n\tHead:%zu\n\tTail:%zu\n\t",
m_is_full,
m_is_empty,
m_head,
m_tail);
template <typename T> void RingBuffer<T>::Print() {
write_log("[INFO] The ring buffer: "
"\n\tIsFull:%d\n\tIsEmpty:%d\n\tHead:%zu\n\tTail:%zu\n\t",
m_is_full, m_is_empty, m_head, m_tail);
}

View File

@@ -1,26 +1,25 @@
#pragma once
#include <vector>
#include "Oscillator.h"
#include "Note.h"
#include "Adder.h"
#include "Note.h"
#include "Oscillator.h"
#include "Settings.h"
class Synth
{
private:
#include <vector>
class Synth {
private:
std::vector<Oscillator*> m_oscillators;
Adder m_adder;
//OscillatorUI* ui_oscillators;
//Note m_current_note;
// OscillatorUI* ui_oscillators;
// Note m_current_note;
std::vector<float> m_out_signal;
std::vector<float> & get_note(int semitone, float beats);
public:
std::vector<float>& get_note(int semitone, float beats);
public:
Synth(/* args */);
~Synth();
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; }
};

View File

@@ -5,7 +5,7 @@
struct OscillatorGuiState {
float volume;
float freq;//todo: remove or change to pitch shift
float freq; // todo: remove or change to pitch shift
OscillatorType waveshape;
bool is_dropdown_open;
Rectangle shape_dropdown_rect;