Added samples and prefixes to variables
This commit is contained in:
@ -479,6 +479,13 @@ class Variable(Event):
|
|||||||
name: str
|
name: str
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(kw_only=True)
|
||||||
|
class Sample(Event):
|
||||||
|
"""Class for samples"""
|
||||||
|
|
||||||
|
name: str
|
||||||
|
|
||||||
|
|
||||||
@dataclass(kw_only=True)
|
@dataclass(kw_only=True)
|
||||||
class VariableList(Item):
|
class VariableList(Item):
|
||||||
"""Class for using variables"""
|
"""Class for using variables"""
|
||||||
|
|||||||
@ -29,8 +29,10 @@ from .items import (
|
|||||||
Function,
|
Function,
|
||||||
Modification,
|
Modification,
|
||||||
Whitespace,
|
Whitespace,
|
||||||
|
Sample,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# TODO: Could be refactored to each class?
|
# TODO: Could be refactored to each class?
|
||||||
def resolve_item(item: Meta, options: dict):
|
def resolve_item(item: Meta, options: dict):
|
||||||
"""Resolve cyclic value"""
|
"""Resolve cyclic value"""
|
||||||
@ -38,7 +40,7 @@ def resolve_item(item: Meta, options: dict):
|
|||||||
if isinstance(item, ListOperation):
|
if isinstance(item, ListOperation):
|
||||||
yield from item.evaluate(options)
|
yield from item.evaluate(options)
|
||||||
elif isinstance(item, (RepeatedSequence, RepeatedListSequence)):
|
elif isinstance(item, (RepeatedSequence, RepeatedListSequence)):
|
||||||
yield from item.resolve_repeat(options)
|
yield from item.resolve_repeat(options)
|
||||||
elif isinstance(item, Subdivision):
|
elif isinstance(item, Subdivision):
|
||||||
item.evaluate_values(options)
|
item.evaluate_values(options)
|
||||||
yield item
|
yield item
|
||||||
@ -58,7 +60,17 @@ def resolve_item(item: Meta, options: dict):
|
|||||||
if item.name in options:
|
if item.name in options:
|
||||||
opt_item = options[item.name]
|
opt_item = options[item.name]
|
||||||
if isinstance(opt_item, LambdaType):
|
if isinstance(opt_item, LambdaType):
|
||||||
yield Function(run=opt_item, text=item.text, kwargs=options)
|
yield Function(
|
||||||
|
run=opt_item,
|
||||||
|
text=item.text,
|
||||||
|
kwargs=(options | item.local_options),
|
||||||
|
)
|
||||||
|
elif isinstance(opt_item, str):
|
||||||
|
yield Sample(
|
||||||
|
name=opt_item,
|
||||||
|
text=item.text,
|
||||||
|
kwargs=(options | item.local_options),
|
||||||
|
)
|
||||||
variable = deepcopy(opt_item)
|
variable = deepcopy(opt_item)
|
||||||
yield from resolve_item(variable, options)
|
yield from resolve_item(variable, options)
|
||||||
elif isinstance(item, VariableList):
|
elif isinstance(item, VariableList):
|
||||||
@ -68,7 +80,19 @@ def resolve_item(item: Meta, options: dict):
|
|||||||
opt_item = options[var.name]
|
opt_item = options[var.name]
|
||||||
if isinstance(opt_item, LambdaType):
|
if isinstance(opt_item, LambdaType):
|
||||||
seqlist.append(
|
seqlist.append(
|
||||||
Function(run=opt_item, text=var.text, kwargs=options)
|
Function(
|
||||||
|
run=opt_item,
|
||||||
|
text=var.text,
|
||||||
|
kwargs=(options | var.local_options),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
elif isinstance(opt_item, str):
|
||||||
|
seqlist.append(
|
||||||
|
Sample(
|
||||||
|
name=opt_item,
|
||||||
|
text=var.text,
|
||||||
|
kwargs=(options | var.local_options),
|
||||||
|
)
|
||||||
)
|
)
|
||||||
elif isinstance(opt_item, Sequence):
|
elif isinstance(opt_item, Sequence):
|
||||||
seqlist.append(opt_item)
|
seqlist.append(opt_item)
|
||||||
@ -87,6 +111,7 @@ def resolve_item(item: Meta, options: dict):
|
|||||||
elif isinstance(item, Meta): # Filters whitespace
|
elif isinstance(item, Meta): # Filters whitespace
|
||||||
yield update_item(item, options)
|
yield update_item(item, options)
|
||||||
|
|
||||||
|
|
||||||
def resolve_integer_value(item, options):
|
def resolve_integer_value(item, options):
|
||||||
"""Helper for resolving integer value of different types"""
|
"""Helper for resolving integer value of different types"""
|
||||||
while isinstance(item, Cyclic):
|
while isinstance(item, Cyclic):
|
||||||
@ -97,6 +122,7 @@ def resolve_integer_value(item, options):
|
|||||||
return item.get_value(options)
|
return item.get_value(options)
|
||||||
return item
|
return item
|
||||||
|
|
||||||
|
|
||||||
def update_item(item, options):
|
def update_item(item, options):
|
||||||
"""Update or create new pitch"""
|
"""Update or create new pitch"""
|
||||||
if set(("key", "scale")) <= options.keys():
|
if set(("key", "scale")) <= options.keys():
|
||||||
@ -116,12 +142,14 @@ def update_item(item, options):
|
|||||||
item = item.evaluate_chord(options)
|
item = item.evaluate_chord(options)
|
||||||
return item
|
return item
|
||||||
|
|
||||||
|
|
||||||
def euclidean_items(euclid: Item, options: dict):
|
def euclidean_items(euclid: Item, options: dict):
|
||||||
"""Loops values from generated euclidean sequence"""
|
"""Loops values from generated euclidean sequence"""
|
||||||
euclid.evaluate(options)
|
euclid.evaluate(options)
|
||||||
for item in euclid.evaluated_values:
|
for item in euclid.evaluated_values:
|
||||||
yield from resolve_item(item, options)
|
yield from resolve_item(item, options)
|
||||||
|
|
||||||
|
|
||||||
def update_modifications(current: Item, options: dict) -> dict:
|
def update_modifications(current: Item, options: dict) -> dict:
|
||||||
"""Update options based on current item"""
|
"""Update options based on current item"""
|
||||||
if isinstance(current, (OctaveChange, DurationChange)):
|
if isinstance(current, (OctaveChange, DurationChange)):
|
||||||
@ -132,6 +160,7 @@ def update_modifications(current: Item, options: dict) -> dict:
|
|||||||
else: # Create value if not existing
|
else: # Create value if not existing
|
||||||
options[current.key] = current.value
|
options[current.key] = current.value
|
||||||
|
|
||||||
|
|
||||||
def create_pitch(current: Item, options: dict) -> Pitch:
|
def create_pitch(current: Item, options: dict) -> Pitch:
|
||||||
"""Create pitch based on values and options"""
|
"""Create pitch based on values and options"""
|
||||||
|
|
||||||
@ -249,6 +278,7 @@ class Sequence(Meta):
|
|||||||
@dataclass(kw_only=True)
|
@dataclass(kw_only=True)
|
||||||
class PolyphonicSequence:
|
class PolyphonicSequence:
|
||||||
"""Class for polyphonic sequence"""
|
"""Class for polyphonic sequence"""
|
||||||
|
|
||||||
values: list
|
values: list
|
||||||
|
|
||||||
|
|
||||||
@ -357,9 +387,9 @@ class ListOperation(Sequence):
|
|||||||
"""Vertical arpeggio operation, eg. (135)@(q 1 2 021)"""
|
"""Vertical arpeggio operation, eg. (135)@(q 1 2 021)"""
|
||||||
left = _filter_operation(left, options)
|
left = _filter_operation(left, options)
|
||||||
right = _filter_operation(right, options)
|
right = _filter_operation(right, options)
|
||||||
if not isinstance(left,list):
|
if not isinstance(left, list):
|
||||||
left = list(left.evaluate_tree(options))
|
left = list(left.evaluate_tree(options))
|
||||||
if not isinstance(right,list):
|
if not isinstance(right, list):
|
||||||
right = list(right.evaluate_tree(options))
|
right = list(right.evaluate_tree(options))
|
||||||
arp_items = []
|
arp_items = []
|
||||||
|
|
||||||
|
|||||||
@ -337,8 +337,15 @@ class ZiffersTransformer(Transformer):
|
|||||||
return items[0].value
|
return items[0].value
|
||||||
|
|
||||||
def variable(self, items):
|
def variable(self, items):
|
||||||
|
if len(items)>1:
|
||||||
|
prefixes = sum_dict(items[0:-1])
|
||||||
|
text_prefix = prefixes.pop("text")
|
||||||
|
return Variable(name=items[-1], text=text_prefix+items[-1], local_options=prefixes)
|
||||||
|
return Variable(name=items[0], text=items[0])
|
||||||
|
|
||||||
|
def variable_char(self, items):
|
||||||
"""Return parsed variable name"""
|
"""Return parsed variable name"""
|
||||||
return Variable(name=items[0].value, text=items[0].value)
|
return items[0].value #Variable(name=items[0].value, text=items[0].value)
|
||||||
|
|
||||||
def variablelist(self, items):
|
def variablelist(self, items):
|
||||||
"""Return list of variables"""
|
"""Return list of variables"""
|
||||||
|
|||||||
@ -16,7 +16,8 @@
|
|||||||
// Variable assignment
|
// Variable assignment
|
||||||
assignment: variable ass_op (list | pitch_class | random_integer | random_pitch | cycle | list_op | repeat_item)
|
assignment: variable ass_op (list | pitch_class | random_integer | random_pitch | cycle | list_op | repeat_item)
|
||||||
ass_op: /[=~]/
|
ass_op: /[=~]/
|
||||||
variable: /[A-Z]/
|
variable: prefix* variable_char
|
||||||
|
variable_char: /[A-Z]/
|
||||||
|
|
||||||
variablelist: variable variable+
|
variablelist: variable variable+
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user