MCPcopy Create free account
hub / github.com/RetiredWizard/PyDOS / piano

Function piano

piano.py:20–174  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

18 pass
19
20def piano():
21
22 noteMS = 500
23 if sys.implementation.name.upper() == 'MICROPYTHON':
24 oldPWM = False
25 try:
26 # Check if alternate PWM mode (nrf boards) is being used
27 pwm=machine.PWM(0,period=10000,pin=Pydos_hw.sndPin,freq=250,duty=0)
28 oldPWM = True
29 except:
30 pwm=machine.PWM(Pydos_hw.sndPin)
31 elif sys.implementation.name.upper() == 'CIRCUITPYTHON':
32 if Pydos_hw.i2sSCK:
33 noteMS = 375
34 i2s = audiobusio.I2SOut(Pydos_hw.i2sSCK,Pydos_hw.i2sWS,Pydos_hw.i2sDATA)
35 synth = synthio.Synthesizer(sample_rate=22050)
36 e = synthio.Envelope(attack_time=.0001,decay_time=.0001,release_time=0,attack_level=.5,sustain_level=.5)
37 i2s.play(synth)
38 else:
39 Pydos_hw.sndGPIO.deinit() # Workaround for ESP32-S2 GPIO issue
40 pwm=PWMOut(Pydos_hw.sndPin, duty_cycle=0, frequency=440, variable_frequency=True)
41
42 print ("\nPress +/- to change volume, 'q' to quit...")
43
44 volume = 400
45 lastVol = 400
46 cmnd = None
47 press = False
48 noneAt = ticks_ms()
49 firstNone = True
50 note = 0
51 rnote = 0.0
52 lastNote = -1
53 while cmnd != "q":
54 if Pydos_ui.serial_bytes_available() != 0:
55 cmnd = Pydos_ui.read_keyboard(1)
56 #print("->"+cmnd+"<- : ",ord(cmnd[0]))
57 firstNone = True
58 if cmnd=="h": # middle C
59 rnote=261.6256
60 press = True
61 elif cmnd == "a": # E
62 rnote=164.8138
63 press = True
64 elif cmnd == "s": # F
65 rnote=174.6141
66 press = True
67 elif cmnd == "d": # G
68 rnote=195.9977
69 press = True
70 elif cmnd == "r": # G sharp/A flat
71 rnote=207.6523
72 press = True
73 elif cmnd == "f": # A
74 rnote=220.0
75 press = True
76 elif cmnd == "t": # A sharp/B flat
77 rnote=233.0819

Callers 1

piano.pyFile · 0.85

Calls 4

quietSndFunction · 0.90
deinitMethod · 0.80
read_keyboardMethod · 0.45

Tested by

no test coverage detected