Automated Piano Playing with Python: Multithreaded Keyboard Simulation
This tutorial demonstrates how to use Python to automatically play piano pieces by simulating keyboard presses on an online piano, detailing a four‑step implementation that includes a play function, multithreaded melody threads, sheet‑music definition, and browser automation to launch the autopiano webpage.
The article presents a method to automatically perform piano music using Python by controlling keyboard keys on the autopiano web page, allowing users to simulate finger movements and play complete songs.
Implementation steps (four parts):
Create a play function that maps musical symbols to keyboard actions and handles timing.
Use multithreading to run separate melody threads for main melody, chords, and individual fingers.
Define the piano score strings for right and left hands, including thumb and index‑finger parts.
Open the autopiano webpage via Chrome and start the threads to simulate playing.
Play function code:
def play_piano(music, keytime):
for n in music:
if n.isupper():
keyboard.press(Key.shift)
time.sleep(0.001)
keyboard.press(n.lower())
time.sleep(keytime - 0.001)
keyboard.release(n.lower())
keyboard.release(Key.shift)
elif n == "|" or n == ")":
pass
elif n in "!@$%^*(":
keyboard.press(Key.shift)
time.sleep(0.001)
keyboard.press("1245689"["!@$%^*(".index(n)])
time.sleep(keytime - 0.001)
keyboard.release("1245689"["!@$%^*(".index(n)])
keyboard.release(Key.shift)
elif n != " " and n != "-":
keyboard.press(n)
if music.index(n) != len(music) - 1 and music[music.index(n) + 1] == ")":
time.sleep(keytime / 2)
else:
time.sleep(keytime)
keyboard.release(n)
elif n == "-":
time.sleep(2 * keytime)
else:
time.sleep(keytime)Sample melody thread definitions (excerpt):
right = "s-as f |a --u |p -ops |" \
"o --uu|i-uis-|u - sss|a-Ii a |" \
"a --|"
left = "etu --|0wr --|qet --|" \
"80w --|9qe --|80w --|7Qr --|" \
"370Wr |"
thread_play(play_piano, 0.3, right, left)
# Additional variations with thumb and index‑finger strings omitted for brevityBrowser automation and execution entry point:
if __name__ == '__main__':
# Prepare keyboard controller
keyboard = Controller()
chromePath = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
webbrowser.register('chrome', None, webbrowser.BackgroundBrowser(chromePath))
webbrowser.get('chrome').open('https://www.autopiano.cn', new=1, autoraise=True)
# Start playing threads (examples shown above)By adjusting the score strings, users can adapt this framework to play different piano pieces automatically.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.