jdxi_editor.midi.playback.transport

MIDI File Transport Controller

Single owner of MIDI file playback state. Wraps a PlaybackEngine and handles: - Play / pause / stop / seek, with the engine as the only clock - Driving the engine from a deadline-based PlaybackScheduler thread - Publishing state, position, tempo and end-of-song as Qt signals

UI code calls the transport methods and listens to the signals; it never starts timers or reads clocks itself.

Example

>>> transport = TransportController(send=lambda msg: port.send_message(msg.bytes()))
>>> transport.state_changed.connect(transport_widget.set_state)
>>> transport.load(midi_file)
>>> transport.play()

Attributes

POSITION_INTERVAL_MS

FINE_WAIT_S

FINE_STEP_S

JDXI_BANK_MSB

Classes

PreciseClockEngine

PlaybackEngine on time.perf_counter().

_PreciseWaitScheduler

PlaybackScheduler whose waits end on time on Windows too.

_PlayingTimerResolution

Precise timers while playing.

TransportController

Transport for MIDI file playback.

Module Contents

jdxi_editor.midi.playback.transport.POSITION_INTERVAL_MS = 50[source]
jdxi_editor.midi.playback.transport.FINE_WAIT_S = 0.02[source]
jdxi_editor.midi.playback.transport.FINE_STEP_S = 0.001[source]
class jdxi_editor.midi.playback.transport.PreciseClockEngine[source]

Bases: picomidi.playback.engine.PlaybackEngine

PlaybackEngine on time.perf_counter().

The engine reads time.monotonic(), which on Windows before Python 3.13 moves in 15.6 ms steps: woken on time, the engine saw an older time and sent the note later (measured on Windows 11: ~8 ms late on average, up to 19 ms). perf_counter() is precise to the microsecond everywhere.

static _now() → float[source]
class jdxi_editor.midi.playback.transport._PreciseWaitScheduler[source]

Bases: picomidi.playback.scheduler.PlaybackScheduler

PlaybackScheduler whose waits end on time on Windows too.

An Event.wait() there ends on the 15.6 ms timer tick, so the wait is an Event.wait() for all but its last 20 ms, then 1 ms sleeps that still look for a wake (pause, seek…) between them. Other systems: as is.

_wait_for_wake(timeout)[source]
class jdxi_editor.midi.playback.transport._PlayingTimerResolution[source]

Precise timers while playing.

Windows: 1 ms timer resolution (timeBeginPeriod); a thread’s timed wait otherwise ends on the 15.6 ms tick and the scheduler wakes up late. macOS: a latency-critical activity, so the system does not throttle the timers (App Nap, timer coalescing: notes were up to 150 ms late). Elsewhere nothing is needed.

_on = False[source]
_activity = None[source]
set(on: bool) → None[source]
jdxi_editor.midi.playback.transport.JDXI_BANK_MSB[source]
class jdxi_editor.midi.playback.transport.TransportController(engine: picomidi.playback.engine.PlaybackEngine | None = None, send: Callable[[mido.Message], None] | None = None, parent: PySide6.QtCore.QObject | None = None, position_interval_ms: int = POSITION_INTERVAL_MS, scheduler_max_sleep_s: float = 1.0, scheduler_interval_ms: int = 5)[source]

Bases: PySide6.QtCore.QObject

Transport for MIDI file playback.

Signals:

state_changed(TransportState): after every transport state change position_changed(float): position in seconds, while playing and after seeks tempo_changed(int): tempo in µs per beat when it changes at the position finished(): playback reached the end of the file (position is rewound) loop_changed(): loop on/off or the A–B range changed

state_changed[source]
position_changed[source]
tempo_changed[source]
finished[source]
loop_changed[source]
_scheduler_ended[source]
engine[source]
send = None[source]
_sounding: dict[tuple[int, int], float][source]
block_foreign_program_changes = True[source]
_bank_msb: dict[int, int][source]
_timer_resolution[source]
_lock[source]
_loaded = False[source]
_last_state[source]
_last_tempo: int | None = None[source]
_scheduler: picomidi.playback.scheduler.PlaybackScheduler | None = None[source]
_scheduler_max_sleep_s = 1.0[source]
_loop_range_ticks: tuple[int, int] | None = None[source]
_position_timer[source]
property state: picomidi.playback.engine.TransportState[source]
property is_loaded: bool[source]
property position_seconds: float[source]
property position_tick: int[source]
property duration_seconds: float[source]
property speed: float[source]
property loop_enabled: bool[source]
property loop_range_seconds: tuple[float, float] | None[source]

A–B range in seconds, or None when the whole file loops.

bar_beat_at_seconds(seconds: float) → tuple[int, int][source]

