Source code for jdxi_editor.ui.widgets.pitch.slider_spinbox

from typing import Callable

from decologr import Decologr as log
from PySide6.QtCore import Signal
from PySide6.QtWidgets import QDoubleSpinBox, QSpinBox, QVBoxLayout, QWidget

from jdxi_editor.ui.widgets.envelope.parameter import EnvelopeParameter
from picomidi.constant import Midi
from picomidi.sysex.parameter.address import AddressParameter
from picomidi.utils.conversion import (
    fraction_to_midi_value,
    midi_value_to_fraction,
    midi_value_to_ms,
    ms_to_midi_value,
)

from typing import NewType

from picoui.helpers.layout import create_layout_with_items
from picoui.signals.signals_blocked import blocked_signals
from picoui.specs.widgets import DoubleSpinBoxSpec, SpinBoxSpec
from picoui.widget.helper import create_double_spinbox_from_spec, create_spinbox_from_spec

[docs] MidiValue = NewType("MidiValue", int)
[docs] MIDI_VALUE_MIN = 0
[docs] MIDI_VALUE_MAX = 127
[docs] def midi_value(value: int) -> MidiValue: """Create a validated 7-bit MIDI value.""" if not MIDI_VALUE_MIN <= value <= MIDI_VALUE_MAX: raise ValueError( f"MIDI value must be between 0 and 127, got {value}" ) return MidiValue(value)
[docs] class PitchEnvSliderSpinbox(QWidget): """ Pitch Env Slider and Spinbox widget for Roland JD-Xi """
[docs] envelope_changed = Signal(dict)
[docs] valueChanged = Signal( object ) # Emitted when value changes (avoids AttributeError in shared controls)
def __init__( self, param: AddressParameter, min_value: float = 0.0, max_value: float = 1.0, units: str = "", label: str = "", value: int = None, create_parameter_slider: Callable = None, parent: QWidget = None, show_spinbox: bool = False ): """ Initialize the ADSR slider and spinbox widget. :param param: AddressParameter :param min_value: int :param max_value: int :param units: str :param label: str :param value: int :param create_parameter_slider: Callable :param parent: QWidget """ super().__init__(parent)
[docs] self.param = param
[docs] self.factor = Midi.value.max.SEVEN_BIT
if max_value > 1: self.factor = max_value
[docs] self.create_parameter_slider = create_parameter_slider
param_type = param.get_envelope_param_type()
[docs] self._bipolar_depth = ( param_type == EnvelopeParameter.PEAK_LEVEL and param.is_bipolar )
slider_initial = value spin_initial = value if self._bipolar_depth: if value is not None: spin_initial = float(value) slider_initial = int(round(float(value))) elif param_type in ( EnvelopeParameter.SUSTAIN_LEVEL, EnvelopeParameter.PEAK_LEVEL, EnvelopeParameter.MOD_DEPTH, EnvelopeParameter.DEPTH, ): if value is not None and float(value) <= float(max_value) and float(value) <= 2.0: spin_initial = float(value) slider_initial = self.convert_from_envelope(spin_initial) elif value is not None: slider_initial = int(value) spin_initial = midi_value_to_fraction(int(value))
[docs] self.slider = self.create_parameter_slider( param=param, label=label, vertical=True, initial_value=slider_initial, )
if param_type in [ EnvelopeParameter.SUSTAIN_LEVEL, EnvelopeParameter.PEAK_LEVEL, ]: self.spinbox_spec = DoubleSpinBoxSpec( min_val=min_value, max_val=max_value, step=0.01, value=spin_initial ) self.spinbox = create_double_spinbox_from_spec(spin_spec=self.spinbox_spec) else: self.spinbox_spec = SpinBoxSpec( min_val=int(min_value), max_val=int(max_value), suffix=units, value=value, ) self.spinbox = create_spinbox_from_spec(spin_spec=self.spinbox_spec) self.spinbox.setRange(min_value, max_value) layout = create_layout_with_items(items=[self.slider]) if show_spinbox: layout.addWidget(self.spinbox) self.setLayout(layout) # Connect both ways self.slider.valueChanged.connect(self._slider_changed) self.spinbox.valueChanged.connect(self._spinbox_changed)
[docs] def convert_to_envelope(self, value: float) -> float: """ Convert MIDI value to envelope value :param value: float :return: float """ param_type = self.param.get_envelope_param_type() if self._bipolar_depth: converted_value = float(value) elif param_type in [ EnvelopeParameter.SUSTAIN_LEVEL, EnvelopeParameter.PEAK_LEVEL, EnvelopeParameter.MOD_DEPTH, EnvelopeParameter.DEPTH, ]: converted_value = midi_value_to_fraction(int(value)) elif param_type in [ EnvelopeParameter.ATTACK_TIME, EnvelopeParameter.DECAY_TIME, EnvelopeParameter.RELEASE_TIME, EnvelopeParameter.FADE_LOWER, EnvelopeParameter.FADE_UPPER, EnvelopeParameter.RANGE_LOWER, EnvelopeParameter.DEPTH, EnvelopeParameter.RANGE_UPPER, ]: converted_value = midi_value_to_ms(int(value), min_time=10, max_time=5000) else: log.error(f"Unknown envelope parameter type {param_type} for {self.param}") converted_value = 0.0 # or raise an error, depending on design return converted_value
[docs] def convert_from_envelope(self, value: float) -> int: """ Convert envelope value to MIDI value :param value: int :return: int """ param_type = self.param.get_envelope_param_type() if self._bipolar_depth: converted_value = int(round(float(value))) elif param_type in [ EnvelopeParameter.PEAK_LEVEL, EnvelopeParameter.SUSTAIN_LEVEL, EnvelopeParameter.MOD_DEPTH, EnvelopeParameter.DEPTH, ]: converted_value = fraction_to_midi_value(float(value)) elif param_type in [ EnvelopeParameter.ATTACK_TIME, EnvelopeParameter.DECAY_TIME, EnvelopeParameter.RELEASE_TIME, EnvelopeParameter.FADE_LOWER, EnvelopeParameter.FADE_UPPER, EnvelopeParameter.RANGE_LOWER, EnvelopeParameter.RANGE_UPPER, ]: # Ensure value is a number, not a string value_num = float(value) if not isinstance(value, (int, float)) else value converted_value = int( ms_to_midi_value(value_num, min_time=10, max_time=5000) ) else: converted_value = 64 result = midi_value(round(converted_value)) log.message(f"{value} -> {result}", scope="convert_from_envelope") return converted_value
[docs] def _slider_changed(self, value: int) -> None: """ slider changed :param value: int slider value :return: None """ envelope_value = self.convert_to_envelope(value) with blocked_signals(self.spinbox): self.spinbox.setValue(int(envelope_value)) self.envelope_changed.emit( {self.param.get_envelope_param_type(): envelope_value} ) self.valueChanged.emit(self.value())
[docs] def _spinbox_changed(self, value: float): """ Spinbox changed :param value: float double spinbox value :return: None """ if value is None: return param_type = self.param.get_envelope_param_type() if param_type in ( EnvelopeParameter.SUSTAIN_LEVEL, EnvelopeParameter.PEAK_LEVEL, EnvelopeParameter.MOD_DEPTH, EnvelopeParameter.DEPTH, ): envelope_value = float(value) midi_value = self.convert_from_envelope(envelope_value) else: if isinstance(value, float): try: value = int(value) except Exception as ex: log.error(f"Error {ex} occurred casting float {value} to int") return if not isinstance(value, int): log.error(f"{value} is neither int nor castable float") return envelope_value = value midi_value = self.convert_from_envelope(envelope_value) if midi_value is None: log.error(f"convert_from_envelope({value}) returned None") return with blocked_signals(self.slider): self.slider.setValue(int(midi_value)) self.envelope_changed.emit({self.param.get_envelope_param_type(): envelope_value}) self.valueChanged.emit(self.value())
[docs] def setValue(self, value: float): """ Set the value of the double spinbox and slider :param value: float :return: None """ try: param_type = self.param.get_envelope_param_type() if param_type in ( EnvelopeParameter.SUSTAIN_LEVEL, EnvelopeParameter.PEAK_LEVEL, EnvelopeParameter.MOD_DEPTH, EnvelopeParameter.DEPTH, ): midi_val = self.convert_from_envelope(float(value)) self.slider.blockSignals(True) self.spinbox.blockSignals(True) self.slider.setValue(midi_val) self.spinbox.setValue(float(value)) self.slider.blockSignals(False) self.spinbox.blockSignals(False) else: self.slider.setValue(int(value)) self.spinbox.setValue(int(value)) except RuntimeError: pass
[docs] def setMidiValue(self, midi_value: int) -> None: """ Show a value received from the synth, without emitting. The slider is in MIDI units and the spinbox in envelope units, so each is set in its own unit; setValue would pass the number to both. :param midi_value: int 0-127 as sent by the JD-Xi """ with blocked_signals(self.slider): with blocked_signals(self.spinbox): try: self.slider.setValue(int(midi_value)) self.spinbox.setValue(int(self.convert_to_envelope(int(midi_value)))) except RuntimeError: pass
[docs] def value(self) -> float: """ Get the semantic envelope value (fraction for depth, ms for time). :return: float """ param_type = self.param.get_envelope_param_type() if self._bipolar_depth: return float(self.slider.value()) if param_type in ( EnvelopeParameter.SUSTAIN_LEVEL, EnvelopeParameter.PEAK_LEVEL, EnvelopeParameter.MOD_DEPTH, EnvelopeParameter.DEPTH, ): return midi_value_to_fraction(self.slider.value()) return float(self.spinbox.value())
[docs] def update(self): """Update the envelope values and plot""" super().update() self.slider.update() self.spinbox.update() self.parent.update()