Skip to content

Commit

Permalink
Create UnionPattern event pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
MaddyGuthridge committed Feb 6, 2022
1 parent ab0dc28 commit 6cb6418
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 56 deletions.
2 changes: 1 addition & 1 deletion common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from .logger import log, verbosity

from .eventpattern import BasicEventPattern, IEventPattern
from .eventpattern import BasicPattern, IEventPattern

from .contextmanager import getContext, resetContext, catchContextResetException

Expand Down
5 changes: 3 additions & 2 deletions common/eventpattern/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

from .bytematch import ByteMatch, fromNibbles
from .ieventpattern import IEventPattern
from .basicpattern import BasicEventPattern
from .forwardedpattern import ForwardedEventPattern
from .basicpattern import BasicPattern
from .forwardedpattern import ForwardedPattern
from .unionpattern import UnionPattern
10 changes: 5 additions & 5 deletions common/eventpattern/basicpattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from common.types import eventData
from . import ByteMatch, IEventPattern

class BasicEventPattern(IEventPattern):
class BasicPattern(IEventPattern):
"""
Represents a pattern to match with MIDI events.
Expand Down Expand Up @@ -129,10 +129,10 @@ def _matchByte(expected: ByteMatch, actual: int) -> bool:
"""
# This is type-safe, I promise
matches: dict[type, Callable[[Any, int], bool]] = {
int: BasicEventPattern._matchByteConst,
range: BasicEventPattern._matchByteRange,
tuple: BasicEventPattern._matchByteTuple,
type(...): BasicEventPattern._matchByteEllipsis
int: BasicPattern._matchByteConst,
range: BasicPattern._matchByteRange,
tuple: BasicPattern._matchByteTuple,
type(...): BasicPattern._matchByteEllipsis
}
return matches[type(expected)](expected, actual)

Expand Down
2 changes: 1 addition & 1 deletion common/eventpattern/forwardedpattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from common.types import eventData

class ForwardedEventPattern(IEventPattern):
class ForwardedPattern(IEventPattern):
"""
The forwarded pattern is used to parse data from events which were forwarded
from the Universal Event Forwarded device script.
Expand Down
33 changes: 33 additions & 0 deletions common/eventpattern/unionpattern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
common > eventpattern > unionpattern
Contains the definition for the union type event pattern, which allows events
to be detected if they match any of a number of patterns.
Authors:
* Miguel Guthridge [[email protected], HDSQ#2154]
"""

from common.types import eventData
from .ieventpattern import IEventPattern

class UnionPattern(IEventPattern):
"""
Represents the union of multiple event patterns. A match with any of those
patterns is considered a match overall.
"""

def __init__(self, *patterns: IEventPattern) -> None:
"""
Create a UnionPattern
Args:
* `*patterns` (`IEventPattern`): event patterns to match, each as
separate arguments
"""
if len(patterns) < 2:
raise ValueError("Expected at least two event patterns to union")
self._patterns = patterns

