openharmony 鸿蒙 capi-native-midi-base-h

2026-08-25 浏览 (1)

native_midi_base.h

Overview

This file declares the basic data structure of the MIDI module. It defines the basic types, enumerations, structs, and callback functions of the MIDI APIs.

File to include: <midi/native_midi_base.h>

Library: libohmidi.so

System capability: SystemCapability.Multimedia.Audio.MIDI

Since: 24

Related module: OHMIDI

Summary

Structs

Nametypedef KeywordDescription
OH_MIDIEventOH_MIDIEventDescribes a generic MIDI event structure, which is applicable to both raw byte stream (MIDI 1.0) and Universal MIDI Packet (UMP) data formats.
OH_MIDIDeviceInformationOH_MIDIDeviceInformationDescribes the device information struct, such as the storage device ID.
OH_MIDIPortInformationOH_MIDIPortInformationDescribes the port information struct, which is used to enumerate ports, including port names.
OH_MIDIPortDescriptorOH_MIDIPortDescriptorDescribes the port descriptor struct, which is used to specify the port index and protocol behavior for opening a port.
OH_MIDICallbacksOH_MIDICallbacksDescribes the client callback struct. Client callbacks include the device change callback and error handling callback.
OH_MIDIClientStructOH_MIDIClientDeclares a MIDI client.
OH_MIDIDeviceStructOH_MIDIDeviceDeclares a MIDI device.

Enums

Nametypedef KeywordDescription
OH_MIDIStatusCodeOH_MIDIStatusCodeEnumerates MIDI status codes. It defines MIDI operation status codes, which are used to indicate operation success or failure causes.
OH_MIDIPortDirectionOH_MIDIPortDirectionEnumerates the port directions, which indicate MIDI ports' data transmission directions.
OH_MIDIProtocolOH_MIDIProtocolEnumerates the MIDI protocol versions, which are used to specify the MIDI protocols used by ports.
OH_MIDIDeviceTypeOH_MIDIDeviceTypeEnumerates MIDI device types, which define MIDI devices' connection types.
OH_MIDIDeviceChangeActionOH_MIDIDeviceChangeActionEnumerates the operations that cause device connection status changes, which are used to identify device connection and disconnection events.

Function

Nametypedef KeywordDescription
typedef void (*OH_MIDICallback_OnDeviceChange)(void *userData, OH_MIDIDeviceChangeAction action, OH_MIDIDeviceInformation deviceInfo)OH_MIDICallback_OnDeviceChangeCallback for monitoring device connection and disconnection.
typedef void (*OH_MIDICallback_OnError)(void *userData, OH_MIDIStatusCode code)OH_MIDICallback_OnErrorCallback for handling client-level errors. It is called when a critical error (such as service crash) occurs in the MIDI service. In this case, an application may need to re-create the client.
typedef void (*OH_MIDIDevice_OnReceived)(void *userData, const OH_MIDIEvent *events, size_t eventCount)OH_MIDIDevice_OnReceivedCallback for receiving MIDI data (batch processing).
typedef void (*OH_MIDIClient_OnDeviceOpened)(void *userData, bool opened, OH_MIDIDevice *device, OH_MIDIDeviceInformation info)OH_MIDIClient_OnDeviceOpenedCallback for the result of asynchronously opening a BLE device.

Enum Description

OH_MIDIStatusCode

enum OH_MIDIStatusCode

Description

Enumerates MIDI status codes. It defines MIDI operation status codes, which are used to indicate operation success or failure causes.

Since: 24

