77 lines
1.4 KiB
Python
77 lines
1.4 KiB
Python
import time
|
|
from grove.gpio import GPIO
|
|
import RPi.GPIO as rGPIO
|
|
|
|
|
|
|
|
led = GPIO(5, GPIO.OUT)
|
|
button = GPIO(6, GPIO.IN)
|
|
|
|
# create PWM instance
|
|
pwmPin = 12
|
|
rGPIO.setmode(rGPIO.BCM)
|
|
rGPIO.setup(pwmPin, rGPIO.OUT)
|
|
pwm = rGPIO.PWM(pwmPin, 10)
|
|
|
|
|
|
pwm.start(0)
|
|
# 1000 Hz sound
|
|
pwm.ChangeFrequency(1000)
|
|
pwm.ChangeDutyCycle(100)
|
|
|
|
def setFreq(freq_hz: float):
|
|
"""
|
|
Replace this with your actual frequency-setting function.
|
|
freq_hz = 0 should silence the output.
|
|
"""
|
|
print(freq_hz)
|
|
pwm.ChangeFrequency(freq_hz)
|
|
pass
|
|
|
|
|
|
# --- Timing ---
|
|
BPM = 100
|
|
BEAT = 60 / BPM # seconds per quarter note
|
|
|
|
|
|
# --- Note frequencies (Hz) ---
|
|
# --- Note frequencies (Hz) ---
|
|
NOTES = {
|
|
"C4": 261.63,
|
|
"D4": 293.66,
|
|
"E4": 329.63,
|
|
"F4": 349.23,
|
|
"G4": 392.00,
|
|
"A4": 440.00,
|
|
"B4": 493.88,
|
|
"C5": 523.25,
|
|
"D5": 587.33,
|
|
"E5": 659.25,
|
|
"REST": 0
|
|
}
|
|
|
|
|
|
# --- Melody (note, beats) ---
|
|
# Based on the standard vocal melody of "Erika"
|
|
MELODY = [
|
|
("E4", 1), ("G4", 1), ("A4", 2),
|
|
("A4", 1), ("G4", 1), ("E4", 2),
|
|
|
|
("E4", 1), ("G4", 1), ("A4", 2),
|
|
("A4", 1), ("G4", 1), ("E4", 2),
|
|
|
|
("A4", 1), ("C5", 1), ("D5", 2),
|
|
("D5", 1), ("C5", 1), ("A4", 2),
|
|
|
|
("G4", 1), ("A4", 1), ("G4", 1), ("E4", 1),
|
|
("E4", 2), ("REST", 2),
|
|
]
|
|
|
|
|
|
# --- Playback ---
|
|
def play():
|
|
for note, beats in MELODY:
|
|
setFreq(NOTES[note])
|
|
time.sleep(beats * BEAT)
|
|
play()
|