jdxi_editor.audio.soundfont_output

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

BLOCK_FRAMES

OUTPUT_LATENCY

AUDIO_SWITCH_INTERVAL

_SCALE

_sounddevice_module

_fluidsynth_module

_INSTALLED

_BUILTIN_OUTPUT_NAMES

_SCOPE_GLOBAL

_SCOPE_OUTPUT

_ELEMENT_MAIN

_SYSTEM_OBJECT

_PROP_DEVICES

_PROP_DEFAULT_OUTPUT

_PROP_NAME

_PROP_TRANSPORT

_PROP_DATA_SOURCE

_PROP_STREAMS

_TRANSPORT_BUILTIN

_coreaudio

MIDI_CHANNELS

DRUM_CHANNEL

DRUM_BANK

MIDI_PORT_NAME

Classes

_AudioAddress

Structure base class

SoundfontMidiPort

FluidSynth's own MIDI input: a virtual MIDI port (on macOS a CoreMIDI

SoundfontOutput

A FluidSynth and the output stream that plays it.

Functions

sounddevice_available() → bool

Whether sounddevice imported.

output_devices([module]) → list[tuple[str, int]]

Output devices as (display name, sounddevice index).

chosen_output(current, saved, labels) → str

Which output label to put back after the combo is rebuilt.

output_device_for(index, devices, *[, platform, ...]) → Optional[int]

The sounddevice index to open.

default_output_route() → Optional[tuple]

(device id, data source) of the macOS default output.

_coreaudio_library()

CoreAudio, loaded once. None when the framework cannot be opened.

_audio_property(obj, selector, scope, size)

(ok, buffer) for one CoreAudio property.

_unindexed_name(label) → str

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

_builtin_output_name(label) → bool

True for the built-in speaker and headphone-jack names.

_coreaudio_outputs() → list[tuple[int, str]]

Output devices as (CoreAudio id, name).

_coreaudio_transport(label) → Optional[str]

CoreAudio transport of the output named label (bltn, ``usb ``, …).

_coreaudio_default_output() → Optional[int]

_coreaudio_data_source(device) → bytes

The output data source (ispk, hdpn), or empty when it has none.

opened_output_name(device_index, devices) → str

The name of the output a stream actually opened.

copy_s16(pcm, out) → None

Convert interleaved int16 stereo into a float32 (frames, 2) buffer.

_select_every_channel(synth, sfid) → None

Give all sixteen MIDI channels a program from the SoundFont.

_delete_midi_driver(fs)

pyfluidsynth binds new_fluid_midi_driver but not its delete.

_delete_midi_router(fs)

_device_label(index, devices) → str

The label listed for an index, or "" for the default.

_new_synth()

_output_stream(**kwargs)

Module Contents

jdxi_editor.audio.soundfont_output.BLOCK_FRAMES = 512[source]
jdxi_editor.audio.soundfont_output.OUTPUT_LATENCY = 0.05[source]
jdxi_editor.audio.soundfont_output.AUDIO_SWITCH_INTERVAL = 0.001[source]
jdxi_editor.audio.soundfont_output._SCALE[source]
jdxi_editor.audio.soundfont_output._sounddevice_module = None[source]
jdxi_editor.audio.soundfont_output._fluidsynth_module = None[source]
jdxi_editor.audio.soundfont_output.sounddevice_available() → bool[source]

Whether sounddevice imported.

Returns:

bool

jdxi_editor.audio.soundfont_output._INSTALLED[source]
jdxi_editor.audio.soundfont_output.output_devices(module=_INSTALLED) → list[tuple[str, int]][source]

Output devices as (display name, sounddevice index).

A repeated name is told apart by its host API; see label_devices(). module=None reports that sounddevice is absent. Omit module to query the installed library.

Parameters:

module – sounddevice, or None when the library did not import

Returns:

list

