-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
103 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import re | ||
|
|
||
|
|
||
| class RTTTLPlayer: | ||
| NOTE_FREQS = { | ||
| 'c': 16.35, 'c#': 17.32, 'd': 18.35, 'd#': 19.45, | ||
| 'e': 20.60, 'f': 21.83, 'f#': 23.12, 'g': 24.50, | ||
| 'g#': 25.96, 'a': 27.50, 'a#': 29.14, 'b': 30.87 | ||
| } | ||
|
|
||
| def __init__(self, ev3, rtttl: str): | ||
| """ | ||
| :param ev3: EV3Brick instance from pybricks.hubs | ||
| :param rtttl: RTTTL string (name:d=4,o=5,b=100:notes) | ||
| """ | ||
| self.ev3 = ev3 | ||
| self.name, self.defaults, self.notes = self.parse_rtttl(rtttl) | ||
|
|
||
| def parse_rtttl(self, rtttl: str): | ||
| name, settings, notes = rtttl.split(':') | ||
| defaults = {} | ||
| for s in settings.split(','): | ||
| k, v = s.split('=') | ||
| defaults[k] = int(v) | ||
| notes = notes.split(',') | ||
| return name, defaults, notes | ||
|
|
||
| def note_to_freq(self, note: str, octave: int): | ||
| """ | ||
| Convert note name + octave to frequency in Hz. | ||
| """ | ||
| base = self.NOTE_FREQS[note] | ||
| return round(base * (2 ** octave), 2) | ||
|
|
||
| def play(self, tempo_scale: float = 1.0): | ||
| """ | ||
| Play the parsed RTTTL melody. | ||
| :param tempo_scale: Scale factor for speed (1.0 = normal, 2.0 = twice as fast, 0.5 = half speed) | ||
| """ | ||
| bpm = self.defaults.get('b', 63) | ||
| whole_note = (60 / bpm) * 4 * 1000 # ms | ||
|
|
||
| for raw in self.notes: | ||
| match = re.match(r'(\d+)?([a-gp][#]?)(\d)?(\.*)?', raw.strip(), re.I) | ||
| if not match: | ||
| continue | ||
|
|
||
| dur, note, octave, dot = match.groups() | ||
| dur = int(dur) if dur else self.defaults.get('d', 4) | ||
| octave = int(octave) if octave else self.defaults.get('o', 6) | ||
|
|
||
| duration = (whole_note / dur) * tempo_scale | ||
| if dot: | ||
| duration *= 1.5 | ||
|
|
||
| if note.lower() == 'p': # pause | ||
| self.ev3.speaker.beep(0, int(duration)) | ||
| else: | ||
| freq = self.note_to_freq(note.lower(), octave) | ||
| self.ev3.speaker.beep(int(freq), int(duration)) |