Mark
Mark

Reputation: 19

Apple Core Audio API Integration Using Python

I have been trying to get this code to work for days now and I am honestly not sure how to resolve this error. The main goal of this code is to be able to control specific apps audio output instead of changing the whole computer volume. Below I have my code and the error I cannot fix, any help is appreciated.

My Code:

import ctypes
from ctypes import c_uint32, c_void_p, byref, Structure, POINTER, c_int, c_char

# Define constants
kAudioObjectSystemObject = 1
kAudioObjectPropertyScopeGlobal = 1
kAudioObjectPropertyElementMaster = 0
kAudioHardwarePropertyDevices = ctypes.c_uint32(1)
kAudioDevicePropertyDeviceNameCFString = ctypes.c_uint32(2)

# Define the AudioObjectPropertyAddress structure
class AudioObjectPropertyAddress(Structure):
    _fields_ = [
        ('mSelector', c_uint32),
        ('mScope', c_uint32),
        ('mElement', c_uint32),  
    ]

# Load CoreAudio framework
coreaudio = ctypes.CDLL('/System/Library/Frameworks/CoreAudio.framework/Versions/A/CoreAudio')

# OLD PATH: '/System/Library/Frameworks/CoreAudio.framework/CoreAudio'

# Define function prototypes
coreaudio.AudioObjectGetPropertyDataSize.argtypes = [c_uint32, POINTER(AudioObjectPropertyAddress), c_uint32, c_void_p, POINTER(c_uint32)]
coreaudio.AudioObjectGetPropertyDataSize.restype = c_int

coreaudio.AudioObjectGetPropertyData.argtypes = [c_uint32, POINTER(AudioObjectPropertyAddress), c_uint32, c_void_p, c_uint32, c_void_p]
coreaudio.AudioObjectGetPropertyData.restype = c_int

def list_audio_devices():
    devices = []

    # Define the property address for getting the list of devices
    property_address = AudioObjectPropertyAddress(
        mSelector=kAudioHardwarePropertyDevices.value,
        mScope=kAudioObjectPropertyScopeGlobal,
        mElement=kAudioObjectPropertyElementMaster
    )
    
    # Get the size of the property data
    property_data_size = c_uint32(0)
    
    status = coreaudio.AudioObjectGetPropertyDataSize(
        kAudioObjectSystemObject,
        byref(property_address),
        0,
        None,
        byref(property_data_size)
    )
    
    if status != 0:
        raise RuntimeError(f"AudioObjectGetPropertyDataSize failed with status {status}")
    
    # Determine the number of devices
    device_count = property_data_size.value // ctypes.sizeof(c_uint32)
    
    # Create an array to hold the device IDs
    device_ids = (c_uint32 * device_count)()
    
    # Get the device IDs
    status = coreaudio.AudioObjectGetPropertyData(
        kAudioObjectSystemObject,
        byref(property_address),
        0,
        None,
        property_data_size.value,
        byref(device_ids)
    )
    
    if status != 0:
        raise RuntimeError(f"AudioObjectGetPropertyData failed with status {status}")
    
    # Get the names of the devices
    for device_id in device_ids:
        property_address.mSelector = kAudioDevicePropertyDeviceNameCFString.value
        
        property_data_size = c_uint32(0)
        status = coreaudio.AudioObjectGetPropertyDataSize(
            device_id,
            byref(property_address),
            0,
            None,
            byref(property_data_size)
        )
        
        if status != 0:
            raise RuntimeError(f"AudioObjectGetPropertyDataSize failed for device {device_id} with status {status}")
        
        # Create a buffer for the device name
        device_name = ctypes.create_string_buffer(property_data_size.value)
        status = coreaudio.AudioObjectGetPropertyData(
            device_id,
            byref(property_address),
            0,
            None,
            property_data_size.value,
            byref(device_name)
        )
        
        if status != 0:
            raise RuntimeError(f"AudioObjectGetPropertyData failed for device {device_id} with status {status}")
        
        devices.append({
            'id': device_id,
            'name': device_name.value.decode('utf-8')
        })
    
    return devices

# List all devices and their IDs
devices = list_audio_devices()
for device in devices:
    print(f"Device ID: {device['id']}, Device Name: {device['name']}")

The Error:

Traceback (most recent call last):
  File "/Users/MYNAME/Desktop/Core_Audio.py", line 112, in <module>
    devices = list_audio_devices()
              ^^^^^^^^^^^^^^^^^^^^
  File "/Users/MYNAME/Desktop/Core_Audio.py", line 53, in list_audio_devices
    raise RuntimeError(f"AudioObjectGetPropertyDataSize failed with status {status}")
RuntimeError: AudioObjectGetPropertyDataSize failed with status 2003332927

I have tried renaming the file, uninstalling an reinstalling all imported files, ChatGPT, Perplexity. The main goal of this code is to just pull the audio sources from different apps so I can separately customize them within MacOS.

Upvotes: 1

Views: 172

Answers (1)

Gordon Childs
Gordon Childs

Reputation: 36169

The error is telling you that you're using an unknown property:

2003332927 = kAudioHardwareUnknownPropertyError = 'who?'

which makes sense because your definition of the property

kAudioHardwarePropertyDevices = ctypes.c_uint32(1)

is wrong. My AudioHardware.h file instead says

