jdxi_editor.audio.soundfont_output ================================== .. py:module:: jdxi_editor.audio.soundfont_output .. autoapi-nested-parse:: SoundFont playback through an output the application chooses. FluidSynth renders each block. sounddevice opens the device. The synth's own audio driver is left closed, so a device name never has to match CoreAudio's list. Attributes ---------- .. autoapisummary:: jdxi_editor.audio.soundfont_output.BLOCK_FRAMES jdxi_editor.audio.soundfont_output.OUTPUT_LATENCY jdxi_editor.audio.soundfont_output.AUDIO_SWITCH_INTERVAL jdxi_editor.audio.soundfont_output._SCALE jdxi_editor.audio.soundfont_output._sounddevice_module jdxi_editor.audio.soundfont_output._fluidsynth_module jdxi_editor.audio.soundfont_output._INSTALLED jdxi_editor.audio.soundfont_output._BUILTIN_OUTPUT_NAMES jdxi_editor.audio.soundfont_output._SCOPE_GLOBAL jdxi_editor.audio.soundfont_output._SCOPE_OUTPUT jdxi_editor.audio.soundfont_output._ELEMENT_MAIN jdxi_editor.audio.soundfont_output._SYSTEM_OBJECT jdxi_editor.audio.soundfont_output._PROP_DEVICES jdxi_editor.audio.soundfont_output._PROP_DEFAULT_OUTPUT jdxi_editor.audio.soundfont_output._PROP_NAME jdxi_editor.audio.soundfont_output._PROP_TRANSPORT jdxi_editor.audio.soundfont_output._PROP_DATA_SOURCE jdxi_editor.audio.soundfont_output._PROP_STREAMS jdxi_editor.audio.soundfont_output._TRANSPORT_BUILTIN jdxi_editor.audio.soundfont_output._coreaudio jdxi_editor.audio.soundfont_output.MIDI_CHANNELS jdxi_editor.audio.soundfont_output.DRUM_CHANNEL jdxi_editor.audio.soundfont_output.DRUM_BANK jdxi_editor.audio.soundfont_output.MIDI_PORT_NAME Classes ------- .. autoapisummary:: jdxi_editor.audio.soundfont_output._AudioAddress jdxi_editor.audio.soundfont_output.SoundfontMidiPort jdxi_editor.audio.soundfont_output.SoundfontOutput Functions --------- .. autoapisummary:: jdxi_editor.audio.soundfont_output.sounddevice_available jdxi_editor.audio.soundfont_output.output_devices jdxi_editor.audio.soundfont_output.chosen_output jdxi_editor.audio.soundfont_output.output_device_for jdxi_editor.audio.soundfont_output.default_output_route jdxi_editor.audio.soundfont_output._coreaudio_library jdxi_editor.audio.soundfont_output._audio_property jdxi_editor.audio.soundfont_output._unindexed_name jdxi_editor.audio.soundfont_output._builtin_output_name jdxi_editor.audio.soundfont_output._coreaudio_outputs jdxi_editor.audio.soundfont_output._coreaudio_transport jdxi_editor.audio.soundfont_output._coreaudio_default_output jdxi_editor.audio.soundfont_output._coreaudio_data_source jdxi_editor.audio.soundfont_output.opened_output_name jdxi_editor.audio.soundfont_output.copy_s16 jdxi_editor.audio.soundfont_output._select_every_channel jdxi_editor.audio.soundfont_output._delete_midi_driver jdxi_editor.audio.soundfont_output._delete_midi_router jdxi_editor.audio.soundfont_output._device_label jdxi_editor.audio.soundfont_output._new_synth jdxi_editor.audio.soundfont_output._output_stream Module Contents --------------- .. py:data:: BLOCK_FRAMES :value: 512 .. py:data:: OUTPUT_LATENCY :value: 0.05 .. py:data:: AUDIO_SWITCH_INTERVAL :value: 0.001 .. py:data:: _SCALE .. py:data:: _sounddevice_module :value: None .. py:data:: _fluidsynth_module :value: None .. py:function:: sounddevice_available() -> bool Whether sounddevice imported. :return: bool .. py:data:: _INSTALLED .. py:function:: output_devices(module=_INSTALLED) -> list[tuple[str, int]] Output devices as ``(display name, sounddevice index)``. A repeated name is told apart by its host API; see :func:`~jdxi_editor.audio.portaudio.label_devices`. ``module=None`` reports that sounddevice is absent. Omit ``module`` to query the installed library. :param module: sounddevice, or None when the library did not import :return: list .. py:function:: chosen_output(current: str, saved: str, labels: list[str]) -> str Which output label to put back after the combo is rebuilt. The label on screen wins, then the saved name. ``""`` means the default output. An explicit ``(Default)`` stays the default, so a saved device does not replace it. :param current: str the label showing before the rebuild :param saved: str the stored preference :param labels: list device labels, without the default entry :return: str a label from ``labels``, or ``""`` for the default .. py:function:: output_device_for(index: Optional[int], devices: list[tuple[str, int]], *, platform: Optional[str] = None, transport_type: Optional[Callable[[str], Optional[str]]] = None) -> Optional[int] The sounddevice index to open. On macOS the built-in output is opened as the system default (``None``), so CoreAudio follows the headphone jack. Every other selected device keeps its index. ``None`` stays the default on every platform. :param index: Optional[int] chosen sounddevice index :param devices: list of ``(label, index)`` :param platform: str override for tests; the running OS otherwise :param transport_type: Callable name -> CoreAudio transport, or None to read it. ``"bltn"`` is the built-in output :return: Optional[int] .. py:function:: default_output_route() -> Optional[tuple] ``(device id, data source)`` of the macOS default output. The data source changes when the headphone jack is plugged or unplugged on the same device. ``None`` off macOS, or when CoreAudio cannot be read. :return: Optional[tuple] .. py:data:: _BUILTIN_OUTPUT_NAMES .. py:data:: _SCOPE_GLOBAL :value: 1735159650 .. py:data:: _SCOPE_OUTPUT :value: 1869968496 .. py:data:: _ELEMENT_MAIN :value: 0 .. py:data:: _SYSTEM_OBJECT :value: 1 .. py:data:: _PROP_DEVICES :value: 1684370979 .. py:data:: _PROP_DEFAULT_OUTPUT :value: 1682929012 .. py:data:: _PROP_NAME :value: 1851878757 .. py:data:: _PROP_TRANSPORT :value: 1953653102 .. py:data:: _PROP_DATA_SOURCE :value: 1936945763 .. py:data:: _PROP_STREAMS :value: 1937009955 .. py:data:: _TRANSPORT_BUILTIN :value: 'bltn' .. py:class:: _AudioAddress Bases: :py:obj:`ctypes.Structure` Structure base class .. py:attribute:: _fields_ .. py:data:: _coreaudio :value: None .. py:function:: _coreaudio_library() CoreAudio, loaded once. None when the framework cannot be opened. .. py:function:: _audio_property(obj: int, selector: int, scope: int, size: int) ``(ok, buffer)`` for one CoreAudio property. .. py:function:: _unindexed_name(label: str) -> str A device's own name, without the suffix added for a repeated one. :param label: str device label :return: str .. py:function:: _builtin_output_name(label: str) -> bool True for the built-in speaker and headphone-jack names. .. py:function:: _coreaudio_outputs() -> list[tuple[int, str]] Output devices as ``(CoreAudio id, name)``. .. py:function:: _coreaudio_transport(label: str) -> Optional[str] CoreAudio transport of the output named *label* (``bltn``, ``usb ``, …). ``None`` when it cannot be read, or when several outputs share the name and disagree. :param label: str sounddevice label :return: Optional[str] .. py:function:: _coreaudio_default_output() -> Optional[int] .. py:function:: _coreaudio_data_source(device: int) -> bytes The output data source (``ispk``, ``hdpn``), or empty when it has none. .. py:function:: opened_output_name(device_index: Optional[int], devices: list[tuple[str, int]]) -> str The name of the output a stream actually opened. Looked up from the device index the stream reports, so a default combo entry cannot rename it. :param device_index: Optional[int] :param devices: list of ``(name, index)`` :return: str the name, or ``""`` when the index is unknown .. py:function:: copy_s16(pcm: numpy.ndarray, out: numpy.ndarray) -> None Convert interleaved int16 stereo into a float32 ``(frames, 2)`` buffer. Samples are scaled by ``1/32768``. Frames past the int16 block are silence. The float buffer is filled in place. :param pcm: np.ndarray int16, interleaved left, right :param out: np.ndarray float32, shape ``(frames, 2)`` :return: None .. py:data:: MIDI_CHANNELS :value: 16 .. py:data:: DRUM_CHANNEL :value: 9 .. py:data:: DRUM_BANK :value: 128 .. py:function:: _select_every_channel(synth, sfid: int) -> None Give all sixteen MIDI channels a program from the SoundFont. Only channel 1 used to be selected. A note sent to it was heard -- which is what the Test Note button does -- while a MIDI file, the keyboard and every JD-Xi part but Digital 1 play on other channels and were silent. :param synth: the FluidSynth instance :param sfid: int the loaded SoundFont's id :return: None .. py:data:: MIDI_PORT_NAME :value: 'SoundFont (JD-Xi Editor)' .. py:class:: SoundfontMidiPort(synth, port_name: str = MIDI_PORT_NAME) FluidSynth's own MIDI input: a virtual MIDI port (on macOS a CoreMIDI destination) that plays the synth. ``Synth.start`` used to open one along with FluidSynth's audio driver, and that port is what offered the SoundFont as a MIDI Output. Rendering now goes through our own stream, so the port is opened here on its own: a MIDI router into the synth, and a MIDI driver feeding the router. .. py:attribute:: _fs .. py:attribute:: router .. py:attribute:: driver .. py:method:: close() -> None Close the port, then the router. Before the synth is deleted. .. py:method:: _delete_router() -> None .. py:function:: _delete_midi_driver(fs) pyfluidsynth binds new_fluid_midi_driver but not its delete. .. py:function:: _delete_midi_router(fs) .. py:class:: SoundfontOutput(lock: threading.RLock) A FluidSynth and the output stream that plays it. ``start`` does not call ``Synth.start``. Rendering happens in the stream callback, into a buffer allocated with the stream. .. py:attribute:: _lock .. py:attribute:: synth :value: None .. py:attribute:: sfid :value: None .. py:attribute:: _stream :value: None .. py:attribute:: _pcm_buf :value: None .. py:attribute:: _pcm :type: Optional[numpy.ndarray] :value: None .. py:attribute:: _logged_render :value: False .. py:attribute:: midi_port :value: None .. py:attribute:: underflows :value: 0 .. py:attribute:: _switch_interval :type: Optional[float] :value: None .. py:attribute:: follows_default :value: False .. py:attribute:: _route :type: Optional[tuple] :value: None .. py:attribute:: _sf_path :value: '' .. py:attribute:: _requested_device :type: Optional[int] :value: None .. py:attribute:: _requested_name :value: '' .. py:attribute:: on_reopened :type: Optional[Callable[[], None]] :value: None .. py:method:: start(sf_path: str, device: Optional[int] = None, stream_factory: Optional[Callable] = None, synth_factory: Optional[Callable] = None, midi_port_factory: Optional[Callable] = None) -> int Load a SoundFont and open the output. An output that is already open is stopped first. ``device`` None is the default output. On macOS the built-in output is opened the same way, so the headphone jack follows the system route. :param sf_path: str :param device: Optional[int] sounddevice index :param stream_factory: Optional[Callable] builds the output stream :param synth_factory: Optional[Callable] builds the synth :param midi_port_factory: Optional[Callable] opens the synth's MIDI port (SoundfontMidiPort); it is optional, so playback carries on without it :return: int SoundFont id .. py:method:: _resolve_device() -> Optional[int] The index to open now, for the device whose name was chosen. Looked up afresh every time. An index is a position in PortAudio's list, and that list is rebuilt whenever the hardware changes, so a remembered one can point at a different device or at nothing. A chosen device that has gone falls back to the system default rather than to whatever now sits at its old position. :return: Optional[int] sounddevice index, None for the default .. py:method:: _open_stream(opener: Callable) Open the output, rereading PortAudio's device list if it refuses. PortAudio reads the hardware once, at startup, so a stream opened after headphones have come and gone asks for a device that has moved or disappeared -- paInternalError, and the same for the default output, whose identity was resolved just as early. One retry on a freshly read list is the difference between playback following the route and nothing working until restart. The index is resolved again for that retry: the reread is what moves it. :param opener: Callable builds the stream :return: tuple (the open stream, the index it opened) .. py:method:: reopen() -> int Open the same SoundFont on the route chosen last time. Used when the macOS default output changes underneath a stream that is following it. :return: int SoundFont id .. py:method:: take_route_change() -> bool True once when the followed system route has changed. :return: bool .. py:method:: opened_device() -> Optional[int] The output index the stream opened, or None when it is closed. :return: Optional[int] .. py:method:: stop() -> None Close the stream, then delete the synth. :return: None .. py:method:: _allocate() -> None .. py:method:: _hand_over_sooner() -> None .. py:method:: _restore_switch_interval() -> None .. py:method:: _callback(outdata, frames, _time_info, status) -> None .. py:method:: _render(outdata, frames: int) -> None .. py:function:: _device_label(index: Optional[int], devices: list[tuple[str, int]]) -> str The label listed for an index, or ``""`` for the default. :param index: Optional[int] sounddevice index :param devices: list of ``(label, index)`` :return: str .. py:function:: _new_synth() .. py:function:: _output_stream(**kwargs)