Source code for jdxi_editor.ui.editors.synth.editor

"""
synth_editor.py

This module defines the `SynthEditor` class, a base class for all editor windows in the JD-Xi Manager application.
It provides an interface for editing synthesizer parameters, handling MIDI messages, and updating UI components.

Key Features:
- UI Elements: Uses PySide6 widgets, including ComboBoxes, Sliders, and SpinBoxes, to adjust synthesizer parameters.
- MIDI Integration: Sends and receives MIDI messages via `MIDIHelper`, supporting parameter changes, SysEx communication,
  and program change handling.
- Preset Management: Loads, updates, and applies instrument presets with `PresetHandler` and `PresetLoader`.
- Parameter Control: Dynamically creates UI controls for synthesizer parameters, supporting bipolar values and digital conversion.
- Shortcuts: Implements keyboard shortcuts for refreshing data and closing the window.

Dependencies:
- PySide6 for the UI components.
- `jdxi_manager.midi` for MIDI communication.
- `jdxi_manager.midi.data.parameter` for synthesizer parameter handling.
- `jdxi_manager.ui.style` for applying UI styles.

"""

import json
import os
import re
from typing import Optional

from decologr import Decologr as log
from PySide6.QtCore import Qt, QTimer, Signal
from PySide6.QtGui import QKeySequence, QPixmap, QShortcut, QShowEvent
from PySide6.QtWidgets import QWidget

from jdxi_editor.core.jdxi import JDXi
from jdxi_editor.ui.midi.selection_info import send_tone_with_expectation
from jdxi_editor.midi.channel.channel import MidiChannel
from jdxi_editor.midi.data.address import JDXiSysExOffsetSuperNATURALLMB
from jdxi_editor.midi.data.address.address import (
    JDXiSysExAddress,
    JDXiSysExAddressStartMSB,
    JDXiSysExOffsetTemporaryToneUMB,
)
from jdxi_editor.midi.data.drum.data import DRUM_PARTIAL_MAP
from jdxi_editor.midi.io.helper import MidiIOHelper
from jdxi_editor.midi.playback.activity import DeviceRefresher
from jdxi_editor.midi.sysex.parser.json_parser import JDXiJsonSysexParser
from jdxi_editor.midi.sysex.request.data import SYNTH_PARTIAL_MAP
from jdxi_editor.midi.sysex.sections import SysExSection
from jdxi_editor.resources import resource_path
from jdxi_editor.ui.editors.digital.utils import (
    filter_sysex_keys,
    get_area,
    get_partial_number,
)
from jdxi_editor.ui.editors.helpers.preset import (
    get_preset_parameter_value,
)
from jdxi_editor.ui.editors.synth.base import SynthBase
from jdxi_editor.ui.editors.synth.helper import log_changes
from jdxi_editor.ui.editors.synth.specs import (
    DRUM_KIT_SPECS,
    ENGINE_KEYWORDS,
    INSTRUMENT_FAMILY_SPECS,
    any_keyword_in,
    engine_image_basenames,
    preset_image_basenames,
    preset_match_texts,
    InstrumentDescriptor,
)
from jdxi_editor.ui.preset.helper import JDXiPresetHelper
from jdxi_editor.ui.style.editor_style import mark_sheen_panels

