MIDI 单元
支持以下产品:
1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD 2# 3# SPDX-License-Identifier: MIT 4 5import os, sys, io 6import M5 7from M5 import * 8from unit import MIDIUnit 9import time 10 11 12title0 = None 13label0 = None 14midi_0 = None 15 16 17def setup(): 18 global title0, label0, midi_0 19 20 M5.begin() 21 Widgets.fillScreen(0x222222) 22 title0 = Widgets.Title("Unit MIDI Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18) 23 label0 = Widgets.Label("PLaying", 124, 101, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18) 24 25 Power.setExtOutput(True) 26 midi_0 = MIDIUnit(id=1, port=(18, 17)) 27 midi_0.set_channel_volume(0, 111) 28 Widgets.fillScreen(0xFFCC66) 29 midi_0.set_instrument(0, 0, 112) 30 31 32def loop(): 33 global title0, label0, midi_0 34 M5.update() 35 midi_0.set_note_on(0, 0, 127) 36 midi_0.set_note_on(0, 36, 127) 37 time.sleep_ms(300) 38 midi_0.set_note_on(0, 48, 127) 39 midi_0.set_note_on(0, 60, 127) 40 41 42if __name__ == "__main__": 43 try: 44 setup() 45 while True: 46 loop() 47 except (Exception, KeyboardInterrupt) as e: 48 try: 49 from utility import print_error_msg 50 51 print_error_msg(e) 52 except ImportError: 53 print("please update to latest firmware")
UiFlow2 应用示例
class MIDIUnit
Constructors
Methods
- MIDIUnit.set_note_on(channel, pitch, velocity)
向指定通道发送 MIDI Note On 消息。
- 参数:
channel – MIDI 通道(0-15)。
pitch – 音符音高(0-127)。
velocity – 音符力度(0-127)。
UiFlow2

- MIDIUnit.set_note_off(channel, pitch)
向指定通道发送 MIDI Note Off 消息。
- 参数:
channel – MIDI 通道(0-15)。
pitch – 音符音高(0-127)。
UiFlow2

- MIDIUnit.set_instrument(bank, channel, value)
更改指定通道上的程序(乐器)。
- 参数:
bank – 用于程序更改的 Bank selector(MSB)。
channel – MIDI 通道(0-15)。
value – 程序编号(0-127)。
UiFlow2

- MIDIUnit.set_drums_instrument(drum_pitch, velocity)
设置鼓组乐器,并在 MIDI 通道 10 上播放一个音符。
- 参数:
drum_pitch – 鼓音高编号。
velocity – 音符力度(0-127)。
UiFlow2

- MIDIUnit.set_pitch_bend(channel, value)
向指定通道发送 MIDI Pitch Bend 消息。
- 参数:
channel – MIDI 通道(0-15)。
value – 弯音值(0-16383)。
UiFlow2

- MIDIUnit.set_pitch_bend_range(channel, value)
在指定通道上设置弯音范围。
- 参数:
channel – MIDI 通道(0-15)。
value – 弯音范围(单位:半音)。
UiFlow2

- MIDIUnit.midi_reset()
发送 MIDI System Exclusive Reset 命令。
UiFlow2

- MIDIUnit.set_channel_volume(channel, level)
设置指定通道的通道音量。
- 参数:
channel – MIDI 通道(0-15)。
level – 音量等级(0-127)。
UiFlow2

- MIDIUnit.set_all_notes_off(channel)
发送 MIDI Control Change 消息,在指定通道上关闭所有音符。
- 参数:
channel – MIDI 通道(0-15)。
UiFlow2

- MIDIUnit.set_master_volume(level)
使用标准 System Exclusive 消息设置主音量。
- 参数:
level – 音量等级(0-127)。
UiFlow2

- MIDIUnit.set_reverb(channel, program, level, delayfeedback)
配置在指定通道上的混响效果。
- 参数:
channel – MIDI 通道(0-15)。
program – 混响程序编号(0 - 7)。
level – 混响级别(0-127)。
delayfeedback – 延迟反馈量(0-127)。
UiFlow2

