[refactor]: c++ implementation (#13)
implemented in c++ to improve readability and simplify maintenance Co-authored-by: HiveBeats <e1lama@protonmail.com> Reviewed-on: #13
This commit is contained in:
30
inc/Adder.h
Normal file
30
inc/Adder.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include "Oscillator.h"
|
||||
#include "Settings.h"
|
||||
#include <numeric>
|
||||
|
||||
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++)
|
||||
{
|
||||
float sample = 0.0f;
|
||||
for (Oscillator* osc : oscillators)
|
||||
{
|
||||
sample += osc->GenerateSample(duration);
|
||||
}
|
||||
|
||||
output->push_back(sample);
|
||||
}
|
||||
|
||||
return (*output);
|
||||
}
|
||||
};
|
||||
32
inc/Application.h
Normal file
32
inc/Application.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
#include "Note.h"
|
||||
#include "Synth.h"
|
||||
#include "raylib.h"
|
||||
#include "RingBuffer.h"
|
||||
#include "Renderer.h"
|
||||
#include "SynthGuiState.h"
|
||||
|
||||
class Application
|
||||
{
|
||||
private:
|
||||
Synth m_synth;
|
||||
SynthGuiState m_synth_gui_state;
|
||||
RingBuffer<float>* m_ring_buffer;
|
||||
AudioStream m_synth_stream;
|
||||
int m_sound_played_count;
|
||||
float* m_temp_buffer;
|
||||
Note* m_current_note;
|
||||
Renderer m_renderer;
|
||||
std::size_t detect_note_pressed(Note* note);
|
||||
void init_synth();
|
||||
void init_audio();
|
||||
void update_on_note_input();
|
||||
void play_buffered_audio();
|
||||
void fill_audio_buffer();
|
||||
|
||||
public:
|
||||
Application(/* args */);
|
||||
~Application();
|
||||
void Run();
|
||||
};
|
||||
|
||||
80
inc/KeyBoard.h
Normal file
80
inc/KeyBoard.h
Normal file
@@ -0,0 +1,80 @@
|
||||
#pragma once
|
||||
#include "Settings.h"
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
|
||||
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" };
|
||||
|
||||
// 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));
|
||||
strncpy(root_pitch_class_str, root_note, strlen(root_note) - 1);
|
||||
|
||||
int root_pitch_class = -1;
|
||||
|
||||
for (int i = 0; i < 12; i++) {
|
||||
if (strcmp(pitch_classes[i], root_pitch_class_str) == 0) {
|
||||
root_pitch_class = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
free(root_pitch_class_str);
|
||||
|
||||
// Extract the note number and pitch class for the target note
|
||||
int target_note_num = (int)target_note[strlen(target_note) - 1] - '0';
|
||||
|
||||
char* target_pitch_class_str =
|
||||
(char*)malloc((strlen(target_note) - 1) * sizeof(char));
|
||||
strncpy(target_pitch_class_str, target_note, strlen(target_note) - 1);
|
||||
|
||||
int target_pitch_class = -1;
|
||||
|
||||
for (int i = 0; i < 12; i++) {
|
||||
if (strcmp(pitch_classes[i], target_pitch_class_str) == 0) {
|
||||
target_pitch_class = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
free(target_pitch_class_str);
|
||||
|
||||
// Calculate the semitone shift using the formula
|
||||
return (target_note_num - root_note_num) * 12 +
|
||||
(target_pitch_class - root_pitch_class);
|
||||
}
|
||||
public:
|
||||
KeyBoard(/* args */);
|
||||
~KeyBoard();
|
||||
|
||||
static float GetHzBySemitone(int semitone) {
|
||||
return PITCH_STANDARD * powf(powf(2.f, (1.f / 12.f)), semitone);
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
}
|
||||
6
inc/Logger.h
Normal file
6
inc/Logger.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
#include "cstdio"
|
||||
|
||||
#define write_log(format,args...) do { \
|
||||
printf(format, ## args); \
|
||||
} while(0)
|
||||
8
inc/Note.h
Normal file
8
inc/Note.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
struct Note {
|
||||
std::string& name;
|
||||
int length;
|
||||
};
|
||||
40
inc/Oscillator.h
Normal file
40
inc/Oscillator.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
#include<vector>
|
||||
#include "OscillatorType.h"
|
||||
|
||||
|
||||
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);
|
||||
};
|
||||
7
inc/OscillatorType.h
Normal file
7
inc/OscillatorType.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
typedef enum {
|
||||
Sine,
|
||||
Triangle,
|
||||
Saw,
|
||||
Square
|
||||
} OscillatorType;
|
||||
27
inc/Renderer.h
Normal file
27
inc/Renderer.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
#include "Synth.h"
|
||||
#include "SynthGuiState.h"
|
||||
#include <vector>
|
||||
#include "raylib.h"
|
||||
|
||||
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,
|
||||
const std::vector<OscillatorGuiState*>& guiOscillators,
|
||||
const Rectangle& panel_bounds);
|
||||
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:
|
||||
Renderer(/* args */);
|
||||
~Renderer();
|
||||
void Draw(Synth& synth, SynthGuiState & synthGui);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
131
inc/RingBuffer.h
Normal file
131
inc/RingBuffer.h
Normal file
@@ -0,0 +1,131 @@
|
||||
#pragma once
|
||||
#include <cstddef>
|
||||
#include "Logger.h"
|
||||
template <typename T>
|
||||
class RingBuffer
|
||||
{
|
||||
private:
|
||||
T* m_items; /* data */
|
||||
std::size_t m_head;
|
||||
std::size_t m_tail;
|
||||
bool m_is_full;
|
||||
bool m_is_empty;
|
||||
std::size_t m_size;
|
||||
void advance_pointer();
|
||||
void retreat_pointer();
|
||||
public:
|
||||
RingBuffer(std::size_t size);
|
||||
~RingBuffer();
|
||||
bool IsFull() { return m_is_full; }
|
||||
bool IsEmpty() { return m_is_empty; }
|
||||
std::size_t GetSize();
|
||||
std::size_t GetCapacity() { return m_size; }
|
||||
void Reset();
|
||||
void Write(T* data, size_t count);
|
||||
bool Read(T* output, size_t count);
|
||||
void Print();
|
||||
};
|
||||
|
||||
template <typename T> RingBuffer<T>::RingBuffer(std::size_t size)
|
||||
{
|
||||
m_items = new T[size];
|
||||
m_head = 0;
|
||||
m_tail = 0;
|
||||
m_is_full = 0;
|
||||
m_is_empty = 1;
|
||||
m_size = size;
|
||||
}
|
||||
|
||||
template <typename T> RingBuffer<T>::~RingBuffer()
|
||||
{
|
||||
delete[] m_items;
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
if (m_is_full) {
|
||||
m_tail++;
|
||||
if (m_tail == m_size) {
|
||||
m_tail = 0;
|
||||
}
|
||||
}
|
||||
m_head++;
|
||||
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;
|
||||
}
|
||||
|
||||
template <typename T> void RingBuffer<T>::retreat_pointer()
|
||||
{
|
||||
m_is_full = 0;
|
||||
m_tail++;
|
||||
if (m_tail == m_size) {
|
||||
m_tail = 0;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
return;
|
||||
}
|
||||
m_is_empty = 0;
|
||||
|
||||
for (std::size_t i = 0; i < count; i++) {
|
||||
m_items[m_head] = data[i];
|
||||
advance_pointer();
|
||||
}
|
||||
//m_is_empty = m_is_full && (m_head == m_tail);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
for (std::size_t i = 0; i < count; i++) {
|
||||
output[i] = m_items[m_tail];
|
||||
retreat_pointer();
|
||||
}
|
||||
m_is_empty = !m_is_full && (m_head == m_tail);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
16
inc/Settings.h
Normal file
16
inc/Settings.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#define SAMPLE_RATE 48000.f
|
||||
#define BPM 120.f
|
||||
#define BEAT_DURATION 60.f/BPM
|
||||
#define PITCH_STANDARD 440.f
|
||||
#define VOLUME 0.5f
|
||||
#define ATTACK_MS 100.f
|
||||
#define STREAM_BUFFER_SIZE 4096
|
||||
|
||||
#define SYNTH_PI 3.1415926535f
|
||||
#define SYNTH_VOLUME 0.5f
|
||||
|
||||
#define WINDOW_WIDTH 640
|
||||
#define WINDOW_HEIGHT 480
|
||||
#define OSCILLATOR_PANEL_WIDTH 200
|
||||
26
inc/Synth.h
Normal file
26
inc/Synth.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include "Oscillator.h"
|
||||
#include "Note.h"
|
||||
#include "Adder.h"
|
||||
#include "Settings.h"
|
||||
|
||||
class Synth
|
||||
{
|
||||
private:
|
||||
std::vector<Oscillator*> m_oscillators;
|
||||
Adder m_adder;
|
||||
//OscillatorUI* ui_oscillators;
|
||||
//Note m_current_note;
|
||||
std::vector<float> m_out_signal;
|
||||
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<Oscillator*>& GetOscillators() { return m_oscillators; }
|
||||
};
|
||||
16
inc/SynthGuiState.h
Normal file
16
inc/SynthGuiState.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
#include "OscillatorType.h"
|
||||
#include "raygui.h"
|
||||
#include <vector>
|
||||
|
||||
struct OscillatorGuiState {
|
||||
float volume;
|
||||
float freq;//todo: remove or change to pitch shift
|
||||
OscillatorType waveshape;
|
||||
bool is_dropdown_open;
|
||||
Rectangle shape_dropdown_rect;
|
||||
};
|
||||
|
||||
struct SynthGuiState {
|
||||
std::vector<OscillatorGuiState*> oscillators;
|
||||
};
|
||||
4824
inc/raygui.h
Normal file
4824
inc/raygui.h
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user