jdxi_editor.audio.soundfont_output.chosen_output(current: str, saved: str, labels: list[str]) → str[source]

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.

Parameters:
  • current – str the label showing before the rebuild

  • saved – str the stored preference

  • labels – list device labels, without the default entry

Returns:

str a label from labels, or "" for the default

jdxi_editor.audio.soundfont_output.output_device_for(index: int | None, devices: list[tuple[str, int]], *, platform: str | None = None, transport_type: Callable[[str], str | None] | None = None) → int | None[source]

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.

Parameters:
  • index – Optional[int] chosen sounddevice index

  • devices – list of (label, index)

  • platform – str override for tests; the running OS otherwise

  • transport_type – Callable name -> CoreAudio transport, or None to read it. "bltn" is the built-in output

Returns:

Optional[int]

jdxi_editor.audio.soundfont_output.default_output_route() → tuple | None[source]

(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.

Returns:

Optional[tuple]

jdxi_editor.audio.soundfont_output._BUILTIN_OUTPUT_NAMES[source]
jdxi_editor.audio.soundfont_output._SCOPE_GLOBAL = 1735159650[source]
jdxi_editor.audio.soundfont_output._SCOPE_OUTPUT = 1869968496[source]
jdxi_editor.audio.soundfont_output._ELEMENT_MAIN = 0[source]
jdxi_editor.audio.soundfont_output._SYSTEM_OBJECT = 1[source]
jdxi_editor.audio.soundfont_output._PROP_DEVICES = 1684370979[source]
jdxi_editor.audio.soundfont_output._PROP_DEFAULT_OUTPUT = 1682929012[source]
jdxi_editor.audio.soundfont_output._PROP_NAME = 1851878757[source]
jdxi_editor.audio.soundfont_output._PROP_TRANSPORT = 1953653102[source]
jdxi_editor.audio.soundfont_output._PROP_DATA_SOURCE = 1936945763[source]
jdxi_editor.audio.soundfont_output._PROP_STREAMS = 1937009955[source]
jdxi_editor.audio.soundfont_output._TRANSPORT_BUILTIN = 'bltn'[source]
class jdxi_editor.audio.soundfont_output._AudioAddress[source]

Bases: ctypes.Structure

Structure base class

_fields_[source]
jdxi_editor.audio.soundfont_output._coreaudio = None[source]
jdxi_editor.audio.soundfont_output._coreaudio_library()[source]

CoreAudio, loaded once. None when the framework cannot be opened.

jdxi_editor.audio.soundfont_output._audio_property(obj: int, selector: int, scope: int, size: int)[source]

(ok, buffer) for one CoreAudio property.

jdxi_editor.audio.soundfont_output._unindexed_name(label: str) → str[source]

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

Parameters:

label – str device label

Returns:

str

jdxi_editor.audio.soundfont_output._builtin_output_name(label: str) → bool[source]

True for the built-in speaker and headphone-jack names.

jdxi_editor.audio.soundfont_output._coreaudio_outputs() → list[tuple[int, str]][source]

Output devices as (CoreAudio id, name).

jdxi_editor.audio.soundfont_output._coreaudio_transport(label: str) → str | None[source]

CoreAudio transport of the output named label (bltn, ``usb ``, …).

None when it cannot be read, or when several outputs share the name and disagree.

Parameters:

label – str sounddevice label

Returns:

Optional[str]

jdxi_editor.audio.soundfont_output._coreaudio_default_output() → int | None[source]
jdxi_editor.audio.soundfont_output._coreaudio_data_source(device: int) → bytes[source]

The output data source (ispk, hdpn), or empty when it has none.

jdxi_editor.audio.soundfont_output.opened_output_name(device_index: int | None, devices: list[tuple[str, int]]) → str[source]

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.

Parameters:
  • device_index – Optional[int]

  • devices – list of (name, index)

Returns:

str the name, or "" when the index is unknown

jdxi_editor.audio.soundfont_output.copy_s16(pcm: numpy.ndarray, out: numpy.ndarray) → None[source]

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.

Parameters:
  • pcm – np.ndarray int16, interleaved left, right

  • out – np.ndarray float32, shape (frames, 2)

Returns:

None

jdxi_editor.audio.soundfont_output.MIDI_CHANNELS = 16[source]
jdxi_editor.audio.soundfont_output.DRUM_CHANNEL = 9[source]
jdxi_editor.audio.soundfont_output.DRUM_BANK = 128[source]
jdxi_editor.audio.soundfont_output._select_every_channel(synth, sfid: int) → None[source]

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.

Parameters:
  • synth – the FluidSynth instance

  • sfid – int the loaded SoundFont’s id

Returns:

None

jdxi_editor.audio.soundfont_output.MIDI_PORT_NAME = 'SoundFont (JD-Xi Editor)'[source]
class jdxi_editor.audio.soundfont_output.SoundfontMidiPort(synth, port_name: str = MIDI_PORT_NAME)[source]

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.

_fs[source]
router[source]
driver[source]
close() → None[source]

Close the port, then the router. Before the synth is deleted.

_delete_router() → None[source]
jdxi_editor.audio.soundfont_output._delete_midi_driver(fs)[source]

pyfluidsynth binds new_fluid_midi_driver but not its delete.

jdxi_editor.audio.soundfont_output._delete_midi_router(fs)[source]
class jdxi_editor.audio.soundfont_output.SoundfontOutput(lock: threading.RLock)[source]

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.

_lock[source]
synth = None[source]
sfid = None[source]
_stream = None[source]
_pcm_buf = None[source]
_pcm: numpy.ndarray | None = None[source]
_logged_render = False[source]
midi_port = None[source]
underflows = 0[source]
_switch_interval: float | None = None[source]
follows_default = False[source]
_route: tuple | None = None[source]
_sf_path = ''[source]
_requested_device: int | None = None[source]
_requested_name = ''[source]
on_reopened: Callable[[], None] | None = None[source]
start(sf_path: str, device: int | None = None, stream_factory: Callable | None = None, synth_factory: Callable | None = None, midi_port_factory: Callable | None = None) → int[source]

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.

Parameters:
  • sf_path – str

  • device – Optional[int] sounddevice index

  • stream_factory – Optional[Callable] builds the output stream

  • synth_factory – Optional[Callable] builds the synth

  • midi_port_factory – Optional[Callable] opens the synth’s MIDI port (SoundfontMidiPort); it is optional, so playback carries on without it

Returns:

int SoundFont id

_resolve_device() → int | None[source]

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.

Returns:

Optional[int] sounddevice index, None for the default

_open_stream(opener: Callable)[source]

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.

Parameters:

opener – Callable builds the stream

Returns:

tuple (the open stream, the index it opened)

reopen() → int[source]

Open the same SoundFont on the route chosen last time.

Used when the macOS default output changes underneath a stream that is following it.

Returns:

int SoundFont id

take_route_change() → bool[source]

True once when the followed system route has changed.

Returns:

bool

opened_device() → int | None[source]

The output index the stream opened, or None when it is closed.

Returns:

Optional[int]

stop() → None[source]

Close the stream, then delete the synth.

Returns:

None

_allocate() → None[source]
_hand_over_sooner() → None[source]
_restore_switch_interval() → None[source]
_callback(outdata, frames, _time_info, status) → None[source]
_render(outdata, frames: int) → None[source]
jdxi_editor.audio.soundfont_output._device_label(index: int | None, devices: list[tuple[str, int]]) → str[source]

The label listed for an index, or "" for the default.

Parameters:
  • index – Optional[int] sounddevice index

  • devices – list of (label, index)

Returns:

str

jdxi_editor.audio.soundfont_output._new_synth()[source]
jdxi_editor.audio.soundfont_output._output_stream(**kwargs)[source]