# Name: Ivan Courrejolles
# Wokwi share link: https://wokwi.com/projects/479802867847546881
# ISM3230 - Lab 11: Functions
# Raspberry Pi Pico music player
import time
from Lights import Light
from Buzzer import PassiveBuzzer
from TerminalEmulator import TerminalEmulator
# Hardware setup: these pins match the earlier class Pico template.
time.sleep(0.1)
red = Light(15, "Red")
green = Light(13, "Green")
beeper = PassiveBuzzer(14, "Beeper")
term = TerminalEmulator()
red.off()
green.off()
# Step 4: Base, low, and high note dictionaries (frequencies in Hz).
basenotes = {
"Do": 523, "Re": 587, "Mi": 659, "Fa": 698,
"Sol": 784, "La": 880, "Ti": 988, "Rest": 0
}
lownotes = {
"Do": 262, "Re": 294, "Mi": 330, "Fa": 349,
"Sol": 392, "La": 440, "Ti": 494, "Rest": 0
}
highnotes = {
"Do": 1047, "Re": 1175, "Mi": 1319, "Fa": 1397,
"Sol": 1568, "La": 1760, "Ti": 1976, "Rest": 0
}
# Steps 5-6: Each entry is (note name, duration in ms, pause in ms).
# These two short verses demonstrate playing upward and downward.
upverse = [
("Do", 250, 250),
("Re", 250, 250),
("Mi", 250, 250)
]
reverse = [
("Ti", 250, 250),
("La", 250, 250),
("Sol", 250, 250)
]
# Step 11: A song is a list of verses. These six verses form
# Twinkle Twinkle Little Star. The same notes work for any register.
def make_verse(note_names):
return [(name, 250, 250) for name in note_names]
twinkle = [
make_verse(["Do", "Do", "Sol", "Sol", "La", "La", "Sol"]),
make_verse(["Fa", "Fa", "Mi", "Mi", "Re", "Re", "Do"]),
make_verse(["Sol", "Sol", "Fa", "Fa", "Mi", "Mi", "Re"]),
make_verse(["Sol", "Sol", "Fa", "Fa", "Mi", "Mi", "Re"]),
make_verse(["Do", "Do", "Sol", "Sol", "La", "La", "Sol"]),
make_verse(["Fa", "Fa", "Mi", "Mi", "Re", "Re", "Do"])
]
def play_note(note, length, pause, notes=basenotes, speed=1):
"""Play one note/rest and its pause; return elapsed milliseconds.
A speed of 2 plays twice as fast; 0.5 plays at half speed.
"""
if speed <= 0:
raise ValueError("Speed must be greater than zero")
note_ms = max(1, int(length / speed))
pause_ms = max(0, int(pause / speed))
frequency = notes[note]
if frequency == 0:
time.sleep_ms(note_ms)
else:
beeper.beep(tone=frequency, duration=note_ms)
time.sleep_ms(pause_ms)
return note_ms + pause_ms
# Steps 7-10 and 13: One reusable function plays every kind of verse.
# The notes parameter is optional: basenotes is the default.
def play_verse(verse, notes=basenotes, speed=1):
verse_length = 0
for note, length, pause in verse:
verse_length += play_note(note, length, pause, notes, speed)
return verse_length
# Steps 12-13: One function plays a complete list of verses and
# returns the total number of milliseconds played.
def play_song(song, notes=basenotes, speed=1):
song_length = 0
for verse in song:
song_length += play_verse(verse, notes, speed)
return song_length
# Steps 5-6: Play upverse and reverse using base and high notes.
# Steps 11-12: Then play Twinkle Twinkle in low, base and high notes.
# Step 13: Record total playback time and turn off the green LED.
total_ms = 0
green.on()
try:
term.print("Base: up and down")
total_ms += play_verse(upverse) # default: basenotes
total_ms += play_verse(reverse) # default: basenotes
term.print("High: up and down")
total_ms += play_verse(upverse, highnotes)
total_ms += play_verse(reverse, highnotes)
term.print("Twinkle: low")
total_ms += play_song(twinkle, lownotes)
term.print("Twinkle: base")
total_ms += play_song(twinkle) # default: basenotes
term.print("Twinkle: high")
total_ms += play_song(twinkle, highnotes)
# Optional speed example (uncomment to try a faster or slower song):
# total_ms += play_song(twinkle, basenotes, speed=2)
finally:
green.off()