Compare commits
2 Commits
56b68bc963
...
d98e311d16
| Author | SHA1 | Date | |
|---|---|---|---|
|
d98e311d16
|
|||
|
d565817d8f
|
4
build.sh
4
build.sh
@@ -1,3 +1,3 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
CC="${CXX:-cc}"
|
CC="clang++"
|
||||||
$CC -Wall -std=c11 ./main.c ./utils.c ./ring_buffer.c ./oscillator.c ./parser.c ./export.c -lm -lraylib -o ./bin/main
|
$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
|
||||||
|
|||||||
38
inc/Adder.h
38
inc/Adder.h
@@ -2,21 +2,29 @@
|
|||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "Oscillator.h"
|
#include "Oscillator.h"
|
||||||
|
#include "Settings.h"
|
||||||
|
#include <numeric>
|
||||||
|
|
||||||
class Adder
|
struct Adder
|
||||||
{
|
{
|
||||||
private:
|
static std::vector<float> & SumOscillators(const std::vector<Oscillator*> & oscillators, float duration)
|
||||||
/* data */
|
{
|
||||||
public:
|
size_t sample_count = (size_t)(duration * SAMPLE_RATE);
|
||||||
Adder(/* args */);
|
|
||||||
~Adder();
|
std::vector<float>* output = new std::vector<float>();
|
||||||
std::vector<float> & SumOscillators(const std::vector<Oscillator*> & oscillators, float duration);
|
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()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ public:
|
|||||||
|
|
||||||
int result = get_semitone_shift_internal("A4", target_note_cstr);
|
int result = get_semitone_shift_internal("A4", target_note_cstr);
|
||||||
|
|
||||||
delete target_note_cstr;
|
delete[] target_note_cstr;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,11 +2,11 @@
|
|||||||
#include<vector>
|
#include<vector>
|
||||||
#include "OscillatorType.h"
|
#include "OscillatorType.h"
|
||||||
|
|
||||||
typedef float (Oscillator::*OscFunction)(void);
|
|
||||||
typedef float (Oscillator::*DtFunction)(float);
|
|
||||||
|
|
||||||
class Oscillator
|
class Oscillator
|
||||||
{
|
{
|
||||||
|
//typedef float (Oscillator::*OscFunction)(void);
|
||||||
|
//typedef float (Oscillator::*DtFunction)(float);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
OscillatorType m_osc;
|
OscillatorType m_osc;
|
||||||
@@ -14,8 +14,9 @@ class Oscillator
|
|||||||
float m_volume;
|
float m_volume;
|
||||||
float m_phase;
|
float m_phase;
|
||||||
float m_phase_dt;
|
float m_phase_dt;
|
||||||
OscFunction m_osc_function;
|
//значение типа "float (Oscillator::*)()" нельзя присвоить сущности типа "float (*)()"
|
||||||
DtFunction m_dt_function;
|
float (Oscillator::*m_osc_function)(void);
|
||||||
|
float (Oscillator::*m_dt_function)(float freq);
|
||||||
void sine_osc_phase_incr();
|
void sine_osc_phase_incr();
|
||||||
void saw_osc_phase_incr();
|
void saw_osc_phase_incr();
|
||||||
float calc_saw_phase_delta(float freq);
|
float calc_saw_phase_delta(float freq);
|
||||||
|
|||||||
105
inc/RingBuffer.h
105
inc/RingBuffer.h
@@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include "Logger.h"
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class RingBuffer
|
class RingBuffer
|
||||||
{
|
{
|
||||||
@@ -24,3 +25,107 @@ public:
|
|||||||
bool Read(T* output, size_t count);
|
bool Read(T* output, size_t count);
|
||||||
void Print();
|
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);
|
||||||
|
}
|
||||||
@@ -22,12 +22,3 @@ public:
|
|||||||
void ProduceNoteSound(Note input);
|
void ProduceNoteSound(Note input);
|
||||||
const std::vector<float> & GetOutSignal() { return m_out_signal; }
|
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
4824
inc/raygui.h
Normal file
File diff suppressed because it is too large
Load Diff
@@ -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;
|
|
||||||
}
|
|
||||||
@@ -13,8 +13,12 @@ Application::Application(/* args */)
|
|||||||
|
|
||||||
Application::~Application()
|
Application::~Application()
|
||||||
{
|
{
|
||||||
|
StopAudioStream(m_synth_stream);
|
||||||
|
UnloadAudioStream(m_synth_stream);
|
||||||
|
CloseAudioDevice();
|
||||||
|
CloseWindow();
|
||||||
delete m_ring_buffer;
|
delete m_ring_buffer;
|
||||||
delete m_temp_buffer;
|
delete[] m_temp_buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Application::init_audio()
|
void Application::init_audio()
|
||||||
@@ -102,7 +106,7 @@ void Application::update_on_note_input()
|
|||||||
{
|
{
|
||||||
m_synth.ProduceNoteSound((*m_current_note));
|
m_synth.ProduceNoteSound((*m_current_note));
|
||||||
m_sound_played_count = 0;
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,20 +26,20 @@ void Oscillator::SetType(OscillatorType osc)
|
|||||||
m_osc = osc;
|
m_osc = osc;
|
||||||
switch (m_osc) {
|
switch (m_osc) {
|
||||||
case Sine:
|
case Sine:
|
||||||
m_osc_function = &sineosc;
|
m_osc_function = &Oscillator::sineosc;
|
||||||
m_dt_function = &calc_sine_phase_delta;
|
m_dt_function = &Oscillator::calc_sine_phase_delta;
|
||||||
break;
|
break;
|
||||||
case Triangle:
|
case Triangle:
|
||||||
m_osc_function = &triangleosc;
|
m_osc_function = &Oscillator::triangleosc;
|
||||||
m_dt_function = &calc_saw_phase_delta;
|
m_dt_function = &Oscillator::calc_saw_phase_delta;
|
||||||
break;
|
break;
|
||||||
case Square:
|
case Square:
|
||||||
m_osc_function = &squareosc;
|
m_osc_function = &Oscillator::squareosc;
|
||||||
m_dt_function = &calc_sine_phase_delta;
|
m_dt_function = &Oscillator::calc_sine_phase_delta;
|
||||||
break;
|
break;
|
||||||
case Saw:
|
case Saw:
|
||||||
m_osc_function = &sawosc;
|
m_osc_function = &Oscillator::sawosc;
|
||||||
m_dt_function = &calc_saw_phase_delta;
|
m_dt_function = &Oscillator::calc_saw_phase_delta;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -48,12 +48,12 @@ void Oscillator::SetFreq(float freq)
|
|||||||
{
|
{
|
||||||
m_freq = freq;
|
m_freq = freq;
|
||||||
m_phase = 0;
|
m_phase = 0;
|
||||||
m_phase_dt = m_dt_function(freq);
|
m_phase_dt = (this->*m_dt_function)(freq);
|
||||||
}
|
}
|
||||||
|
|
||||||
float Oscillator::GenerateSample(float duration)
|
float Oscillator::GenerateSample(float duration)
|
||||||
{
|
{
|
||||||
return m_osc_function() * m_volume;
|
return (this->*m_osc_function)() * m_volume;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Oscillator::sine_osc_phase_incr()
|
void Oscillator::sine_osc_phase_incr()
|
||||||
|
|||||||
@@ -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
8
src/SeeSynth.cpp
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#include "Application.h"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
Application* app = new Application();
|
||||||
|
app->Run();
|
||||||
|
|
||||||
|
delete app;
|
||||||
|
}
|
||||||
@@ -3,6 +3,15 @@
|
|||||||
#include "KeyBoard.h"
|
#include "KeyBoard.h"
|
||||||
#include "OscillatorType.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)
|
std::vector<float> & Synth::get_note(int semitone, float beats)
|
||||||
{
|
{
|
||||||
float hz = KeyBoard::GetHzBySemitone(semitone);
|
float hz = KeyBoard::GetHzBySemitone(semitone);
|
||||||
|
|||||||
Reference in New Issue
Block a user