2 Commits

Author SHA1 Message Date
d98e311d16 wip: project compiles 2023-08-08 12:51:37 +04:00
d565817d8f fix: oscillator 2023-08-08 00:06:23 +04:00
13 changed files with 4996 additions and 176 deletions

View File

@@ -1,3 +1,3 @@
#!/bin/bash
CC="${CXX:-cc}"
$CC -Wall -std=c11 ./main.c ./utils.c ./ring_buffer.c ./oscillator.c ./parser.c ./export.c -lm -lraylib -o ./bin/main
CC="clang++"
$CC -std=c++17 -I./inc/ ./src/SeeSynth.cpp ./src/Application.cpp ./src/Renderer.cpp ./src/Synth.cpp ./src/Oscillator.cpp -lm -lraylib -o ./bin/main

View File

@@ -2,21 +2,29 @@
#include <vector>
#include "Oscillator.h"
#include "Settings.h"
#include <numeric>
class Adder
struct Adder
{
private:
/* data */
public:
Adder(/* args */);
~Adder();
std::vector<float> & SumOscillators(const std::vector<Oscillator*> & oscillators, float duration);
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);
}
};
Adder::Adder(/* args */)
{
}
Adder::~Adder()
{
}

View File

@@ -66,7 +66,7 @@ public:
int result = get_semitone_shift_internal("A4", target_note_cstr);
delete target_note_cstr;
delete[] target_note_cstr;
return result;
}
};

View File

@@ -2,11 +2,11 @@
#include<vector>
#include "OscillatorType.h"
typedef float (Oscillator::*OscFunction)(void);
typedef float (Oscillator::*DtFunction)(float);
class Oscillator
{
//typedef float (Oscillator::*OscFunction)(void);
//typedef float (Oscillator::*DtFunction)(float);
private:
OscillatorType m_osc;
@@ -14,8 +14,9 @@ class Oscillator
float m_volume;
float m_phase;
float m_phase_dt;
OscFunction m_osc_function;
DtFunction m_dt_function;
//значение типа "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);

View File

@@ -1,5 +1,6 @@
#pragma once
#include <cstddef>
#include "Logger.h"
template <typename T>
class RingBuffer
{
@@ -23,4 +24,108 @@ public:
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);
}

View File

@@ -21,13 +21,4 @@ public:
~Synth();
void ProduceNoteSound(Note input);
const std::vector<float> & GetOutSignal() { return m_out_signal; }
};
Synth::Synth(/* args */)
{
m_oscillators.push_back(new Oscillator(OscillatorType::Sine, 440.f, VOLUME));
}
Synth::~Synth()
{
}
};

4824
inc/raygui.h Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,24 +0,0 @@
#include "Adder.h"
#include "Settings.h"
#include <numeric>
std::vector<float> & Adder::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;
}

View File

@@ -13,8 +13,12 @@ Application::Application(/* args */)
Application::~Application()
{
StopAudioStream(m_synth_stream);
UnloadAudioStream(m_synth_stream);
CloseAudioDevice();
CloseWindow();
delete m_ring_buffer;
delete m_temp_buffer;
delete[] m_temp_buffer;
}
void Application::init_audio()
@@ -102,7 +106,7 @@ void Application::update_on_note_input()
{
m_synth.ProduceNoteSound((*m_current_note));
m_sound_played_count = 0;
write_log("Note played: %s\n", m_current_note->name);
write_log("Note played: %s\n", m_current_note->name.c_str());
}
}
@@ -166,7 +170,7 @@ void Application::Run()
fill_audio_buffer();
play_buffered_audio();
update_on_note_input();
m_renderer.Draw();
}
}

View File

@@ -26,20 +26,20 @@ void Oscillator::SetType(OscillatorType osc)
m_osc = osc;
switch (m_osc) {
case Sine:
m_osc_function = &sineosc;
m_dt_function = &calc_sine_phase_delta;
m_osc_function = &Oscillator::sineosc;
m_dt_function = &Oscillator::calc_sine_phase_delta;
break;
case Triangle:
m_osc_function = &triangleosc;
m_dt_function = &calc_saw_phase_delta;
m_osc_function = &Oscillator::triangleosc;
m_dt_function = &Oscillator::calc_saw_phase_delta;
break;
case Square:
m_osc_function = &squareosc;
m_dt_function = &calc_sine_phase_delta;
m_osc_function = &Oscillator::squareosc;
m_dt_function = &Oscillator::calc_sine_phase_delta;
break;
case Saw:
m_osc_function = &sawosc;
m_dt_function = &calc_saw_phase_delta;
m_osc_function = &Oscillator::sawosc;
m_dt_function = &Oscillator::calc_saw_phase_delta;
break;
}
}
@@ -48,12 +48,12 @@ void Oscillator::SetFreq(float freq)
{
m_freq = freq;
m_phase = 0;
m_phase_dt = m_dt_function(freq);
m_phase_dt = (this->*m_dt_function)(freq);
}
float Oscillator::GenerateSample(float duration)
{
return m_osc_function() * m_volume;
return (this->*m_osc_function)() * m_volume;
}
void Oscillator::sine_osc_phase_incr()

View File

@@ -1,106 +0,0 @@
#include "RingBuffer.h"
#include "Logger.h"
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(buffer);
}
//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(buffer);
}
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);
}

8
src/SeeSynth.cpp Normal file
View File

@@ -0,0 +1,8 @@
#include "Application.h"
int main() {
Application* app = new Application();
app->Run();
delete app;
}

View File

@@ -3,6 +3,15 @@
#include "KeyBoard.h"
#include "OscillatorType.h"
Synth::Synth(/* args */)
{
m_oscillators.push_back(new Oscillator(OscillatorType::Sine, 440.f, VOLUME));
}
Synth::~Synth()
{
}
std::vector<float> & Synth::get_note(int semitone, float beats)
{
float hz = KeyBoard::GetHzBySemitone(semitone);