Skip to content

Commit

Permalink
Implement UpdateDictPattern
Browse files Browse the repository at this point in the history
  • Loading branch information
josephine-wolf-oberholtzer committed Jan 14, 2025
1 parent f218de9 commit f0b790c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
9 changes: 8 additions & 1 deletion supriya/patterns/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
from .eventpatterns import ChainPattern, EventPattern, MonoEventPattern, UpdatePattern
from .eventpatterns import (
ChainPattern,
EventPattern,
MonoEventPattern,
UpdateDictPattern,
UpdatePattern,
)
from .events import (
BusAllocateEvent,
BusFreeEvent,
Expand Down Expand Up @@ -54,4 +60,5 @@
"SynthAllocateEvent",
"UnaryOpPattern",
"UpdatePattern",
"UpdateDictPattern",
]
28 changes: 25 additions & 3 deletions supriya/patterns/eventpatterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .patterns import Pattern, SequencePattern


class EventPattern(Pattern):
class EventPattern(Pattern[Event]):
"""
Akin to SuperCollider's Pbind.
"""
Expand Down Expand Up @@ -82,7 +82,7 @@ def is_infinite(self) -> bool:
return True


class UpdatePattern(Pattern):
class UpdatePattern(Pattern[Event]):
"""
Akin to SuperCollider's Pbindf.
"""
Expand Down Expand Up @@ -129,7 +129,29 @@ def is_infinite(self) -> bool:
return self._pattern.is_infinite


class ChainPattern(Pattern):
class UpdateDictPattern(Pattern[Event]):
def __init__(self, pattern: Pattern[Event], dictionary: Dict[str, Any]) -> None:
self._pattern = pattern
self._dictionary = dictionary

def _iterate(
self, state: Optional[Dict[str, UUID]] = None
) -> Generator[Event, bool, None]:
event_iterator = iter(self._pattern)
while True:
try:
event = next(event_iterator)
except StopIteration:
return
if (yield new(event, **self._dictionary)):
return

@property
def is_infinite(self) -> bool:
return self._pattern.is_infinite


class ChainPattern(Pattern[Event]):
"""
Akin to SuperCollider's Pchain.
"""
Expand Down
3 changes: 2 additions & 1 deletion tests/patterns/test_PatternPlayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from supriya.osc import OscBundle, OscMessage
from supriya.patterns import (
BusPattern,
Event,
EventPattern,
GroupPattern,
MonoEventPattern,
Expand All @@ -25,7 +26,7 @@
"pattern, until, target_node, expected",
[
(
SequencePattern(
SequencePattern[Event](
[
EventPattern(frequency=SequencePattern([440, 550, 660])),
MonoEventPattern(frequency=SequencePattern([440, 550, 660])),
Expand Down

0 comments on commit f0b790c

Please sign in to comment.