preface
As a child, I always had a dream of becoming a pianist. Recently, I saw an open-source piano performance web page on the Internet autopiano , it can support keyboard key playing and mouse click playing.
Suddenly had an idea, can you play a wonderful piano music with Python automated script? Today, let's show you how to realize automation with Python and pop up a song "city of the sky"!
First, let's take a look at the final performance effect:
Next, we will begin to introduce how to realize this automatic piano playing script.
1, Core function design
The overall implementation is relatively simple, which is mainly divided into the following four steps:
- Realize the performance function and simulate playing the piano through fingers and time interval
- Add each melody thread to simulate the performance effect of two hands through multithreading
- Determine the piano score to be played, and confirm the main melody, chord, right thumb, right index finger, left thumb and left index finger
- Automatically switch to open the auto piano web page, and realize the simulation performance function through keyboard typing
2, Implementation steps
1. Performance function
First, we need to simulate the pause interval of each key during performance, realize keyboard control, and realize the performance of each live finger through the incoming music symbol and time. The core code is as follows:
# author: Dragon junior 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)
2. Add melody multithreading
When playing the whole music, sometimes both hands need to control the performance at the same time, so we need to simulate the performance effects of the main melody, chord, right thumb, right index finger, left thumb and left index finger through threads, and start the above performance functions through multiple threads. The core code is as follows:
# author: Dragon junior def thread_play(play_piano, keytime, right="", left="", rightThumb="", rightIndexFinger="", leftThumb="", leftIndexFinger=""): # Run thread rt = threading.Thread(target=play_piano, args=(right, keytime)) # Theme thread lt = threading.Thread(target=play_piano, args=(left, keytime)) # Chord thread rtt = threading.Thread(target=play_piano, args=(rightThumb, keytime)) # Right thumb thread rift = threading.Thread(target=play_piano, args=(rightIndexFinger, keytime)) # Right index finger thread ltt = threading.Thread(target=play_piano, args=(leftThumb, keytime)) # Left thumb thread lift = threading.Thread(target=play_piano, args=(leftIndexFinger, keytime)) # Right index finger thread
3. Finger playing music
Playing the piano with both hands requires confirmation of the rhythm and content of each finger in the whole music. We need to simulate the playing music needed at each stage and spell out the whole music. Take the city of the sky as an example. The core code is as follows:
# author: Dragon junior # one 's right hand right = "s-as f |a --u |p -ops |" \ "o --uu|i-uis-|u - sss|a-Ii a |" \ "a --|" # left hand left = "etu --|0wr --|qet --|" \ "80w --|9qe --|80w --|7Qr --|" \ "370Wr |" # Playing thread thread_play(play_piano, 0.3, right, left) right = "---op|s-as f |a --u |p -ops |" \ "o --uu|i-uis-|u - sss|a-Ii a |" \ "a --pa|s-as f |a --u |p -ops |" left = "----|etu --|0wr --|qet --|" \ "80w --|9qe --|80w --|7Qr --|" \ "370Wr u |etu --|0wr --|qet --|" # Playing thread thread_play(play_piano, 0.25, right, left) # one 's right hand right = "o --uu|i sa-s |d fs--|sap a O |" \ "p --sd|f-df h |d --o |s-as f |" \ "f --oo|pas asd |s-oo- |d s a p |" # left hand left = "80w --|9qe --|680 --|9ey 0 -|" \ "e ---|89w -t |579 --|60e -t |" \ "370 w -|q -q -|0 ---|9 ---|" # Right thumb rightThumb = "----|----|----|--W -|" \ "s ---|----|----|----|" \ "----|----|----|g f d s |" # Right index finger rightIndexFinger = "----|----|----|--r -|" \ "u ---|----|----|----|" \ "----|e -r -|w ---|e ---|" # thumb leftThumb = "----|----|----|----|" \ "----|----|----|----|" \ "----|t -y -|t ---|t ---|" # Playing thread thread_play(play_piano, 0.25, right, left, rightThumb, rightIndexFinger, leftThumb) # one 's right hand right = "a --f |j -h -|fds -s |d-sd h |" \ "f --f |j -h -|" # left hand left = "3 %70Wru|60e 37w |48qer w |59q e t |" \ "80wty -|60e 37w |" # right thumb rightThumb = "f ---|----|----|----|" \ "----|----|" # Right index finger rightIndexFinger = "----|----|----|----|" \ "----|----|" # thumb leftThumb = "----|----|----|----|" \ "----|----|" # Playing thread thread_play(play_piano, 0.25, right, left, rightThumb, rightIndexFinger, leftThumb) # one 's right hand right = "fds -s |d-sd a |u --op|" # left hand left = "48qer w |7 -7 % |6 ---|" # Right thumb rightThumb = "----|9 ---|8" # Right index finger rightIndexFinger = "----|q ---|0" # thumb leftThumb = "----|----|p" # Multithreaded analog finger playing piano, the key pressing time is 0.3s thread_play(play_piano, 0.3, right, left, rightThumb, rightIndexFinger, leftThumb) 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.4, right, left)
4. Piano simulation performance
Finally, we just need to turn on the piano auto piano Web page, (Note: the blogger here is a browser window opened directly through the desktop taskbar program, so you need to manually open the free piano web page) by controlling the keyboard keys to realize the thread of finger playing music score, you can simulate playing the whole music. The core code is as follows:
# author: Dragon junior # Control keyboard typing keyboard = Controller() # Switch to keyboard and Piano web page keyboard.press(Key.cmd) # delayed time.sleep(1) keyboard.press("d") keyboard.release("d") keyboard.release(Key.cmd) # Click the running program print in the desktop taskbar by linking_ control_ identifiers() dlg = Desktop(backend="uia").taskbar.Running program.child_window(title="Google Chrome - 1 Run windows", auto_id="Chrome", control_type="Button").click() time.sleep(2) keyboard.press(Key.f11) keyboard.release(Key.f11)
So far, the automatic piano playing has been completed. Of course, if you need to perform other piano performances, you only need to read the music score and modify the finger playing threads at each stage according to the music score, you can perform the piano of different music!
Finally, let's enjoy the piano music "city of the sky" played by Python. What's the effect!
If the content of this article is helpful to you, please like it three times, pay attention and collect it with support.
Creation is not easy, white whoring is not good. Your support and recognition is the biggest driving force for my creation. See you in the next article!
Dragon youth
If there are any mistakes in this blog, please comment and advice. Thank you very much!