kAudioHardwarePropertyDevices = 'dev#' (1684370979).

Similarly, kAudioObjectPropertyScopeGlobal should be 'glob' = 1735159650 and kAudioDevicePropertyDeviceNameCFString should be 'lnam' = 1819173229.

Try that and see if there are any more errors.

Update
The crash was due to AudioObjectGetPropertyData() being incorrectly wrapped: the 5th argument should be a pointer to a UInt32, as the actual number of bytes read is returned via this argument.

After that there's an error trying to convert a CFStringRef to utf-8, because CFStringRef is some kind of object and not a buffer of bytes. Switching to the c string selector, kAudioDevicePropertyDeviceName, is easier than explaining CFStrings to python probably.

Now when I run it, I get this:

gchilds@M1Fist ca-python-wrap % python3 ca-wrap.py
Device ID: 171, Device Name: PacificFist Microphone
Device ID: 166, Device Name: MacBook Pro Microphone
Device ID: 159, Device Name: MacBook Pro Speakers
Device ID: 151, Device Name: LoomAudioDevice
Device ID: 54, Device Name: ✊Aggregate  Device

That's not bad. But the last device's name isn't quite right, it's missing a trailing ✊, so maybe it is worth explaining CFStringRefs to Python, or there's some other bug.

Here's the updated code:

import ctypes
from ctypes import c_uint32, c_void_p, byref, Structure, POINTER, c_int, c_char

# Define constants
kAudioObjectSystemObject = 1
kAudioObjectPropertyScopeGlobal = 1735159650
kAudioObjectPropertyElementMaster = 0
kAudioHardwarePropertyDevices = 1684370979
kAudioDevicePropertyDeviceNameCFString = 1819173229 # 'lnam', uses CFString
kAudioDevicePropertyDeviceName = 1851878757 # 'name'

# Define the AudioObjectPropertyAddress structure
class AudioObjectPropertyAddress(Structure):
    _fields_ = [
        ('mSelector', c_uint32),
        ('mScope', c_uint32),
        ('mElement', c_uint32),  
    ]

# Load CoreAudio framework
coreaudio = ctypes.CDLL('/System/Library/Frameworks/CoreAudio.framework/Versions/A/CoreAudio')

# OLD PATH: '/System/Library/Frameworks/CoreAudio.framework/CoreAudio'

# Define function prototypes
coreaudio.AudioObjectGetPropertyDataSize.argtypes = [c_uint32, POINTER(AudioObjectPropertyAddress), c_uint32, c_void_p, POINTER(c_uint32)]
coreaudio.AudioObjectGetPropertyDataSize.restype = c_int

coreaudio.AudioObjectGetPropertyData.argtypes = [c_uint32, POINTER(AudioObjectPropertyAddress), c_uint32, c_void_p, POINTER(c_uint32), c_void_p]
coreaudio.AudioObjectGetPropertyData.restype = c_int

def list_audio_devices():
    devices = []

    # Define the property address for getting the list of devices
    property_address = AudioObjectPropertyAddress(
        mSelector=kAudioHardwarePropertyDevices,
        mScope=kAudioObjectPropertyScopeGlobal,
        mElement=kAudioObjectPropertyElementMaster
    )
    
    # Get the size of the property data
    property_data_size = c_uint32(0)
    
    status = coreaudio.AudioObjectGetPropertyDataSize(
        kAudioObjectSystemObject,
        byref(property_address),
        0,
        None,
        byref(property_data_size)
    )
    
    if status != 0:
        raise RuntimeError(f"AudioObjectGetPropertyDataSize failed with status {status}")
    
    # Determine the number of devices
    device_count = property_data_size.value // ctypes.sizeof(c_uint32)
    
    # Create an array to hold the device IDs
    device_ids = (c_uint32 * device_count)()
    
    # Get the device IDs
    status = coreaudio.AudioObjectGetPropertyData(
        kAudioObjectSystemObject,
        byref(property_address),
        0,
        None,
        byref(property_data_size),
        byref(device_ids)
    )
    
    if status != 0:
        raise RuntimeError(f"AudioObjectGetPropertyData failed with status {status}")
    
    # Get the names of the devices
    for device_id in device_ids:
        property_address.mSelector = kAudioDevicePropertyDeviceName # kAudioDevicePropertyDeviceNameCFString.value
        
        property_data_size = c_uint32(0)
        status = coreaudio.AudioObjectGetPropertyDataSize(
            device_id,
            byref(property_address),
            0,
            None,
            byref(property_data_size)
        )
        
        if status != 0:
            raise RuntimeError(f"AudioObjectGetPropertyDataSize failed for device {device_id} with status {status}")

        # Create a buffer for the device name
        device_name = ctypes.create_string_buffer(property_data_size.value)
        status = coreaudio.AudioObjectGetPropertyData(
            device_id,
            byref(property_address),
            0,
            None,
            byref(property_data_size),
            byref(device_name)
        )
        
        if status != 0:
            raise RuntimeError(f"AudioObjectGetPropertyData failed for device {device_id} with status {status}")
        
        devices.append({
            'id': device_id,
            'name': device_name.value.decode('utf-8')
        })
    
    return devices

# List all devices and their IDs
devices = list_audio_devices()
for device in devices:
    print(f"Device ID: {device['id']}, Device Name: {device['name']}")

Upvotes: 1

Related Questions