jdxi_editor.audio.portaudio =========================== .. py:module:: jdxi_editor.audio.portaudio .. autoapi-nested-parse:: 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 :func:`holding`, and a refresh is refused while a holder is active rather than silencing a recording in progress. Attributes ---------- .. autoapisummary:: jdxi_editor.audio.portaudio._SCOPE jdxi_editor.audio.portaudio._lock jdxi_editor.audio.portaudio._holders jdxi_editor.audio.portaudio._tokens jdxi_editor.audio.portaudio._INDEX_SUFFIX jdxi_editor.audio.portaudio._plain_names Functions --------- .. autoapisummary:: jdxi_editor.audio.portaudio._sounddevice jdxi_editor.audio.portaudio.hold jdxi_editor.audio.portaudio.release jdxi_editor.audio.portaudio.holding jdxi_editor.audio.portaudio.holders jdxi_editor.audio.portaudio.refresh_devices jdxi_editor.audio.portaudio.host_api_name jdxi_editor.audio.portaudio.label_devices jdxi_editor.audio.portaudio.plain_name Module Contents --------------- .. py:data:: _SCOPE :value: 'portaudio' .. py:data:: _lock .. py:data:: _holders :type: dict[int, str] .. py:data:: _tokens .. py:function:: _sounddevice() The installed sounddevice module, or None. Imported on use: the library is optional, and importing it loads PortAudio. :return: module or None .. py:function:: hold(label: str) -> int Record that a stream is open, so a refresh will not close it. :param label: str what is listening, for the log :return: int token to pass to :func:`release` .. py:function:: release(token: Optional[int]) -> None Drop a hold taken by :func:`hold`. An unknown token is ignored. :param token: Optional[int] :return: None .. py:function:: holding(label: str) -> Iterator[int] Hold for the duration of a block. :param label: str what is listening :return: Iterator[int] the token .. py:function:: holders() -> list[str] Labels of the streams currently open. :return: list[str] .. py:function:: refresh_devices(module=None) -> bool 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. :param module: sounddevice, for tests; the installed library otherwise :return: bool True when the device list was rebuilt .. py:data:: _INDEX_SUFFIX .. py:data:: _plain_names :type: dict[str, str] .. py:function:: host_api_name(device, apis: list) -> str Name of the host API a device belongs to, or ``""``. :param device: a sounddevice device record :param apis: the result of ``query_hostapis()`` :return: str .. py:function:: label_devices(devices, apis: list, channels_key: str, suffix: Optional[Callable[[dict], str]] = None) -> list[tuple[str, int]] 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. :param devices: the result of ``query_devices()`` :param apis: the result of ``query_hostapis()``; ``[]`` is tolerated :param channels_key: str "max_input_channels" or "max_output_channels" :param suffix: Optional[Callable] extra text for a label, from the record :return: list[tuple[str, int]] .. py:function:: plain_name(label: str) -> str A device's own name, without the suffix added for a repeated one. Looked up in :data:`_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)``. :param label: str device label :return: str