From b02a5d2873ba73d3b7ee28a409292767b198eb5b Mon Sep 17 00:00:00 2001 From: HiveBeats Date: Mon, 7 Aug 2023 11:58:53 +0400 Subject: [PATCH] wip: templated ring buffer --- inc/RingBuffer.h | 25 +++++++++++ src/Adder.cpp | 4 +- src/RingBuffer.cpp | 107 +++++++++++++++++++++++++++++++++++++++++++++ src/Synth.cpp | 3 +- 4 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 inc/RingBuffer.h create mode 100644 src/RingBuffer.cpp diff --git a/inc/RingBuffer.h b/inc/RingBuffer.h new file mode 100644 index 0000000..2844601 --- /dev/null +++ b/inc/RingBuffer.h @@ -0,0 +1,25 @@ +#pragma once +#include +template +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(); +}; \ No newline at end of file diff --git a/src/Adder.cpp b/src/Adder.cpp index 6c5b096..5a3ea43 100644 --- a/src/Adder.cpp +++ b/src/Adder.cpp @@ -1,10 +1,12 @@ #include "Adder.h" +#include "Settings.h" +#include std::vector & Adder::SumOscillators(const std::vector & oscillators, float duration) { size_t sample_count = (size_t)(duration * SAMPLE_RATE); - std::vector output = new std::vector(); + std::vector output;// = new std::vector(); output.reserve(sample_count); for (size_t i = 0; i < sample_count; i++) diff --git a/src/RingBuffer.cpp b/src/RingBuffer.cpp new file mode 100644 index 0000000..41619fe --- /dev/null +++ b/src/RingBuffer.cpp @@ -0,0 +1,107 @@ +#include "RingBuffer.h" + +template RingBuffer::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 RingBuffer::~RingBuffer() +{ + delete m_items; +} + +template void RingBuffer::Reset() +{ + m_head = 0; + m_tail = 0; + m_is_full = 0; +} + +template void RingBuffer::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 void RingBuffer::retreat_pointer() +{ + m_is_full = 0; + m_tail++; + if (m_tail == m_size) { + m_tail = 0; + } +} + +template void RingBuffer::Write(T* data, std::size_t count) +{ + if (m_is_full || m_head + count > m_size) { + //todo: implement + // 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 bool RingBuffer::Read(T* output, std::size_t count) +{ + if (m_is_empty) { + //todo: implement + //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 std::size_t RingBuffer::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 void RingBuffer::Print() { + //todo: implement + // write_log("[INFO] The ring buffer: \n\tIsFull:%d\n\tIsEmpty:%d\n\tHead:%zu\n\tTail:%zu\n\t", + // me->m_is_full, + // me->m_is_empty, + // me->m_head, + // me->m_tail); +} \ No newline at end of file diff --git a/src/Synth.cpp b/src/Synth.cpp index a7ddb9e..65f3e4d 100644 --- a/src/Synth.cpp +++ b/src/Synth.cpp @@ -9,7 +9,8 @@ std::vector & 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); }