mirror of
https://github.com/cookiengineer/audacity
synced 2026-01-12 07:35:51 +01:00
Start work on new Loudness effect
This is based on my old loudness effect prototype which was included in the Normalize effect. Create all source files and add them to all build systems. Currently, the effect only consists of a GUI mockup. Create Octave+mod-script-pipe based dummy unit-test as well.
This commit is contained in:
16
tests/octave/README.md
Normal file
16
tests/octave/README.md
Normal file
@@ -0,0 +1,16 @@
|
||||
# Unit tests for Audacity effects
|
||||
|
||||
These unit tests check the correctness of Audacity's effect calculations
|
||||
against GNU octave. Therefore some simple deterministic and random
|
||||
sample data is generated and passed to Audacity via mod-script-pipe.
|
||||
|
||||
To run a test, run: (replace `<desired_test.m>` with the correct filename)
|
||||
```
|
||||
./run_test.m <desired_test.m>
|
||||
```
|
||||
|
||||
The tests will print the results to the terminal and will return 0 on
|
||||
success and non-zero on error.
|
||||
|
||||
To run those tests you need a Linux system with GNU Octave,
|
||||
octave-forge-signal and Audacity mod-script-pipe installed.
|
||||
126
tests/octave/loudness_test.m
Normal file
126
tests/octave/loudness_test.m
Normal file
@@ -0,0 +1,126 @@
|
||||
## Audacity Loudness effect unit test
|
||||
#
|
||||
# Max Maisel
|
||||
#
|
||||
# This tests the Loudness effect with 30 seconds long pseudo-random stereo
|
||||
# noise sequences. The test sequences have different amplitudes per
|
||||
# channel and sometimes a DC component. For best test coverage, irrelevant
|
||||
# parameters for the current operation are randomly varied.
|
||||
#
|
||||
|
||||
printf("Running Loudness effect tests.\n");
|
||||
printf("This requires the octave-forge-signal package to be installed.\n");
|
||||
|
||||
pkg load signal;
|
||||
|
||||
EXPORT_TEST_SIGNALS = true;
|
||||
TEST_LUFS_HELPER = true;
|
||||
# LUFS need a higher epsilon because they are a logarithmic unit.
|
||||
LUFS_epsilon = 0.02;
|
||||
|
||||
# A straightforward and simple LUFS implementation which can
|
||||
# be easily compared with the specification ITU-R BS.1770-4.
|
||||
function [gated_lufs] = calc_LUFS(x, fs)
|
||||
# HSF
|
||||
f0 = 38.13547087602444;
|
||||
Q = 0.5003270373238773;
|
||||
K = tan(pi * f0 / fs);
|
||||
|
||||
rb0 = 1.0;
|
||||
rb1 = -2.0;
|
||||
rb2 = 1.0;
|
||||
ra0 = 1.0;
|
||||
ra1 = 2.0 * (K * K - 1.0) / (1.0 + K / Q + K * K);
|
||||
ra2 = (1.0 - K / Q + K * K) / (1.0 + K / Q + K * K);
|
||||
|
||||
rb = [rb0 rb1 rb2];
|
||||
ra = [ra0 ra1 ra2];
|
||||
|
||||
# HPF
|
||||
db = 3.999843853973347;
|
||||
f0 = 1681.974450955533;
|
||||
Q = 0.7071752369554196;
|
||||
K = tan(pi * f0 / fs);
|
||||
Vh = power(10.0, db / 20.0);
|
||||
Vb = power(Vh, 0.4996667741545416);
|
||||
|
||||
pa0 = 1.0;
|
||||
a0 = 1.0 + K / Q + K * K;
|
||||
pb0 = (Vh + Vb * K / Q + K * K) / a0;
|
||||
pb1 = 2.0 * (K * K - Vh) / a0;
|
||||
pb2 = (Vh - Vb * K / Q + K * K) / a0;
|
||||
pa1 = 2.0 * (K * K - 1.0) / a0;
|
||||
pa2 = (1.0 - K / Q + K * K) / a0;
|
||||
|
||||
pb = [pb0 pb1 pb2];
|
||||
pa = [pa0 pa1 pa2];
|
||||
|
||||
# Apply k-weighting
|
||||
x = filter(rb, ra, x, [], 1);
|
||||
x = filter(pb, pa, x, [], 1);
|
||||
|
||||
# - gating blocks (every 100 ms over 400 ms)
|
||||
block_size = 0.4*fs;
|
||||
block_overlap = 0.3*fs;
|
||||
block_count = floor((size(x)(1)-block_size)/(block_size-block_overlap))+1+1;
|
||||
|
||||
x_blocked = zeros(block_size, block_count, size(x)(2));
|
||||
for i=1:1:size(x)(2)
|
||||
x_blocked(:,:,i) = buffer(x(:,i), block_size, 0.3*fs, 'nodelay');
|
||||
end
|
||||
|
||||
lufs_blocked = 1/(block_size)*sum(x_blocked.^2, 1);
|
||||
lufs_blocked = sum(lufs_blocked, 3);
|
||||
|
||||
# Apply absolute threshold
|
||||
GAMMA_A = -70;
|
||||
lufs_blocked = -0.691 + 10*log10(lufs_blocked);
|
||||
valid_blocks = length(lufs_blocked);
|
||||
valid_blocks = valid_blocks - length(lufs_blocked(lufs_blocked < GAMMA_A));
|
||||
lufs_blocked(lufs_blocked < GAMMA_A) = -100;
|
||||
lufs_blocked = 10.^((lufs_blocked+0.691)/10);
|
||||
|
||||
# Apply relative threshold
|
||||
GAMMA_R = -0.691 + 10*log10(sum(lufs_blocked)/valid_blocks) - 10;
|
||||
lufs_blocked = -0.691 + 10*log10(lufs_blocked);
|
||||
valid_blocks = length(lufs_blocked);
|
||||
valid_blocks = valid_blocks - length(lufs_blocked(lufs_blocked < GAMMA_R));
|
||||
lufs_blocked(lufs_blocked < GAMMA_R) = -100;
|
||||
lufs_blocked = 10.^((lufs_blocked+0.691)/10);
|
||||
hold off
|
||||
|
||||
gated_lufs = -0.691 + 10*log10(sum(lufs_blocked)/valid_blocks);
|
||||
end
|
||||
|
||||
if TEST_LUFS_HELPER
|
||||
printf("Running calc_LUFS() selftest.\n");
|
||||
printf("Compare the following results with a trusted LUFS calculator.\n");
|
||||
|
||||
fs = 44100;
|
||||
k = 1:1:60*fs;
|
||||
x = 0.3*sin(2*pi*1000/fs*k) + 0.2*sin(2*pi*1200/fs*k);
|
||||
x = (x .* [1:1:30*fs, 30*fs:-1:1]./60./fs).';
|
||||
|
||||
audiowrite(cstrcat(pwd(), "/LUFS-selftest1.wav"), x, fs);
|
||||
printf("LUFS-selftest1.wav should be %f LUFS\n", calc_LUFS(x, fs));
|
||||
|
||||
randn("seed", 1);
|
||||
x = [0.2*randn(2, 10*fs) zeros(2, 10*fs) 0.1*randn(2, 10*fs)].';
|
||||
x(:,1) = x(:,1) * 0.4 + 0.2;
|
||||
|
||||
audiowrite(cstrcat(pwd(), "/LUFS-selftest2.wav"), x, fs);
|
||||
printf("LUFS-selftest2.wav should be %f LUFS\n", calc_LUFS(x, fs));
|
||||
|
||||
fs = 8000;
|
||||
randn("seed", 2);
|
||||
x = [0.2*randn(2, 10*fs) zeros(2, 10*fs) 0.1*randn(2, 10*fs)].';
|
||||
x(:,1) = x(:,1) * 0.6 - 0.1;
|
||||
|
||||
# MMM: I'm not sure how trustworthy free loudness meters are
|
||||
# in case of non-standard sample rates.
|
||||
audiowrite(cstrcat(pwd(), "/LUFS-selftest3.wav"), x, fs);
|
||||
printf("LUFS-selftest3.wav should be %f LUFS\n", calc_LUFS(x, fs));
|
||||
end
|
||||
|
||||
# TODO: add tests here
|
||||
|
||||
174
tests/octave/run_test.m
Executable file
174
tests/octave/run_test.m
Executable file
@@ -0,0 +1,174 @@
|
||||
#!/usr/bin/octave -qf
|
||||
## Audacity Octave unit test runner
|
||||
#
|
||||
# Max Maisel
|
||||
#
|
||||
|
||||
if !(nargin == 1 || nargin == 2)
|
||||
printf("Usage: ./testbench.m <test-file.m> [-v]\n");
|
||||
exit(2);
|
||||
end
|
||||
|
||||
arg_list = argv();
|
||||
|
||||
if exist(arg_list{1}, "file") != 2
|
||||
printf("Specified test file does not exist!\n");
|
||||
exit(2);
|
||||
end
|
||||
|
||||
global VERBOSE;
|
||||
VERBOSE=0;
|
||||
|
||||
if nargin == 2
|
||||
if strcmp(arg_list{2}, "-v") == 1
|
||||
VERBOSE=1;
|
||||
else
|
||||
printf("Unknown argument %s. Abort!", arg_list{2});
|
||||
exit(2);
|
||||
end
|
||||
end
|
||||
|
||||
## Initialization and helper functions
|
||||
UID=num2str(getuid());
|
||||
PIPE_TO_PATH=strcat("/tmp/audacity_script_pipe.to.", UID);
|
||||
PIPE_FROM_PATH=strcat("/tmp/audacity_script_pipe.from.", UID);
|
||||
TMP_FILENAME=strcat(pwd(), "/tmp.wav");
|
||||
|
||||
printf("Open scripting pipes, this may freeze if Audacity does not run...\n");
|
||||
|
||||
global PIPE_TO;
|
||||
global PIPE_FROM;
|
||||
global PRINT_COMMANDS;
|
||||
PRINT_COMMANDS=false;
|
||||
PIPE_TO=fopen(PIPE_TO_PATH, "w");
|
||||
PIPE_FROM=fopen(PIPE_FROM_PATH, "r");
|
||||
|
||||
## aud-do helper function
|
||||
function aud_do(command)
|
||||
global PIPE_TO;
|
||||
global PIPE_FROM;
|
||||
global PRINT_COMMANDS;
|
||||
if PRINT_COMMANDS
|
||||
puts(command);
|
||||
end
|
||||
fwrite(PIPE_TO, command);
|
||||
fflush(PIPE_TO);
|
||||
do
|
||||
string = fgets(PIPE_FROM);
|
||||
if PRINT_COMMANDS
|
||||
puts(string);
|
||||
end
|
||||
fflush(stdout);
|
||||
until strncmp(string, "BatchCommand finished:", length("BatchCommand finished:"));
|
||||
end
|
||||
|
||||
## Float equal comparison helper
|
||||
function [ret] = float_eq(x, y, eps=0.001)
|
||||
ret = abs(x - y) < eps;
|
||||
end
|
||||
|
||||
## Test report helper
|
||||
global TESTS_FAILED;
|
||||
global TESTS_RUN;
|
||||
global TESTS_SKIPPED;
|
||||
global CURRENT_TEST;
|
||||
TESTS_FAILED = 0;
|
||||
TESTS_RUN = 0;
|
||||
TESTS_SKIPPED = 0;
|
||||
CURRENT_TEST="";
|
||||
|
||||
function plot_failure(x, y)
|
||||
global VERBOSE;
|
||||
if VERBOSE == 0
|
||||
return;
|
||||
end
|
||||
|
||||
figure(1)
|
||||
plot(x, 'r')
|
||||
hold on
|
||||
plot(y, 'b')
|
||||
plot(log10(abs(x-y)), 'g')
|
||||
hold off
|
||||
legend("Audacity", "Octave", "log-delta", "location", "southeast")
|
||||
input("Press enter to continue", "s")
|
||||
end
|
||||
|
||||
function do_test_equ(x, y, msg, eps=0.001, skip = false)
|
||||
cmp = all(all(float_eq(x, y, eps)));
|
||||
if do_test(cmp, msg, skip) == 0
|
||||
plot_failure(x, y);
|
||||
end
|
||||
end
|
||||
|
||||
function do_test_neq(x, y, msg, eps=0.001, skip = false)
|
||||
cmp = all(all(!float_eq(x, y, eps)));
|
||||
if do_test(cmp, msg, skip) == 0
|
||||
plot_failure(x, y);
|
||||
end
|
||||
end
|
||||
|
||||
function do_test_gte(x, y, msg, skip = false)
|
||||
cmp = all(all(x >= y));
|
||||
if do_test(cmp, msg, skip) == 0
|
||||
plot_failure(x, y);
|
||||
end
|
||||
end
|
||||
|
||||
function do_test_lte(x, y, msg, skip = false)
|
||||
cmp = all(all(x <= y));
|
||||
if do_test(cmp, msg, skip) == 0
|
||||
plot_failure(x, y);
|
||||
end
|
||||
end
|
||||
|
||||
function result = do_test(result, msg, skip = false)
|
||||
global TESTS_RUN;
|
||||
global TESTS_FAILED;
|
||||
global TESTS_SKIPPED;
|
||||
global CURRENT_TEST;
|
||||
TESTS_RUN = TESTS_RUN + 1;
|
||||
|
||||
suffix = "";
|
||||
if !strcmp(msg, "")
|
||||
suffix = cstrcat(" - ", msg);
|
||||
end
|
||||
|
||||
if skip
|
||||
TESTS_SKIPPED = TESTS_SKIPPED + 1;
|
||||
printf(cstrcat("[Skip]: ", CURRENT_TEST, suffix, "\n"));
|
||||
result = 1;
|
||||
else
|
||||
if result
|
||||
printf(cstrcat("[Success]: ", CURRENT_TEST, suffix, "\n"));
|
||||
return;
|
||||
else
|
||||
TESTS_FAILED = TESTS_FAILED + 1;
|
||||
printf(cstrcat("[Failed]: ", CURRENT_TEST, suffix, "\n"));
|
||||
return;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
## Run tests
|
||||
printf("Starting tests...\n");
|
||||
source(arg_list{1});
|
||||
|
||||
## Cleanup and result reporting
|
||||
unlink(TMP_FILENAME);
|
||||
fclose(PIPE_FROM);
|
||||
fclose(PIPE_TO);
|
||||
|
||||
printf("%d tests run, %d tests failed, %d tests skipped\n", TESTS_RUN, TESTS_FAILED, TESTS_SKIPPED);
|
||||
if TESTS_FAILED != 0
|
||||
printf("Some tests failed!\n");
|
||||
if VERBOSE == 0
|
||||
printf("Re-run with -v option for details.\n");
|
||||
end
|
||||
exit(1);
|
||||
elseif TESTS_SKIPPED != 0
|
||||
printf("Some tests were skipped!\n");
|
||||
else
|
||||
printf("All tests succeeded!\n");
|
||||
end
|
||||
|
||||
exit(0)
|
||||
Reference in New Issue
Block a user