PyObjC CoreMIDI Framework

12.1 · active · verified Tue Apr 14

PyObjC CoreMIDI provides Python bindings for Apple's CoreMIDI framework on macOS, enabling Python applications to interact with MIDI devices and services. It is part of the larger PyObjC bridge, which allows full-featured Cocoa applications to be written in pure Python. The current version is 12.1, with releases typically tied to macOS SDK updates and Python version support changes.

Warnings

Install

Imports

Quickstart

This example demonstrates how to import the CoreMIDI framework and iterate through the available MIDI devices, printing their names. CoreMIDI is a low-level API; more complex interactions often require a deeper understanding of its C API and manual resource management.

import CoreMIDI
import objc

def list_midi_devices():
    """Lists all available CoreMIDI devices."""
    print("MIDI Devices:")
    num_devices = CoreMIDI.MIDIGetNumberOfDevices()
    if num_devices == 0:
        print("  No MIDI devices found.")
        return

    for i in range(num_devices):
        midi_device_ref = CoreMIDI.MIDIGetDevice(i)
        if midi_device_ref:
            # MIDIObjectGetStringProperty returns a status code and the string value.
            # PyObjC automatically bridges CFStringRef to Python string.
            (name_result, name_str) = CoreMIDI.MIDIObjectGetStringProperty(
                midi_device_ref, CoreMIDI.kMIDIPropertyName
            )
            if name_result == 0: # noErr
                print(f"  Device {i}: {name_str}")
            else:
                print(f"  Device {i}: (Error getting name, code {name_result})")
        else:
            print(f"  Device {i}: (Could not get device reference)")

if __name__ == "__main__":
    list_midi_devices()

view raw JSON →