This commit is contained in:
Steven Dan
2025-12-11 09:43:42 +08:00
commit d8b2974133
1822 changed files with 280037 additions and 0 deletions

View File

@@ -0,0 +1,161 @@
cmake_minimum_required(VERSION 3.21)
include($ENV{XMOS_CMAKE_PATH}/xcommon.cmake)
project(fixed_gain_test)
set(APP_HW_TARGET XCORE-AI-EXPLORER)
set(APP_COMPILER_FLAGS
-O3
-g
-report
-Wall
-Werror
-fxscope)
set(APP_DEPENDENT_MODULES lib_audio_dsp)
set(APP_C_SRCS src/fixed_gain.c)
set(XMOS_SANDBOX_DIR ${CMAKE_SOURCE_DIR}/../../..)
XMOS_REGISTER_APP()
project(subtractor_test)
set(APP_HW_TARGET XCORE-AI-EXPLORER)
set(APP_COMPILER_FLAGS
-O3
-g
-report
-Wall
-Werror
-fxscope)
set(APP_DEPENDENT_MODULES lib_audio_dsp)
set(APP_C_SRCS src/subtractor.c)
set(XMOS_SANDBOX_DIR ${CMAKE_SOURCE_DIR}/../../..)
XMOS_REGISTER_APP()
project(adder_test)
set(APP_HW_TARGET XCORE-AI-EXPLORER)
set(APP_COMPILER_FLAGS
-O3
-g
-report
-Wall
-Werror
-fxscope)
set(APP_DEPENDENT_MODULES lib_audio_dsp)
set(APP_C_SRCS src/adder.c)
set(XMOS_SANDBOX_DIR ${CMAKE_SOURCE_DIR}/../../..)
XMOS_REGISTER_APP()
project(mixer_test)
set(APP_HW_TARGET XCORE-AI-EXPLORER)
set(APP_COMPILER_FLAGS
-O3
-g
-report
-Wall
-Werror
-fxscope)
set(APP_DEPENDENT_MODULES lib_audio_dsp)
set(APP_C_SRCS src/mixer.c)
set(XMOS_SANDBOX_DIR ${CMAKE_SOURCE_DIR}/../../..)
XMOS_REGISTER_APP()
project(volume_control_test)
set(APP_HW_TARGET XCORE-AI-EXPLORER)
set(APP_COMPILER_FLAGS
-O3
-g
-report
-Wall
-Werror
-fxscope)
set(APP_DEPENDENT_MODULES lib_audio_dsp)
set(APP_C_SRCS src/volume_control.c)
set(XMOS_SANDBOX_DIR ${CMAKE_SOURCE_DIR}/../../..)
XMOS_REGISTER_APP()
project(delay_test)
set(APP_HW_TARGET XCORE-AI-EXPLORER)
set(APP_COMPILER_FLAGS
-O3
-g
-report
-Wall
-Werror
-fxscope)
set(APP_DEPENDENT_MODULES lib_audio_dsp)
set(APP_C_SRCS src/delay.c)
set(XMOS_SANDBOX_DIR ${CMAKE_SOURCE_DIR}/../../..)
XMOS_REGISTER_APP()
project(switch_slew_test)
set(APP_HW_TARGET XCORE-AI-EXPLORER)
set(APP_COMPILER_FLAGS
-O3
-g
-report
-Wall
-Werror
-fxscope)
set(APP_DEPENDENT_MODULES lib_audio_dsp)
set(APP_C_SRCS src/switch_slew.c)
set(XMOS_SANDBOX_DIR ${CMAKE_SOURCE_DIR}/../../..)
XMOS_REGISTER_APP()
project(crossfader_test)
set(APP_HW_TARGET XCORE-AI-EXPLORER)
set(APP_COMPILER_FLAGS
-O3
-g
-report
-Wall
-Werror
-fxscope)
set(APP_DEPENDENT_MODULES lib_audio_dsp)
set(APP_C_SRCS src/crossfader.c)
set(XMOS_SANDBOX_DIR ${CMAKE_SOURCE_DIR}/../../..)
XMOS_REGISTER_APP()

View File

@@ -0,0 +1,49 @@
// Copyright 2024-2025 XMOS LIMITED.
// This Software is subject to the terms of the XMOS Public Licence: Version 1.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "dsp/adsp.h"
FILE * _fopen(char * fname, char* mode) {
FILE * fp = fopen(fname, mode);
if (fp == NULL)
{
printf("Error opening a file\n");
exit(1);
}
return fp;
}
int main()
{
FILE * in = _fopen("../sig_48k.bin", "rb");
FILE * in1 = _fopen("../sig1_48k.bin", "rb");
FILE * out = _fopen("sig_out.bin", "wb");
fseek(in, 0, SEEK_END);
int in_len = ftell(in) / sizeof(int32_t);
fseek(in, 0, SEEK_SET);
for (unsigned i = 0; i < in_len; i++)
{
int32_t samp = 0, samp1 = 0, samp_out = 0;
int64_t acc = 0;
fread(&samp, sizeof(int32_t), 1, in);
fread(&samp1, sizeof(int32_t), 1, in1);
//printf("%ld ", samp);
acc += samp;
acc += samp1;
samp_out = adsp_saturate_32b(acc);
//printf("%ld ", samp_out);
fwrite(&samp_out, sizeof(int32_t), 1, out);
}
fclose(in);
fclose(in1);
fclose(out);
return 0;
}

