目录

OH_MIDI Overview (C/C++)

Introduction to MIDI

Musical Instrument Digital Interface (MIDI) is an international standard protocol for communication between electronic musical instruments, computers, and other devices. It defines the transmission method for music data such as notes and control signals, and is widely used in fields such as music production, stage performance, and game development.

Starting from API version 24, Audio Kit supports MIDI functionality. Applications can use the OH_MIDI API to exchange data with external MIDI devices (such as USB MIDI keyboards and Bluetooth MIDI devices). Since OH_MIDI adopts Universal MIDI Packet (UMP) as the unified data exchange format, you do not need to concern underlying protocol differences and can simply build applications for music creation, instrument control, and MIDI data processing in a modern way.

[h2]MIDI Protocol Versions

OH_MIDI supports connecting standard MIDI 1.0 devices and automatically converts traditional MIDI 1.0 byte streams into the 32-bit UMP format through the system service layer, thereby simplifying application development. Meanwhile, this architectural design is compatible with the MIDI 2.0 standard in terms of data format, providing the necessary technical foundation for future system kernels to support high-resolution MIDI devices.

[h2]UMP Data Format Description

The universal MIDI packet is a unified packet format defined in the MIDI 2.0 standard. It adopts a 32-bit aligned structure and can carry both MIDI 1.0 and MIDI 2.0 protocol messages simultaneously.

Relationship between protocol and data format (analogous to "envelope" and "language of the letter content"):

Regardless of which protocol is chosen, the data encapsulation format (UMP) is unified.

OH_MIDI includes four core concepts: client, device, port, and event. The basic workflow for developing MIDI applications using OH_MIDI is: create a client → enumerate/open a device → open a port → send and receive MIDI messages → close the port/device/client. For the detailed development procedure and sample code, see Using OH_MIDI for MIDI Development (C/C++).

Relationships between core concepts:

OH_MIDI Client

The client is the entry point for applications to use MIDI services. All MIDI operations require creating a client first. Additionally, the client is responsible for managing the connection with the MIDI service, providing core functions such as device enumeration, device opening, and port management.

After creating a client through the OH_MIDIClient_Create interface, the application can obtain a list of all currently connected MIDI devices and listen for device hot-plug events (OH_MIDICallback_OnDeviceChange) and system-level errors (OH_MIDICallback_OnError) through registered callbacks.

When closing the client, the system automatically closes all devices and ports opened under that client (automatic resource reclamation mechanism). It is recommended that applications actively release resources in reverse order (port → device → client) to ensure clear code logic.

OH_MIDI Device

A MIDI device refers to a physical or virtual MIDI interface device, including USB MIDI keyboards, Bluetooth MIDI controllers, and so on. After a device is connected, the MIDI service assigns it an identifier (midiDeviceId in OH_MIDIDeviceInformation), which is used to distinguish and manage different devices within the current session.

Device information includes attributes such as device type (USB or Bluetooth), native protocol capability, device name, vendor ID, product ID, and device address.

Connection method: USB devices are opened synchronously through the OH_MIDIClient_OpenDevice API, while Bluetooth Low Energy (BLE) MIDI devices need to be connected asynchronously through the OH_MIDIClient_OpenBLEDevice API.

Native protocol capability: Indicates the current communication protocol mode of the device. In the current version, when a device supports multiple protocols, the system prioritizes communication with the device in MIDI 1.0 mode and encapsulates the data into MIDI 2.0 UMP format at the MIDI service layer for the application.

OH_MIDI Port

A MIDI port is a data transmission endpoint on a device, categorized into input ports and output ports.

[App] <--Receive-- [Input port] <-- [Device]
[App] --Send--> [Output port] --> [Device]

Each device can have multiple input and output ports. When each port is opened, the desired protocol behavior can be configured through the OH_MIDIPortDescriptor structure.

OH_MIDI Event

A MIDI event is the basic unit of MIDI data transmission. OH_MIDI mandates the use of the UMP format, which uniformly carries MIDI 1.0 and MIDI 2.0 data.

Each event contains three core fields:

[h2]Complete Message Type Reference

The UMP format distinguishes different messages by message type. It is the upper 4 bits of a UMP packet, determining the functional scope, packet size, and status field size of the message.

MT Value Name Size (bit) Description
0x0 Utility Messages 32 Utility messages, such as JR timestamps.
0x1 System Real Time and System Common Messages 32 System real-time messages and system common messages (excluding System Exclusive).
0x2 MIDI 1.0 Channel Voice Messages 32 MIDI 1.0 channel voice messages, compatible with existing devices.
0x3 Data Messages 64 Data messages, such as SysEx (including System Exclusive).
0x4 MIDI 2.0 Channel Voice Messages 64 MIDI 2.0 high-resolution Channel Voice messages.
0x5 Data Messages 128 Data messages.
0x6 Reserved 32 Reserved for future use.
0x7 Reserved 32 Reserved for future use.
0x8 Reserved 64 Reserved for future use.
0x9 Reserved 64 Reserved for future use.
0xA Reserved 64 Reserved for future use.
0xB Reserved 96 Reserved for future use.
0xC Reserved 96 Reserved for future use.
0xD Flex Data Messages 128 Flex Data flexible data messages, such as text and lyrics.
0xE Reserved 128 Reserved for future use.
0xF UMP Stream Messages 128 UMP stream messages, such as endpoint discovery and function block configuration.
  • In most application scenarios, you only need to use MT 0x2 (MIDI 1.0 channel voice) and MT 0x4 (MIDI 2.0 channel voice) to send and receive MIDI notes.
  • The events array and the data pointers it contains in the receive callback are only valid during the callback execution. If the app needs to retain MIDI data for subsequent processing, it must deep copy the data into its own memory before the callback returns to avoid undefined behavior caused by accessing expired pointers.

Glossary

Term Full Name Description
UMP Universal MIDI Packet A universal MIDI data packet in a 32-bit aligned unified container format.
MT Message Type Message type, the upper 4 bits of a UMP data packet, which determines the message function and size.
SysEx System Exclusive System exclusive message used for vendor-defined data transmission.
JR Jitter Reduction Jitter reduction, a timestamp synchronization mechanism for network/Bluetooth transmission.
Channel Voice Channel Voice A core message type that controls notes, velocity, controllers, and more.
Flex Data Flex Data New in MIDI 2.0, supports 128-bit long messages such as text and lyrics.
MIDI-CI MIDI Capability Inquiry MIDI capability inquiry, an automatic device configuration protocol for MIDI 2.0.

Constraints

References