jdxi_editor.midi.playback.transport =================================== .. py:module:: jdxi_editor.midi.playback.transport .. autoapi-nested-parse:: 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. .. rubric:: 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 ---------- .. autoapisummary:: jdxi_editor.midi.playback.transport.POSITION_INTERVAL_MS jdxi_editor.midi.playback.transport.FINE_WAIT_S jdxi_editor.midi.playback.transport.FINE_STEP_S jdxi_editor.midi.playback.transport.JDXI_BANK_MSB Classes ------- .. autoapisummary:: jdxi_editor.midi.playback.transport.PreciseClockEngine jdxi_editor.midi.playback.transport._PreciseWaitScheduler jdxi_editor.midi.playback.transport._PlayingTimerResolution jdxi_editor.midi.playback.transport.TransportController Module Contents --------------- .. py:data:: POSITION_INTERVAL_MS :value: 50 .. py:data:: FINE_WAIT_S :value: 0.02 .. py:data:: FINE_STEP_S :value: 0.001 .. py:class:: PreciseClockEngine Bases: :py:obj:`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. .. py:method:: _now() -> float :staticmethod: .. py:class:: _PreciseWaitScheduler Bases: :py:obj:`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. .. py:method:: _wait_for_wake(timeout) .. py:class:: _PlayingTimerResolution 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. .. py:attribute:: _on :value: False .. py:attribute:: _activity :value: None .. py:method:: set(on: bool) -> None .. py:data:: JDXI_BANK_MSB .. py:class:: TransportController(engine: Optional[picomidi.playback.engine.PlaybackEngine] = None, send: Optional[Callable[[mido.Message], None]] = None, parent: Optional[PySide6.QtCore.QObject] = None, position_interval_ms: int = POSITION_INTERVAL_MS, scheduler_max_sleep_s: float = 1.0, scheduler_interval_ms: int = 5) Bases: :py:obj:`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 .. py:attribute:: state_changed .. py:attribute:: position_changed .. py:attribute:: tempo_changed .. py:attribute:: finished .. py:attribute:: loop_changed .. py:attribute:: _scheduler_ended .. py:attribute:: engine .. py:attribute:: send :value: None .. py:attribute:: _sounding :type: dict[tuple[int, int], float] .. py:attribute:: block_foreign_program_changes :value: True .. py:attribute:: _bank_msb :type: dict[int, int] .. py:attribute:: _timer_resolution .. py:attribute:: _lock .. py:attribute:: _loaded :value: False .. py:attribute:: _last_state .. py:attribute:: _last_tempo :type: Optional[int] :value: None .. py:attribute:: _scheduler :type: Optional[picomidi.playback.scheduler.PlaybackScheduler] :value: None .. py:attribute:: _scheduler_max_sleep_s :value: 1.0 .. py:attribute:: _loop_range_ticks :type: Optional[tuple[int, int]] :value: None .. py:attribute:: _position_timer .. py:property:: state :type: picomidi.playback.engine.TransportState .. py:property:: is_loaded :type: bool .. py:property:: position_seconds :type: float .. py:property:: position_tick :type: int .. py:property:: duration_seconds :type: float .. py:property:: speed :type: float .. py:property:: loop_enabled :type: bool .. py:property:: loop_range_seconds :type: Optional[tuple[float, float]] A–B range in seconds, or None when the whole file loops. .. py:method:: bar_beat_at_seconds(seconds: float) -> tuple[int, int] 1-based (bar, beat) at *seconds*, following tempo map and time signatures. .. py:method:: nearest_bar_seconds(seconds: float) -> float Time of the bar line nearest to *seconds*. .. py:method:: load(midi_file: mido.MidiFile, keep_position: bool = False) -> None Load *midi_file* into the engine. :param midi_file: File to play :param 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 .. py:method:: play() -> bool Play from the current position, or resume if paused. :return: True if playing afterwards .. py:method:: pause() -> None Pause, keeping the position; sounding notes are released. .. py:method:: toggle_play_pause() -> None Pause if playing, otherwise play. .. py:method:: stop() -> None Stop, silence the instrument and rewind to the start. .. py:method:: seek_seconds(seconds: float) -> None Move to *seconds* without changing the play/pause/stop state. .. py:method:: set_loop(enabled: bool, start_seconds: Optional[float] = None, end_seconds: Optional[float] = None) -> None Turn looping on or off. :param enabled: Loop when the end of the range is reached :param start_seconds: A point; with *end_seconds* sets an A–B loop :param end_seconds: B point; leave both None to keep the current range .. py:method:: clear_loop_range() -> None Loop the whole file instead of an A–B range (on/off is unchanged). .. py:method:: set_speed(speed: float) -> None Set playback speed (1.0 = file tempo; 0.25–2.0). .. py:method:: set_send_clock(enabled: bool) -> None Send MIDI clock, Start/Stop/Continue and Song Position Pointer. .. py:method:: panic() -> None Send note-offs and All Sound Off / All Notes Off on every channel. .. py:method:: set_mutes(muted_tracks: set[int], muted_channels: set[int]) -> None Apply mute state; takes effect immediately during playback. :param muted_tracks: 0-based track indexes to mute :param muted_channels: 0-based MIDI channels to mute .. py:method:: set_track_velocity_scales(scales: dict[int, float]) -> None Scale tracks' note-on velocities; takes effect on the next notes. :param scales: 0-based track index -> factor (absent = 1.0) .. py:method:: set_track_volumes(volumes: dict[int, float]) -> None Set tracks' overall note-on levels (0.0–1.0); next notes only. :param volumes: 0-based track index -> level (absent = 1.0) .. py:method:: set_track_transposes(semitones: dict[int, int]) -> None Transpose tracks by semitones; takes effect on the next notes. :param semitones: 0-based track index -> offset (absent = 0) .. py:method:: set_track_channel_maps(channels: dict[int, Optional[int]]) -> None Remap tracks to output MIDI channels; None restores the file channel. :param channels: 0-based track index -> channel 0–15 or None .. py:method:: set_suppression(program_changes: bool, control_changes: bool, foreign_program_changes: Optional[bool] = None) -> None Choose whether program changes / control changes in the file are sent. :param foreign_program_changes: block the program changes that do not choose a JD-Xi sound (None: leave as it is) .. py:method:: shutdown() -> None Stop playback and the scheduler thread (safe to call more than once). .. py:method:: _on_engine_event(msg: mido.Message) -> None .. py:method:: _jdxi_program_change(msg: mido.Message) -> bool 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. .. py:attribute:: DOUBLED_WITHIN_S :value: 0.03 .. py:method:: _one_voice_per_key(msg: mido.Message, now: Optional[float] = None) -> list[mido.Message] 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. .. py:method:: _ensure_scheduler() -> None .. py:method:: _wake_scheduler() -> None .. py:method:: _on_scheduler_ended_from_thread() -> None EOF callback from the scheduler thread; marshal onto the Qt thread. .. py:method:: _on_scheduler_ended() -> None End of file reached (queued from the scheduler thread). .. py:method:: _after_command() -> None Publish state and position, and run the position timer only while playing. .. py:method:: _emit_position() -> None