View File

@@ -0,0 +1,61 @@
// Copyright 2024-2025 XMOS LIMITED.
// This Software is subject to the terms of the XMOS Public Licence: Version 1.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "dsp/adsp.h"
FILE * _fopen(char * fname, char* mode) {
FILE * fp = fopen(fname, mode);
if (fp == NULL)
{
printf("Error opening a file\n");
exit(1);
}
return fp;
}
int main()
{
int32_t gains[4] = {0};
int32_t slew = 0;
FILE * in = _fopen("../sig_48k.bin", "rb");
FILE * in1 = _fopen("../sig1_48k.bin", "rb");
FILE * out = _fopen("sig_out.bin", "wb");
FILE * gain = _fopen("gain.bin", "rb");
fseek(in, 0, SEEK_END);
int in_len = ftell(in) / sizeof(int32_t);
fseek(in, 0, SEEK_SET);
fread(&slew, sizeof(int32_t), 1, gain);
fread(gains, sizeof(int32_t), 4, gain);
fclose(gain);
printf("gains: %ld %ld\n", gains[0], gains[1]);
crossfader_slew_t cfs = {.gain_1.gain=gains[0], .gain_1.target_gain=gains[2], .gain_1.slew_shift=slew,
.gain_2.gain=gains[1], .gain_2.target_gain=gains[3], .gain_2.slew_shift=slew,
.mix=0};
for (unsigned i = 0; i < in_len; i++)
{
int32_t samp = 0, samp1 = 0, samp_out = 0;
fread(&samp, sizeof(int32_t), 1, in);
fread(&samp1, sizeof(int32_t), 1, in1);
//printf("%ld ", samp);
samp_out = adsp_crossfader_slew(&cfs, samp, samp1);
//printf("%ld ", samp_out);
fwrite(&samp_out, sizeof(int32_t), 1, out);
}
fclose(in);
fclose(in1);
fclose(out);
return 0;
}

View File

@@ -0,0 +1,58 @@
// Copyright 2024-2025 XMOS LIMITED.
// This Software is subject to the terms of the XMOS Public Licence: Version 1.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "dsp/adsp.h"
FILE * _fopen(char * fname, char* mode) {
FILE * fp = fopen(fname, mode);
if (fp == NULL)
{
printf("Error opening a file\n");
exit(1);
}
return fp;
}
int main()
{
FILE * in = _fopen("../sig_48k.bin", "rb");
FILE * out = _fopen("sig_out.bin", "wb");
FILE * info = _fopen("delay.bin", "rb");
fseek(in, 0, SEEK_END);
int in_len = ftell(in) / sizeof(int32_t);
fseek(in, 0, SEEK_SET);
uint32_t start_delay, max_delay;
fread(&max_delay, sizeof(uint32_t), 1, info);
fread(&start_delay, sizeof(uint32_t), 1, info);
fclose(info);
int32_t * buffer = (int32_t *) malloc(DELAY_DSP_REQUIRED_MEMORY_SAMPLES(max_delay));
if (buffer == NULL)
{
printf("Error allocating memory\n");
exit(2);
}
memset(buffer, 0, DELAY_DSP_REQUIRED_MEMORY_SAMPLES(max_delay));
delay_t delay = (delay_t) {0, start_delay, max_delay, 0, buffer};
for (unsigned i = 0; i < in_len; i++)
{
int32_t samp = 0, samp_out = 0;
fread(&samp, sizeof(int32_t), 1, in);
//printf("%ld ", samp);
samp_out = adsp_delay(&delay, samp);
//printf("%ld ", samp_out);
fwrite(&samp_out, sizeof(int32_t), 1, out);
}
fclose(in);
fclose(out);
return 0;
}

View File

@@ -0,0 +1,48 @@
// Copyright 2024-2025 XMOS LIMITED.
// This Software is subject to the terms of the XMOS Public Licence: Version 1.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "dsp/adsp.h"
FILE * _fopen(char * fname, char* mode) {
FILE * fp = fopen(fname, mode);
if (fp == NULL)
{
printf("Error opening a file\n");
exit(1);
}
return fp;
}
int main()
{
int32_t fixed_gain = 0;
FILE * in = _fopen("../sig_48k.bin", "rb");
FILE * out = _fopen("sig_out.bin", "wb");
FILE * gain = _fopen("gain.bin", "rb");
fseek(in, 0, SEEK_END);
int in_len = ftell(in) / sizeof(int32_t);
fseek(in, 0, SEEK_SET);
fread(&fixed_gain, sizeof(int32_t), 1, gain);
fclose(gain);
for (unsigned i = 0; i < in_len; i++)
{
int32_t samp = 0, samp_out = 0;
fread(&samp, sizeof(int32_t), 1, in);
//printf("%ld ", samp);
samp_out = adsp_fixed_gain(samp, fixed_gain);
//printf("%ld ", samp_out);
fwrite(&samp_out, sizeof(int32_t), 1, out);
}
fclose(in);
fclose(out);
return 0;
}

View File