1-based (bar, beat) at seconds, following tempo map and time signatures.

nearest_bar_seconds(seconds: float) → float[source]

Time of the bar line nearest to seconds.

load(midi_file: mido.MidiFile, keep_position: bool = False) → None[source]

Load midi_file into the engine.

Parameters:
  • midi_file – File to play

  • keep_position – Keep the current position (in ticks, so a tempo change stays on the same beat) and play/pause state, e.g. after editing the file that is playing

play() → bool[source]

Play from the current position, or resume if paused.

Returns:

True if playing afterwards

pause() → None[source]

Pause, keeping the position; sounding notes are released.

toggle_play_pause() → None[source]

Pause if playing, otherwise play.

stop() → None[source]

Stop, silence the instrument and rewind to the start.

seek_seconds(seconds: float) → None[source]

Move to seconds without changing the play/pause/stop state.

set_loop(enabled: bool, start_seconds: float | None = None, end_seconds: float | None = None) → None[source]

Turn looping on or off.

Parameters:
  • enabled – Loop when the end of the range is reached

  • start_seconds – A point; with end_seconds sets an A–B loop

  • end_seconds – B point; leave both None to keep the current range

clear_loop_range() → None[source]

Loop the whole file instead of an A–B range (on/off is unchanged).

set_speed(speed: float) → None[source]

Set playback speed (1.0 = file tempo; 0.25–2.0).

set_send_clock(enabled: bool) → None[source]

Send MIDI clock, Start/Stop/Continue and Song Position Pointer.

panic() → None[source]

Send note-offs and All Sound Off / All Notes Off on every channel.

set_mutes(muted_tracks: set[int], muted_channels: set[int]) → None[source]

Apply mute state; takes effect immediately during playback.

Parameters:
  • muted_tracks – 0-based track indexes to mute

  • muted_channels – 0-based MIDI channels to mute

set_track_velocity_scales(scales: dict[int, float]) → None[source]

Scale tracks’ note-on velocities; takes effect on the next notes.

Parameters:

scales – 0-based track index -> factor (absent = 1.0)

set_track_volumes(volumes: dict[int, float]) → None[source]

Set tracks’ overall note-on levels (0.0–1.0); next notes only.

Parameters:

volumes – 0-based track index -> level (absent = 1.0)

set_track_transposes(semitones: dict[int, int]) → None[source]

Transpose tracks by semitones; takes effect on the next notes.

Parameters:

semitones – 0-based track index -> offset (absent = 0)

set_track_channel_maps(channels: dict[int, int | None]) → None[source]

Remap tracks to output MIDI channels; None restores the file channel.

Parameters:

channels – 0-based track index -> channel 0–15 or None

set_suppression(program_changes: bool, control_changes: bool, foreign_program_changes: bool | None = None) → None[source]

Choose whether program changes / control changes in the file are sent.

Parameters:

foreign_program_changes – block the program changes that do not choose a JD-Xi sound (None: leave as it is)

shutdown() → None[source]

Stop playback and the scheduler thread (safe to call more than once).

_on_engine_event(msg: mido.Message) → None[source]
_jdxi_program_change(msg: mido.Message) → bool[source]

Whether msg may go to the JD-Xi as far as program changes go.

A program change chooses a JD-Xi sound only after a JD-Xi Bank Select (MSB 85 programs, 86 drum kits, 94 analog tones, 95 digital tones). The General MIDI ones of a file made for another instrument (no bank, or MSB 0 / 120 / 121) pick a sound of whatever bank the part is in: another sound than the one set in the editors, unsaved edits lost. With block_foreign_program_changes they are not sent.

DOUBLED_WITHIN_S = 0.03[source]
_one_voice_per_key(msg: mido.Message, now: float | None = None) → list[mido.Message][source]

Never start a second voice on a key that is already sounding.

Two tracks on one channel playing the same note send two note-ons and, by design of the engine, a single note-off when the last one ends. The JD-Xi starts a voice per note-on and a note-off may end only one of them: the other hangs until Panic.

  • The same note started at the same moment by another track (within DOUBLED_WITHIN_S): the second note-on is dropped, as if the part were played once. Retriggering it cut every note of a doubled bass on the mono Analog part.

  • A later note-on on a key still sounding: the note is retriggered, its note-off first, still one voice for the one note-off to come.

_ensure_scheduler() → None[source]
_wake_scheduler() → None[source]
_on_scheduler_ended_from_thread() → None[source]

EOF callback from the scheduler thread; marshal onto the Qt thread.

_on_scheduler_ended() → None[source]

End of file reached (queued from the scheduler thread).

_after_command() → None[source]

Publish state and position, and run the position timer only while playing.

_emit_position() → None[source]