- added MultiBuffer
- improved tests
This commit is contained in:
+39
-2
@@ -31,6 +31,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <dsp_types.hpp>
|
#include <dsp_types.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
namespace dsp {
|
namespace dsp {
|
||||||
|
|
||||||
@@ -76,12 +77,12 @@ class Buffer
|
|||||||
|
|
||||||
T & operator[](int offset)
|
T & operator[](int offset)
|
||||||
{
|
{
|
||||||
return m_buffer[(m_index + (uint)offset) & m_mask];
|
return at(offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
const T & operator[](int offset) const
|
const T & operator[](int offset) const
|
||||||
{
|
{
|
||||||
return m_buffer[(m_index + (uint)offset) & m_mask];
|
return at(offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
Buffer & operator ++()
|
Buffer & operator ++()
|
||||||
@@ -112,7 +113,43 @@ class Buffer
|
|||||||
return *this;
|
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
|
#endif // DSP_BUFFER_H
|
||||||
|
|||||||
+15
-8
@@ -31,18 +31,17 @@
|
|||||||
namespace dsp {
|
namespace dsp {
|
||||||
|
|
||||||
template <typename T>
|
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_numChannels;
|
||||||
|
size_t m_capacity;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BufferMulti(size_t capacity, size_t numChannels)
|
BufferMulti(size_t numChannels, size_t capacity)
|
||||||
: m_capacity(capacity)
|
: Buffer<T>(capacity*numChannels)
|
||||||
, m_numChannels(numChannels)
|
, m_numChannels(numChannels)
|
||||||
|
, m_capacity(capacity)
|
||||||
{
|
{
|
||||||
m_buffer.resize(capacity*numChannels);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t capacity()
|
size_t capacity()
|
||||||
@@ -55,11 +54,19 @@ class BufferMulti
|
|||||||
return m_numChannels;
|
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:
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -59,6 +59,28 @@ class BufferView
|
|||||||
return m_buffer.size();
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ CC=clang
|
|||||||
CCC=clang++
|
CCC=clang++
|
||||||
CXX=clang++
|
CXX=clang++
|
||||||
|
|
||||||
NAME := test_dsp_buffer
|
NAME := test_main
|
||||||
CONFIG ?= debug
|
CONFIG ?= debug
|
||||||
TARGET ?= $(NAME)
|
TARGET ?= $(NAME)
|
||||||
|
|
||||||
@@ -13,7 +13,7 @@ CXXFLAGS += -std=c++20
|
|||||||
|
|
||||||
INCLUDES += -I $(realpath .) -I $(realpath ../../)
|
INCLUDES += -I $(realpath .) -I $(realpath ../../)
|
||||||
|
|
||||||
CXX_SRCS := test_buffer.cpp
|
CXX_SRCS := test_main.cpp test_buffer.cpp test_buffer_multi.cpp
|
||||||
|
|
||||||
LIBS := -lstdc++
|
LIBS := -lstdc++
|
||||||
|
|
||||||
|
|||||||
@@ -7,10 +7,10 @@
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
void print(dsp::BufferView<T> &v, size_t size)
|
void print(dsp::BufferView<T> &v, size_t size)
|
||||||
{
|
{
|
||||||
// for(const auto&i : v)
|
for(const auto&i : v)
|
||||||
// {
|
{
|
||||||
// std::cout << i << std::endl;
|
std::cout << i << std::endl;
|
||||||
// }
|
}
|
||||||
std::cout << "----------------------------------------------" << std::endl;
|
std::cout << "----------------------------------------------" << std::endl;
|
||||||
for (int i=0; i < size; i++)
|
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);
|
dsp::Buffer<int> buffer(10);
|
||||||
printf("Buffer capacity = %zu\n", buffer.capacity());
|
printf("Buffer capacity = %zu\n", buffer.capacity());
|
||||||
for (int i=0; i < 10; i++)
|
for (int i=0; i < 10; i++)
|
||||||
@@ -44,5 +53,10 @@ int main()
|
|||||||
v2[2] = 77;
|
v2[2] = 77;
|
||||||
print(v2,10);
|
print(v2,10);
|
||||||
|
|
||||||
|
for(const auto&i : buffer)
|
||||||
|
{
|
||||||
|
std::cout << i+100 << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
extern void test_buffer();
|
||||||
|
extern void test_buffer_multi();
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
test_buffer();
|
||||||
|
test_buffer_multi();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user