@@ -0,0 +1,54 @@
// Copyright 2024-2025 XMOS LIMITED.
// This Software is subject to the terms of the XMOS Public Licence: Version 1.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "dsp/adsp.h"
FILE * _fopen(char * fname, char* mode) {
FILE * fp = fopen(fname, mode);
if (fp == NULL)
{
printf("Error opening a file\n");
exit(1);
}
return fp;
}
int main()
{
int32_t fixed_gain = 0;
FILE * in = _fopen("../sig_48k.bin", "rb");
FILE * in1 = _fopen("../sig1_48k.bin", "rb");
FILE * out = _fopen("sig_out.bin", "wb");
FILE * gain = _fopen("gain.bin", "rb");
fseek(in, 0, SEEK_END);
int in_len = ftell(in) / sizeof(int32_t);
fseek(in, 0, SEEK_SET);
fread(&fixed_gain, sizeof(int32_t), 1, gain);
fclose(gain);
for (unsigned i = 0; i < in_len; i++)
{
int32_t samp = 0, samp1 = 0, samp_out = 0;
int64_t acc = 0;
fread(&samp, sizeof(int32_t), 1, in);
fread(&samp1, sizeof(int32_t), 1, in1);
//printf("%ld ", samp);
acc += adsp_fixed_gain(samp, fixed_gain);
acc += adsp_fixed_gain(samp1, fixed_gain);
samp_out = adsp_saturate_32b(acc);
//printf("%ld ", samp_out);
fwrite(&samp_out, sizeof(int32_t), 1, out);
}
fclose(in);
fclose(in1);
fclose(out);
return 0;
}

View File

@@ -0,0 +1,46 @@
// Copyright 2024-2025 XMOS LIMITED.
// This Software is subject to the terms of the XMOS Public Licence: Version 1.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "dsp/adsp.h"
FILE * _fopen(char * fname, char* mode) {
FILE * fp = fopen(fname, mode);
if (fp == NULL)
{
printf("Error opening a file\n");
exit(1);
}
return fp;
}
int main()
{
FILE * in = _fopen("../sig_48k.bin", "rb");
FILE * in1 = _fopen("../sig1_48k.bin", "rb");
FILE * out = _fopen("sig_out.bin", "wb");
fseek(in, 0, SEEK_END);
int in_len = ftell(in) / sizeof(int32_t);
fseek(in, 0, SEEK_SET);
for (unsigned i = 0; i < in_len; i++)
{
int32_t samp = 0, samp1 = 0, samp_out = 0;
fread(&samp, sizeof(int32_t), 1, in);
fread(&samp1, sizeof(int32_t), 1, in1);
//printf("%ld ", samp);
samp_out = adsp_subtractor(samp, samp1);
//printf("%ld ", samp_out);
fwrite(&samp_out, sizeof(int32_t), 1, out);
}
fclose(in);
fclose(in1);
fclose(out);
return 0;
}

View File

@@ -0,0 +1,65 @@
// Copyright 2024-2025 XMOS LIMITED.
// This Software is subject to the terms of the XMOS Public Licence: Version 1.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "dsp/adsp.h"
#include "control/adsp_control.h"
FILE * _fopen(char * fname, char* mode) {
FILE * fp = fopen(fname, mode);
if (fp == NULL)
{
printf("Error opening a file\n");
exit(1);
}
return fp;
}
int main()
{
FILE * in = _fopen("../sig_48k.bin", "rb");
FILE * in1 = _fopen("../sig1_48k.bin", "rb");
FILE * out = _fopen("sig_out.bin", "wb");
fseek(in, 0, SEEK_END);
int in_len = ftell(in) / sizeof(int32_t);
fseek(in, 0, SEEK_SET);
switch_slew_t switch_state = adsp_switch_slew_init(48000, 0);
for (unsigned i = 0; i < in_len/2; i++)
{
int32_t samples[2];
int32_t samp_out = 0;
fread(&samples[0], sizeof(int32_t), 1, in);
fread(&samples[1], sizeof(int32_t), 1, in1);
//printf("%ld ", samp);
samp_out = adsp_switch_slew(&switch_state,
samples);
//printf("%ld ", samp_out);
fwrite(&samp_out, sizeof(int32_t), 1, out);
}
adsp_switch_slew_move(&switch_state, 1);
for (unsigned i = in_len/2; i < in_len; i++)
{
int32_t samples[2];
int32_t samp_out = 0;
fread(&samples[0], sizeof(int32_t), 1, in);
fread(&samples[1], sizeof(int32_t), 1, in1);
//printf("%ld ", samp);
samp_out = adsp_switch_slew(&switch_state,
samples);
//printf("%ld ", samp_out);
fwrite(&samp_out, sizeof(int32_t), 1, out);
}
fclose(in);
fclose(in1);
fclose(out);
return 0;
}

View File

