用python弹奏钢琴,以鸟之诗为例

代码如下

import ctypes
import threading
import time

winmm = ctypes.windll.winmm
#ctypes.windll.winmm 表示加载名为 winmm.dll 的动态链接库,并返回一个可以调用该库中函数的对象。
#定义音域
class Scale:
    Rest = 0
    C8 = 108
    B7 = 107
    A7s = 106
    A7 = 105
    G7s = 104
    G7 = 103
    F7s = 102
    F7 = 101
    E7 = 100
    D7s = 99
    D7 = 98
    C7s = 97
    C7 = 96
    B6 = 95
    A6s = 94
    A6 = 93
    G6s = 92
    G6 = 91
    F6s = 90
    F6 = 89
    E6 = 88
    D6s = 87
    D6 = 86
    C6s = 85
    C6 = 84
    B5 = 83
    A5s = 82
    A5 = 81
    G5s = 80
    G5 = 79
    F5s = 78
    F5 = 77
    E5 = 76
    D5s = 75
    D5 = 74
    C5s = 73
    C5 = 72
    B4 = 71
    A4s = 70
    A4 = 69
    G4s = 68
    G4 = 67
    F4s = 66
    F4 = 65
    E4 = 64
    D4s = 63
    D4 = 62
    C4s = 61
    C4 = 60
    B3 = 59
    A3s = 58
    A3 = 57
    G3s = 56
    G3 = 55
    F3s = 54
    F3 = 53
    E3 = 52
    D3s = 51
    D3 = 50
    C3s = 49
    C3 = 48
    B2 = 47
    A2s = 46
    A2 = 45
    G2s = 44
    G2 = 43
    F2s = 42
    F2 = 41
    E2 = 40
    D2s = 39
    D2 = 38
    C2s = 37
    C2 = 36
    B1 = 35
    A1s = 34
    A1 = 33
    G1s = 32
    G1 = 31
    F1s = 30
    F1 = 29
    E1 = 28
    D1s = 27
    D1 = 26
    C1s = 25
    C1 = 24
    B0 = 23
    A0s = 22
    A0 = 21

#定义按键,选你需要的
class Voice:
    X1 = Scale.C2
    X2 = Scale.D2
    X3 = Scale.E2
    X4 = Scale.F2
    X5 = Scale.G2
    X6 = Scale.A2
    X7 = Scale.B2
    L1 = Scale.C3
    L2 = Scale.D3
    L3 = Scale.E3
    L4 = Scale.F3
    L5 = Scale.G3
    L6 = Scale.A3
    L7 = Scale.B3
    M1 = Scale.C4
    MS1 = Scale.C4s
    M2 = Scale.D4
    M3 = Scale.E4
    M4 = Scale.F4
    MS4 = Scale.F4s
    M5 = Scale.G4
    M6 = Scale.A4
    M7 = Scale.B4
    H1 = Scale.C5
    HS1 = Scale.C5s
    H2 = Scale.D5
    H3 = Scale.E5
    H4 = Scale.F5
    HS4 = Scale.F5s
    H5 = Scale.G5
    H6 = Scale.A5
    H7 = Scale.B5
    HR1 = Scale.C6
    HRS1 = Scale.C6s
    HR2 = Scale.D6
    HR3 = Scale.E6
    HR4 = Scale.F6
    HRS4 = Scale.F6s
    HR5 = Scale.G6
    HR6 = Scale.A6
    HR7 = Scale.B6
    LOW_SPEED = 500
    MIDDLE_SPEED = 400
    HIGH_SPEED = 300
    _ = 0xFF


