Skip to content
Snippets Groups Projects
Commit 42228b28 authored by cf2g21's avatar cf2g21
Browse files

Upload solution.py

parents
No related branches found
No related tags found
No related merge requests found
from machine import Pin, PWM
import time
BUZZER_PIN = 18
# Note to frequency
note_freq = {
'B1': 31,
'C2': 33,
'CS2': 35,
'D2': 37,
'DS2': 39,
'E2': 41,
'F2': 44,
'FS2': 46,
'G2': 49,
'GS2': 52,
'A2': 55,
'AS2': 58,
'B2': 62,
'C3': 65,
'CS3': 69,
'D3': 73,
'DS3': 78,
'E3': 82,
'F3': 87,
'FS3': 93,
'G3': 98,
'GS3': 104,
'A3': 110,
'AS3': 117,
'B3': 123,
'C4': 131,
'CS4': 139,
'D4': 147,
'DS4': 156,
'E4': 165,
'F4': 175,
'FS4': 185,
'G4': 196,
'GS4': 208,
'A4': 220,
'AS4': 233,
'B4': 247,
'C5': 262,
'CS5': 277,
'D5': 294,
'DS5': 311,
'E5': 330,
'F5': 349,
'FS5': 370,
'G5': 392,
'GS5': 415,
'A5': 440,
'AS5': 466,
'B5': 494,
'C6': 523,
'CS6': 554,
'D6': 587,
'DS6': 622,
'E6': 659,
'F6': 698,
'FS6': 740,
'G6': 784,
'GS6': 831,
'A6': 880,
'AS6': 932,
'B6': 988,
'C7': 1047,
'CS7': 1109,
'D7': 1175,
'DS7': 1245,
'E7': 1319,
'F7': 1397,
'FS7': 1480,
'G7': 1568,
'GS7': 1661,
'A7': 1760,
'AS7': 1865,
'B7': 1976,
'C8': 2093,
'CS8': 2217,
'D8': 2349,
'DS8': 2489,
'E8': 2637,
'F8': 2794,
'FS8': 2960,
'G8': 3136,
'GS8': 3322,
'A8': 3520,
'AS8': 3729,
'B8': 3951,
'C9': 4186,
'CS9': 4435,
'D9': 4699,
'DS9': 4978,
'P': 0
}
def parseRTTTL(text):
try:
title, defaults, song = text.split(':')
d, o, b = defaults.split(',')
d = int(d.split('=')[1])
o = int(o.split('=')[1])
b = int(b.split('=')[1])
whole = (60000/b)*4
noteList = song.split(',')
except:
return 'Not a valid RTTTL string'
notes = 'abcdefgp'
freqLengthList = []
for note in noteList:
index = 0
for i in note:
# Find letter
if i in notes:
index = note.find(i)
break
length = note[0:index]
# Get duration specifier (up to letter exclusive)
value = note[index:].replace('#','s').replace('.','')
# Get bit after letter and replace sharps or dots for eval
if not any(char.isdigit() for char in value):
value += str(o)
# If no octave specifier then add default octave for eval
if 'p' in value:
value = 'p'
if length == '':
# If no duration specifier then use default duration
length = d
else:
length = int(length)
length = whole/length
if '.' in note:
length += length/2
freqLengthList.append((note_freq[value.upper()], length))
return freqLengthList
def play(text):
song = parseRTTTL(text)
if type(song) is not list:
return song
for freq, seconds in song:
milliseconds = seconds * 0.001
if freq > 0:
buzzer = PWM(Pin(BUZZER_PIN))
buzzer.freq(freq)
buzzer.duty_u16(512)
time.sleep(milliseconds*0.9)
if freq > 0:
buzzer.deinit()
# Stop buzzer after playing
time.sleep(milliseconds*0.1) # 0.1 second gap between notes
exampleSong = 'MissionImpossible:d=16,o=6,b=95:32d,32d#,32d,32d#,32d,32d#,32d,32d#,32d,32d,32d#,32e,32f,32f#,32g,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,a#,g,2d,32p,a#,g,2c#,32p,a#,g,2c,a#5,8c,2p,32p,a#5,g5,2f#,32p,a#5,g5,2f,32p,a#5,g5,2e,d#,8d'
play(exampleSong)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment