jdxi_editor.audio.portaudio

PortAudio’s picture of the audio hardware, and how to make it current.

PortAudio builds its device list once, in Pa_Initialize, and never looks again. A process that outlives a change to the hardware – AirPods connecting, headphones unplugged, a dock pulled out – keeps asking for devices by positions that have since moved or gone, and CoreAudio answers that with paInternalError (-9986). Opening the default output fails the same way, because PortAudio resolved which device that was at startup too.

That is what wedges SoundFont playback after a route change: the editor runs for hours, the headphones come off, and every attempt to open an output from then on fails until the application is restarted.

The only remedy PortAudio offers is to terminate it and initialise it again. That is process-wide: it tears down every open stream, not only the one being reopened. So anything else that is listening registers itself here with holding(), and a refresh is refused while a holder is active rather than silencing a recording in progress.

Attributes

_SCOPE

_lock

_holders

_tokens

_INDEX_SUFFIX

_plain_names

Functions

_sounddevice()

The installed sounddevice module, or None.

hold(label) → int

Record that a stream is open, so a refresh will not close it.

release(token) → None

Drop a hold taken by hold(). An unknown token is ignored.

holding(label) → Iterator[int]

Hold for the duration of a block.

holders() → list[str]

Labels of the streams currently open.

refresh_devices([module]) → bool

Make PortAudio read the hardware again.

host_api_name(device, apis) → str

Name of the host API a device belongs to, or "".

label_devices(devices, apis, channels_key[, suffix]) → list[tuple[str, int]]

Devices with at least one channel, as (label, index).

plain_name(label) → str

A device's own name, without the suffix added for a repeated one.

Module Contents

jdxi_editor.audio.portaudio._SCOPE = 'portaudio'[source]
jdxi_editor.audio.portaudio._lock[source]
jdxi_editor.audio.portaudio._holders: dict[int, str][source]
jdxi_editor.audio.portaudio._tokens[source]
jdxi_editor.audio.portaudio._sounddevice()[source]

The installed sounddevice module, or None.

Imported on use: the library is optional, and importing it loads PortAudio.

Returns:

module or None

jdxi_editor.audio.portaudio.hold(label: str) → int[source]

Record that a stream is open, so a refresh will not close it.

Parameters:

label – str what is listening, for the log

Returns:

int token to pass to release()

jdxi_editor.audio.portaudio.release(token: int | None) → None[source]

Drop a hold taken by hold(). An unknown token is ignored.

Parameters:

token – Optional[int]

Returns:

None

jdxi_editor.audio.portaudio.holding(label: str) → Iterator[int][source]

Hold for the duration of a block.

Parameters:

label – str what is listening

Returns:

Iterator[int] the token

jdxi_editor.audio.portaudio.holders() → list[str][source]

Labels of the streams currently open.

Returns:

list[str]

jdxi_editor.audio.portaudio.refresh_devices(module=None) → bool[source]

Make PortAudio read the hardware again.

Refused, and nothing is torn down, while another stream is open: see the module docstring. The caller’s own stream must already be closed.

Parameters:

module – sounddevice, for tests; the installed library otherwise

Returns:

bool True when the device list was rebuilt

jdxi_editor.audio.portaudio._INDEX_SUFFIX[source]
jdxi_editor.audio.portaudio._plain_names: dict[str, str][source]
jdxi_editor.audio.portaudio.host_api_name(device, apis: list) → str[source]

Name of the host API a device belongs to, or "".

Parameters:
  • device – a sounddevice device record

  • apis – the result of query_hostapis()

Returns:

str

jdxi_editor.audio.portaudio.label_devices(devices, apis: list, channels_key: str, suffix: Callable[[dict], str] | None = None) → list[tuple[str, int]][source]

Devices with at least one channel, as (label, index).

A name that occurs once is the label. A repeated one is told apart by its host API, and only when that is repeated too – a pair of identical interfaces – by its position, which is the last resort because it moves.

Parameters:
  • devices – the result of query_devices()

  • apis – the result of query_hostapis(); [] is tolerated

  • channels_key – str “max_input_channels” or “max_output_channels”

  • suffix – Optional[Callable] extra text for a label, from the record

Returns:

list[tuple[str, int]]

jdxi_editor.audio.portaudio.plain_name(label: str) → str[source]

A device’s own name, without the suffix added for a repeated one.

Looked up in _plain_names, so no parenthesis in a genuine device name is ever mistaken for the suffix. A label this module did not hand out – one built by hand, or stored by an older version – falls back to dropping a trailing `` (index)``.

Parameters:

label – str device label

Returns:

str