Synth Unit

支持以下产品:

SynthUnit

MicroPython 应用示例

 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 SynthUnit
 9import time
10
11
12synth_0 = None
13
14
15def setup():
16    global synth_0
17
18    M5.begin()
19    Widgets.fillScreen(0x222222)
20
21    synth_0 = SynthUnit(1, port=(1, 2))
22    synth_0.set_channel_volume(0, 64)
23    synth_0.set_instrument(0, 0, 112)
24
25
26def loop():
27    global synth_0
28    M5.update()
29    synth_0.set_note_on(0, 0, 61)
30    synth_0.set_note_on(0, 36, 127)
31    time.sleep_ms(300)
32    synth_0.set_note_on(0, 48, 124)
33    synth_0.set_note_on(0, 60, 124)
34
35
36if __name__ == "__main__":
37    try:
38        setup()
39        while True:
40            loop()
41    except (Exception, KeyboardInterrupt) as e:
42        try:
43            from utility import print_error_msg
44
45            print_error_msg(e)
46        except ImportError:
47            print("please update to latest firmware")

UiFlow2 应用示例

example.png

synth_cores3_example.m5f2

class SynthUnit

Constructors

class SynthUnit(id, port)
使用指定的 UART ID 和端口引脚初始化 MIDI 单元。

UART 接口用于传输 MIDI 消息。

参数:
  • id (Literal[0,1,2]) – UART 设备 ID。

  • port (List[int]|Tuple[int,int]) – UART TX 和 RX 引脚。

UIFLOW2:

init.png

Methods

SynthUnit.set_note_on(channel, pitch, velocity)

向指定通道发送 MIDI Note On 消息。

参数:
  • channel – MIDI 通道(0-15)。

  • pitch – 音符音高(0-127)。

  • velocity – 音符力度(0-127)。

UIFLOW2:

set_note_on2.png

set_note_on.png

SynthUnit.set_note_off(channel, pitch)

向指定通道发送 MIDI Note Off 消息。

参数:
  • channel – MIDI 通道(0-15)。

  • pitch – 音符音高(0-127)。

UIFLOW2:

set_note_off.png

SynthUnit.set_instrument(bank, channel, value)

更改指定通道上的程序(乐器)。

参数:
  • bank – 用于 program change 的 Bank 选择器(MSB)。

  • channel – MIDI 通道(0-15)。

  • value – 程序编号(0-127)。

UIFLOW2:

set_instrument.png

SynthUnit.set_drums_instrument(drum_pitch, velocity)

设置鼓组乐器,并在 MIDI 通道 10 上播放一个音符。

参数:
  • drum_pitch – 鼓音高编号。

  • velocity – 音符力度(0-127)。

UIFLOW2:

set_drums_instrument.png

SynthUnit.set_pitch_bend(channel, value)

向指定通道发送 MIDI Pitch Bend 消息。

参数:
  • channel – MIDI 通道(0-15)。

  • value – 弯音值(0-16383)。

UIFLOW2:

set_pitch_bend.png

SynthUnit.set_pitch_bend_range(channel, value)

在指定通道上设置弯音范围。

参数:
  • channel – MIDI 通道(0-15)。

  • value – 弯音范围(以半音为单位)。

UIFLOW2:

set_pitch_bend_range.png

SynthUnit.midi_reset()

发送 MIDI System Exclusive Reset 命令。

UIFLOW2:

midi_reset.png

SynthUnit.set_channel_volume(channel, level)

设置指定通道的通道音量。

参数:
  • channel – MIDI 通道(0-15)。

  • level – 音量等级(0-100)。

UIFLOW2:

set_channel_volume.png

SynthUnit.set_all_notes_off(channel)

向指定通道发送 MIDI Control Change 消息,以关闭所有音符。

参数:

channel – MIDI 通道(0-15)。

UIFLOW2:

set_all_notes_off.png

SynthUnit.set_master_volume(level)

使用标准的 System Exclusive 消息设置主音量。

参数:

level – 音量等级(0-100)。

UIFLOW2:

set_master_volume.png

SynthUnit.set_reverb(channel, program, level, delayfeedback)

在指定通道上配置混响效果。

参数:
  • channel – MIDI 通道(0-15)。

  • program – 混响程序编号(0 - 7)。

  • level – 混响级别(0-127)。

  • delayfeedback – 延迟反馈量(0-127)。

UIFLOW2:

set_reverb.png

SynthUnit.set_chorus(channel, program, level, feedback, chorusdelay)

在指定通道上配置合唱效果。

参数:
  • channel – MIDI 通道(0-15)。

  • program – 合唱程序编号 (0-7)。

  • level – 合唱级别(0-127)。

  • feedback – 合唱反馈量(0-127)。

  • chorusdelay – 合唱延迟量(0-127)。

UIFLOW2:

set_chorus.png

SynthUnit.set_pan(channel, value)

设置指定通道的 pan 位置。

参数:
  • channel – MIDI 通道(0-15)。

  • value – Pan 位置 (0-127)。

UIFLOW2:

set_pan.png

SynthUnit.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:

set_equalizer.png

SynthUnit.set_tuning(channel, fine, coarse)

设置指定通道的调谐参数。

参数:
  • channel – MIDI 通道(0-15)。

  • fine – 微调值(分)。

  • coarse – 粗调值(半音)。

UIFLOW2:

set_tuning.png

SynthUnit.set_vibrate(channel, rate, depth, delay)

设置指定通道上的颤音效果参数。

参数:
  • channel – 要应用颤音效果的 MIDI 通道(0-15)。

  • rate – 颤音速率(0-127)。

  • depth – 颤音深度(0-127)。

  • delay – 颤音延迟(0-127)。

UIFLOW2:

set_vibrate.png

SynthUnit.set_tvf(channel, cutoff, resonance)

设置指定通道上的 TVF(Tone-Voltage Filter,音调-电压滤波器)的参数。

参数:
  • channel – 要应用过滤器的 MIDI 通道(0-15)。

  • cutoff – 滤波器截止频率 (0-127)。

  • resonance – 滤波器谐振(0-127)。

UIFLOW2:

set_tvf.png

SynthUnit.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:

set_envelope.png

SynthUnit.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:

set_scale_tuning.png

SynthUnit.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:

set_mod_wheel.png

SynthUnit.set_all_drums()

发送一条 System Exclusive 消息,将通道 10 上的所有鼓设置为默认值。

UIFLOW2:

set_all_drums.png

SynthUnit.cmd_write(cmd)

将 MIDI 命令写入 UART 接口。

参数:

cmd – MIDI 命令字节列表。

SynthUnit.map(x, in_min, in_max, out_min, out_max)

将一个值从一个范围映射到另一个范围。

参数:
  • x – 要映射的值。

  • in_min – 输入范围的最小值。

  • in_max – 输入范围的最大值。

  • out_min – 输出范围的最小值。

  • out_max – 输出范围的最大值。