improve test

This test was written in such a way that having a wrong data type
for the mp3 samples wasn't detected. Instead of using
np.frombuffer(dtype=int16), just do arithmetic directly on the
samples. During testing time we don't care if it might be a little
slower or use a little more RAM than ulab, and we don't care
whether it's actually an RMS calculation.  Just that it's
consistent and shows the audio data is correct, including its
defined data type.
This commit is contained in:
Jeff Epler 2025-03-26 09:40:07 -05:00
parent d365e2cc9f
commit 17497e425f
2 changed files with 13 additions and 19 deletions

View file

@ -1,5 +1,4 @@
import audiomp3, audiocore
import ulab.numpy as np
TEST_FILE = (
__file__.rsplit("/", 1)[0]
@ -7,19 +6,14 @@ TEST_FILE = (
)
def normalized_rms_ulab(values):
values = np.frombuffer(values, dtype=np.int16)
# this function works with ndarrays only
minbuf = np.mean(values)
values = values - minbuf
samples_sum = np.sum(values * values)
return (samples_sum / len(values)) ** 0.5
def loudness(values):
return sum(abs(a) for a in values)
def print_frame_loudness(decoder, n):
for i in range(n):
result, buf = audiocore.get_buffer(decoder)
print(f"{i} {result} {normalized_rms_ulab(buf):5.0f}")
print(f"{i} {result} {loudness(buf):5.0f}")
print()

View file

@ -1,16 +1,16 @@
0 1 0
1 1 4730
2 1 27914
3 1 28737
4 1 29251
5 1 29219
6 1 28672
7 1 28213
1 1 25
2 1 830
3 1 880
4 1 932
5 1 892
6 1 869
7 1 839
0 1 0
1 1 4730
1 1 25
0 1 0
1 1 4730
2 1 27914
1 1 25
2 1 830