- 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
+1
View File
@@ -0,0 +1 @@
build
+26
View File
@@ -0,0 +1,26 @@
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
+113
View File
@@ -0,0 +1,113 @@
#include "test_common.hpp"
#include <delay/buffer.hpp>
#include <cstdio>
#include <iostream>
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;
}
+198
View File
@@ -0,0 +1,198 @@
#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, 5>buffer(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, 5>buffer(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, 5>buffer(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, 5>buffer(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);
}
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, 5>buffer(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++)
{
int qbuf[5];
buffer.quer(qbuf, i);
print(qbuf, 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, 5>buffer(10);
printf("Buffer capacity = %zu\n", buffer.capacity());
for (int i=0; i < 10; i++)
{
buffer = i+100;
buffer += 1;
}
buffer.reset();
int qbuf[5];
int delays[] = {-2, -1, -4, -5, 0};
for (int i=0; i < 10; i++)
{
buffer.quer(qbuf, delays);
print(qbuf, 5);
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;
}
+69
View File
@@ -0,0 +1,69 @@
#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)
{
for(int i=0; i < size; i++)
{
std::cout << v[i] << " ";
}
std::cout << std::endl;
}
template<typename T>
void print(T *v, size_t size)
{
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;
std::cout << "Target" << std::endl;
print(target, target.capacity());
std::cout << "Actual" << std::endl;
print(actual, actual.capacity());
}
assert(success);
}
#endif
+11
View File
@@ -0,0 +1,11 @@
extern void test_buffer();
extern void test_buffer_multi();
extern void test_mix();
int main()
{
test_buffer();
test_buffer_multi();
test_mix();
return 0;
}
+157
View File
@@ -0,0 +1,157 @@
#include "delay/buffer_multi.hpp"
#include "test_common.hpp"
#include <delay/buffer.hpp>
#include <common/mix.hpp>
#include <reverb/reverb.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;
const int numCh = 5;
{
dsp::Buffer<int> r_buf(5);
dsp::Buffer<int> t_buf{105, 106, 107, 108, 109};
t_buf = {105, 106, 107, 108, 109};
int s_spec[numCh] = {1, 3, 2, 4, 0};
int sbuf[numCh];
Shuffle<int, numCh>::outOfPlace(t_buf.raw(), sbuf, s_spec);
print(r_buf, 5);
}
{
int sbuf[numCh];
int s_spec[numCh] = {-1, 0, -3, 2, -4};
dsp::BufferMulti<int, 5> r_buf(5);
dsp::BufferMulti<int, 5> t_buf{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);
print(t_buf[1], 5);
for (int i=0; i < numCh; i++)
{
Shuffle<int, numCh>::outOfPlace(t_buf[i].raw(), sbuf, s_spec);
print(r_buf[i], 5);
}
}
return 0;
}
int test_mix_2()
{
dsp::BufferMulti<int, 5> r_buf(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);
std::cout << "----------------------------------------------" << std::endl;
}
for (int c=0; c < r_buf.numChannels(); c++)
{
dsp::Buffer<int> v(r_buf[c], c);
print(v, 20);
std::cout << "----------------------------------------------" << std::endl;
}
return 0;
}
int test_mix_3()
{
const int nCh = 5;
dsp::BufferMulti<double, nCh> r_buf(10);
for (int i=0; i < r_buf.capacity(); i++)
{
r_buf = (double)i/10 ;
++r_buf;
}
// r_buf.reset();
double sbuf[nCh];
double qbuf[nCh];
int delays[nCh] = {-2, -10, -4, 3, -3};
r_buf.quer(qbuf, delays);
int s_spec[nCh] = {-1, 0, -3, 2, -4};
Shuffle<double, nCh>::outOfPlace(qbuf, sbuf, s_spec);
for (int i=0; i < nCh; i++)
{
std::cout << sbuf[i] << " ";
}
std::cout << std::endl;
Hadamard<double, nCh>::inPlace(sbuf);
for (int i=0; i < nCh; i++)
{
std::cout << sbuf[i] << " ";
}
std::cout << std::endl;
Householder<double, nCh>::inPlace(sbuf);
for (int i=0; i < nCh; i++)
{
std::cout << sbuf[i] << " ";
}
std::cout << std::endl;
return 0;
}
void test_diffusor()
{
std::cout << "----------------------------------------------" << std::endl;
std::cout << "test_diffusor()" << std::endl;
Diffusor<double, 8> diff(5);
double data_in[10] = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7,0.8, 0.9, 1.0};
double data_out[10] = {0, 0, 0, 0, 0, 0, 0,0, 0, 0};
for (int i=0; i < 10; i++)
{
data_out[i] = diff.process(data_in[i]);
}
print(data_out, 10);
}
void test_reverb()
{
std::cout << "----------------------------------------------" << std::endl;
std::cout << "test_reverb()" << std::endl;
Reverb<double, 8> rev(32);
float data_in[10] = {1, 2, 3, 4, 5, 6, 7,8, 9, 10};
float data_out[10] = {0, 0, 0, 0, 0, 0, 0,0, 0, 0};
rev.process(data_in, data_out, 10);
print(data_out, 10);
}
int test_mix()
{
test_mix_1();
test_mix_2();
test_mix_3();
test_reverb();
test_diffusor();
return 0;
}