Using Python to make an automated piano playing script, I actually popped up "City in the Sky"!

foreword

When I was a child, I always had a dream of becoming a pianist. Recently, I saw an open-source piano performance page , autopiano , on the Internet, which can support keyboard keys and mouse clicks.

Suddenly, I have an idea, can I play a beautiful piano song with Python automation script? Today, I will show you how to use Python to automate and pop up a "City in the Sky"!

First, let's take a look at the final performance effect:
insert image description here
let's start to introduce how to implement this automatic piano playing script.

Skip directly to the end of the article to get exclusive fan benefits.

1. Core function design

The overall implementation is relatively simple, mainly divided into the following four steps:

  • Implement the performance function to simulate playing the piano through fingers and time intervals
  • Add individual playing melody threads to simulate the effect of two-handed playing through multi-threading
  • Determine the piano score to be played, confirm the main melody, chords, right thumb, right index finger, left thumb, left index finger
  • Automatically switch to open the keyboard piano (auto piano) webpage, and realize the simulation performance function through keyboard typing

2. Implementation steps

1. Play function

We first need to simulate the pause interval of each key when playing, realize keyboard control, and realize the performance of each live finger through the incoming musical notes and time. The core code is as follows:

# author:Dragon少年
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 playing melody multithreading

Because when the whole piece of music is played, sometimes both hands are required to control the performance at the same time, so we need to simulate the main melody, chords, right thumb, right index finger, left thumb, and left index finger through threads. play function. The core code is as follows:

# author:Dragon少年
def thread_play(play_piano, keytime, right="", left="", rightThumb="", rightIndexFinger="", leftThumb="",
                leftIndexFinger=""):
    # 运行线程
    rt = threading.Thread(target=play_piano, args=(right, keytime))  # 主旋律线程
    lt = threading.Thread(target=play_piano, args=(left, keytime))  # 和弦线程
    rtt = threading.Thread(target=play_piano, args=(rightThumb, keytime))  # 右手拇指线程
    rift = threading.Thread(target=play_piano, args=(rightIndexFinger, keytime))  # 右手食指线程
    ltt = threading.Thread(target=play_piano, args=(leftThumb, keytime))  # 左手拇指线程
    lift = threading.Thread(target=play_piano, args=(leftIndexFinger, keytime))  # 右手食指线程

3. Play the score with your fingers

If you play the piano with both hands, you need to confirm the rhythm and content of each finger in the whole music. We need to simulate the playing music required for each stage and spell out the whole piece of music. Let's take "City in the Sky" as an example, the core code is as follows:

# author:Dragon少年
# 右手
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)
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 --|"
# 演奏线程
thread_play(play_piano, 0.25, right, left)
# 右手
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 = "80w --|9qe --|680 --|9ey 0 -|" \
       "e ---|89w -t |579 --|60e -t |" \
       "370 w -|q -q -|0 ---|9 ---|"
# 右拇指
rightThumb = "----|----|----|--W -|" \
             "s ---|----|----|----|" \
             "----|----|----|g f d s |"
# 右食指
rightIndexFinger = "----|----|----|--r -|" \
                   "u ---|----|----|----|" \
                   "----|e -r -|w ---|e ---|"
# 左拇指
leftThumb = "----|----|----|----|" \
            "----|----|----|----|" \
            "----|t -y -|t ---|t ---|"
# 演奏线程
thread_play(play_piano, 0.25, right, left, rightThumb, rightIndexFinger, leftThumb)
# 右手
right = "a --f |j -h -|fds -s |d-sd h |" \
        "f --f |j -h -|"
# 左手
left = "3 %70Wru|60e 37w |48qer w |59q e t |" \
       "80wty -|60e 37w |"
# 右手拇指
rightThumb = "f ---|----|----|----|" \
             "----|----|"
# 右食指
rightIndexFinger = "----|----|----|----|" \
                   "----|----|"
# 左拇指
leftThumb = "----|----|----|----|" \
            "----|----|"
# 演奏线程
thread_play(play_piano, 0.25, right, left, rightThumb, rightIndexFinger, leftThumb)
# 右手
right = "fds -s |d-sd a |u --op|"
# 左手
left = "48qer w |7 -7 % |6 ---|"
# 右拇指
rightThumb = "----|9 ---|8"
# 右食指
rightIndexFinger = "----|q ---|0"
# 左拇指
leftThumb = "----|----|p"
# 多线程模拟手指弹琴,按键时间为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 only need to open the piano auto piano webpage, ( note: the blogger here is the browser window opened directly through the desktop taskbar program, so you need to open the free piano webpage manually in the browser ) By controlling the keyboard keys, the finger-playing score thread is realized , you can simulate playing the whole music. The core code is as follows:

# author:Dragon少年
# 控制键盘键入
keyboard = Controller()
# 切换到键盘钢琴网页端
keyboard.press(Key.cmd)
# 延时
time.sleep(1)
keyboard.press("d")
keyboard.release("d")
keyboard.release(Key.cmd)
# 链接的方式点击桌面任务栏的正在运行程序print_control_identifiers()
dlg = Desktop(backend="uia").任务栏.运行中的程序.child_window(title="Google Chrome - 1 个运行窗口", auto_id="Chrome",
                                                     control_type="Button").click()
time.sleep(2)
keyboard.press(Key.f11)
keyboard.release(Key.f11)

At this point, the automatic piano playing has been completed. Of course, if you need to perform other piano performances, you only need to read the sheet music, and modify the finger playing threads at each stage according to the sheet music, and you can realize the piano performance of different songs!

Finally, let's enjoy and appreciate the effect of the piano song "City in the Sky" played by Python!

The source code and data have been uploaded, pay attention to the public account at the end of the article and reply to [source code] to get the complete source code

Python's past highlights:

Guess you like

Origin blog.csdn.net/hhladminhhl/article/details/120148627