Skip to content

Commit 3f91a0a

Browse files
rjtokenringdsammaruga
authored andcommitted
tests
1 parent 149f389 commit 3f91a0a

File tree

2 files changed

+163
-0
lines changed

2 files changed

+163
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
from arduino.app_bricks.sound_generator import ABCNotationLoader
2+
3+
4+
def test_abc_loader():
5+
full_abc = """
6+
X:1
7+
T:Main Theme
8+
M:4/4
9+
L:1/8
10+
Q:1/4=130
11+
K:Cm
12+
"Cm"E2 E2 E2 "Ab"C>G | "Cm"E2 "Ab"C>G "Cm"E4 |
13+
"Cm"B2 B2 B2 "Ab"c>G | "Fm"^D#2 "Ab"C>G "Cm"E4 |
14+
"""
15+
16+
reference_notes = [
17+
("E4", 60 / 130),
18+
("E4", 60 / 130),
19+
("E4", 60 / 130),
20+
("C4", (60 / 130) / 2),
21+
("G4", (60 / 130) / 2),
22+
("E4", 60 / 130),
23+
("C4", (60 / 130) / 2),
24+
("G4", (60 / 130) / 2),
25+
("E4", (60 / 130) * 2),
26+
("B4", 60 / 130),
27+
("B4", 60 / 130),
28+
("B4", 60 / 130),
29+
("C5", (60 / 130) / 2),
30+
("G4", (60 / 130) / 2),
31+
("D#4", 60 / 130),
32+
("C4", (60 / 130) / 2),
33+
("G4", (60 / 130) / 2),
34+
("E4", (60 / 130) * 2),
35+
]
36+
37+
metadata, loaded = ABCNotationLoader.parse_abc_notation(full_abc)
38+
assert metadata["title"] == "Main Theme"
39+
assert "transpose" not in metadata
40+
assert metadata["tempo"] == "1/4=130"
41+
42+
i_ref = 0
43+
for note, duration in loaded:
44+
print(f"Note: {note}, Duration: {duration}")
45+
assert note == reference_notes[i_ref][0]
46+
assert abs(duration - reference_notes[i_ref][1]) < 0.01
47+
i_ref += 1
48+
49+
50+
def test_abc_loader_with_transpose():
51+
full_abc = """
52+
X:1
53+
T:Main Theme
54+
M:4/4
55+
L:1/8
56+
Q:1/4=130
57+
K:Cm
58+
%%transpose -12
59+
"Cm"E2 E2 E2 "Ab"C>G | "Cm"E2 "Ab"C>G "Cm"E4 |
60+
"Cm"B2 B2 B2 "Ab"c>G | "Fm"^D#2 "Ab"C>G "Cm"E4 |
61+
"""
62+
63+
reference_notes = [
64+
("E3", 60 / 130),
65+
("E3", 60 / 130),
66+
("E3", 60 / 130),
67+
("C3", (60 / 130) / 2),
68+
("G3", (60 / 130) / 2),
69+
("E3", 60 / 130),
70+
("C3", (60 / 130) / 2),
71+
("G3", (60 / 130) / 2),
72+
("E3", (60 / 130) * 2),
73+
("B3", 60 / 130),
74+
("B3", 60 / 130),
75+
("B3", 60 / 130),
76+
("C4", (60 / 130) / 2),
77+
("G3", (60 / 130) / 2),
78+
("D#3", 60 / 130),
79+
("C3", (60 / 130) / 2),
80+
("G3", (60 / 130) / 2),
81+
("E3", (60 / 130) * 2),
82+
]
83+
84+
metadata, loaded = ABCNotationLoader.parse_abc_notation(full_abc)
85+
assert metadata["title"] == "Main Theme"
86+
assert "transpose" in metadata
87+
assert metadata["transpose"] == -1
88+
assert metadata["tempo"] == "1/4=130"
89+
90+
i_ref = 0
91+
for note, duration in loaded:
92+
print(f"Note: {note}, Duration: {duration}")
93+
assert note == reference_notes[i_ref][0]
94+
assert abs(duration - reference_notes[i_ref][1]) < 0.01
95+
i_ref += 1
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# SPDX-FileCopyrightText: Copyright (C) 2025 ARDUINO SA <http://www.arduino.cc>
2+
#
3+
# SPDX-License-Identifier: MPL-2.0
4+
5+
import pytest
6+
from arduino.app_bricks.sound_generator.effects import SoundEffect
7+
from arduino.app_bricks.sound_generator import SoundGenerator
8+
from arduino.app_utils.audio import WaveGenerator
9+
10+
11+
def test_adsr_effect():
12+
generator = WaveGenerator(sample_rate=16000, wave_form="square")
13+
adsr = SoundEffect.adsr()
14+
blk = generator.generate_block(440.0, 1 / 8, 1.0) # Generate a block to initialize
15+
assert adsr is not None
16+
17+
# Apply ADSR effect
18+
processed = adsr.apply(blk)
19+
20+
assert processed is not None
21+
assert len(processed) == len(blk)
22+
23+
24+
def test_available_notes():
25+
note_sequence = [
26+
("E5", 0.125),
27+
("E5", 0.125),
28+
("REST", 0.125),
29+
("E5", 0.125),
30+
("REST", 0.125),
31+
("C5", 0.125),
32+
("E5", 0.125),
33+
("REST", 0.125),
34+
("G5", 0.25),
35+
("REST", 0.25),
36+
("G4", 0.25),
37+
("REST", 0.25),
38+
("C5", 0.25),
39+
("REST", 0.125),
40+
("G4", 0.25),
41+
("REST", 0.125),
42+
("E4", 0.25),
43+
("REST", 0.125),
44+
("A4", 0.25),
45+
("B4", 0.25),
46+
("Bb4", 0.125),
47+
("A4", 0.25),
48+
("G4", 0.125),
49+
("E5", 0.125),
50+
("G5", 0.125),
51+
("A5", 0.25),
52+
("F5", 0.125),
53+
("G5", 0.125),
54+
("REST", 0.125),
55+
("E5", 0.25),
56+
("C5", 0.125),
57+
("D5", 0.125),
58+
("B4", 0.25),
59+
]
60+
61+
generator = SoundGenerator()
62+
for note, duration in note_sequence:
63+
print(f"Testing note: {note}")
64+
frequency = generator._get_note(note)
65+
if "REST" != note:
66+
assert frequency is not None and frequency > 0.0
67+
else:
68+
assert frequency is not None and frequency == 0.0

0 commit comments

Comments
 (0)