Skip to content

Commit 1e17337

Browse files
committed
Add new sound effect to buzzer example
1 parent 6ac881d commit 1e17337

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

examples/buzzer.py

+33
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
"""
1313

1414
from modulino import ModulinoBuzzer
15+
from time import sleep
1516

1617
buzzer = ModulinoBuzzer()
1718

1819
# Super Mario Bros theme intro
1920
melody = [
2021
(ModulinoBuzzer.NOTES["E5"], 125),
22+
(ModulinoBuzzer.NOTES["REST"], 25),
2123
(ModulinoBuzzer.NOTES["E5"], 125),
2224
(ModulinoBuzzer.NOTES["REST"], 125),
2325
(ModulinoBuzzer.NOTES["E5"], 125),
@@ -31,4 +33,35 @@
3133
]
3234

3335
for note, duration in melody:
36+
buzzer.tone(note, duration, blocking=True)
37+
38+
# Wait 2 seconds before playing the next melody
39+
sleep(2)
40+
41+
# Police siren sound effect
42+
def generate_siren(frequency_start, frequency_end, total_duration, steps, iterations):
43+
siren = []
44+
mid_point = steps // 2
45+
duration_rise = total_duration // 2
46+
duration_fall = total_duration // 2
47+
48+
for _ in range(iterations):
49+
for i in range(steps):
50+
if i < mid_point:
51+
# Easing in rising part
52+
step_duration = duration_rise // mid_point + (duration_rise // mid_point * (mid_point - i) // mid_point)
53+
frequency = int(frequency_start + (frequency_end - frequency_start) * (i / mid_point))
54+
else:
55+
# Easing in falling part
56+
step_duration = duration_fall // mid_point + (duration_fall // mid_point * (i - mid_point) // mid_point)
57+
frequency = int(frequency_end - (frequency_end - frequency_start) * ((i - mid_point) / mid_point))
58+
59+
siren.append((frequency, step_duration))
60+
61+
return siren
62+
63+
# 4 seconds up and down siren, with 200 steps and 2 iterations
64+
siren_melody = generate_siren(440, 880, 4000, 200, 2)
65+
66+
for note, duration in siren_melody:
3467
buzzer.tone(note, duration, blocking=True)

0 commit comments

Comments
 (0)