- refactored

- BufferMulti numChannels is template parameter
This commit is contained in:
2025-11-09 13:20:16 +01:00
parent 01d644b76e
commit 681290c9ae
13 changed files with 318 additions and 221 deletions
-2
View File
@@ -27,12 +27,10 @@
#define DSP_BUFFER_H
#include <vector>
#include <list>
#include <cstddef>
#include <math.h>
#include <stdint.h>
#include <sys/types.h>
#include <dsp_types.hpp>
namespace dsp {
+19 -51
View File
@@ -27,23 +27,22 @@
#define DSP_BUFFER_MULTI_H
#include "buffer.hpp"
#include <array>
namespace dsp {
template <typename T>
template <typename T, uint numCh>
class BufferMulti
{
Buffer<T> *m_buffer;
uint m_numChannels;
size_t m_capacity;
public:
BufferMulti(size_t numChannels, size_t capacity)
: m_buffer(new Buffer<T>[numChannels])
, m_numChannels(numChannels)
BufferMulti(size_t capacity)
: m_buffer(new Buffer<T>[numCh])
, m_capacity(capacity)
{
for (int c=0; c < numChannels; c++)
for (int c=0; c < numCh; c++)
{
m_buffer[c].resize(capacity);
}
@@ -56,7 +55,7 @@ class BufferMulti
void reset()
{
for (int c=0; c < m_numChannels; c++)
for (int c=0; c < numCh; c++)
{
m_buffer[c].reset();
}
@@ -69,7 +68,7 @@ class BufferMulti
size_t numChannels()
{
return m_numChannels;
return numCh;
}
Buffer<T> operator[](uint channel)
@@ -84,7 +83,7 @@ class BufferMulti
BufferMulti & operator ++()
{
for (int c=0; c < m_numChannels; c++)
for (int c=0; c < numCh; c++)
{
++(m_buffer[c]);
}
@@ -93,7 +92,7 @@ class BufferMulti
BufferMulti & operator +=(int i)
{
for (int c=0; c < m_numChannels; c++)
for (int c=0; c < numCh; c++)
{
m_buffer[c] += i;
}
@@ -102,7 +101,7 @@ class BufferMulti
BufferMulti & operator --()
{
for (int c=0; c < m_numChannels; c++)
for (int c=0; c < numCh; c++)
{
--(m_buffer[c]);
}
@@ -111,68 +110,37 @@ class BufferMulti
BufferMulti & operator -=(int i)
{
for (int c=0; c < m_numChannels; c++)
for (int c=0; c < numCh; c++)
{
m_buffer[c] -= i;
}
return *this;
}
BufferMulti & operator = (const T& value)
BufferMulti & operator =(const T& value)
{
for (uint c=0; c < m_numChannels; c++)
for (uint c=0; c < numCh; c++)
{
m_buffer[c][0] = value;
}
return *this;
}
dsp::Buffer<T> quer(int offset=0)
void quer(T *out, int offset=0) const
{
Buffer<T> querBuffer(numChannels());
for (int c=0; c < m_numChannels; c++)
for (int c=0; c < numCh; c++)
{
querBuffer[c] = m_buffer[c][offset];
out[c] = m_buffer[c][offset];
}
return querBuffer;
}
const dsp::Buffer<T> quer(int offset=0) const
void quer(T *out, const int *offsets) const
{
Buffer<T> querBuffer(numChannels());
for (int c=0; c < m_numChannels; c++)
for (int c=0; c < numCh; c++)
{
querBuffer[c] = m_buffer[c][offset];
out[c] = m_buffer[c][offsets[c]];
}
return querBuffer;
}
dsp::Buffer<T> quer(std::initializer_list<int> offsets)
{
Buffer<T> querBuffer(offsets.size());
uint ch = 0;
for (int offset: offsets)
{
querBuffer[ch] = m_buffer[ch][offset];
ch++;
}
return querBuffer;
}
const dsp::Buffer<T> quer(std::initializer_list<int> offsets) const
{
Buffer<T> querBuffer(offsets.size());
uint ch = 0;
for (int offset: offsets)
{
querBuffer[ch] = m_buffer[ch][offset];
ch++;
}
return querBuffer;
}
Buffer<T> at(uint channel)
{
-1
View File
@@ -1 +0,0 @@
build
-26
View File
@@ -1,26 +0,0 @@
include $(MAKE_HOME)/defaults.mk
# Environment
CC=clang
CCC=clang++
CXX=clang++
NAME := test_main
CONFIG ?= debug
TARGET ?= $(NAME)
CXXFLAGS += -std=c++20
INCLUDES += -I $(realpath .) -I $(realpath ../../)
CXX_SRCS := test_main.cpp test_buffer.cpp test_buffer_multi.cpp test_mix.cpp
LIBS := -lstdc++ -lm
run: link
@echo Running $(TARGET)
@${BUILD_DIR}/${TARGET}
include $(MAKE_HOME)/compile.mk
include $(MAKE_HOME)/link.mk
-114
View File
@@ -1,114 +0,0 @@
#include "test_common.hpp"
#include <delay/buffer.hpp>
#include <cstdio>
#include <iostream>
#include <vector>
int test_buffer_1()
{
std::cout << "----------------------------------------------" << std::endl;
std::cout << "Start test_buffer_1" << std::endl;
std::cout << "----------------------------------------------" << std::endl;
dsp::Buffer<int> buffer(10);
for (int i=0; i < buffer.capacity(); i++)
{
buffer[i] = i+100;
}
buffer += 5;
{
dsp::Buffer<int> t_buf{105, 106, 107, 108, 109};
check(buffer, t_buf, t_buf.capacity());
}
dsp::Buffer<int> res_buf(5);
for (int i=0; i < res_buf.capacity(); i++)
{
int v = buffer[i-5];
res_buf[i] = v;
}
{
dsp::Buffer<int> t_buf{100, 101, 102, 103, 104};
check(res_buf, t_buf);
}
buffer.reset();
{
dsp::Buffer<int> t_buf{100, 101, 102, 103, 104, 105, 106, 107, 108, 109};
check(dsp::Buffer<int>(buffer, 0), t_buf);
}
{
dsp::Buffer<int> t_buf{0, 0, 0, 0, 0, 100, 101, 102, 103, 104};
check(dsp::Buffer<int>(buffer, -5), t_buf);
}
return 0;
}
int test_buffer_2()
{
std::cout << "----------------------------------------------" << std::endl;
std::cout << "Start test_buffer_2" << std::endl;
std::cout << "----------------------------------------------" << std::endl;
dsp::Buffer<int> buffer(10);
for (int i=0; i < buffer.capacity(); i++)
{
buffer[i] = i+100;
}
{
dsp::Buffer<int> t_buf{0, 100, 101, 102, 103, 104, 105, 106, 107, 108};
check(dsp::Buffer<int>(buffer, -1), t_buf);
}
{
dsp::Buffer<int> t_buf{0, 0, 0, 0, 0, 100, 101, 102, 103, 104};
check(dsp::Buffer<int>(buffer, -5), t_buf);
}
return 0;
}
int test_buffer_3()
{
std::cout << "----------------------------------------------" << std::endl;
std::cout << "Start test_buffer_3" << std::endl;
std::cout << "----------------------------------------------" << std::endl;
dsp::Buffer<int> buffer(50);
for (int i=0; i < buffer.capacity(); i++)
{
buffer[i] = i+100;
}
buffer.reset();
uint incr = 5;
for (int offset=0; offset < 5; offset++)
{
std::cout << "----------------------------------------------" << std::endl;
std::cout << "offset = " << offset << std::endl;
std::cout << "----------------------------------------------" << std::endl;
auto v1 = dsp::Buffer<int>(buffer, offset, incr);
for(int i=0; i < v1.capacity(); i++)
{
std::cout << v1[i] << std::endl;
}
}
return 0;
}
int test_buffer()
{
test_buffer_1();
test_buffer_2();
test_buffer_3();
return 0;
}
-193
View File
@@ -1,193 +0,0 @@
#include "test_common.hpp"
#include <delay/buffer_multi.hpp>
#include <delay/buffer.hpp>
#include <cstdio>
#include <iostream>
#include <ostream>
int test_buffer_multi_1()
{
std::cout << "----------------------------------------------" << std::endl;
std::cout << "Start test_buffer_multi_1()" << std::endl;
std::cout << "----------------------------------------------" << std::endl;
dsp::BufferMulti<int>buffer(5, 10);
printf("Buffer capacity = %zu\n", buffer.capacity());
for (int i=0; i < 10; i++)
{
for (int c=0; c < 5; c++)
{
buffer[c][0] = i+100;
}
buffer += 1;
}
for (int c=0; c < 5; c++)
{
buffer[c].reset();
}
for (int i=0; i < 10; i++)
{
for (int c=0; c < 5; c++)
{
int v = buffer[c][i];
printf("c=%d: i=%d: v=%d\n", c, i, v);
}
}
return 0;
}
int test_buffer_multi_2()
{
std::cout << "----------------------------------------------" << std::endl;
std::cout << "Start test_buffer_multi_2()" << std::endl;
std::cout << "----------------------------------------------" << std::endl;
dsp::BufferMulti<int>buffer(5, 3);
printf("Buffer capacity = %zu\n", buffer.capacity());
for (int i=0; i < 10; i++)
{
for (int c=0; c < 5; c++)
{
buffer[c][0] = i+100;
int v = buffer[c][-2];
printf("c=%d: i=%d: v=%d\n", c, i, v);
}
buffer += 1;
}
#if 0
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);
}
}
#endif
return 0;
}
int test_buffer_multi_3()
{
std::cout << "----------------------------------------------" << std::endl;
std::cout << "Start test_buffer_multi_3()" << std::endl;
std::cout << "----------------------------------------------" << std::endl;
dsp::BufferMulti<int>buffer(5, 10);
printf("Buffer capacity = %zu\n", buffer.capacity());
for (int i=0; i < 20; i++)
{
for (int c=0; c < 5; c++)
{
buffer[c][0] = i+100;
int v = buffer[c][-c];
printf("c=%d: i=%d: v=%d\n", c, i, v);
}
}
return 0;
}
int test_buffer_multi_4()
{
std::cout << "----------------------------------------------" << std::endl;
std::cout << "Start test_buffer_multi_4()" << std::endl;
std::cout << "----------------------------------------------" << std::endl;
dsp::BufferMulti<int>buffer(5, 10);
printf("Buffer capacity = %zu\n", buffer.capacity());
dsp::Buffer<int> *mbv[6];
for (int c=0; c <= 5; c++)
{
mbv[c] = new dsp::Buffer<int>(buffer[c], -c);
}
for (int i=0; i < 10; i++)
{
for (int c=0; c < 5; c++)
{
buffer[c][0] = i+100;
}
buffer += 1;
}
buffer.reset();
for (int c=0; c < 5; c++)
{
print(*mbv[c], 10, "mbv[c]");
}
return 0;
}
int test_buffer_multi_5()
{
std::cout << "----------------------------------------------" << std::endl;
std::cout << "Start test_buffer_multi_5()" << std::endl;
std::cout << "----------------------------------------------" << std::endl;
dsp::BufferMulti<int>buffer(5, 10);
printf("Buffer capacity = %zu\n", buffer.capacity());
for (int i=0; i < 10; i++)
{
buffer = i+100;
buffer += 1;
}
buffer.reset();
for (int i=0; i < 10; i++)
{
print(buffer.quer(i), buffer.quer().capacity(), "Title here 5");
}
return 0;
}
int test_buffer_multi_6()
{
std::cout << "----------------------------------------------" << std::endl;
std::cout << "Start test_buffer_multi_6()" << std::endl;
std::cout << "----------------------------------------------" << std::endl;
dsp::BufferMulti<int>buffer(5, 10);
printf("Buffer capacity = %zu\n", buffer.capacity());
for (int i=0; i < 10; i++)
{
buffer = i+100;
buffer += 1;
}
buffer.reset();
for (int i=0; i < 10; i++)
{
print(buffer.quer({-2, -1, -4, -5, 0}), buffer.quer().capacity(), "Title here 6");
buffer += 1;
}
return 0;
}
int test_buffer_multi()
{
test_buffer_multi_1();
test_buffer_multi_2();
test_buffer_multi_3();
test_buffer_multi_4();
test_buffer_multi_5();
test_buffer_multi_6();
return 0;
}
-59
View File
@@ -1,59 +0,0 @@
#include <cmath>
#include <delay/buffer_multi.hpp>
#include <delay/buffer.hpp>
#include <cstdio>
#include <assert.h>
#include <iostream>
#include <ostream>
#ifndef TEST_COMMON_H
#define TEST_COMMON_H
template<typename T>
void print(const dsp::Buffer<T> &v, size_t size, std::string name=std::string(""))
{
std::cout << "----------------------------------------------" << std::endl;
std::cout << "- " << name << ", " << size << std::endl;
for(int i=0; i < size; i++)
{
std::cout << v[i] << " ";
}
std::cout << std::endl;
}
template<typename T>
void check(const dsp::Buffer<T> &target, const dsp::Buffer<T> &actual, size_t size=0)
{
bool success = false;
do
{
if (size == 0)
{
if (target.capacity() != actual.capacity())
{
break;
}
size = target.capacity();
}
for (int i=0; i < size; i++)
{
if (target[i] != actual[i])
{
break;
}
}
success = true;
} while(false);
if (!success)
{
std::cout << "Check failed" << std::endl;
print(target, target.capacity(), "Target");
print(actual, actual.capacity(), "Actual");
}
assert(success);
}
#endif
-11
View File
@@ -1,11 +0,0 @@
extern void test_buffer();
extern void test_buffer_multi();
extern void test_mix();
int main()
{
test_buffer();
test_buffer_multi();
test_mix();
return 0;
}
-121
View File
@@ -1,121 +0,0 @@
#include "delay/buffer_multi.hpp"
#include "test_common.hpp"
#include <delay/buffer.hpp>
#include <mix.hpp>
#include <cstdio>
#include <iostream>
#include <ostream>
int test_mix_1()
{
std::cout << "----------------------------------------------" << std::endl;
std::cout << "Start test_mix_1::shuffle" << std::endl;
std::cout << "----------------------------------------------" << std::endl;
{
dsp::Buffer<int> r_buf(5);
dsp::Buffer<int> t_buf{105, 106, 107, 108, 109};
t_buf = {105, 106, 107, 108, 109};
dsp::Buffer<int> s_spec = {1, 3, 2, 4, 0};
Shuffle<int>::outOfPlace(t_buf, r_buf, s_spec);
print(r_buf, 5, "r-buf");
}
{
dsp::BufferMulti<int> r_buf(5, 5);
dsp::BufferMulti<int> t_buf{5, 5};
t_buf[0] = {105, 106, 107, 108, 109};
t_buf[1] = {205, 206, 207, 208, 209};
t_buf[2] = {305, 306, 307, 308, 309};
t_buf[3] = {405, 406, 407, 408, 409};
t_buf[4] = {505, 506, 507, 508, 509};
print(t_buf[0], 5, "t-buf 0");
print(t_buf[1], 5, "t-buf 1");
dsp::Buffer<int> s_spec = {-1, 0, -3, 2, -4};
Shuffle<int>::outOfPlace(t_buf, r_buf, s_spec);
print(r_buf[0], 5, "r-buf 0");
print(r_buf[1], 5, "r-buf 1");
print(r_buf[2], 5, "r-buf 2");
print(r_buf[3], 5, "r-buf 3");
print(r_buf[4], 5, "r-buf 4");
}
return 0;
}
int test_mix_2()
{
dsp::BufferMulti<int> r_buf(5, 20);
for (int i=0; i < 20; i++)
{
r_buf = 100 +i ;
++r_buf;
}
r_buf.reset();
for (int c=0; c < r_buf.numChannels(); c++)
{
print(r_buf[c], 20, "r-buf");
std::cout << "----------------------------------------------" << std::endl;
}
for (int c=0; c < r_buf.numChannels(); c++)
{
dsp::Buffer<int> v(r_buf[c], c);
print(v, 20, "v-buf");
std::cout << "----------------------------------------------" << std::endl;
}
return 0;
}
int test_mix_3()
{
const int nCh = 5;
dsp::BufferMulti<double> r_buf(nCh, 10);
for (int i=0; i < r_buf.capacity(); i++)
{
r_buf = (double)i/10 ;
++r_buf;
}
// r_buf.reset();
dsp::Buffer<double> qbuf(r_buf.quer({-2, -10, -4, 3, -3}));
dsp::Buffer<double> sbuf(nCh);
dsp::Buffer<double> s_spec = {-1, 0, -3, 2, -4};
Shuffle<double>::outOfPlace(qbuf, sbuf, s_spec);
double *q = sbuf.raw();
for (int i=0; i < nCh; i++)
{
std::cout << q[i] << " ";
}
std::cout << std::endl;
Hadamard<double, nCh>::inPlace(q);
for (int i=0; i < nCh; i++)
{
std::cout << q[i] << " ";
}
std::cout << std::endl;
Householder<double, nCh>::inPlace(q);
for (int i=0; i < nCh; i++)
{
std::cout << q[i] << " ";
}
std::cout << std::endl;
return 0;
}
int test_mix()
{
test_mix_1();
test_mix_2();
test_mix_3();
return 0;
}