使用mido库处理音频

  1. 打开MIDI文件并解析其内容
import mido
midi = mido.MidiFile('example.mid')
for message in midi.play():
    print(message)
  1. 创建新的MIDI文件并保存
import mido
midi = mido.MidiFile()
track = mido.MidiTrack()
midi.tracks.append(track)
track.append(mido.Message('note_on', note=60, velocity=64, time=0))
track.append(mido.Message('note_off', note=60, velocity=64, time=200))
midi.save('new_file.mid')
  1. 发送MIDI消息到MIDI设备
import mido
output = mido.open_output('My MIDI Device')
output.send(mido.Message('note_on', note=60, velocity=64))
output.close()
  1. 接收来自MIDI设备的MIDI消息
import mido
input = mido.open_input('My MIDI Device')
for message in input:
    print(message)
  1. 将MIDI消息转换为音符和时间
import mido
message = mido.Message('note_on', note=60, velocity=64, time=100)
note = message.note
velocity = message.velocity
time = message.time
  1. 将音符和时间转换为MIDI消息
import mido
note = 60
velocity = 64
time = 100
message = mido.Message('note_on', note=note, velocity=velocity, time=time)
  1. 设置MIDI消息的通道
import mido
message = mido.Message('note_on', note=60, velocity=64, time=0, channel=1)
  1. 查找MIDI设备的输入和输出端口名称
import mido
input_names = mido.get_input_names()
output_names = mido.get_output_names()
print(f'Input Names: {input_names}')
print(f'Output Names: {output_names}')
  1. 将MIDI节拍转换为时间单位
import mido
ticks_per_beat = 480
beats_per_minute = 120
time_units = mido.tick2second(ticks=480, tick_relative=480, tempo=500000)
  1. 将时间单位转换为MIDI节拍
import mido
ticks_per_beat = 480
beats_per_minute = 120
ticks = mido.second2tick(seconds=1, ticks_per_beat=480, tempo=500000)

已发布

分类

标签:

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注