def matchEvent(self, event: 'eventData') -> bool:
return any(p.matchEvent(event) for p in self._patterns)
6 changes: 3 additions & 3 deletions controlsurfaces/aftertouch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Miguel Guthridge [[email protected], HDSQ#2154]
"""

from common.eventpattern import BasicEventPattern, fromNibbles, IEventPattern
from common.eventpattern import BasicPattern, fromNibbles, IEventPattern
from . import ControlSurface, IValueStrategy, Data2Strategy, Data1Strategy

class AfterTouch(ControlSurface):
Expand Down Expand Up @@ -39,7 +39,7 @@ class ChannelAfterTouch(AfterTouch):
"""
def __init__(self, channel: 'int|ellipsis' = ...) -> None:
super().__init__(
BasicEventPattern(fromNibbles(0xD,channel), ..., ...),
BasicPattern(fromNibbles(0xD,channel), ..., ...),
Data1Strategy()
)

Expand All @@ -50,6 +50,6 @@ class NoteAfterTouch(AfterTouch):
"""
def __init__(self, note: int, channel: 'int|ellipsis' = ...) -> None:
super().__init__(
BasicEventPattern(fromNibbles(0xA,channel), note, ...),
BasicPattern(fromNibbles(0xA,channel), note, ...),
Data2Strategy()
)
4 changes: 2 additions & 2 deletions controlsurfaces/note.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Miguel Guthridge [[email protected], HDSQ#2154]
"""
from common.types import eventData
from common.eventpattern import BasicEventPattern, fromNibbles
from common.eventpattern import BasicPattern, fromNibbles
from . import ControlSurface, IValueStrategy

class NoteValueStrategy(IValueStrategy):
Expand All @@ -32,7 +32,7 @@ class Note(ControlSurface):
"""
def __init__(self, note_num: int, channel:int = 0) -> None:
super().__init__(
BasicEventPattern(fromNibbles((8, 9), channel), note_num, ...),
BasicPattern(fromNibbles((8, 9), channel), note_num, ...),
NoteValueStrategy(),
"notes",
(channel, note_num)
Expand Down
8 changes: 4 additions & 4 deletions controlsurfaces/pedal.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
'SOFT'
]

from common.eventpattern import BasicEventPattern, IEventPattern
from common.eventpattern import BasicPattern, IEventPattern
from . import ControlSurface
from .valuestrategies import ButtonData2Strategy

Expand Down Expand Up @@ -47,18 +47,18 @@ class SustainPedal(Pedal):
Represents a sustain pedal
"""
def __init__(self) -> None:
super().__init__(BasicEventPattern(0xB0, SUSTAIN, ...))
super().__init__(BasicPattern(0xB0, SUSTAIN, ...))

class SostenutoPedal(Pedal):
"""
Represents a sostenuto pedal
"""
def __init__(self) -> None:
super().__init__(BasicEventPattern(0xB0, SOSTENUTO, ...))
super().__init__(BasicPattern(0xB0, SOSTENUTO, ...))

class SoftPedal(Pedal):
"""
Represents a soft pedal
"""
def __init__(self) -> None:
super().__init__(BasicEventPattern(0xB0, SOFT, ...))
super().__init__(BasicPattern(0xB0, SOFT, ...))
8 changes: 4 additions & 4 deletions controlsurfaces/wheels.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Miguel Guthridge [[email protected], HDSQ#2154]
"""

from common.eventpattern import IEventPattern, BasicEventPattern, fromNibbles
from common.eventpattern import IEventPattern, BasicPattern, fromNibbles
from common.types import eventData
from . import ControlSurface
from . import Data2Strategy, IValueStrategy
Expand All @@ -34,7 +34,7 @@ class StandardModWheel(ModWheel):
"""
def __init__(self) -> None:
super().__init__(
BasicEventPattern(fromNibbles(0xB, ...), 0x1, ...),
BasicPattern(fromNibbles(0xB, ...), 0x1, ...),
Data2Strategy(),
"wheels"
)
Expand Down Expand Up @@ -73,7 +73,7 @@ class StandardPitchWheel(PitchWheel):
"""
def __init__(self) -> None:
super().__init__(
BasicEventPattern(fromNibbles(0xE, ...), ..., ...),
BasicPattern(fromNibbles(0xE, ...), ..., ...),
PitchValueStrategy()
)

Expand All @@ -84,6 +84,6 @@ class Data2PitchWheel(PitchWheel):
"""
def __init__(self) -> None:
super().__init__(
BasicEventPattern(fromNibbles(0xE, ...), 0x0, ...),
BasicPattern(fromNibbles(0xE, ...), 0x0, ...),
Data2Strategy()
)
22 changes: 11 additions & 11 deletions devices/maudio/hammer88pro/hammer88pro.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@


from typing import Optional
from common.eventpattern import BasicEventPattern, ForwardedEventPattern
from common.eventpattern import BasicPattern, ForwardedPattern
from common.types import eventData
from common.extensionmanager import ExtensionManager
from controlsurfaces.valuestrategies import ForwardedStrategy, ButtonData2Strategy
Expand Down Expand Up @@ -34,10 +34,10 @@ def __init__(self) -> None:
matcher = BasicControlMatcher()
# Null events
matcher.addControl(NullEvent(
BasicEventPattern(0xFA, 0x0, 0x0)
BasicPattern(0xFA, 0x0, 0x0)
))
matcher.addControl(NullEvent(
BasicEventPattern(0xFC, 0x0, 0x0)
BasicPattern(0xFC, 0x0, 0x0)
))

# Notes and pedals
Expand All @@ -47,31 +47,31 @@ def __init__(self) -> None:

# Transport buttons
matcher.addControl(StopButton(
ForwardedEventPattern(3, BasicEventPattern(0xBF, 102, ...)),
ForwardedPattern(3, BasicPattern(0xBF, 102, ...)),
ForwardedStrategy(ButtonData2Strategy())
))
matcher.addControl(PlayButton(
ForwardedEventPattern(3, BasicEventPattern(0xBF, 103, ...)),
ForwardedPattern(3, BasicPattern(0xBF, 103, ...)),
ForwardedStrategy(ButtonData2Strategy())
))
matcher.addControl(RecordButton(
ForwardedEventPattern(3, BasicEventPattern(0xBF, 104, ...)),
ForwardedPattern(3, BasicPattern(0xBF, 104, ...)),
ForwardedStrategy(ButtonData2Strategy())
))
matcher.addControl(RewindButton(
ForwardedEventPattern(3, BasicEventPattern(0xBF, 105, ...)),
ForwardedPattern(3, BasicPattern(0xBF, 105, ...)),
ForwardedStrategy(ButtonData2Strategy())
))
matcher.addControl(FastForwardButton(
ForwardedEventPattern(3, BasicEventPattern(0xBF, 106, ...)),
ForwardedPattern(3, BasicPattern(0xBF, 106, ...)),
ForwardedStrategy(ButtonData2Strategy())
))
matcher.addControl(LoopButton(
ForwardedEventPattern(3, BasicEventPattern(0xBF, 107, ...)),
ForwardedPattern(3, BasicPattern(0xBF, 107, ...)),
ForwardedStrategy(ButtonData2Strategy())
))
matcher.addControl(MetronomeButton(
ForwardedEventPattern(3, BasicEventPattern(0xB9, 0x74, ...)),
ForwardedPattern(3, BasicPattern(0xB9, 0x74, ...)),
ForwardedStrategy(ButtonData2Strategy())
))
matcher.addControl(StandardModWheel())
Expand All @@ -89,7 +89,7 @@ def getId() -> str:

@staticmethod
def getUniversalEnquiryResponsePattern():
return BasicEventPattern(
return BasicPattern(
[
0xF0, # Sysex start
0x7E, # Device response
Expand Down
4 changes: 2 additions & 2 deletions devices/maudio/hammer88pro/hammerpitch.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

from common.eventpattern import BasicEventPattern, fromNibbles
from common.eventpattern import BasicPattern, fromNibbles
from controlsurfaces import PitchWheel
from controlsurfaces.valuestrategies import Data2Strategy

Expand All @@ -10,6 +10,6 @@ class HammerPitchWheel(PitchWheel):
"""
def __init__(self) -> None:
super().__init__(
BasicEventPattern(fromNibbles(0xE, ...), (0x0, 0x7F), ...),
BasicPattern(fromNibbles(0xE, ...), (0x0, 0x7F), ...),
Data2Strategy()
)
28 changes: 14 additions & 14 deletions devices/novation/launchkey/mk2/launchkey.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

from typing import Optional
from common.eventpattern import BasicEventPattern
from common.eventpattern import BasicPattern
from common.types import eventData
from common.extensionmanager import ExtensionManager
from controlsurfaces.valuestrategies import Data2Strategy
Expand Down Expand Up @@ -40,7 +40,7 @@ def __init__(self, matcher: BasicControlMatcher) -> None:
for i in range(1, 9):
matcher.addControl(
Knob(
BasicEventPattern(0xB0, 0x14 + i, ...),
BasicPattern(0xB0, 0x14 + i, ...),
Data2Strategy(),
"knobs",
(i, 0)
Expand All @@ -49,35 +49,35 @@ def __init__(self, matcher: BasicControlMatcher) -> None:

# Transport
matcher.addControl(StopButton(
BasicEventPattern(0xB0, 0x72, ...),
BasicPattern(0xB0, 0x72, ...),
Data2Strategy()
))
matcher.addControl(PlayButton(
BasicEventPattern(0xB0, 0x73, ...),
BasicPattern(0xB0, 0x73, ...),
Data2Strategy()
))
matcher.addControl(LoopButton(
BasicEventPattern(0xB0, 0x74, ...),
BasicPattern(0xB0, 0x74, ...),
Data2Strategy(),
))
matcher.addControl(RecordButton(
BasicEventPattern(0xB0, 0x75, ...),
BasicPattern(0xB0, 0x75, ...),
Data2Strategy()
))
matcher.addControl(DirectionNext(
BasicEventPattern(0xB0, 0x66, ...),
BasicPattern(0xB0, 0x66, ...),
Data2Strategy()
))
matcher.addControl(DirectionPrevious(
BasicEventPattern(0xB0, 0x67, ...),
BasicPattern(0xB0, 0x67, ...),
Data2Strategy(),
))
matcher.addControl(RewindButton(
BasicEventPattern(0xB0, 0x70, ...),
BasicPattern(0xB0, 0x70, ...),
Data2Strategy(),
))
matcher.addControl(FastForwardButton(
BasicEventPattern(0xB0, 0x71, ...),
BasicPattern(0xB0, 0x71, ...),
Data2Strategy(),
))
matcher.addControl(StandardPitchWheel())
Expand All @@ -98,7 +98,7 @@ def __init__(self) -> None:
for i in range(1, 9):
matcher.addControl(
Fader(
BasicEventPattern(0xB0, 0x28 + i, ...),
BasicPattern(0xB0, 0x28 + i, ...),
Data2Strategy(),
"faders",
(i, 0)
Expand All @@ -107,7 +107,7 @@ def __init__(self) -> None:
# Master fader
matcher.addControl(
Fader(
BasicEventPattern(0xB0, 0x07, ...),
BasicPattern(0xB0, 0x07, ...),
Data2Strategy(),
"knobs",
(0, 0)
Expand All @@ -126,7 +126,7 @@ def getId() -> str:

@staticmethod
def getUniversalEnquiryResponsePattern():
return BasicEventPattern(
return BasicPattern(
[
0xF0, # Sysex start
0x7E, # Device response
Expand Down Expand Up @@ -162,7 +162,7 @@ def getId() -> str:

@staticmethod
def getUniversalEnquiryResponsePattern():
return BasicEventPattern(
return BasicPattern(
[0xF0, 0x7E, 0x00, 0x06, 0x02, 0x00, 0x20, 0x29, 0x7B]
)

Expand Down
Loading

0 comments on commit 6cb6418

Please sign in to comment.