Skip to content

Commit

Permalink
Refactor to improve NoteStrategy
Browse files Browse the repository at this point in the history
  • Loading branch information
MaddyGuthridge committed Jul 30, 2023
1 parent 9aa47fe commit 78f110f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
7 changes: 7 additions & 0 deletions src/common/tracks/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ class AbstractTrack:
Allows for their properties to be get/set in a simplified manner
"""
@abstractmethod
@property
def index(self) -> int:
"""
Index of the track
"""

@abstractmethod
@property
def color(self) -> Color:
Expand Down
34 changes: 31 additions & 3 deletions src/common/tracks/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import channels

from .abstract import AbstractTrack
from typing import TypeVar, ParamSpec, Concatenate, Callable, Union
from typing import Optional, TypeVar, ParamSpec, Concatenate, Callable, Union
from common.types import Color
from common.util.api_fixes import getGroupChannelIndex

Expand Down Expand Up @@ -83,11 +83,39 @@ class Channel(AbstractTrack):
def __init__(self, index: int) -> None:
self.__index = index

def triggerNote(self, note_number: int, value: float) -> None:
@property
def index(self) -> int:
"""
Global index of the channel
"""
return self.__index

@property
def group_index(self) -> Optional[int]:
"""
Index of the channel, respecting groups
"""
return getGroupChannelIndex(self.__index)

def triggerNote(
self,
note_number: int,
value: float,
channel: Optional[int] = None,
) -> None:
"""
Trigger a note on this channel
"""
channels.midiNoteOn(self.__index, note_number, int(value * 127))
if channel is None:
channel = -1
channels.midiNoteOn(
self.__index,
note_number,
int(value * 127),
# NOTE: Currently FL Studio won't set the note color correctly
# from this channel
channel,
)

@property
def color(self) -> Color:
Expand Down
20 changes: 5 additions & 15 deletions src/plugs/mapping_strategies/note_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
more details.
"""

import channels

from typing import Any
from common.plug_indexes.fl_index import UnsafeIndex
from common.plug_indexes.fl_index import FlIndex
from common import getContext

from control_surfaces import Note
Expand All @@ -38,21 +36,13 @@ def apply(self, shadow: DeviceShadow) -> None:
def noteCallback(
self,
control: ControlShadowEvent,
index: UnsafeIndex,
index: FlIndex,
*args: Any,
**kwargs: Any
) -> bool:
try:
i = channels.getChannelIndex(*getContext().activity.getGenerator())
except TypeError:
# Index out of range - we're using a plugin from a different group
i = channels.channelNumber()
channels.midiNoteOn(
i,
getContext().activity.getGenerator().track.triggerNote(
control.getControl().coordinate[1],
int(control.value*127),
# NOTE: Currently FL Studio won't set the note color correctly
# from this channel
control.channel
control.value,
control.channel,
)
return True

0 comments on commit 78f110f

Please sign in to comment.