You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何pygame.midi在macOS失效而pygame.mixer正常?MIDI播放报错求助

Hey there! Let's break down why your pygame.midi code is failing on macOS, clear up the confusion between pygame.mixer and pygame.midi, and get your MIDI playback working.

Why You're Seeing the "Device id invalid" Error

Windows comes with a pre-configured default MIDI output device, so pygame.midi.Output(0) works out of the box. But macOS doesn't include a default virtual MIDI output device by default. When you try to use device ID 0, pygame can't locate any valid MIDI output device at that index, hence the error.

Why pygame.mixer Works But pygame.midi Doesn't

These two libraries operate entirely differently under the hood:

  • pygame.mixer: It generates audio directly using your system's core audio stack. It doesn't rely on MIDI devices at all—it takes sound data (like pre-made samples or generated waveforms) and sends it straight to your speakers.
  • pygame.midi: This is a wrapper for interacting with your system's MIDI hardware or virtual devices. It sends MIDI control messages (like "play note 64 at volume 127") to a MIDI device, which is responsible for generating the actual sound. Without a valid MIDI device to receive these messages, it can't function.

Fixes to Get pygame.midi Running on macOS

1. First, Find Your Valid MIDI Device IDs

Stop guessing device IDs—list all available MIDI devices to see which ones are output-capable. Run this script:

import pygame.midi

pygame.midi.init()
# Loop through all detected MIDI devices
for device_id in range(pygame.midi.get_count()):
    device_info = pygame.midi.get_device_info(device_id)
    # Decode the device name (stored as a byte string)
    device_name = device_info[1].decode()
    # Check if this is an output device (info[3] returns True for outputs)
    is_output_device = device_info[3]
    print(f"Device ID: {device_id} | Name: {device_name} | Is Output: {is_output_device}")
pygame.midi.quit()

Look for a line where Is Output is True, then use that device ID instead of 0 in your original code.

2. Create a Virtual MIDI Output Device (If None Exist)

If the above script doesn't show any output devices, you can make a virtual one using macOS's built-in tool:

  • Open Audio MIDI Setup (search for it in Spotlight or find it in Launchpad > Other)
  • Go to Window > Show MIDI Studio in the top menu bar
  • Click the + icon in the top-left corner of the MIDI Studio window
  • Select Create Virtual MIDI Output Device, name it something like "My Virtual MIDI Output", and click OK

Restart your Python environment, run the device-listing script again, and you'll see your new virtual device listed. Use its ID in your pygame.midi.Output() call.

3. Test the Fixed Code

Once you have the correct device ID, update your original code (replace YOUR_VALID_DEVICE_ID with the ID you found):

import pygame.midi
import time

pygame.midi.init()
# Use the valid device ID from the listing script
player = pygame.midi.Output(YOUR_VALID_DEVICE_ID)
player.set_instrument(0)
player.note_on(64, 127)
time.sleep(1)
player.note_off(64, 127)
del player
pygame.midi.quit()

内容的提问来源于stack exchange,提问作者Matyáš Krupička

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:42:11