def playNote(hmo, channel, instrument, note, velocity, duration=0):
    # 设置乐器
    msg = 0xC0 | channel | (instrument << 8)
    winmm.midiOutShortMsg(hmo, msg)

    # 发送 Note On 消息
    msg = 0x90 | channel | (note << 8) | (velocity << 16)
    winmm.midiOutShortMsg(hmo, msg)

    if duration:
        # 暂停 duration 秒钟
        time.sleep(duration)

        # 发送 Note Off 消息
        msg = 0x80 | channel | (note << 8) | (velocity << 16)
        winmm.midiOutShortMsg(hmo, msg)

    return channel | (instrument << 8) | (note << 8) | (velocity << 16) + 0x90

#主旋律
def go(handle, instrument, velocity):
    wind = [400,300,Voice.H6,Voice.H7,Voice.HR2,700,Voice._,800,Voice._,700,Voice.HRS1, Voice._,300,Voice.HRS1, Voice.H6, Voice.H7,Voice.HR2,600,700,Voice.HR3,Voice._,300,Voice.HR3,Voice.HR3,Voice.H6,Voice.H7,Voice.HR2,
            600,700,Voice.HR6,Voice._,Voice._,300,Voice.HR6,Voice.HR5,600,700,Voice.HRS4,Voice.HRS4,300,Voice.HRS1,Voice.H6,Voice.H7,Voice.HR2,
            600,700,Voice.HRS1, Voice._,300,Voice.HRS1, Voice.H6, Voice.H7,Voice.HR2,600,700,Voice.HR3,Voice._,300,Voice.HR3,Voice.HS4,Voice.H6,Voice.HRS1,600,700,Voice.H7,Voice._,Voice._,Voice._,600,700,800,Voice.HR7,700,Voice.M7,300,Voice.HS1,Voice.H2,Voice.H6,600,700,Voice.HS4,300,Voice.HS4,150,Voice.H3,Voice.HS4,700,Voice._,Voice._,
            600,700,Voice.HS4,300,Voice.H3,Voice.HS4,Voice.H6,Voice.H2,Voice.HS1,Voice.H2,600,700,Voice.HS1,300,Voice.HS1,150,Voice.M7,Voice.MS4,700,Voice._,Voice._,600,700,800,Voice.MS4,300,Voice.M7,Voice.M7,Voice.HS1,Voice.H2,Voice.H6,600,700,Voice.HS4,300,Voice.HS4,150,Voice.H3,Voice.HS4,700,Voice._,Voice._, 600,700,Voice.HS4,300,Voice.H3,Voice.HS4,Voice.H6,Voice.HS4,Voice.H6,Voice.HR2,
            600,700,300,800,Voice.HRS1,Voice.H7,300,Voice.HS4,700,800,Voice._,300,Voice.H3,600,700,Voice.HS4,Voice.H6,Voice.H7,Voice.HRS1,600,700,Voice.HS4,300,Voice.HS4,150,Voice.H3,Voice.HS4,700,Voice._,Voice._,600,700,Voice.HS4,300,Voice.H3,Voice.HS4,Voice.H6,Voice.H2,Voice.HS1,Voice.H2,
            600,700,Voice.HS1,300,Voice.HS1,150,Voice.M7,Voice.MS4,700,Voice._,Voice._,600,700,Voice.MS4,300,0,700,Voice.M7,300,Voice.HS1,Voice.H2,Voice.H6,600,700,Voice.HS4,300,Voice.HS4,150,Voice.H3,Voice.HS4,700,Voice._,Voice._, 600,700,Voice.HS4,300,Voice.H3,Voice.HS4,Voice.H6,Voice.HS4,Voice.H6,Voice.HR2,
            -1
            ]

    sleep = 0.5

    for i in wind:
        if i == -1:
            break
        if i == 0:
            time.sleep(0.25)
            continue
        if i == 600:
            time.sleep(0.01)
            continue
        if i == 150:
            sleep = 0.125
            continue
        if i == 700:
            sleep = 0.5
            continue
        if i == 800:
            sleep = sleep + sleep / 2
            continue
        if i == 300:
            sleep = 0.25
            continue
        if i == Voice._:
            time.sleep(0.5)
            continue
        back = playNote(handle, 0, instrument, i, velocity, sleep)
        print(back, end="\n")
        # time.sleep(sleep)
