- added MultiBuffer

- improved tests
This commit is contained in:
2025-10-27 21:48:51 +01:00
parent 9fedd040b3
commit 7f66f9e60d
7 changed files with 153 additions and 19 deletions
+39 -2
View File
@@ -31,6 +31,7 @@
#include <stdint.h>
#include <sys/types.h>
#include <dsp_types.hpp>
#include <iostream>
namespace dsp {
@@ -76,12 +77,12 @@ class Buffer
T & operator[](int offset)
{
return m_buffer[(m_index + (uint)offset) & m_mask];
return at(offset);
}
const T & operator[](int offset) const
{
return m_buffer[(m_index + (uint)offset) & m_mask];
return at(offset);
}
Buffer & operator ++()
@@ -112,7 +113,43 @@ class Buffer
return *this;
}
typedef std::vector<T>::iterator iterator;
typedef std::vector<T>::const_iterator const_iterator;
iterator begin()
{
return m_buffer.begin();
}
iterator end()
{
return m_buffer.end();
}
const_iterator begin() const
{
return m_buffer.begin();
}
const_iterator end() const
{
return m_buffer.end();
}
protected:
virtual T & at(int offset)
{
int i = (m_index + (uint)offset) & m_mask;
return m_buffer[i];
}
virtual const T & at(int offset) const
{
return at(offset);
}
};
}
#endif // DSP_BUFFER_H
+15 -8
View File
@@ -31,18 +31,17 @@
namespace dsp {
template <typename T>
class BufferMulti
class BufferMulti : public Buffer<T>
{
Buffer<T> m_buffer;
size_t m_capacity;
size_t m_numChannels;
size_t m_capacity;
public:
BufferMulti(size_t capacity, size_t numChannels)
: m_capacity(capacity)
BufferMulti(size_t numChannels, size_t capacity)
: Buffer<T>(capacity*numChannels)
, m_numChannels(numChannels)
, m_capacity(capacity)
{
m_buffer.resize(capacity*numChannels);
}
size_t capacity()
@@ -55,11 +54,19 @@ class BufferMulti
return m_numChannels;
}
void reset()
using Buffer<T>::at;
T & at(uint channel, int offset)
{
m_buffer.reset();
return at(int(m_capacity*channel + offset));
}
const T & at(uint channel, int offset) const
{
return at(channel, offset);
}
protected:
};
}
+22
View File
@@ -59,6 +59,28 @@ class BufferView
return m_buffer.size();
}
typedef std::vector<T>::iterator iterator;
typedef std::vector<T>::const_iterator const_iterator;
iterator begin()
{
return m_buffer.begin();
}
iterator end()
{
return m_buffer.end();
}
const_iterator begin() const
{
return m_buffer.begin();
}
const_iterator end() const
{
return m_buffer.end();
}
};
}
+2 -2
View File
@@ -5,7 +5,7 @@ CC=clang
CCC=clang++
CXX=clang++
NAME := test_dsp_buffer
NAME := test_main
CONFIG ?= debug
TARGET ?= $(NAME)
@@ -13,7 +13,7 @@ CXXFLAGS += -std=c++20
INCLUDES += -I $(realpath .) -I $(realpath ../../)
CXX_SRCS := test_buffer.cpp
CXX_SRCS := test_main.cpp test_buffer.cpp test_buffer_multi.cpp
LIBS := -lstdc++
+19 -5
View File
@@ -7,10 +7,10 @@
template<typename T>
void print(dsp::BufferView<T> &v, size_t size)
{
// for(const auto&i : v)
// {
// std::cout << i << std::endl;
// }
for(const auto&i : v)
{
std::cout << i << std::endl;
}
std::cout << "----------------------------------------------" << std::endl;
for (int i=0; i < size; i++)
{
@@ -19,8 +19,17 @@ void print(dsp::BufferView<T> &v, size_t size)
}
int main()
static void info()
{
std::cout << "----------------------------------------------" << std::endl;
std::cout << "Start test_buffer()" << std::endl;
std::cout << "----------------------------------------------" << std::endl;
}
int test_buffer()
{
info();
dsp::Buffer<int> buffer(10);
printf("Buffer capacity = %zu\n", buffer.capacity());
for (int i=0; i < 10; i++)
@@ -44,5 +53,10 @@ int main()
v2[2] = 77;
print(v2,10);
for(const auto&i : buffer)
{
std::cout << i+100 << std::endl;
}
return 0;
}
+44
View File
@@ -0,0 +1,44 @@
#include <delay/buffer_multi.hpp>
#include <cstdio>
#include <iostream>
static void info()
{
std::cout << "----------------------------------------------" << std::endl;
std::cout << "Start test_buffer_multi()" << std::endl;
std::cout << "----------------------------------------------" << std::endl;
}
int test_buffer_multi()
{
info();
dsp::BufferMulti<int>buffer(5, 20);
printf("Buffer capacity = %zu\n", buffer.capacity());
for (int i=0; i < 10; i++)
{
for (int c=0; c < 5; c++)
{
buffer.at(c, 0) = i+1;
}
buffer += 1;
}
buffer.reset();
for (int i=0; i < 10; i++)
{
for (int c=0; c < 5; c++)
{
int v = buffer.at(c, i);
printf("c=%d: i=%d: v=%d\n", c, i, v);
}
}
for(const auto&i : buffer)
{
}
return 0;
}
+10
View File
@@ -0,0 +1,10 @@
extern void test_buffer();
extern void test_buffer_multi();
int main()
{
test_buffer();
test_buffer_multi();
return 0;
}