Enum ItemDescription
OH_MIDI_STATUS_OK = 0The operation succeeded.
Since: 24
OH_MIDI_STATUS_GENERIC_INVALID_ARGUMENT = 35500001Invalid parameter (for example, a null pointer).
Since: 24
OH_MIDI_STATUS_GENERIC_IPC_FAILURE = 35500002IPC fails.
Since: 24
OH_MIDI_STATUS_INVALID_CLIENT = 35500003Invalid client handle.
Since: 24
OH_MIDI_STATUS_INVALID_DEVICE_HANDLE = 35500004Invalid device handle.
Since: 24
OH_MIDI_STATUS_INVALID_PORT = 35500005Invalid port index.
Since: 24
OH_MIDI_STATUS_WOULD_BLOCK = 35500006The send buffer is temporarily full, indicating that the shared memory buffer is currently out of space.
This code is returned by a non-blocking send operation if messages cannot be placed into the buffer. In this case, you are advised to wait for about 10 ms and try again.
Since: 24
OH_MIDI_STATUS_TIMEOUT = 35500007The operation times out.
Since: 24
OH_MIDI_STATUS_TOO_MANY_OPEN_DEVICES = 35500008The number of devices that can be opened on the client has reached the maximum (16).
To open a new device, you must first close an existing one.
Since: 24
OH_MIDI_STATUS_TOO_MANY_OPEN_PORTS = 35500009The number of ports opened on the client has reached the maximum (64).
To open a new port, you must first close an existing one.
Since: 24
OH_MIDI_STATUS_DEVICE_ALREADY_OPEN = 35500010The device has already been opened on the client. A device cannot be opened repeatedly on the same client.
Since: 24
OH_MIDI_STATUS_PORT_ALREADY_OPEN = 35500011The port has already been opened on the client. A port cannot be opened repeatedly on the same client.
Since: 24
OH_MIDI_STATUS_TOO_MANY_CLIENTS = 35500012The maximum number of clients (8 system-level, or 2 application-level per UID) has been reached. The application should wait or release other resources and then try again.
Since: 24
OH_MIDI_STATUS_PERMISSION_DENIED = 35500013Permission denied. This code is returned when an application attempts to perform an operation without the required permission (for example, the Bluetooth permission for BLE devices).
Since: 24
OH_MIDI_STATUS_SERVICE_DIED = 35500014The MIDI system service has crashed or been disconnected. The client must be destroyed and re-created.
Since: 24
OH_MIDI_STATUS_SYSTEM_ERROR = 35500100Internal system error. An unexpected system-level error occurs.
Since: 24

OH_MIDIPortDirection

enum OH_MIDIPortDirection

Description

Enumerates the port directions, which indicate MIDI ports' data transmission directions.

Since: 24

Enum ItemDescription
OH_MIDI_PORT_DIRECTION_INPUT = 0Input (device -> host).
Since: 24
OH_MIDI_PORT_DIRECTION_OUTPUT = 1Output (host -> device).
Since: 24

OH_MIDIProtocol

enum OH_MIDIProtocol

Description

Enumerates the MIDI protocol versions, which are used to specify the MIDI protocols used by ports.

NOTE

The SDK always uses the UMP format for data transmission, regardless of which protocol is selected. This enum defines the data behavior and semantics of the connection, not the data structure. MT is the identifier for the message type within a UMP packet; different MT values correspond to different types of MIDI messages.

Since: 24

Enum ItemDescription
OH_MIDI_PROTOCOL_1_0 = 1Traditional MIDI 1.0 semantics.
Under this protocol, the MIDI system service expects to receive the following types of UMP messages:
- UMP packets that strictly comply with the MIDI 1.0 protocol specifications.
- MT 0x0: utility messages (such as timestamps).
- MT 0x1: system real-time and system common messages.
- MT 0x2: MIDI 1.0 channel voice messages (32-bit).
- MT 0x3: data messages (64-bit), used for SysEx (7-bit payload).
- If the target hardware uses MIDI 1.0, the service converts UMP packets back to byte streams (F0...F7).
- If the target hardware uses MIDI 2.0, the service directly sends the unconverted UMP packets (encapsulated based on MIDI 1.0).
Since: 24
OH_MIDI_PROTOCOL_2_0 = 2MIDI 2.0 semantics.
Under this protocol, the MIDI system service expects to receive the following types of UMP messages:
- UMP packets that utilize the features of MIDI 2.0.
- MT 0x4: MIDI 2.0 channel voice messages (64-bit, high resolution).
- MT 0x0: utility messages (timestamp).
- MT 0xD: Flex data messages (128-bit, such as text and lyrics).
- MT 0xF: UMP stream messages (128-bit, endpoint discovery and functional blocks).
- MT 0x3/MT 0x5: data messages (64-bit or 128-bit).
Since: 24

OH_MIDIDeviceType

enum OH_MIDIDeviceType

Description

Enumerates MIDI device types, which define MIDI devices' connection types.

Since: 24

