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
Classes
PlaybackEngine on time.perf_counter(). |
|
PlaybackScheduler whose waits end on time on Windows too. |
|
Precise timers while playing. |
|
Transport for MIDI file playback. |
Module Contents
- class jdxi_editor.midi.playback.transport.PreciseClockEngine[source]
Bases:
picomidi.playback.engine.PlaybackEnginePlaybackEngine 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.
- class jdxi_editor.midi.playback.transport._PreciseWaitScheduler[source]
Bases:
picomidi.playback.scheduler.PlaybackSchedulerPlaybackScheduler 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.
- 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.
- 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.QObjectTransport 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
- 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.
- 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
- 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
- set_send_clock(enabled: bool) None[source]
Send MIDI clock, Start/Stop/Continue and Song Position Pointer.
- 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)
- _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.
- _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.
- _on_scheduler_ended_from_thread() None[source]
EOF callback from the scheduler thread; marshal onto the Qt thread.