@@ -0,0 +1,74 @@
// Copyright 2024-2025 XMOS LIMITED.
// This Software is subject to the terms of the XMOS Public Licence: Version 1.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "dsp/adsp.h"
FILE * _fopen(char * fname, char* mode) {
FILE * fp = fopen(fname, mode);
if (fp == NULL)
{
printf("Error opening a file\n");
exit(1);
}
return fp;
}
static inline void run_interval(FILE * in, FILE * out, unsigned bgn, unsigned end, volume_control_t * vol_ctl) {
for (unsigned i = bgn; i < end; i++)
{
int32_t samp = 0, samp_out = 0;
fread(&samp, sizeof(int32_t), 1, in);
//printf("%ld ", samp);
samp_out = adsp_volume_control(vol_ctl, samp);
//printf("%ld ", samp_out);
fwrite(&samp_out, sizeof(int32_t), 1, out);
}
}
int main()
{
int32_t mute_test = 0;
int32_t slew_shift = 0;
int32_t gains[3] = {0};
unsigned intervals[4] = {0};
FILE * in = _fopen("../sig_48k.bin", "rb");
FILE * out = _fopen("sig_out.bin", "wb");
FILE * conf = _fopen("gain.bin", "rb");
fseek(in, 0, SEEK_END);
int in_len = ftell(in) / sizeof(int32_t);
fseek(in, 0, SEEK_SET);
intervals[1] = in_len / 3;
intervals[2] = intervals[1] * 2;
intervals[3] = in_len;
fread(&mute_test, sizeof(int32_t), 1, conf);
fread(&slew_shift, sizeof(int32_t), 1, conf);
fread(gains, sizeof(int32_t), 3, conf);
fclose(conf);
//printf("ls %ld %ld %ld %ld\n", slew_shift, gains[0], gains[1], gains[2]);
volume_control_t vol_ctl = (volume_control_t){gains[0], gains[0], slew_shift};
run_interval(in, out, intervals[0], intervals[1], &vol_ctl);
adsp_volume_control_set_gain(&vol_ctl, gains[1]);
if (mute_test) {adsp_volume_control_mute(&vol_ctl);}
run_interval(in, out, intervals[1], intervals[2], &vol_ctl);
adsp_volume_control_set_gain(&vol_ctl, gains[2]);
if (mute_test) {adsp_volume_control_unmute(&vol_ctl);}
run_interval(in, out, intervals[2], intervals[3], &vol_ctl);
fclose(in);
fclose(out);
return 0;
}

View File