[docs] TONE_DISPATCH = { JDXiSysExOffsetSuperNATURALLMB.COMMON: "_update_common_controls", JDXiSysExOffsetSuperNATURALLMB.MODIFY: "_update_modify_controls", }
[docs] SYNTH_CAPABILITIES = { JDXiSysExOffsetTemporaryToneUMB.DRUM_KIT: { "partials": DRUM_PARTIAL_MAP, "supports_common": False, }, JDXiSysExOffsetTemporaryToneUMB.ANALOG_SYNTH: { "partials": SYNTH_PARTIAL_MAP, "supports_common": True, }, }
[docs] def get_storage_scope(msb: int) -> str: return JDXiSysExAddressStartMSB(msb).name
[docs] def get_editor_target(msb: int, umb: int) -> str: if msb == JDXiSysExAddressStartMSB.TEMPORARY_PROGRAM: return "PROGRAM" return JDXiSysExOffsetTemporaryToneUMB(umb).name
[docs] class SynthEditor(SynthBase): """Base class for all editor windows"""
[docs] parameter_received = Signal(list, int) # address, value
def __init__( self, midi_helper: Optional[object] = None, parent: Optional[QWidget] = None, address: Optional[JDXiSysExAddress] = None, ): super().__init__( midi_helper=midi_helper, parent=parent, address=address ) # Dict of JDXiSynth Types
[docs] self.partial_map = SYNTH_PARTIAL_MAP
[docs] self.sysex_current_data = None
[docs] self.preset_preset_list = None
[docs] self.programs = None
# Use passed-in midi_helper so MIDI sending works (e.g. Analog editor); only create new if None if midi_helper is None: self.midi_helper = MidiIOHelper() # Re-reads this editor's values after changes made on the JD-Xi # (debounced, and held back while a MIDI file or pattern plays)
[docs] self.device_refresher = DeviceRefresher( lambda: self.data_request(), self.isVisible, parent=self )
self.midi_helper.midi_program_changed.connect(self._handle_program_change) self.midi_helper.midi_control_changed.connect(self._handle_control_change)
[docs] self.cc_parameters = dict()
[docs] self.nrpn_parameters = dict()
[docs] self.nrpn_map = dict()
# self.controls from SynthBase (ControlRegistry)
[docs] self.bipolar_parameters = list()
# Midi request for Temporary program
[docs] self.midi_requests = list()
[docs] self.instrument_default_image = None
[docs] self.instrument_title_label = None
[docs] self.instrument_image_label = None
[docs] self.instrument_icon_folder = None
[docs] self.partial_number = None
[docs] self.preset_helper = None
[docs] self.instrument_selection_combo = None
[docs] self.preset_type = None
if hasattr(self, "instrument_title_label"): self.midi_helper.update_tone_name.connect( lambda title, synth_type: self.set_instrument_title_label( title, synth_type ) ) log.parameter( scope=self.__class__.__name__, message="Initialized:", parameter=self.__class__.__name__, ) log.parameter( scope=self.__class__.__name__, message="---> Using MIDI helper:", parameter=midi_helper, ) # midi message bytes # To be over-ridden by subclasses # Set window flags: frameless window for editor (consistent with main instrument window) self.setWindowFlags( Qt.WindowType.Window | Qt.WindowType.FramelessWindowHint ) # --- Apply common style JDXi.UI.Theme.apply_editor_style(self) # Outermost panels get the Instrument window's sheen once the subclass # has built them (runs on the next event loop pass; context = self) QTimer.singleShot(0, self, lambda: mark_sheen_panels(self)) # --- Add keyboard shortcuts
[docs] self.refresh_shortcut = QShortcut(QKeySequence.StandardKey.Refresh, self)
self.refresh_shortcut.activated.connect(self.data_request) # --- Add close window shortcut
[docs] self.close_shortcut = QShortcut(QKeySequence.StandardKey.Close, self)
self.close_shortcut.activated.connect(self.close) # --- Common minimum size for all editors self.setMinimumSize(200, 200) # Connect to program change signal if MIDI helper exists if self.midi_helper: self.midi_helper.midi_program_changed.connect(self._handle_program_change) self.midi_helper.midi_control_changed.connect(self._handle_control_change) # self.midi_helper.midi_sysex_json.connect(self._dispatch_sysex_to_area) self.preset_loader = JDXiPresetHelper( self.midi_helper, JDXi.UI.Preset.Digital.ENUMERATED ) # Initialize preset handlers dynamically preset_configs = [ ( JDXi.Synth.DIGITAL_SYNTH_1, JDXi.UI.Preset.Digital.ENUMERATED, MidiChannel.DIGITAL_SYNTH_1, ), ( JDXi.Synth.DIGITAL_SYNTH_2, JDXi.UI.Preset.Digital.ENUMERATED, MidiChannel.DIGITAL_SYNTH_2, ), ( JDXi.Synth.ANALOG_SYNTH, JDXi.UI.Preset.Analog.ENUMERATED, MidiChannel.ANALOG_SYNTH, ), ( JDXi.Synth.DRUM_KIT, JDXi.UI.Preset.Drum.ENUMERATED, MidiChannel.DRUM_KIT, ), ] self.preset_helpers = { synth_type: JDXiPresetHelper( self.midi_helper, presets, channel=channel, preset_type=synth_type ) for synth_type, presets, channel in preset_configs } log.message( scope=self.__class__.__name__, message="MIDI helper initialized" ) else: log.message( scope=self.__class__.__name__, message="MIDI helper not initialized" )
[docs] self.json_parser = JDXiJsonSysexParser()
[docs] def __str__(self): return f"{self.__class__.__name__}"
[docs] def __repr__(self): return f"{self.__class__.__name__}"
[docs] def _init_synth_data( self, synth_type: str = JDXi.Synth.DIGITAL_SYNTH_1, partial_number: Optional[int] = 0, ): """Initialize synth-specific data.""" from jdxi_editor.core.synth.factory import create_synth_data self.synth_data = create_synth_data(synth_type, partial_number=partial_number) # Dynamically assign attributes for attr in [ "address", "preset_type", "instrument_default_image", "instrument_icon_folder", "presets", "preset_list", "midi_requests", "midi_channel", ]: setattr(self, attr, getattr(self.synth_data, attr))
[docs] def showEvent(self, event: QShowEvent) -> None: """ Override showEvent to request current settings from the instrument when the editor is shown. This ensures the sliders pick up the current settings from the instrument, similar to Digital 1, Digital 2, and Analog synth editors. :param event: QShowEvent """ super().showEvent(event) if self.midi_helper: log.message( "🎛️Effects Editor shown - requesting current settings from instrument", scope="SynthEditor", ) self.device_refresher.request_now()
[docs] def get_controls_as_dict(self): """ Get the current values of all controls (see all_controls) as a dictionary. :returns: dict A dictionary of control parameter names and their values. """ controls_data = {} for param, widget in self.all_controls().items(): try: if hasattr(widget, "value"): val = getattr(widget, "value") controls_data[param.name] = val() if callable(val) else val else: log.debug( scope=self.__class__.__name__, message=f"Skipping {param.name}: widget has no value() ({type(widget).__name__})", ) except RuntimeError: log.warning( scope=self.__class__.__name__, message=f"Widget for {param.name} was deleted, skipping", ) except Exception as ex: log.warning( scope=self.__class__.__name__, message=f"Failed to read {param.name}: {ex}", ) return controls_data
[docs] def _get_preset_helper_for_current_synth(self): """Return the appropriate preset handler based on the current synth preset_type.""" handler = self.preset_helpers.get(self.preset_type) if handler is None: log.warning( scope=self.__class__.__name__, message=f"Unknown synth preset_type: {self.preset_type}, defaulting to digital_1", ) return self.preset_helpers[JDXi.Synth.DIGITAL_SYNTH_1] # Safe fallback return handler
[docs] def dispatch_sysex_to_area(self, json_sysex_data: str) -> None: """ Dispatch SysEx data to the appropriate area for processing. :param json_sysex_data: :return: None """ sysex_data = self._parse_sysex_json(json_sysex_data) if not sysex_data: return # log.message(f"self.address.msb, self.address.umb, {self.address.msb}, {self.address.umb}") current_synth = get_area([self.address.msb, self.address.umb]) temporary_area = sysex_data.get(SysExSection.TEMPORARY_AREA) synth_tone = sysex_data.get(SysExSection.SYNTH_TONE) if current_synth != temporary_area: log.message( scope=self.__class__.__name__, message=f"temporary_area: {temporary_area} is not current_synth: {current_synth}, Skipping update", silent=True, ) return log.header_message( scope=self.__class__.__name__, message=f"Updating UI components from SysEx data for {temporary_area} {synth_tone}", ) sysex_data = filter_sysex_keys(sysex_data) successes, failures = [], [] if temporary_area == JDXiSysExOffsetTemporaryToneUMB.DRUM_KIT.name: partial_map = DRUM_PARTIAL_MAP else: partial_map = SYNTH_PARTIAL_MAP partial_number = get_partial_number(synth_tone, partial_map=partial_map) if temporary_area == JDXiSysExOffsetTemporaryToneUMB.ANALOG_SYNTH.name: self._update_controls(partial_number, sysex_data, successes, failures) if synth_tone == JDXiSysExOffsetSuperNATURALLMB.COMMON.name: self._update_common_controls( partial_number, sysex_data, successes, failures ) elif synth_tone == JDXiSysExOffsetSuperNATURALLMB.MODIFY.name: self._update_modify_controls( partial_number, sysex_data, successes, failures ) else: # --- Drums and Digital 1 & 2 are dealt with via partials if partial_number is None: log.error( scope=self.__class__.__name__, message=f"Unknown partial number for synth_tone: {synth_tone}", ) return log.parameter( scope=self.__class__.__name__, message="partial_number _update_controls", parameter=partial_number, ) self._update_controls(partial_number, sysex_data, successes, failures) log.debug_info( successes=successes, failures=failures, scope=self.__class__.__name__ )
[docs] def _update_controls( self, partial_no: int, sysex_data: dict, successes: list, failures: list ) -> None: """ Apply updates to the UI components based on the received SysEx data. :param partial_no: int :param sysex_data: dict :param successes: list :param failures: list :return: None By default has no partials, so subclass to implement partial updates """ raise NotImplementedError( "should be over-ridden in a sub class with implementation" )
[docs] def _parse_sysex_json(self, json_sysex_data: str) -> Optional[dict]: """ _parse_sysex_json :param json_sysex_data: str :return: dict """ try: data = self.json_parser.parse_json(json_sysex_data) self.sysex_previous_data = self.sysex_current_data self.sysex_current_data = data log_changes(self.sysex_previous_data, data) return data except json.JSONDecodeError as ex: log.message( scope=self.__class__.__name__, message=f"Invalid JSON format: {ex}" ) return None
[docs] def set_instrument_title_label(self, name: str, synth_type: str): """ set_instrument_title_label :param name: str :param synth_type: str :return: None """ if self.preset_type == synth_type: # Get title label from widget or direct attribute title_label = None if hasattr(self, "instrument_preset") and self.instrument_preset: title_label = getattr( self.instrument_preset, "instrument_title_label", None ) if not title_label: title_label = getattr(self, "instrument_title_label", None) if title_label: title_label.setText(name) self._sync_preset_selection_to_tone(name, title_label) self.tone_names[synth_type] = name
[docs] def _sync_preset_selection_to_tone(self, name: str, title_label=None) -> None: """ Point the preset combo (and the display's BANK/PC line) at the preset named *name*, the tone the synth reported. Without a match (an edited or user tone) the BANK/PC line is cleared rather than describing another sound. Nothing is sent to the synth. :param name: str tone name read from the synth :param title_label: DigitalTitle or None """ combo = self._get_instrument_selection_combo() if combo is None or not hasattr(combo, "options"): return target = name.strip().casefold() for option, value in zip(combo.options, combo.values): # Options read "001 - TR-909 Kit 1" preset_name = option.split(" - ", 1)[-1].strip().casefold() if preset_name == target: combo.setValue(value) # signals blocked: selects, does not load self.update_instrument_image() info = getattr(getattr(self, "instrument_preset", None), "tone_midi_info", None) if info is not None: info.refresh(value) return if title_label is not None and hasattr(title_label, "set_wire_info"): title_label.set_wire_info(None)
[docs] def update_combo_box_index(self, preset_number): """Updates the QComboBox to reflect the loaded preset.""" log.message( scope=self.__class__.__name__, message=f"Updating combo to preset {preset_number}", ) combo_box = self._get_instrument_selection_combo() if combo_box: combo_box.combo_box.setCurrentIndex(preset_number)
[docs] def update_instrument_title(self): """ update instrument title :return: """ selected_synth_text = self._get_selected_instrument_text() log.message( scope=self.__class__.__name__, message=f"selected_synth_text: {selected_synth_text}", ) # --- Get title label from widget or direct attribute title_label = None if hasattr(self, "instrument_preset") and self.instrument_preset: title_label = getattr( self.instrument_preset, "instrument_title_label", None ) if not title_label: title_label = getattr(self, "instrument_title_label", None) if title_label: title_label.setText(selected_synth_text)
[docs] def update_instrument_preset(self, text: str): """ update_instrument_preset :param text: :return: """ selected_synth_text = self._get_selected_instrument_text() if synth_matches := re.search( r"(\d{3}): (\S+).+", selected_synth_text, re.IGNORECASE ): selected_synth_padded_number = ( synth_matches.group(1).lower().replace("&", "_").split("_")[0] ) one_based_preset_index = int(selected_synth_padded_number) log.message( scope=self.__class__.__name__, message=f"preset_index: {one_based_preset_index}", ) self.load_preset(one_based_preset_index - 1) # use 0-based index
[docs] def load_preset(self, preset_index, *, request_data: bool = True): """Load a preset by program change. :param request_data: If True, follow with data_request(); set False when the caller will run read_from_jdxi() for tracked readback. """ # --- Get the combo box - it might be in instrument_preset widget or directly on self combo_box = self._get_instrument_selection_combo() if not combo_box: log.error( scope=self.__class__.__name__, message="Instrument selection combo box is not available", ) return preset_name = combo_box.combo_box.currentText() # Get the selected preset name log.message( scope=self.__class__.__name__, message=f"combo box preset_name : {preset_name}", ) program_number = preset_name[:3] log.message( scope=self.__class__.__name__, message=f"combo box program_number : {program_number}", ) # --- Determine preset list if not already set if self.preset_preset_list is None: # Determine preset list based on preset_type if ( self.preset_type == JDXi.Synth.DIGITAL_SYNTH_1 or self.preset_type == JDXi.Synth.DIGITAL_SYNTH_2 ): self.preset_preset_list = JDXi.UI.Preset.Digital.PROGRAM_CHANGE elif self.preset_type == JDXi.Synth.ANALOG_SYNTH: self.preset_preset_list = JDXi.UI.Preset.Analog.PROGRAM_CHANGE elif self.preset_type == JDXi.Synth.DRUM_KIT: self.preset_preset_list = JDXi.UI.Preset.Drum.PROGRAM_CHANGE else: # Default to digital preset list self.preset_preset_list = JDXi.UI.Preset.Digital.PROGRAM_CHANGE log.warning( scope=self.__class__.__name__, message=f"Unknown preset_type {self.preset_type}, defaulting to JDXi.UI.Preset.Digital.LIST", ) # --- Get MSB, LSB, PC values from the preset using get_preset_parameter_value if self.preset_preset_list is None: log.error( scope=self.__class__.__name__, message="preset_preset_list is still None after initialization", ) return msb = get_preset_parameter_value("msb", program_number, self.preset_preset_list) lsb = get_preset_parameter_value("lsb", program_number, self.preset_preset_list) pc = get_preset_parameter_value("pc", program_number, self.preset_preset_list) if None in [msb, lsb, pc]: log.message( scope=self.__class__.__name__, message=f"Could not retrieve preset parameters for program {program_number}", ) return # --- Ensure midi_channel is set - it should be set by _init_synth_data, but check anyway if self.midi_channel is None: # --- Try to determine channel from preset_type channel_map = { JDXi.Synth.DIGITAL_SYNTH_1: MidiChannel.DIGITAL_SYNTH_1, JDXi.Synth.DIGITAL_SYNTH_2: MidiChannel.DIGITAL_SYNTH_2, JDXi.Synth.ANALOG_SYNTH: MidiChannel.ANALOG_SYNTH, JDXi.Synth.DRUM_KIT: MidiChannel.DRUM_KIT, } if self.preset_type in channel_map: self.midi_channel = channel_map[self.preset_type] log.message( scope=self.__class__.__name__, message=f"Set midi_channel to {self.midi_channel} based on preset_type {self.preset_type}", ) else: log.error( scope=self.__class__.__name__, message=f"midi_channel is None and could not determine from preset_type {self.preset_type}", ) return log.message( scope=self.__class__.__name__, message=f"retrieved msb, lsb, pc : {msb}, {lsb}, {pc}", ) log.message( scope=self.__class__.__name__, message=f"Using MIDI channel: {self.midi_channel}", ) send_tone_with_expectation( self.midi_helper, int(program_number), self.preset_preset_list, self.midi_channel, source="synth_editor", ) if request_data: self.data_request()
[docs] def _handle_program_change(self, channel: int, program: int): """Handle program change messages by requesting updated data""" log.message( scope=self.__class__.__name__, message=f"Program change {program} detected on channel {channel}, requesting data update", ) history = getattr(self, "history", None) # the sound is being replaced: the old steps no longer apply if history is not None: history.reset() self.device_refresher.schedule()
[docs] def _handle_control_change(self, channel: int, control: int, value: int): """Handle program change messages by requesting updated data""" log.message( scope=self.__class__.__name__, message=f"Control change {channel} {control} detected on channel {channel} with value {value}, ", ) log.message(scope=self.__class__.__name__, message="requesting data update") self.device_refresher.schedule()
[docs] def load_and_set_image(self, image_path, secondary_image_path=None): """Helper function to load and set the image on the label.""" log.debug( scope=self.__class__.__name__, message=f"load_and_set_image called with primary: {image_path}, secondary: {secondary_image_path}", ) if image_path and os.path.exists(image_path): file_to_load = image_path log.debug( scope=self.__class__.__name__, message=f"Using primary image path: {file_to_load}", ) elif secondary_image_path and os.path.exists(secondary_image_path): file_to_load = secondary_image_path log.debug( scope=self.__class__.__name__, message=f"Using secondary image path: {file_to_load}", ) else: # --- Fallback to default image using resource_path if hasattr(self, "instrument_icon_folder") and hasattr( self, "instrument_default_image" ): file_to_load = resource_path( os.path.join( "resources", self.instrument_icon_folder, self.instrument_default_image, ) ) log.debug( scope=self.__class__.__name__, message=f"Falling back to default image: {file_to_load}", ) else: log.error( scope=self.__class__.__name__, message=f"Cannot load image: missing instrument_icon_folder ({getattr(self, 'instrument_icon_folder', None)}) or instrument_default_image ({getattr(self, 'instrument_default_image', None)})", ) image_label = self._get_instrument_image_label() if image_label: image_label.clear() return False if not os.path.exists(file_to_load): log.warning( scope=self.__class__.__name__, message=f"Image file does not exist: {file_to_load}", ) image_label = self._get_instrument_image_label() if image_label: image_label.clear() return False pixmap = QPixmap(file_to_load) if pixmap.isNull(): log.error( scope=self.__class__.__name__, message=f"Failed to load pixmap from: {file_to_load}", ) image_label = self._get_instrument_image_label() if image_label: image_label.clear() return False # --- Scale maintaining aspect ratio, fitting within width and height constraints scaled_pixmap = pixmap.scaled( JDXi.UI.Style.INSTRUMENT_IMAGE_WIDTH, JDXi.UI.Style.INSTRUMENT_IMAGE_HEIGHT, Qt.AspectRatioMode.KeepAspectRatio, Qt.TransformationMode.SmoothTransformation, ) image_label = self._get_instrument_image_label() if image_label: image_label.setPixmap(scaled_pixmap) image_label.setScaledContents(False) # Don't stretch, maintain aspect ratio image_label.setStyleSheet(JDXi.UI.Style.INSTRUMENT_IMAGE_LABEL) log.debug( scope=self.__class__.__name__, message=f"Successfully loaded image: {file_to_load}", ) else: log.error( scope=self.__class__.__name__, message="Instrument image label not found - cannot set image", ) return False return True
[docs] def update_instrument_image(self): """Update the instrument image based on the selected synth.""" selected_text = self._get_selected_instrument_text() if not selected_text: self._fallback_to_default_image("No instrument text selected.") return descriptor = self._parse_instrument_text(selected_text) if descriptor is None: self._fallback_to_default_image("Failed to parse instrument text.") return # Extract name and type from InstrumentDescriptor # Use name as name (e.g., "fm", "jd") or extract from raw_text name = descriptor.engine # Special handling for drum kits: Extract model number (e.g., "909", "808", "707", "cr-78") if descriptor.category == "drum": text_lower = descriptor.raw_text.lower() # Look for drum model numbers in the text import re # First, try to match "cr-78" pattern (special case for CR-78) if "cr-78" in text_lower or "cr78" in text_lower: name = "cr-78" # Then try to match "tr-XXX" pattern (e.g., "tr-909", "tr-808") elif re.search(r"tr-(\d{3})", text_lower): tr_match = re.search(r"tr-(\d{3})", text_lower) name = tr_match.group(1) # Extract the 3-digit number after "tr-" else: # Fallback: look for known drum model numbers in the text # Match standalone 3-digit numbers that are drum models known_models = ["909", "808", "707", "727", "606", "626"] for model in known_models: if model in text_lower: name = model break else: # No model found, use engine name name = descriptor.engine # Special handling: Check for specific engine names in raw_text that might not be detected # This ensures "JP8" matches "jp8.png" even if the family was detected first. # ENGINE_KEYWORDS is the single source of truth here; the icon filename # aliases (e.g. "tb" -> "tb-303.png") live in ENGINE_IMAGE_ALIASES. text_lower = preset_match_texts(descriptor.raw_text) # Check if any engine name pattern matches in the text (only for non-drum) if descriptor.category != "drum": for engine_spec in ENGINE_KEYWORDS: if any_keyword_in(text_lower, engine_spec.keywords): name = engine_spec.name break if not name: # Extract first word after number and dash as fallback name words = descriptor.raw_text.split() for i, word in enumerate(words): if i > 0 and word != "-" and not word.isdigit(): name = word.lower().replace("&", "_").split("_")[0] break # Use family as type (e.g., "piano", "pad", "bass") type_ = descriptor.family # Either one is enough: an name with no recognized family still has # its own artwork ("Sweep JD 1" -> jd.png), and so does a family with # no recognized name. if name or type_: image_loaded = self._try_load_specific_or_generic_image( name, type_, descriptor.raw_text ) else: image_loaded = False if not image_loaded: self._fallback_to_default_image("Failed to load specific/generic image.")
[docs] def _get_instrument_selection_combo(self): """ Get the instrument selection combo box from either the widget or direct attribute. Returns None if not found. """ # --- Try to get from InstrumentPresetWidget first (for Digital/Drum editors) if hasattr(self, "instrument_preset") and self.instrument_preset: return getattr(self.instrument_preset, "instrument_selection_combo", None) # --- Fallback to direct attribute (for Analog editor or legacy code) return getattr(self, "instrument_selection_combo", None)
[docs] def _get_instrument_image_label(self): """ Get the instrument image label from either the widget or direct attribute. Returns None if not found. """ # --- Try to get from InstrumentPresetWidget first (for Digital/Drum editors) if hasattr(self, "instrument_preset") and self.instrument_preset: label = getattr(self.instrument_preset, "instrument_image_label", None) if label: log.debug( scope=self.__class__.__name__, message="Found instrument_image_label.", ) return label # Fallback to direct attribute (for Analog editor or legacy code) label = getattr(self, "instrument_image_label", None) if label: log.debug( scope=self.__class__.__name__, message="Found instrument_image_label as direct attribute", ) else: log.warning( "[SynthEditor] Instrument image label not found in widget or direct attribute" ) return label
[docs] def _get_selected_instrument_text(self) -> str: """ _get_selected_instrument_text :return: str """ combo_box = self._get_instrument_selection_combo() if combo_box: combo = getattr(combo_box, "combo_box", None) if combo and hasattr(combo, "currentText"): return combo.currentText() log.error( scope=self.__class__.__name__, message="Instrument combo box is missing or malformed.", ) return ""
[docs] def _parse_instrument_text(self, text: str) -> Optional[InstrumentDescriptor]: text_lower = preset_match_texts(text) # -------------------------------------------------- # 1. Drum kits (highest priority) # -------------------------------------------------- for spec in DRUM_KIT_SPECS: if any_keyword_in(text_lower, spec.keywords): return InstrumentDescriptor( category="drum", family=spec.family, engine=spec.engine, raw_text=text, ) # -------------------------------------------------- # 2. Detect synth engine # -------------------------------------------------- engine = None for engine_spec in ENGINE_KEYWORDS: if any_keyword_in(text_lower, engine_spec.keywords): engine = engine_spec.name break # -------------------------------------------------- # 3. Detect synth family # -------------------------------------------------- for spec in INSTRUMENT_FAMILY_SPECS: if any_keyword_in(text_lower, spec.keywords): return InstrumentDescriptor( category="synth", family=spec.family, engine=engine, raw_text=text, ) # -------------------------------------------------- # 4. No family, but a known name is enough to pick artwork # ("Sweep JD 1" -> jd.png rather than the default image) # -------------------------------------------------- if engine: return InstrumentDescriptor( category="synth", family=None, engine=engine, raw_text=text, ) log.warning( scope=self.__class__.__name__, message=f"Unrecognized instrument preset: {text}", ) return None
[docs] def _icon_path(self, basename: str) -> str: """ Absolute path of an icon in the current instrument folder. :param basename: Filename without extension. :return: Absolute path to ``<basename>.png``. """ return resource_path( os.path.join("resources", self.instrument_icon_folder, f"{basename}.png") )
[docs] def _resolve_icon_path(self, path: str) -> Optional[str]: """ Resolve *path*, ignoring filename case. Artwork is not always named in lower case ("JD Piano.png"), which only matters on case-sensitive filesystems: the lookup would miss the file on Linux while working on macOS and Windows. :param path: Candidate absolute path. :return: The path that exists, or None. """ if not path: return None if os.path.exists(path): return path directory, filename = os.path.split(path) try: entries = os.listdir(directory) except OSError: return None for entry in entries: if entry.lower() == filename.lower(): return os.path.join(directory, entry) return None
[docs] def _preset_image_paths(self, preset_text: str) -> list: """ Candidate paths for artwork named after the preset itself. Lets "164: Trem EP 1" pick up "trem-ep.png" ahead of the family image, so dropping in a file named after a preset is all it takes. :param preset_text: Preset label (may be empty). :return: list of absolute paths, most specific first. """ if not preset_text: return [] return [ self._icon_path(basename) for basename in preset_image_basenames(preset_text) ]
[docs] def _name_image_paths(self, name: str) -> list: """ Build the candidate image paths for an instrument/engine name. Each name is expanded through its known filename aliases, so an name detected as ``tb`` matches ``tb.png``, ``tb-303.png``, ``tb303.png`` or ``303.png`` depending on which artwork a resource folder ships. :param name: Detected name or instrument name (may be empty). :return: list of absolute paths, most specific first. """ return [ self._icon_path(basename) for basename in engine_image_basenames(name) ]
[docs] def _try_load_specific_or_generic_image( self, name: str, type_: str, preset_text: str = "" ) -> bool: try: if not self.instrument_icon_folder: log.error( scope=self.__class__.__name__, message=f"Instrument icon folder not set. Cannot load image for {name}/{type_}", ) return False # Build list of paths to try (in order of preference) # 0. Artwork named after the preset itself always wins: it is more # specific than the family image ("trem-ep.png" over "piano.png") # and than the name image ("jd piano.png" over "jd.png"). paths_to_try = self._preset_image_paths(preset_text) # PCM samples belong to the fictional JDXV, so its artwork comes # ahead of the family picture ("E. Grand" -> jdxv.png, not # piano.png; "Bright Strat" -> jdxv.png, not strat.png). A file # named after the preset itself still wins, above. if name == "jdxv": paths_to_try.extend(self._name_image_paths(name)) if type_: paths_to_try.append( resource_path( os.path.join( "resources", self.instrument_icon_folder, f"{type_}.png" ) ) ) # Special case: For piano family, prioritize piano.png over name-specific images # This ensures "JD Piano 1" matches piano.png instead of jd.png elif type_ == "piano": # 1. Try piano.png first (family image) paths_to_try.append( resource_path( os.path.join( "resources", self.instrument_icon_folder, f"{type_}.png" ) ) ) # 2. Then try specific name images as fallback (e.g., "jd.png") paths_to_try.extend(self._name_image_paths(name)) # Special case: For guitar family, prioritize strat.png over other images # This ensures guitar presets (179-184) match strat.png elif type_ == "guitar": # 1. Try strat.png first paths_to_try.append( resource_path( os.path.join( "resources", self.instrument_icon_folder, "strat.png" ) ) ) # 2. Then try specific name images as fallback paths_to_try.extend(self._name_image_paths(name)) # 3. Finally try generic guitar.png as fallback paths_to_try.append( resource_path( os.path.join( "resources", self.instrument_icon_folder, f"{type_}.png" ) ) ) # Special case: For FM name synths, prioritize fm.png over family images # This ensures FM synths (155, 156, 237) match fm.png instead of bass.png or brass.png elif name == "fm": # 1. Try fm.png first (name image) paths_to_try.append( resource_path( os.path.join("resources", self.instrument_icon_folder, "fm.png") ) ) # 2. Then try generic type image as fallback (e.g., "bass.png", "brass.png") if type_: paths_to_try.append( resource_path( os.path.join( "resources", self.instrument_icon_folder, f"{type_}.png" ) ) ) else: # Default behavior: Try specific name images first, then generic type image # 1. Try specific name images (e.g., "tb.png", "tb-303.png") paths_to_try.extend(self._name_image_paths(name)) # 2. Try generic type image (e.g., "pad.png" if type_ is "pad") if type_: paths_to_try.append( resource_path( os.path.join( "resources", self.instrument_icon_folder, f"{type_}.png" ) ) ) log.debug( scope=self.__class__.__name__, message=f"Trying to load images (in order): {paths_to_try}", ) log.debug( scope=self.__class__.__name__, message=f"Instrument icon folder: {self.instrument_icon_folder}", ) # Try every candidate in order, not just the first two: pick the # first path that exists so later fallbacks (e.g. "guitar.png" or an # aliased name icon such as "tb-303.png") are not dropped. existing = [ resolved for resolved in (self._resolve_icon_path(path) for path in paths_to_try) if resolved ] if existing: primary_path = existing[0] secondary_path = existing[1] if len(existing) > 1 else None else: # Nothing matched; hand the first candidate over so # load_and_set_image falls back to the default image. primary_path = paths_to_try[0] if paths_to_try else None secondary_path = paths_to_try[1] if len(paths_to_try) > 1 else None return self.load_and_set_image(primary_path, secondary_path) except Exception as ex: log.error( scope=self.__class__.__name__, message=f"Error loading specific/generic images: {ex}", ) import traceback log.error(traceback.format_exc()) return False
[docs] def _fallback_to_default_image(self, reason: str): log.info( scope=self.__class__.__name__, message=f"{reason} Falling back to default image.", ) try: default_path = resource_path( os.path.join( "resources", self.instrument_icon_folder, self.instrument_default_image, ) ) if not self.load_and_set_image(default_path): log.error( scope=self.__class__.__name__, message="Default instrument image not found. Clearing label.", ) image_label = self._get_instrument_image_label() if image_label: image_label.clear() except Exception as ex: log.error( scope=self.__class__.__name__, message=f"Error loading default image: {ex}", ) image_label = self._get_instrument_image_label() if image_label: image_label.clear()
[docs] def _update_common_controls( self, partial_number: int, filtered_data, successes, failures ): pass
[docs] def _update_modify_controls( self, partial_number: int, filtered_data, successes, failures ): pass
[docs] def midi_load_file_from_path(self, file_path): pass