Source code for jdxi_editor.ui.editors.midi_player.automation

"""
Automation Widget class
"""

from typing import Any

from PySide6.QtWidgets import QComboBox, QGridLayout, QGroupBox

from jdxi_editor.core.jdxi import JDXi
from jdxi_editor.midi.playback.state import MidiPlaybackState
from jdxi_editor.ui.editors.helpers.preset import preset_to_jdxi_bank_pc
from jdxi_editor.ui.editors.helpers.widgets import create_jdxi_button_from_spec
from jdxi_editor.ui.editors.midi_player.helper import (
    create_widget_cell_with_button_spec,
)
from jdxi_editor.midi.bank import Bank
from jdxi_editor.ui.dialogs.program_change import DEFAULT_SOURCE_FOR_CHANNEL
from jdxi_editor.ui.midi.selection_info import MidiWireInfo
from jdxi_editor.ui.preset.source import PresetSource
from jdxi_editor.ui.preset.utils import convert_preset_dict_to_list
from jdxi_editor.ui.widgets.editor.helper import (
    create_group_and_grid_layout,
    create_icon_and_label,
)
from jdxi_editor.ui.widgets.jdxi.midi_group import JDXiMidiGroup
from jdxi_editor.ui.widgets.midi.selection_info_bar import MidiSelectionInfoBar
from picoui.specs.widgets import ButtonSpec


[docs] class AutomationWidget(JDXiMidiGroup): """Automation Widget class""" def __init__(self, midi_state: MidiPlaybackState, parent: "MidiFilePlayer"): super().__init__(parent=parent, midi_state=midi_state)
[docs] self.group_title = "Automation"
[docs] self.automation_channel_combo: QComboBox | None = None
[docs] self.automation_type_combo: QComboBox | None = None
[docs] self.automation_program_combo: QComboBox | None = None
[docs] self.automation_midi_info_bar: MidiSelectionInfoBar | None = None
self.setup_ui()
[docs] def _build_button_specs(self) -> dict[str, ButtonSpec]: """Build button specs""" return { "automation_insert": ButtonSpec( label="Insert Program Change Here", tooltip="Insert Program Change at current position", icon=JDXi.UI.Icon.ADD, slot=self.insert_program_change_current_position, ) }
[docs] def _build_group(self) -> QGroupBox: """_build group""" row = 0 group, grid = create_group_and_grid_layout(self.group_title) automation_layout, automation_label = create_icon_and_label( label="Automation:", icon=JDXi.UI.Icon.MAGIC ) grid.addLayout(automation_layout, row, 0) self.automation_channel_combo = QComboBox() for ch in range(1, 17): self.automation_channel_combo.addItem(f"Ch {ch}", ch) self.automation_channel_combo.currentIndexChanged.connect( self.on_automation_channel_changed ) grid.addWidget(self.automation_channel_combo, row, 1) self.automation_type_combo = QComboBox() self.automation_type_combo.addItems(["Digital", "Analog", "Drums"]) self.automation_type_combo.currentIndexChanged.connect( self.on_automation_type_changed ) grid.addWidget(self.automation_type_combo, row, 2) self.automation_program_combo = QComboBox() self.automation_program_combo.currentIndexChanged.connect( self._update_automation_midi_info ) grid.addWidget(self.automation_program_combo, row, 3) spec = self.specs["buttons"]["automation_insert"] self.automation_insert_button = create_jdxi_button_from_spec( spec, checkable=False ) insert_cell, self.automation_insert_label = create_widget_cell_with_button_spec( spec, self.automation_insert_button ) grid.addWidget(insert_cell, row, 4) row += 1 self.automation_midi_info_bar = MidiSelectionInfoBar() grid.addWidget(self.automation_midi_info_bar, row, 0, 1, 5) return group
[docs] def populate_automation_programs(self, source: PresetSource) -> None: """ Populate the program combo based on source list. source: "Digital" | "Analog" | "Drums" """ self.automation_program_combo.clear() preset_list_sources = { PresetSource.DIGITAL: JDXi.UI.Preset.Digital.PROGRAM_CHANGE, PresetSource.ANALOG: JDXi.UI.Preset.Analog.PROGRAM_CHANGE, PresetSource.DRUMS: JDXi.UI.Preset.Drum.PROGRAM_CHANGE, } preset_list_source = preset_list_sources.get( source, JDXi.UI.Preset.Drum.PROGRAM_CHANGE ) preset_list = convert_preset_dict_to_list(preset_list_source) self._add_items_to_automation_combo(preset_list) self._update_automation_midi_info()
[docs] def _add_items_to_automation_combo( self, preset_list: list[dict[str, str | Any]] | Any ): """items to combo box""" for item in preset_list: label = f"{str(item.get('id')).zfill(3)} {item.get('name')}" msb = int(item.get("msb")) lsb = int(item.get("lsb")) pc = int(item.get("pc")) self.automation_program_combo.addItem(label, (msb, lsb, pc))
[docs] def on_automation_type_changed(self, _: int) -> None: """ Show the presets of the chosen synth type. The combo shows "Digital" / "Analog" / "Drums", the values of :class:`PresetSource`; the preset lists are keyed by the enum, so the text must be converted (looking it up as a string found nothing and fell back to the drum kits). :param _: int combo index :return: None """ source = PresetSource(self.automation_type_combo.currentText()) self.populate_automation_programs(source)
[docs] def on_automation_channel_changed(self, _: int) -> None: """ Offer the presets of the JD-Xi part on the chosen channel. Each part only accepts its own banks: Digital tones are bank 95, Analog tones 94 and drum kits 86. A Digital preset inserted on channel 3 (the Analog part) is ignored by the JD-Xi, so the type follows the channel: Analog for channel 3, Drums for channel 10, Digital otherwise. It can still be changed afterwards for a non-standard channel setup. :param _: int combo index :return: None """ channel = int(self.automation_channel_combo.currentData()) - 1 source = DEFAULT_SOURCE_FOR_CHANNEL.get(channel, PresetSource.DIGITAL) index = self.automation_type_combo.findText(source.value) if index != self.automation_type_combo.currentIndex(): self.automation_type_combo.setCurrentIndex(index) # repopulates else: self._update_automation_midi_info()
[docs] def _update_automation_midi_info(self, *_args) -> None: """Refresh MIDI wire readout for automation program + channel selection.""" if not self.automation_midi_info_bar: return data = ( self.automation_program_combo.currentData() if self.automation_program_combo else None ) if not data: self.automation_midi_info_bar.set_message("MIDI —") return msb, lsb, pc = data display_channel = int(self.automation_channel_combo.currentData()) channel = display_channel - 1 bank_msb, bank_lsb, midi_pc = preset_to_jdxi_bank_pc( int(msb), int(lsb), int(pc) ) info = MidiWireInfo( channel=channel, bank=Bank(msb=bank_msb, lsb=bank_lsb), program=midi_pc, ) self.automation_midi_info_bar.set_info(info)
[docs] def insert_program_change_current_position(self) -> None: """ Insert Bank Select (CC#0, CC#32) and Program Change at the current slider time. """ if not self.parent: return self.parent.insert_program_change_current_position()