@@ -0,0 +1,271 @@
# Copyright 2024-2025 XMOS LIMITED.
# This Software is subject to the terms of the XMOS Public Licence: Version 1.
import numpy as np
import soundfile as sf
from pathlib import Path
import shutil
import subprocess
import audio_dsp.dsp.signal_chain as sc
from audio_dsp.dsp.generic import Q_SIG
import audio_dsp.dsp.signal_gen as gen
import pytest
from test.test_utils import xdist_safe_bin_write, float_to_qxx, qxx_to_float, q_convert_flt
bin_dir = Path(__file__).parent / "bin"
gen_dir = Path(__file__).parent / "autogen"
fs = 48000
def get_sig(len=0.05):
sig_fl = []
sig_fl.append(gen.sin(fs, len, 997, 0.7))
sig_fl.append(gen.sin(fs, len, 100, 0.7))
sig_fl = np.stack(sig_fl, axis=0)
sig_fl = q_convert_flt(sig_fl, 23, Q_SIG)
sig_int = float_to_qxx(sig_fl)
name = "sig_48k"
sig_path = bin_dir / str(name + ".bin")
xdist_safe_bin_write(sig_int[0], sig_path)
# wav file does not need to be locked as it is only used for debugging outside pytest
wav_path = gen_dir / str(name + ".wav")
sf.write(wav_path, sig_fl[0], int(fs), "PCM_24")
name = "sig1_48k"
sig_path = bin_dir / str(name + ".bin")
xdist_safe_bin_write(sig_int[1], sig_path)
# wav file does not need to be locked as it is only used for debugging outside pytest
wav_path = gen_dir / str(name + ".wav")
sf.write(wav_path, sig_fl[1], int(fs), "PCM_24")
return sig_fl
def get_c_wav(dir_name, comp_name, verbose=False, sim = True):
app = "xsim" if sim else "xrun --io"
run_cmd = app + " " + str(bin_dir / f"{comp_name}_test.xe")
stdout = subprocess.check_output(run_cmd, cwd = dir_name, shell = True)
if verbose: print("run msg:\n", stdout.decode())
sig_bin = dir_name / "sig_out.bin"
assert sig_bin.is_file(), f"Could not find output bin {sig_bin}"
sig_int = np.fromfile(sig_bin, dtype=np.int32)
sig_fl = qxx_to_float(sig_int)
sf.write(gen_dir / "sig_c.wav", sig_fl, fs, "PCM_24")
return sig_fl
def write_gain(test_dir, gain):
all_filt_info = np.empty(0, dtype=np.int32)
all_filt_info = np.append(all_filt_info, np.array(gain, dtype=np.int32))
all_filt_info.tofile(test_dir / "gain.bin")
def single_channels_test(filt, test_dir, fname, sig_fl):
out_py = np.zeros(sig_fl.shape[1])
for n in range(sig_fl.shape[1]):
out_py[n] = filt.process_channels_xcore(sig_fl[:, n])[0]
sf.write(gen_dir / "sig_py_int.wav", out_py, fs, "PCM_24")
out_c = get_c_wav(test_dir, fname)
shutil.rmtree(test_dir)
np.testing.assert_allclose(out_c, out_py, rtol=0, atol=0)
@pytest.fixture(scope="module")
def in_signal():
bin_dir.mkdir(exist_ok=True, parents=True)
gen_dir.mkdir(exist_ok=True, parents=True)
return get_sig()
@pytest.mark.parametrize("gain_dB", [-10, 0, 24])
def test_gains_c(in_signal, gain_dB):
filt = sc.fixed_gain(fs, 1, gain_dB)
test_dir = bin_dir / f"fixed_gain_{gain_dB}"
test_dir.mkdir(exist_ok = True, parents = True)
write_gain(test_dir, filt.gain_int)
out_py = np.zeros(in_signal.shape[1])
for n in range(in_signal.shape[1]):
out_py[n] = filt.process_xcore(in_signal[0][n])
sf.write(gen_dir / "sig_py_int.wav", out_py, fs, "PCM_24")
out_c = get_c_wav(test_dir, "fixed_gain")
shutil.rmtree(test_dir)
np.testing.assert_allclose(out_c, out_py, rtol=0, atol=0)
def test_subtractor_c(in_signal):
filt = sc.subtractor(fs)
test_dir = bin_dir / "subtractor"
test_dir.mkdir(exist_ok = True, parents = True)
single_channels_test(filt, test_dir, "subtractor", in_signal)
def test_adder_c(in_signal):
filt = sc.adder(fs, 2)
test_dir = bin_dir / "adder"
test_dir.mkdir(exist_ok = True, parents = True)
single_channels_test(filt, test_dir, "adder", in_signal)
@pytest.mark.parametrize("gain_dB", [-12, -6, 0])
def test_mixer_c(in_signal, gain_dB):
filt = sc.mixer(fs, 2, gain_dB)
test_dir = bin_dir / f"mixer_{gain_dB}"
test_dir.mkdir(exist_ok = True, parents = True)
write_gain(test_dir, filt.gain_int)
single_channels_test(filt, test_dir, "mixer", in_signal)
@pytest.mark.parametrize("gains_dB", [[0, -6, 6], [-10, 3, 0]])
@pytest.mark.parametrize("slew", [1, 10])
@pytest.mark.parametrize("mute_test", [True, False])
def test_volume_control_c(in_signal, gains_dB, slew, mute_test):
filt = sc.volume_control(fs, 1, gains_dB[0], slew)
test_dir = bin_dir / f"volume_control_{gains_dB[0]}_{gains_dB[1]}_{gains_dB[2]}_{slew}_{mute_test}"
test_dir.mkdir(exist_ok = True, parents = True)
test_info = [0] * 5
test_info[0] = mute_test
test_info[1] = filt.slew_shift
test_info[2] = filt.target_gain_int
out_py = np.zeros(in_signal.shape[1])
intervals = [0] * 4
intervals[1] = in_signal.shape[1] // 3
intervals[2] = intervals[1] * 2
intervals[3] = in_signal.shape[1]
for n in range(intervals[0], intervals[1]):
out_py[n] = filt.process_xcore(in_signal[0][n])
filt.set_gain(gains_dB[1])
if mute_test: filt.mute()
test_info[3] = filt.target_gain_int
for n in range(intervals[1], intervals[2]):
out_py[n] = filt.process_xcore(in_signal[0][n])
filt.set_gain(gains_dB[2])
if mute_test: filt.unmute()
test_info[4] = filt.target_gain_int
for n in range(intervals[2], intervals[3]):
out_py[n] = filt.process_xcore(in_signal[0][n])
sf.write(gen_dir / "sig_py_int.wav", out_py, fs, "PCM_24")
test_info = np.array(test_info, dtype=np.int32)
test_info.tofile(test_dir / "gain.bin")
out_c = get_c_wav(test_dir, "volume_control")
shutil.rmtree(test_dir)
np.testing.assert_allclose(out_c, out_py, rtol=0, atol=0)
@pytest.mark.parametrize("delay_spec", [[1, 0, "samples"],
[0.5, 0.5, "ms"],
[0.02, 0.01, "s"],
[0.5, 0, "ms"]])
def test_delay_c(in_signal, delay_spec):
filter = sc.delay(fs, 1, *delay_spec)
test_dir = bin_dir / f"delay_{delay_spec[0]}_{delay_spec[1]}_{delay_spec[2]}"
test_dir.mkdir(exist_ok = True, parents = True)
delay_info = np.empty(0, dtype=np.int32)
delay_info = np.append(delay_info, filter._max_delay)
delay_info = np.append(delay_info, filter._delay)
delay_info = np.array(delay_info, dtype=np.int32)
print(delay_info)
delay_info.tofile(test_dir / "delay.bin")
out_py = np.zeros((1, in_signal.shape[1]))
for n in range(len(in_signal[0])):
out_py[:, n] = filter.process_channels_xcore(in_signal[0, n:n+1].tolist())
sf.write(gen_dir / "sig_py_int.wav", out_py[0], fs, "PCM_24")
out_c = get_c_wav(test_dir, "delay")
shutil.rmtree(test_dir)
np.testing.assert_allclose(out_c, out_py[0], rtol=0, atol=0)
def test_switch_slew_c(in_signal):
filt = sc.switch_slew(fs, 2)
test_dir = bin_dir / "switch_slew"
test_dir.mkdir(exist_ok = True, parents = True)
fname = "switch_slew"
out_py = np.zeros(in_signal.shape[1])
for n in range(in_signal.shape[1]//2):
out_py[n] = filt.process_channels_xcore(in_signal[:, n])[0]
filt.move_switch(1)
for n in range(in_signal.shape[1]//2, in_signal.shape[1]):
out_py[n] = filt.process_channels_xcore(in_signal[:, n])[0]
sf.write(gen_dir / "sig_py_int.wav", out_py, fs, "PCM_24")
out_c = get_c_wav(test_dir, fname)
shutil.rmtree(test_dir)
np.testing.assert_allclose(out_c, out_py, rtol=0, atol=0)
@pytest.mark.parametrize("mix", [0, 0.1, 0.5, 0.9, 1.0])
def test_crossfader_c(in_signal, mix):
# initial mix of zero
filt = sc.crossfader(fs, 2, 0)
test_dir = bin_dir / f"crossfader_{mix}"
test_dir.mkdir(exist_ok = True, parents = True)
write_gain(test_dir, filt.gains_int)
test_info = [0] * 5
test_info[0] = filt.slew_shift
test_info[1:3] = filt.target_gains_int
# set mix to desired mix
filt.mix = mix
test_info[3:] = filt.target_gains_int
test_info = np.array(test_info, dtype=np.int32)
test_info.tofile(test_dir / "gain.bin")
single_channels_test(filt, test_dir, "crossfader", in_signal)
if __name__ =="__main__":
bin_dir.mkdir(exist_ok=True, parents=True)
gen_dir.mkdir(exist_ok=True, parents=True)
sig_fl = get_sig()
#test_gains_c(sig_fl, -6)
#test_subtractor_c(sig_fl)
#test_adder_c(sig_fl)
#test_mixer_c(sig_fl, -3)
# test_volume_control_c(sig_fl, [0, -6, 6], 7, False)
# test_switch_slew_c(sig_fl)
test_crossfader_c(sig_fl, 0.1)

View File

@@ -0,0 +1,379 @@
# Copyright 2024-2025 XMOS LIMITED.
# This Software is subject to the terms of the XMOS Public Licence: Version 1.
import pytest
import numpy as np
import audio_dsp.dsp.signal_chain as sc
import audio_dsp.dsp.signal_gen as gen
import audio_dsp.dsp.utils as utils
import audio_dsp.dsp.generic as dspg
from audio_dsp.dsp.generic import HEADROOM_DB
from test.test_utils import q_convert_flt
import soundfile as sf
def chirp_filter_test(filter, fs):
length = 0.05
signal = gen.log_chirp(fs, length, 0.5)
output_flt = np.zeros(len(signal))
output_xcore = np.zeros(len(signal))
for n in np.arange(len(signal)):
output_flt[n] = filter.process(signal[n])
for n in np.arange(len(signal)):
output_xcore[n] = filter.process_xcore(signal[n])
# small signals are always going to be ropey due to quantizing, so just check average error of top half
top_half = utils.db(output_flt) > -50
if np.any(top_half):
error_vpu = np.abs(utils.db(output_flt[top_half])-utils.db(output_xcore[top_half]))
mean_error_vpu = utils.db(np.nanmean(utils.db2gain(error_vpu)))
assert mean_error_vpu < 0.05
@pytest.mark.parametrize("fs", [48000])
@pytest.mark.parametrize("filter_n", np.arange(4))
@pytest.mark.parametrize("n_chans", [1])
def test_gains(filter_n, fs, n_chans):
filter_spec = [['fixed_gain', -10],
['fixed_gain', 24],
['volume_control', 24],
['volume_control', -10]]
filter_spec = filter_spec[filter_n]
class_name = f"{filter_spec[0]}"
class_handle = getattr(sc, class_name)
filter = class_handle(fs, n_chans, *filter_spec[1:])
chirp_filter_test(filter, fs)
@pytest.mark.parametrize("fs", [48000])
@pytest.mark.parametrize("filter_n", np.arange(4))
@pytest.mark.parametrize("n_chans", [1, 2, 4])
@pytest.mark.parametrize("q_format", [27, 31])
def test_gains_frames(filter_n, fs, n_chans, q_format):
filter_spec = [['fixed_gain', -10],
['fixed_gain', 24],
['volume_control', 24],
['volume_control', -10]]
filter_spec = filter_spec[filter_n]
class_name = f"{filter_spec[0]}"
class_handle = getattr(sc, class_name)
filter = class_handle(fs, n_chans, *filter_spec[1:], Q_sig=q_format)
length = 0.05
signal = gen.log_chirp(fs, length, 0.5)
signal = np.tile(signal, [n_chans, 1])
signal_frames = utils.frame_signal(signal, 1, 1)
output_flt = np.zeros_like(signal)
output_xcore = np.zeros_like(signal)
frame_size = 1
for n in range(len(signal_frames)):
output_flt[:, n*frame_size:(n+1)*frame_size] = filter.process_frame(signal_frames[n])
assert np.all(output_flt[0, :] == output_flt)
for n in range(len(signal_frames)):
output_xcore[:, n*frame_size:(n+1)*frame_size] = filter.process_frame_xcore(signal_frames[n])
assert np.all(output_xcore[0, :] == output_xcore)
@pytest.mark.parametrize("fs", [48000])
@pytest.mark.parametrize("filter_spec", [['mixer', 2, 0],
['adder', 2],
['subtractor', 2]])
def test_saturation(filter_spec, fs):
class_name = f"{filter_spec[0]}"
class_handle = getattr(sc, class_name)
if filter_spec[0] == "subtractor":
# subtractor has fewer inputs
filter = class_handle(fs)
else:
filter = class_handle(fs, *filter_spec[1:])
length = 0.05
signals = []
for n in range(filter_spec[1]):
# max level is 24db (16), so 10 + 10 will saturate
signals.append(gen.sin(fs, length, 997, 10.0))
if class_name == "subtractor":
signals[1] *= -1
signal = np.stack(signals, axis=0)
signal = utils.saturate_float_array(signal, dspg.Q_SIG)
signal = q_convert_flt(signal, 23, dspg.Q_SIG)
output_flt = np.zeros(signal.shape[1])
output_xcore = np.zeros(signal.shape[1])
for n in range(signal.shape[1]):
output_flt[n] = filter.process_channels(signal[:, n])[0]
for n in range(signal.shape[1]):
output_xcore[n] = filter.process_channels_xcore(signal[:, n])[0]
# small signals are always going to be ropey due to quantizing, so just check average error of top half
# also, int signals can't go above HEADROOM_BITS
top_half = (utils.db(output_flt) > -50) * (utils.db(output_flt) < HEADROOM_DB)
if np.any(top_half):
error_flt = np.abs(utils.db(output_xcore[top_half])-utils.db(output_flt[top_half]))
mean_error_flt = utils.db(np.nanmean(utils.db2gain(error_flt)))
assert mean_error_flt < 0.055
assert np.all(utils.db(output_xcore) <= HEADROOM_DB)
def test_volume_change():
fs = 48000
start_gain = -60
filter = sc.volume_control(fs, 1, start_gain)
length = 5
signal = gen.sin(fs, length, 997/2, 0.5)
signal += gen.sin(fs, length, 997, 0.5)
output_flt = np.zeros(len(signal))
output_xcore = np.zeros(len(signal))
steps = 12
for step in range(steps):
start = step*len(signal)//steps
for n in range(len(signal)//steps):
output_flt[start + n] = filter.process(signal[start + n])
for n in range(len(signal)//steps):
output_xcore[start + n] = filter.process_xcore(signal[start + n])
filter.set_gain(start_gain + 6*step)
# this is a very useful signal to listen to for clicks
sf.write("vol_test_output_flt_slew.wav", output_flt, fs)
# small signals are always going to be ropey due to quantizing, so just check average error of top half
top_half = utils.db(output_flt) > -50
if np.any(top_half):
error_flt = np.abs(utils.db(output_xcore[top_half])-utils.db(output_flt[top_half]))
mean_error_flt = utils.db(np.nanmean(utils.db2gain(error_flt)))
assert mean_error_flt < 0.055
def test_mute():
fs = 48000
start_gain = -10
filter = sc.volume_control(fs, 1, start_gain)
length = 5
signal = gen.sin(fs, length, 997/2, 0.5)
signal += gen.sin(fs, length, 997, 0.5)
output_flt = np.zeros(len(signal))
output_xcore = np.zeros(len(signal))
# check muting while muted, unmuting while unmuted, gain change while muted
step_states = ["gain", "mute", "unmute", "mute", "mute", "unmute", "unmute", "mute", "gain", "unmute"]
steps = len(step_states)
for step in range(len(step_states)):
if step_states[step] == "gain":
start_gain += 3
filter.set_gain(start_gain)
elif step_states[step] == "mute":
filter.mute()
elif step_states[step] == "unmute":
filter.unmute()
start = step*len(signal)//steps
for n in range(len(signal)//steps):
output_flt[start + n] = filter.process(signal[start + n])
for n in range(len(signal)//steps):
output_xcore[start + n] = filter.process_xcore(signal[start + n])
sf.write("mute_test_output_flt_slew.wav", output_flt, fs)
# small signals are always going to be ropey due to quantizing, so just check average error of top half
top_half = utils.db(output_flt) > -50
if np.any(top_half):
error_flt = np.abs(utils.db(output_xcore[top_half])-utils.db(output_flt[top_half]))
mean_error_flt = utils.db(np.nanmean(utils.db2gain(error_flt)))
assert mean_error_flt < 0.055
@pytest.mark.parametrize("fs", [48000])
@pytest.mark.parametrize("filter_spec", [['mixer', 2, 0],
['mixer', 3, -9],
['mixer', 2, -6],
['mixer', 4, -12],
['adder', 2],
['adder', 4],
['subtractor', 2],
["crossfader", 2, 0],
["crossfader", 2, 0.95],
["crossfader", 2, 1],
["crossfader", 4, 0],
["crossfader", 4, 0.5],
["crossfader", 4, 0.95]])
def test_combiners(filter_spec, fs):
class_name = f"{filter_spec[0]}"
class_handle = getattr(sc, class_name)
if filter_spec[0] == "subtractor":
# subtractor has fewer inputs
filter = class_handle(fs)
else:
filter = class_handle(fs, *filter_spec[1:])
length = 0.05
signals = []
for n in range(filter_spec[1]):
signals.append(gen.pink_noise(fs, length, 1.0))
signal = np.stack(signals, axis=0)
signal = utils.saturate_float_array(signal, dspg.Q_SIG)
output_flt = np.zeros(signal.shape[1])
output_xcore = np.zeros(signal.shape[1])
for n in range(signal.shape[1]):
output_flt[n] = filter.process_channels(signal[:, n])[0]
for n in range(signal.shape[1]):
output_xcore[n] = filter.process_channels_xcore(signal[:, n])[0]
# small signals are always going to be ropey due to quantizing, so just check average error of top half
top_half = utils.db(output_flt) > -50
if np.any(top_half):
error_flt = np.abs(utils.db(output_xcore[top_half])-utils.db(output_flt[top_half]))
mean_error_flt = utils.db(np.nanmean(utils.db2gain(error_flt)))
assert mean_error_flt < 0.055
@pytest.mark.parametrize("fs", [48000])
@pytest.mark.parametrize("filter_spec", [['mixer', 2, 0],
['mixer', 3, -9],
['mixer', 2, -6],
['mixer', 4, -12],
['adder', 2],
['adder', 4],
['subtractor', 2],
["crossfader", 2, 0],
["crossfader", 2, 0.95],
["crossfader", 2, 1],
["crossfader", 4, 0],
["crossfader", 4, 0.5],
["crossfader", 4, 0.95]])
@pytest.mark.parametrize("q_format", [27, 31])
def test_combiners_frames(filter_spec, fs, q_format):
class_name = f"{filter_spec[0]}"
class_handle = getattr(sc, class_name)
if filter_spec[0] == "subtractor":
# subtractor has fewer inputs
filter = class_handle(fs, Q_sig=q_format)
else:
filter = class_handle(fs, *filter_spec[1:], Q_sig=q_format)
length = 0.05
signals = []
for n in range(filter_spec[1]):
signals.append(gen.pink_noise(fs, length, 1.0))
signal = np.stack(signals, axis=0)
signal = utils.saturate_float_array(signal, dspg.Q_SIG)
signal_frames = utils.frame_signal(signal, 1, 1)
output_flt = np.zeros((1, signal.shape[1]))
output_xcore = np.zeros_like(output_flt)
frame_size = 1
for n in range(len(signal_frames)):
output_flt[:, n*frame_size:(n+1)*frame_size] = filter.process_frame(signal_frames[n])
for n in range(len(signal_frames)):
output_xcore[:, n*frame_size:(n+1)*frame_size] = filter.process_frame_xcore(signal_frames[n])
# small signals are always going to be ropey due to quantizing, so just check average error of top half
top_half = utils.db(output_flt) > -50
if np.any(top_half):
error_flt = np.abs(utils.db(output_xcore[top_half])-utils.db(output_flt[top_half]))
mean_error_flt = utils.db(np.nanmean(utils.db2gain(error_flt)))
assert mean_error_flt < 0.055
@pytest.mark.parametrize("fs", [48000])
@pytest.mark.parametrize("delay_spec", [[15, 10, "samples"],
[128, 128, "samples"],
[2, 1.7, "ms"],
[1.056, 0.94, "s"],
[1, 2, "s"],
[2, 0, "s"]])
@pytest.mark.parametrize("n_chans", [1, 2, 4])
def test_delay(fs, delay_spec, n_chans):
filter = sc.delay(fs, n_chans, *delay_spec)
delay_samps = utils.time_to_samples(fs, delay_spec[1], delay_spec[2])
max_delay_samps = utils.time_to_samples(fs, delay_spec[0], delay_spec[2])
# delay can't be > max delay
delay_samps = min(delay_samps, max_delay_samps)
# delay of 0 actually yields 1
delay_samps = max(1, delay_samps)
length = 0.005
sig_len = int(length * fs)
signal = gen.pink_noise(fs, length, 0.5)
signal = np.pad(signal, (0, delay_samps))
signal = np.tile(signal, [n_chans, 1])
signal_frames = utils.frame_signal(signal, 1, 1)
output_flt = np.zeros_like(signal)
output_xcore = np.zeros_like(signal)
frame_size = 1
for n in range(len(signal_frames)):
output_flt[:, n*frame_size:(n+1)*frame_size] = filter.process_frame(signal_frames[n])
assert np.all(signal[:, : sig_len] == output_flt[:, delay_samps :])
for n in range(len(signal_frames)):
output_xcore[:, n*frame_size:(n+1)*frame_size] = filter.process_frame_xcore(signal_frames[n])
assert np.all(signal[:, : sig_len] == output_xcore[:, delay_samps :])
def test_switch_slew():
fs = 48000
signal_1 = gen.sin(fs, 1, 500, 0.25) + 0.5
signal_2 = gen.sin(fs, 1, 500, 0.25) - 0.5
signal = np.stack((signal_1, signal_2))
sw = sc.switch_slew(fs, 2)
sw_x = sc.switch_slew(fs, 2)
signal_frames = utils.frame_signal(signal, 1, 1)
output_flt = np.zeros((1, signal.shape[1]))
output_xcore = np.zeros_like(output_flt)
frame_size = 1
for n in range(len(signal_frames)//2):
output_flt[:, n*frame_size:(n+1)*frame_size] = sw.process_frame(signal_frames[n])
output_xcore[:, n*frame_size:(n+1)*frame_size] = sw_x.process_frame_xcore(signal_frames[n])
sw.move_switch(1)
sw_x.move_switch(1)
for n in range(len(signal_frames)//2, len(signal_frames)):
output_flt[:, n*frame_size:(n+1)*frame_size] = sw.process_frame(signal_frames[n])
output_xcore[:, n*frame_size:(n+1)*frame_size] = sw_x.process_frame_xcore(signal_frames[n])
top_half = utils.db(output_flt) > -50
if np.any(top_half):
error_flt = np.abs(utils.db(output_xcore[top_half])-utils.db(output_flt[top_half]))
mean_error_flt = utils.db(np.nanmean(utils.db2gain(error_flt)))
assert mean_error_flt < 0.055
pass
if __name__ == "__main__":
test_combiners(["crossfader", 2, 0], 48000)
# test_volume_change()
# test_gains(1, 48000, 1)
# test_delay(48000, [2, 0, "s"], 1)
# test_switch_slew()