#伴奏
def go_right(handle, instrument, velocity):
    wind = [
        400,700,0,0,0,0,600,700,800,Voice.L5,300,Voice.M2,Voice.MS4,Voice.M5,Voice.M7,Voice.H2,600,700,800,Voice.M6,300,Voice.M3,700,Voice.M6,Voice._,
        600,700,800,Voice.M7,700,Voice.M7,300,Voice.L7,Voice.MS4,Voice.M7,600,700,800,Voice.M7,700,Voice.M7,300,Voice.M7,Voice.H2,Voice.HS4,
        600,700,800,Voice.L5,300,Voice.M2,Voice.MS4,Voice.M5,Voice.M7,Voice.H2,600,700,800,Voice.M6,300,Voice.M3,700,Voice.HS1,Voice._,600,700,300,Voice.L7,Voice.MS4,Voice.M7,Voice.HS1,Voice.H2,Voice.HS4,Voice.H7,Voice.HR2,600,700,0,0,0,0,600,700,Voice.L5,Voice._,Voice._,Voice._,
        600,700,Voice.L6,Voice._,Voice._,Voice._,600,700,Voice.L7,Voice._,Voice._,Voice._,600,700,Voice.L7,Voice._,Voice._,Voice.L6,600,700,300,Voice.L5,Voice.L7,700,Voice.M2,Voice._,Voice.L5,600,700,Voice.L6,Voice._,Voice._,300,Voice.MS4,Voice.M6,
        600,700,300,Voice.L7,Voice.M2,Voice.MS4,700,800,Voice.M7,700,Voice._,600,700,300,Voice.L7,Voice.M2,Voice.MS4,700,800,Voice.M6,700,Voice.L6,600,700,300,Voice.L5,Voice.L7,700,Voice.M2,Voice._,Voice.L5,600,700,Voice.L6,Voice._,Voice._,Voice._,
        600,700,Voice.L7,Voice._,Voice._,Voice._,600,700,300,Voice.L7,Voice.M2,700,800,Voice.MS4,300,Voice.MS1,Voice.L7,Voice.L6,600,700,300,Voice.L5,Voice.L7,700,Voice.M2,Voice.L5,600,700,Voice.L6,Voice._,Voice._,Voice._,
        -1
            ]

    sleep = 0.5

    for i in wind:
        if i == -1:
            break
        if i == 0:
            time.sleep(0.5)
            continue
        if i == 800:
            sleep = sleep + sleep/2
            continue
        if i == 600:
            time.sleep(0.01)
            continue
        if i == 150:
            sleep = 0.125
            continue
        if i == 700:
            sleep = 0.5
            continue
        if i == 300:
            sleep = 0.25
            continue
        if i == Voice._:
            time.sleep(0.5)
            continue
        back = playNote(handle, 0, instrument, i, velocity, sleep)
        print(back, end="\n")

# MIDI_MAPPER 常量
MIDI_MAPPER = 0xFFFFFFFF

# 创建一个变量来接收 MIDI 输出句柄
hMidiOut = ctypes.c_void_p()

# 调用 midiOutOpen 函数
result = winmm.midiOutOpen(ctypes.byref(hMidiOut), MIDI_MAPPER, 0, 0, 0)
if result == 0:
    print("midiOutOpen succeeded")
    #go(hMidiOut,107)
    thread1 = threading.Thread(target=go, args=(hMidiOut, 0, 127))
    thread2 = threading.Thread(target=go_right, args=(hMidiOut, 0, 127))
    thread1.start()
    thread2.start()
else:
    print("midiOutOpen failed")
thread1.join()
thread2.join()
# 关闭 MIDI 输出
winmm.midiOutClose(hMidiOut)

参考资料

Python弹奏起风了钢琴曲,带你回到2018年的夏天

midiOutShortMsg 函数的用法以及乐器编号-CSDN博客

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值