MIDI code - by ChatGPT
import tkinter as tk from tkinter import ttk from mido import MidiFile, Message, MidiTrack
# --- 全局变量 ---
midi_file = MidiFile()
track = MidiTrack()
midi_file.tracks.append(track)
current_instrument = 0 # 默认乐器
# --- 函数 ---
def add_note():
pitch = pitch_slider.get()
duration = duration_slider.get()
velocity = velocity_slider.get() # 获取力度值
track.append(Message('program_change', program=current_instrument, time=0)) # 设置乐器
track.append(Message('note_on', note=pitch, velocity=velocity, time=0))
track.append(Message('note_off', note=pitch, velocity=velocity, time=duration))
def change_instrument(event):
global current_instrument
current_instrument = instrument_var.get()
def save_midi():
file_path = tk.filedialog.asksaveasfilename(defaultextension=".mid")
if file_path:
midi_file.save(file_path)
# --- GUI设置 ---
root = tk.Tk()
root.title("MIDI编曲器")
# 音高
pitch_label = ttk.Label(root, text="音高:")
pitch_label.grid(row=0, column=0)
pitch_slider = ttk.Scale(root, from_=0, to=127, orient="horizontal")
pitch_slider.grid(row=0, column=1)
# 时值
duration_label = ttk.Label(root, text="时值:")
duration_label.grid(row=1, column=0)
duration_slider = ttk.Scale(root, from_=1, to=127, orient="horizontal") # 时值至少为1
duration_slider.grid(row=1, column=1)
# 力度
velocity_label = ttk.Label(root, text="力度:")
velocity_label.grid(row=2, column=0)
velocity_slider = ttk.Scale(root, from_=0, to=127, orient="horizontal")
velocity_slider.grid(row=2, column=1)
# 乐器选择
instrument_label = ttk.Label(root, text="乐器:")
instrument_label.grid(row=3, column=0)
instrument_options = [i for i in range(128)] # 128种MIDI乐器
instrument_var = tk.IntVar(value=current_instrument)
instrument_dropdown = ttk.OptionMenu(root, instrument_var, *instrument_options, command=change_instrument)
instrument_dropdown.grid(row=3, column=1)
# 添加音符按钮
add_button = ttk.Button(root, text="添加音符", command=add_note)
add_button.grid(row=4, column=0, columnspan=2)
# 保存按钮
save_button = ttk.Button(root, text="保存MIDI", command=save_midi)
save_button.grid(row=5, column=0, columnspan=2)
root.mainloop()

还没有人发言,快来抢沙发!