- MIDIUnit.set_chorus(channel, program, level, feedback, chorusdelay)
在指定通道上配置合唱效果。
- 参数:
channel – MIDI 通道(0-15)。
program – 合唱程序编号(0-7)。
level – 合唱级别(0-127)。
feedback – 合唱反馈量(0-127)。
chorusdelay – 合唱延迟量(0-127)。
UiFlow2

- MIDIUnit.set_pan(channel, value)
设置指定通道的 pan 位置。
- 参数:
channel – MIDI 通道(0-15)。
value – 平移位置(0-127)。
UiFlow2

- MIDIUnit.set_equalizer(channel, lowband, medlowband, medhighband, highband, lowfreq, medlowfreq, medhighfreq, highfreq)
设置指定通道的均衡器电平和频率。
- 参数:
channel – MIDI 通道(0-15)。
lowband – 低频电平(-12 dB 到 +12 dB)。
medlowband – 中低频段电平(-12 dB 到 +12 dB)。
medhighband – 中高频段电平(-12 dB 至 +12 dB)。
highband – 高频增益等级(-12 dB 至 +12 dB)。
lowfreq – 低频段频率 (Hz)。
medlowfreq – 中低频段频率 (Hz)。
medhighfreq – 中高频段频率 (Hz)。
highfreq – 高频段频率 (Hz)。
UiFlow2

- MIDIUnit.set_tuning(channel, fine, coarse)
设置指定通道的调谐参数。
- 参数:
channel – MIDI 通道(0-15)。
fine – 微调值(分)。
coarse – 粗调值(半音)。
UiFlow2

- MIDIUnit.set_vibrate(channel, rate, depth, delay)
设置指定通道上的颤音效果参数。
- 参数:
channel – 应用颤音效果的 MIDI 通道(0-15)。
rate – 颤音速率(0 - 127)。
depth – 颤音深度(0-127)。
delay – 颤音延迟(0-127)。
UiFlow2

- MIDIUnit.set_tvf(channel, cutoff, resonance)
设置指定通道上的 TVF(Tone-Voltage Filter,音调-电压滤波器)参数。
- 参数:
channel – 要应用筛选器的 MIDI 通道(0-15)。
cutoff – 滤波器截止频率 (0-127)。
resonance – 滤波器共振(0-127)。
UiFlow2

- MIDIUnit.set_envelope(channel, attack, decay, release)
在指定通道上设置 ADSR(Attack、Decay、Sustain、Release)包络参数。
- 参数:
channel – 要应用包络的 MIDI 通道(0-15)。
attack – 攻击时间(0-127)。
decay – 衰减时间(0-127)。
release – 释放时间 (0-127)。
UiFlow2

- MIDIUnit.set_scale_tuning(channel, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12)
为指定通道设置比例调谐。
- 参数:
channel – 要应用音阶调音的 MIDI 通道(0-15)。
v1~v12 – 为音阶中的每个音符(0-127)调整数值。
UiFlow2

- MIDIUnit.set_mod_wheel(channel, pitch, tvtcutoff, amplitude, rate, pitchdepth, tvfdepth, tvadepth)
设置调制轮参数,这些参数会影响指定通道上的各种效果。
- 参数:
channel – 要将调制应用到的 MIDI 通道(0-15)。
pitch – 音高调制深度。
tvtcutoff – 截止频率调制深度。
amplitude – 调幅深度。
rate – 调制速率。
pitchdepth – 音高调制深度。
tvfdepth – TVF 调制深度。
tvadepth – TVA(Tone-Voltage Amplifier)调制深度。
UiFlow2

- MIDIUnit.set_all_drums()
发送一条 System Exclusive 消息,将通道 10 上的所有鼓设置为默认值。
UiFlow2

- MIDIUnit.cmd_write(cmd)
将 MIDI 命令写入 UART 接口。
- 参数:
cmd – MIDI 命令字节列表。
- MIDIUnit.map(x, in_min, in_max, out_min, out_max)
将一个值从一个范围映射到另一个范围。
- 参数:
x – 要映射的值。
in_min – 输入范围的最小值。
in_max – 输入范围的最大值。
out_min – 输出范围的最小值。
out_max – 输出范围的最大值。


