1+ from functools import partial
2+
13from _Framework .SubjectSlot import SlotManager
2- from typing import Optional , Type
4+ from typing import Optional , Type , TYPE_CHECKING
35
46from protocol0 .domain .lom .device .Device import Device
57from protocol0 .domain .lom .device .DeviceEnum import DeviceEnum
2224from protocol0 .domain .lom .instrument .preset .preset_initializer .PresetInitializerInterface import (
2325 PresetInitializerInterface ,
2426)
27+ from protocol0 .domain .shared .backend .Backend import Backend
2528from protocol0 .domain .shared .errors .Protocol0Warning import Protocol0Warning
29+ from protocol0 .shared .SongFacade import SongFacade
2630from protocol0 .shared .sequence .Sequence import Sequence
2731
2832
33+ if TYPE_CHECKING :
34+ from protocol0 .domain .lom .track .abstract_track .AbstractTrack import AbstractTrack
35+
36+
37+ def _get_insert_instrument_track (instrument_cls ):
38+ # type: (Type["InstrumentInterface"]) -> AbstractTrack
39+ """Current track or last instrument track or last track"""
40+ target_color = instrument_cls .TRACK_COLOR .value
41+
42+ if SongFacade .current_track ().color == target_color :
43+ return SongFacade .current_track ()
44+
45+ instrument_tracks = [
46+ t
47+ for t in SongFacade .simple_tracks ()
48+ if t .group_track is None and t .color == target_color
49+ ]
50+
51+ last_track = list (SongFacade .simple_tracks ())[- 1 ]
52+
53+ return next (reversed (instrument_tracks ), last_track )
54+
55+
56+ def load_instrument_track (instrument_cls ):
57+ # type: (Type["InstrumentInterface"]) -> Sequence
58+ insert_track = _get_insert_instrument_track (instrument_cls )
59+ track_color = insert_track .color
60+
61+ insert_track .focus ()
62+ seq = Sequence ()
63+ seq .add (partial (Backend .client ().load_instrument_track , instrument_cls .INSTRUMENT_TRACK_NAME ))
64+ seq .wait_for_backend_event ("instrument_loaded" )
65+ seq .add (partial (setattr , insert_track , "color" , track_color ))
66+ return seq .done ()
67+
68+
2969class InstrumentInterface (SlotManager ):
3070 NAME = ""
3171 DEVICE = None # type: Optional[DeviceEnum]
@@ -39,6 +79,7 @@ class InstrumentInterface(SlotManager):
3979 PRESET_OFFSET = 0 # if we store presets not at the beginning of the list
4080 PRESET_CHANGER = ProgramChangePresetChanger # type: Type[PresetChangerInterface]
4181 PRESET_INITIALIZER = PresetInitializerDevicePresetName # type: Type[PresetInitializerInterface]
82+ INSTRUMENT_TRACK_NAME = ""
4283
4384 def __init__ (self , device , track_name ):
4485 # type: (Optional[Device], str) -> None
0 commit comments