4 Commits

Author SHA1 Message Date
78c202a9d6 wip: logging 2023-08-07 12:05:06 +04:00
b02a5d2873 wip: templated ring buffer 2023-08-07 11:58:53 +04:00
64fa5c9271 wip: adder 2023-08-07 10:31:12 +04:00
6561666f7a fix: unitialized note struct 2023-08-07 02:32:34 +04:00
7 changed files with 143 additions and 2 deletions

View File

@@ -1,4 +1,5 @@
#pragma once
#include <vector>
#include "Oscillator.h"

6
inc/Logger.h Normal file
View File

@@ -0,0 +1,6 @@
#pragma once
#include "cstdio"
#define write_log(format,args...) do { \
printf(format, ## args); \
} while(0)

25
inc/RingBuffer.h Normal file
View File

@@ -0,0 +1,25 @@
#pragma once
#include <cstddef>
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();
void Reset();
void Write(T* data, size_t count);
bool Read(T* output, size_t count);
void Print();
};

View File

@@ -12,7 +12,7 @@ private:
std::vector<Oscillator*> m_oscillators;
Adder m_adder;
//OscillatorUI* ui_oscillators;
Note m_current_note;
//Note m_current_note;
std::vector<float> m_out_signal;
std::vector<float> & get_note(int semitone, float beats);

View File

@@ -1,4 +1,6 @@
#include "Adder.h"
#include "Settings.h"
#include <numeric>
std::vector<float> & Adder::SumOscillators(const std::vector<Oscillator*> & oscillators, float duration)
{

106
src/RingBuffer.cpp Normal file
View File

@@ -0,0 +1,106 @@
#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);
}

View File

@@ -9,7 +9,8 @@ std::vector<float> & Synth::get_note(int semitone, float beats)
float duration = beats * BEAT_DURATION;
// will change after oscillator starts to be more autonomous
for (auto osc : m_oscillators) {
for (Oscillator* osc : m_oscillators)
{
osc->SetFreq(hz);
}