Skip to content

Commit 1bf4efd

Browse files
committed
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.
1 parent d365e2c commit 1bf4efd

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

tests/circuitpython/issue9705.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
11
import audiomp3, audiocore
2-
import ulab.numpy as np
32

43
TEST_FILE = (
54
__file__.rsplit("/", 1)[0]
65
+ "/../circuitpython-manual/audiocore/jeplayer-splash-44100-stereo.mp3"
76
)
87

98

10-
def normalized_rms_ulab(values):
11-
values = np.frombuffer(values, dtype=np.int16)
12-
# this function works with ndarrays only
13-
minbuf = np.mean(values)
14-
values = values - minbuf
15-
samples_sum = np.sum(values * values)
16-
return (samples_sum / len(values)) ** 0.5
9+
def loudness(values):
10+
return sum(abs(a) for a in values)
1711

1812

1913
def print_frame_loudness(decoder, n):
2014
for i in range(n):
2115
result, buf = audiocore.get_buffer(decoder)
22-
print(f"{i} {result} {normalized_rms_ulab(buf):5.0f}")
16+
print(f"{i} {result} {loudness(buf):5.0f}")
2317
print()
2418

2519

0 commit comments

Comments
 (0)