MIDI Unit

Support the following products:

MIDIUnit

 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 Example:

example.png

midi_cores3_example.m5f2

class MIDIUnit

Constructors

class MIDIUnit(id, port)
Initializes the MIDI unit with a specified UART ID and port pins.

The UART interface is used to transmit MIDI messages.

Parameters:
  • id (Literal[0,1,2]) – UART device ID.

  • port (List[int]|Tuple[int,int]) – UART TX and RX pins.

UIFLOW2:

init.png

Methods

MIDIUnit.set_note_on(channel, pitch, velocity)

Sends a MIDI Note On message to the specified channel.

Parameters:
  • channel – MIDI channel (0-15).

  • pitch – Note pitch (0-127).

  • velocity – Note velocity (0-127).

UIFLOW2:

set_note_on.png

MIDIUnit.set_note_off(channel, pitch)

Sends a MIDI Note Off message to the specified channel.

Parameters:
  • channel – MIDI channel (0-15).

  • pitch – Note pitch (0-127).

UIFLOW2:

set_note_off.png

MIDIUnit.set_instrument(bank, channel, value)

Changes the program (instrument) on the specified channel.

Parameters:
  • bank – Bank selector (MSB) for the program change.

  • channel – MIDI channel (0-15).

  • value – Program number (0-127).

UIFLOW2:

set_instrument.png

MIDIUnit.set_drums_instrument(drum_pitch, velocity)

Sets a drum instrument and plays a note on MIDI channel 10.

Parameters:
  • drum_pitch – Drum pitch number.

  • velocity – Note velocity (0-127).

UIFLOW2:

set_drums_instrument.png

MIDIUnit.set_pitch_bend(channel, value)

Sends a MIDI Pitch Bend message to the specified channel.

Parameters:
  • channel – MIDI channel (0-15).

  • value – Pitch bend value (0-16383).

UIFLOW2:

set_pitch_bend.png

MIDIUnit.set_pitch_bend_range(channel, value)

Sets the pitch bend range on the specified channel.

Parameters:
  • channel – MIDI channel (0-15).

  • value – Pitch bend range in semitones.

UIFLOW2:

set_pitch_bend_range.png

MIDIUnit.midi_reset()

Sends a MIDI System Exclusive Reset command.

UIFLOW2:

midi_reset.png

MIDIUnit.set_channel_volume(channel, level)

Sets the channel volume for the specified channel.

Parameters:
  • channel – MIDI channel (0-15).

  • level – Volume level (0-127).

UIFLOW2:

set_channel_volume.png

MIDIUnit.set_all_notes_off(channel)

Sends a MIDI Control Change message to turn off all notes on the specified channel.

Parameters:

channel – MIDI channel (0-15).

UIFLOW2:

set_all_notes_off.png

MIDIUnit.set_master_volume(level)

Sets the master volume using a standard System Exclusive message.

Parameters:

level – Volume level (0-127).

UIFLOW2:

set_master_volume.png

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

Configures reverb effect on the specified channel.

Parameters:
  • channel – MIDI channel (0-15).

  • program – Reverb program number (0-7).

  • level – Reverb level (0-127).

  • delayfeedback – Delay feedback amount (0-127).

UIFLOW2:

set_reverb.png

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

Configures chorus effect on the specified channel.

Parameters:
  • channel – MIDI channel (0-15).

  • program – Chorus program number (0-7).

  • level – Chorus level (0-127).

  • feedback – Chorus feedback amount (0-127).

  • chorusdelay – Chorus delay amount (0-127).

UIFLOW2:

set_chorus.png

MIDIUnit.set_pan(channel, value)

Sets the pan position for the specified channel.

Parameters:
  • channel – MIDI channel (0-15).

  • value – Pan position (0-127).

UIFLOW2:

set_pan.png

MIDIUnit.set_equalizer(channel, lowband, medlowband, medhighband, highband, lowfreq, medlowfreq, medhighfreq, highfreq)

Sets the equalizer levels and frequencies for the specified channel.

Parameters:
  • channel – MIDI channel (0-15).

  • lowband – Low band level (-12dB to +12dB).

  • medlowband – Mid-low band level (-12dB to +12dB).

  • medhighband – Mid-high band level (-12dB to +12dB).

  • highband – High band level (-12dB to +12dB).

  • lowfreq – Low band frequency (Hz).

  • medlowfreq – Mid-low band frequency (Hz).

  • medhighfreq – Mid-high band frequency (Hz).

  • highfreq – High band frequency (Hz).

UIFLOW2:

set_equalizer.png

MIDIUnit.set_tuning(channel, fine, coarse)

Sets the tuning for the specified channel.

Parameters:
  • channel – MIDI channel (0-15).

  • fine – Fine tuning value (cents).

  • coarse – Coarse tuning value (semitones).

UIFLOW2:

set_tuning.png

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

Sets the vibrato effect parameters on the specified channel.

Parameters:
  • channel – The MIDI channel to apply the vibrato effect to (0-15).

  • rate – The vibrato rate (0-127).

  • depth – The vibrato depth (0-127).

  • delay – The vibrato delay (0-127).

UIFLOW2:

set_vibrate.png

MIDIUnit.set_tvf(channel, cutoff, resonance)

Sets the parameters for a TVF (Tone-Voltage Filter) on the specified channel.

Parameters:
  • channel – The MIDI channel to apply the filter to (0-15).

  • cutoff – The filter cutoff frequency (0-127).

  • resonance – The filter resonance (0-127).

UIFLOW2:

set_tvf.png

MIDIUnit.set_envelope(channel, attack, decay, release)

Sets the ADSR (Attack, Decay, Sustain, Release) envelope parameters on the specified channel.

Parameters:
  • channel – The MIDI channel to apply the envelope to (0-15).

  • attack – The attack time (0-127).

  • decay – The decay time (0-127).

  • release – The release time (0-127).

UIFLOW2:

set_envelope.png

MIDIUnit.set_scale_tuning(channel, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12)

Sets scale tuning for the specified channel.

Parameters:
  • channel – The MIDI channel to apply the scale tuning to (0-15).

  • v1~v12 – Tuning values for each note in the scale (0-127).

UIFLOW2:

set_scale_tuning.png

MIDIUnit.set_mod_wheel(channel, pitch, tvtcutoff, amplitude, rate, pitchdepth, tvfdepth, tvadepth)

Sets modulation wheel parameters that affect various effects on the specified channel.

Parameters:
  • channel – The MIDI channel to apply the modulation to (0-15).

  • pitch – Pitch modulation depth.

  • tvtcutoff – Cutoff frequency modulation depth.

  • amplitude – Amplitude modulation depth.

  • rate – Modulation rate.

  • pitchdepth – Depth of pitch modulation.

  • tvfdepth – Depth of TVF modulation.

  • tvadepth – Depth of TVA (Tone-Voltage Amplifier) modulation.

UIFLOW2:

set_mod_wheel.png

MIDIUnit.set_all_drums()

Sends a System Exclusive message to set all drums on channel 10 to default values.

UIFLOW2:

set_all_drums.png

MIDIUnit.cmd_write(cmd)

Writes a MIDI command to the UART interface.

Parameters:

cmd – List of MIDI command bytes.

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

Maps a value from one range to another.

Parameters:
  • x – Value to map.

  • in_min – Minimum value of the input range.

  • in_max – Maximum value of the input range.

  • out_min – Minimum value of the output range.

  • out_max – Maximum value of the output range.