Enum ItemDescription
OH_MIDI_DEVICE_TYPE_USB = 0USB MIDI device.
OH_MIDI_DEVICE_TYPE_BLE = 1Bluetooth Low Energy (BLE) MIDI device.

OH_MIDIDeviceChangeAction

enum OH_MIDIDeviceChangeAction

Description

Enumerates the operations that cause device connection status changes, which are used to identify device connection and disconnection events.

Since: 24

Enum ItemDescription
OH_MIDI_DEVICE_CHANGE_ACTION_CONNECTED = 0The device is connected.
Since: 24
OH_MIDI_DEVICE_CHANGE_ACTION_DISCONNECTED = 1The device is disconnected.
Since: 24

Function Description

OH_MIDICallback_OnDeviceChange()

typedef void (*OH_MIDICallback_OnDeviceChange)(void *userData, OH_MIDIDeviceChangeAction action, OH_MIDIDeviceInformation deviceInfo)

Description

Callback for monitoring device connection and disconnection.

Since: 24

Parameters

NameDescription
void *userDataPointer to the custom data passed when OH_MIDIClient_Create is called.
OH_MIDIDeviceChangeAction actionDevice change operation (connected/disconnected).
OH_MIDIDeviceInformation deviceInfoInformation about the device change.

OH_MIDICallback_OnError()

typedef void (*OH_MIDICallback_OnError)(void *userData, OH_MIDIStatusCode code)

Description

Callback for handling client-level errors. It is called when a critical error (such as service crash) occurs in the MIDI service. In this case, an application may need to re-create the client.

Since: 24

Parameters

NameDescription
void *userDataPointer to the custom data passed when OH_MIDIClient_Create is called.
OH_MIDIStatusCode codeStatus code, indicating the cause of the error.

OH_MIDIDevice_OnReceived()

typedef void (*OH_MIDIDevice_OnReceived)(void *userData, const OH_MIDIEvent *events, size_t eventCount)

Description

Callback for receiving MIDI data (batch processing).

NOTE

Memory safety and thread safety precautions:

  • Memory safety: The events array and all data pointers within it are temporary and valid only during this callback. Accessing these pointers after the callback returns results in undefined behavior (crashes or memory corruption). Callers must copy any data that needs to be retained.
  • Thread safety: This callback is called on a high-priority system thread. Avoid blocking operations, heavy computation, or I/O operations in the callback.

Since: 24

Parameters

NameDescription
void *userDataPointer to the custom data passed when OH_MIDIClient_Create is called.
const OH_MIDIEvent *eventsPointer to the array of received MIDI events.
size_t eventCountNumber of events in the array.

OH_MIDIClient_OnDeviceOpened()

typedef void (*OH_MIDIClient_OnDeviceOpened)(void *userData, bool opened, OH_MIDIDevice *device, OH_MIDIDeviceInformation info)

Description

Callback for the result of asynchronously opening a BLE device.

Since: 24

Parameters

NameDescription
void *userDataPointer to the custom data passed when OH_MIDIClient_OpenBLEDevice is called.
bool openedWhether the device is successfully opened.
The value true indicates that the device is successfully opened and the device handle is valid. The value false indicates that the device fails to be opened and the device handle is NULL.
OH_MIDIDevice *deviceHandle to the opened device.
If the value of opened is true, the application must call OH_MIDIClient_CloseDevice to close the handle when it is no longer needed.
If the value of opened is false, the value of this parameter is NULL.
OH_MIDIDeviceInformation infoInformation about the opened device.
Note: This object is valid only within this callback. To persist specific properties (such as the ID or name), make a copy of the device information.

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 capi-native-audiostreambuilder-h

openharmony 鸿蒙 capi-ohaudiosuite

openharmony 鸿蒙 js-apis-inner-multimedia-systemSoundPlayer

openharmony 鸿蒙 arkts-apis-audio-i

openharmony 鸿蒙 capi-ohaudio

openharmony 鸿蒙 capi-ohmidi

openharmony 鸿蒙 arkts-apis-audio-AudioManager

openharmony 鸿蒙 capi-ohaudiosuite-oh-audiosuite-spacerenderpositionparams

openharmony 鸿蒙 capi-ohaudio-oh-audiostreaminfo

openharmony 鸿蒙 errorcode-ringtone

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/NPwHk19h