8000 improve test · adafruit/circuitpython@17497e4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 17497e4

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 17497e4

File tree

2 files changed

+13
-19
lines changed

2 files changed

+13
-19
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

tests/circuitpython/issue9705.py.exp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
0 1 0
2-
1 1 4730
3-
2 1 27914
4-
3 1 28737
5-
4 1 29251
6-
5 1 29219
7-
6 1 28672
8-
7 1 28213
2+
1 1 25
3+
2 1 830
4+
3 1 880
5+
4 1 932
6+
5 1 892
7+
6 1 869
8+
7 1 839
99

1010
0 1 0
11-
1 1 4730
11+
1 1 25
1212

1313
0 1 0
14-
1 1 4730
15-
2 1 27914
14+
1 1 25
15+
2 1 830
1616

0 commit comments

Comments
 (0)
0