wip: project compiles

This commit is contained in:
2023-08-08 12:51:37 +04:00
parent d565817d8f
commit d98e311d16
11 changed files with 4981 additions and 162 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

@@ -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

@@ -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);