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.

New contributor

Mark Jabre is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật