Skip to content

Add Coreaudio ID to Portaudio device index mapping - #1165

Open
douglasheld2 wants to merge 2 commits into
PortAudio:masterfrom
douglasheld2:patch-1
Open

Add Coreaudio ID to Portaudio device index mapping#1165
douglasheld2 wants to merge 2 commits into
PortAudio:masterfrom
douglasheld2:patch-1

Conversation

@douglasheld2

Copy link
Copy Markdown

It is sometimes useful to be able to correlate a coreaudio device to a Portaudio device, especially when multiple coreaudio devices have the same name and need to be differentiated by their ID numbers.

After compiling in this extra function, the IDs can be correlated with the following function in python:

from cffi import FFI

_pa_extra_ffi = FFI()
_pa_extra_ffi.cdef("""
    int PaMacCore_GetDeviceIndexForAudioDeviceID(unsigned int id);
""")

_pa_extra_lib = _pa_extra_ffi.dlopen(
    "/Users/doug/Library/Python/3.12/lib/python/site-packages/_sounddevice_data/portaudio-binaries/libportaudio.dylib"
)


def get_portaudio_index_from_coreaudio_id(target_core_id):
    """
    Convert a macOS CoreAudio AudioDeviceID into a PortAudio/sounddevice index.
    """
    index = _pa_extra_lib.PaMacCore_GetDeviceIndexForAudioDeviceID(target_core_id)

    if index < 0:
        return None

    return index

I have also removed two unused variables i in unrelated parts of the file.

It is sometimes useful to be able to correlate a coreaudio device to a Portaudio device, especially when multiple coreaudio devices have the same name and need to be differentiated by their ID numbers.

After compiling in this extra function, the IDs can be correlated with the following function in python:

from cffi import FFI

_pa_extra_ffi = FFI()
_pa_extra_ffi.cdef("""
    int PaMacCore_GetDeviceIndexForAudioDeviceID(unsigned int id);
""")

_pa_extra_lib = _pa_extra_ffi.dlopen(
    "/Users/doug/Library/Python/3.12/lib/python/site-packages/_sounddevice_data/portaudio-binaries/libportaudio.dylib"
)


def get_portaudio_index_from_coreaudio_id(target_core_id):
    """
    Convert a macOS CoreAudio AudioDeviceID into a PortAudio/sounddevice index.
    """
    index = _pa_extra_lib.PaMacCore_GetDeviceIndexForAudioDeviceID(target_core_id)

    if index < 0:
        return None

    return index

@RossBencina RossBencina left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have also removed two unused variables i in unrelated parts of the file.

We welcome this kind of cleanup in separate PRs, but please keep each PRs focused to a single purpose. [I've removed the unrelated changes myself, hope you don't mind.]

Comment thread src/hostapi/coreaudio/pa_mac_core.c
Comment thread src/hostapi/coreaudio/pa_mac_core.c
Co-authored-by: Ross Bencina <rossb@audiomulch.com>

@RossBencina RossBencina left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Phil and I have reviewed this PR and have decided to reject the approach taken here. We would accept a new PR for the inverse function:

AudioDeviceId PaMacCore_DeviceIndexToAudioDeviceID(PaDeviceIndex index)

This would return kAudioDeviceUnknown for any lookup failures. Note that the implementation can use PaUtil_DeviceIndexToHostApiDeviceIndex to get the index into the devIds array but be sure to check for errors.

We would prefer this approach because:

  • The implementation would be simpler
  • It facilitates lookup both ways (client code can implement the search that your PR implements)
  • It is consistent with other PortAudio host API extension functions

@philburk

philburk commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator

CI is failing with whitespace errors:
error: src/hostapi/coreaudio/pa_mac_core.c(2900) contains tab
error: src/hostapi/coreaudio/pa_mac_core.c(2901) contains tab
error: src/hostapi/coreaudio/pa_mac_core.c(2900) trailing whitespace:
b' '

@philburk philburk added this to the V19.9 milestone Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants