Forum Archive

Learn Piano Notes With Tempo Setting

ramvee

Hi Friends,
After a long time, I was writing code in pythonista, to help learn, playing piano notes.
I am sure this may not be the optimal solutiion. But it works, and I am always ready to
learn from the experts here. Thank You!

# to learn playing piano keys from phone display
# not scaled for ipad
import ui
import sound
import time
from random import choice
from scripter import script, start_scripter

td=1 # default time delay 1 sec

# piano keys from A3 to G4# are Available with following names (21 keys)
piano_klist = [
        "piano:C3", "piano:C3#", 
        "piano:D3", "piano:D3#", "piano:E3", 
        "piano:F3", "piano:F3#", 
        "piano:G3", "piano:G3#", 
        "piano:A3", "piano:A3#", "piano:B3",
        "piano:C4", "piano:C4#", 
        "piano:D4", "piano:D4#", "piano:E4", 
        "piano:F4", "piano:F4#", 
        "piano:G4", "piano:G4#"
        ]

# to load sound effects for minimum latency
for key_name in piano_klist:
    sound.load_effect(key_name)

def slider_action(sender):
    global td
    # to set slider value reversed, so that
    # bpm can go up sliding towards right
    td = round(((1-sender.value)*1.75)+0.2, 2)
    bpm=int (60/td)
    lb.text= str(bpm) + ' BPM'
    # I put this here hoping it will avoid keyboard popping up
    # and covering the display area
    ui.end_editing()

# very important to update label with name of music note
@script
def rnd_note(sender):
    # set to practice for 12 notes at a time
    repeat = 12
    for note in range(repeat):
        key_name = choice(piano_klist)
        time.sleep(td)
        lb1.text = key_name[6:]
        sound.play_effect(key_name)
        yield
    time.sleep(td)
    lb1.text = '♫'
    lb.text = 'Tempo Slider'

w,h = ui.get_screen_size()
# if in landscape mode
if w>h:
    w,h=h,w
gap = 10

v = ui.View(name='Piano Practice', bg_color = 'lightyellow')
v.frame = (gap,gap,w,h)
start_scripter(v)

lb = ui.Label(name = 'mylabel')
lb.frame=(gap*2, 10, w-4*gap, 30)
lb.background_color='gray'
lb.text_color = 'yellow'
lb.font = ('Helvetica', 18)
lb.text = 'Set Tempo BPM'
lb.alignment =1
lb.flex = 'W'

slider = ui.Slider()
slider.value=0.5
slider.frame=(gap*2,gap+32,w-4*gap,50)
slider.background_color='gray'
slider.flex = 'W'
slider.action = slider_action

lb1 = ui.Label(name = 'note')
lb1.frame= (gap*2, gap+100, w-4*gap, h-475)
lb1.font = ('Helvetica', 150 )
lb1.text_color = 'yellow'
lb1.alignment =1
lb1.background_color = 'gray'
lb1.text = '♫'
lb1.flex = 'W'

button = ui.Button(name='start')
button.frame = (gap*2, 300, w-4*gap, 40)
button.bg_color='yellow'
button.border_width=1
button.title='Start'
button.font = ('Helvetica', 18)
button.flex = 'W'
button.action=rnd_note

v.add_subview(slider)
v.add_subview(lb)
v.add_subview(lb1)
v.add_subview(button)

v.present('sheet')
ccc
keys = "C3 C3# D3 D3# E3 F3 F3# G3 G3# A3 A3# B3 C4 C4# D4 D4# E4 F4 F4# G4 G4#"
piano_klist = [f"piano:{key}" for key in keys.split()]
ramvee

Thank You @ccc .

You're always there :)
And I'm sure, you can reduce this code by half !