Added repeats
Added evaluation of [: :] and (: :) and cycles: (: (1,5) <q e <e s>> (2,3) :6) ... etc.
This commit is contained in:
@ -15,8 +15,7 @@ class Meta:
|
|||||||
|
|
||||||
def __post_init__(self):
|
def __post_init__(self):
|
||||||
if self.kwargs:
|
if self.kwargs:
|
||||||
for key, val in self.kwargs.items():
|
self.update_new(self.kwargs)
|
||||||
setattr(self, key, val)
|
|
||||||
|
|
||||||
def update(self, new_values):
|
def update(self, new_values):
|
||||||
"""Update attributes from dict"""
|
"""Update attributes from dict"""
|
||||||
@ -32,6 +31,7 @@ class Meta:
|
|||||||
setattr(self, key, value)
|
setattr(self, key, value)
|
||||||
|
|
||||||
def dict(self):
|
def dict(self):
|
||||||
|
"""Returns safe dict from the dataclass"""
|
||||||
return {k: str(v) for k, v in asdict(self).items()}
|
return {k: str(v) for k, v in asdict(self).items()}
|
||||||
|
|
||||||
|
|
||||||
@ -41,10 +41,6 @@ class Item(Meta):
|
|||||||
|
|
||||||
text: str = field(default=None)
|
text: str = field(default=None)
|
||||||
|
|
||||||
def get_item(self):
|
|
||||||
"""Return the item"""
|
|
||||||
return self
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(kw_only=True)
|
@dataclass(kw_only=True)
|
||||||
class Whitespace:
|
class Whitespace:
|
||||||
@ -53,13 +49,14 @@ class Whitespace:
|
|||||||
text: str
|
text: str
|
||||||
item_type: str = field(default=None, repr=False, init=False)
|
item_type: str = field(default=None, repr=False, init=False)
|
||||||
|
|
||||||
def get_item(self):
|
|
||||||
"""Returns None. Used in filtering"""
|
@dataclass(kw_only=True)
|
||||||
return None
|
class Modification(Item):
|
||||||
|
"""Superclass for pitch modifications"""
|
||||||
|
|
||||||
|
|
||||||
@dataclass(kw_only=True)
|
@dataclass(kw_only=True)
|
||||||
class DurationChange(Item):
|
class DurationChange(Modification):
|
||||||
"""Class for changing duration"""
|
"""Class for changing duration"""
|
||||||
|
|
||||||
value: float
|
value: float
|
||||||
@ -68,7 +65,7 @@ class DurationChange(Item):
|
|||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class OctaveChange(Item):
|
class OctaveChange(Modification):
|
||||||
"""Class for changing octave"""
|
"""Class for changing octave"""
|
||||||
|
|
||||||
value: int
|
value: int
|
||||||
@ -77,7 +74,7 @@ class OctaveChange(Item):
|
|||||||
|
|
||||||
|
|
||||||
@dataclass(kw_only=True)
|
@dataclass(kw_only=True)
|
||||||
class OctaveAdd(Item):
|
class OctaveAdd(Modification):
|
||||||
"""Class for modifying octave"""
|
"""Class for modifying octave"""
|
||||||
|
|
||||||
value: int
|
value: int
|
||||||
@ -110,13 +107,13 @@ class Pitch(Event):
|
|||||||
self.text = str(self.pitch_class)
|
self.text = str(self.pitch_class)
|
||||||
self.update_note()
|
self.update_note()
|
||||||
|
|
||||||
def update_note(self):
|
def update_note(self, force: bool=False):
|
||||||
"""Update note if Key, Scale and Pitch-class is present"""
|
"""Update note if Key, Scale and Pitch-class are present"""
|
||||||
if (
|
if (
|
||||||
(self.key is not None)
|
(self.key is not None)
|
||||||
and (self.scale is not None)
|
and (self.scale is not None)
|
||||||
and (self.pitch_class is not None)
|
and (self.pitch_class is not None)
|
||||||
and (self.note is None)
|
and (self.note is None or force)
|
||||||
):
|
):
|
||||||
note = note_from_pc(
|
note = note_from_pc(
|
||||||
root=self.key,
|
root=self.key,
|
||||||
@ -128,6 +125,18 @@ class Pitch(Event):
|
|||||||
self.freq = midi_to_freq(note)
|
self.freq = midi_to_freq(note)
|
||||||
self.note = note
|
self.note = note
|
||||||
|
|
||||||
|
def check_note(self, options: dict):
|
||||||
|
"""Check for note modification"""
|
||||||
|
if "key" in options and self.key is not options["key"]:
|
||||||
|
self.key = options["key"]
|
||||||
|
edit = True
|
||||||
|
if "scale" in options and self.scale is not options["scale"]:
|
||||||
|
self.scale = options["scale"]
|
||||||
|
edit = True
|
||||||
|
if edit:
|
||||||
|
self.update_note(True)
|
||||||
|
|
||||||
|
|
||||||
def set_note(self, note: int) -> int:
|
def set_note(self, note: int) -> int:
|
||||||
"""Sets a note for the pitch and returns the note.
|
"""Sets a note for the pitch and returns the note.
|
||||||
|
|
||||||
@ -141,6 +150,7 @@ class Pitch(Event):
|
|||||||
return note
|
return note
|
||||||
|
|
||||||
def set_freq(self, freq: float):
|
def set_freq(self, freq: float):
|
||||||
|
"""Set frequency for the pitch object"""
|
||||||
self.freq = freq
|
self.freq = freq
|
||||||
|
|
||||||
# pylint: disable=locally-disabled, unused-argument
|
# pylint: disable=locally-disabled, unused-argument
|
||||||
@ -159,6 +169,7 @@ class RandomPitch(Event):
|
|||||||
|
|
||||||
pitch_class: int = field(default=None)
|
pitch_class: int = field(default=None)
|
||||||
|
|
||||||
|
# FIXME: Get scale length as max somehow?
|
||||||
# pylint: disable=locally-disabled, unused-argument
|
# pylint: disable=locally-disabled, unused-argument
|
||||||
def get_value(self) -> int:
|
def get_value(self) -> int:
|
||||||
"""Return random value
|
"""Return random value
|
||||||
@ -166,7 +177,7 @@ class RandomPitch(Event):
|
|||||||
Returns:
|
Returns:
|
||||||
int: Returns random pitch
|
int: Returns random pitch
|
||||||
"""
|
"""
|
||||||
return self.pitch_class
|
return random.randint(0, 9)
|
||||||
|
|
||||||
|
|
||||||
@dataclass(kw_only=True)
|
@dataclass(kw_only=True)
|
||||||
@ -240,7 +251,7 @@ class Sequence(Meta):
|
|||||||
self.text = self.__collect_text()
|
self.text = self.__collect_text()
|
||||||
|
|
||||||
def __getitem__(self, index):
|
def __getitem__(self, index):
|
||||||
return self.values[index]
|
return self.evaluated_values[index % len(self.evaluated_values)]
|
||||||
|
|
||||||
def update_values(self, new_values):
|
def update_values(self, new_values):
|
||||||
"""Update value attributes from dict"""
|
"""Update value attributes from dict"""
|
||||||
@ -258,60 +269,50 @@ class Sequence(Meta):
|
|||||||
text = text + self.wrap_end
|
text = text + self.wrap_end
|
||||||
return text
|
return text
|
||||||
|
|
||||||
def evaluate_tree(self, options=None, eval_tree=False):
|
def evaluate_tree(self, options: dict = None, eval_tree: bool = False):
|
||||||
"""Evaluates and flattens the Ziffers object tree"""
|
"""Evaluate the tree and return array of resolved pitches
|
||||||
values = self.evaluated_values if eval_tree else self.values
|
|
||||||
for item in values:
|
Args:
|
||||||
|
options (dict, optional): Options for the pitches. Defaults to None.
|
||||||
|
eval_tree (bool, optional): Flag for using the evaluated subtree. Defaults to False.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def resolve_item(item: Meta, options: dict):
|
||||||
|
"""Resolve cyclic value"""
|
||||||
if isinstance(item, Sequence):
|
if isinstance(item, Sequence):
|
||||||
if isinstance(item, ListOperation):
|
if isinstance(item, ListOperation):
|
||||||
yield from item.evaluate_tree(options, True)
|
yield from item.evaluate_tree(options, True)
|
||||||
|
elif isinstance(item, RepeatedSequence):
|
||||||
|
repeats = item.repeats.get_value()
|
||||||
|
yield from normal_repeat(item.evaluated_values, repeats, options)
|
||||||
|
elif isinstance(item, RepeatedListSequence):
|
||||||
|
repeats = item.repeats.get_value()
|
||||||
|
yield from generative_repeat(item, repeats, options)
|
||||||
else:
|
else:
|
||||||
yield from item.evaluate_tree(options)
|
yield from item.evaluate_tree(options)
|
||||||
else:
|
elif isinstance(item, Cyclic):
|
||||||
# Get value / generated value from the item
|
yield from resolve_item(item.get_value(), options)
|
||||||
current = item.get_item()
|
elif isinstance(item, Modification):
|
||||||
# Ignore items that returns None
|
options = update_options(item, options)
|
||||||
if current is not None:
|
elif isinstance(item, Meta): # Filters whitespace
|
||||||
if isinstance(current, (DurationChange, OctaveChange, OctaveAdd)):
|
yield update_item(item, options)
|
||||||
options = self.__update_options(current, options)
|
|
||||||
else:
|
|
||||||
if set(("key", "scale")) <= options.keys():
|
|
||||||
if isinstance(current, Cyclic):
|
|
||||||
current = current.get_value()
|
|
||||||
if isinstance(current, (Pitch, RandomPitch, RandomInteger)):
|
|
||||||
current = self.__update_pitch(current, options)
|
|
||||||
elif isinstance(current, Chord):
|
|
||||||
current = self.__update_chord(current, options)
|
|
||||||
elif isinstance(current, RomanNumeral):
|
|
||||||
current = self.__create_chord_from_roman(
|
|
||||||
current, options
|
|
||||||
)
|
|
||||||
current.update_new(options)
|
|
||||||
yield current
|
|
||||||
|
|
||||||
def filter(self, keep: tuple):
|
# pylint: disable=locally-disabled, unused-variable
|
||||||
"""Filter out items from sequence.
|
def generative_repeat(tree: list, times: int, options: dict):
|
||||||
|
"""Repeats items and generates new random values"""
|
||||||
|
for i in range(times):
|
||||||
|
for item in tree.evaluate_tree(options):
|
||||||
|
yield from resolve_item(item, options)
|
||||||
|
|
||||||
Args:
|
# pylint: disable=locally-disabled, unused-variable
|
||||||
keep (tuple): Tuple describing classes to keep
|
def normal_repeat(tree: list, times: int, options: dict):
|
||||||
|
"""Repeats items with the same random values"""
|
||||||
|
for i in range(times):
|
||||||
|
for item in tree:
|
||||||
|
yield from resolve_item(item, options)
|
||||||
|
|
||||||
Returns:
|
def update_options(current: Item, options: dict) -> dict:
|
||||||
Sequence: Copy of the sequence with filtered values.
|
"""Update options based on current item"""
|
||||||
"""
|
|
||||||
return replace(
|
|
||||||
self, values=[item for item in self.values if isinstance(item, keep)]
|
|
||||||
)
|
|
||||||
|
|
||||||
def __update_options(self, current: Item, options: dict) -> dict:
|
|
||||||
"""Update options based on current item
|
|
||||||
|
|
||||||
Args:
|
|
||||||
current (Item): Current item like Duration change, Octave change etc.
|
|
||||||
options (dict): Current options
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
dict: Updated options
|
|
||||||
"""
|
|
||||||
if current.item_type == "change": # Change options
|
if current.item_type == "change": # Change options
|
||||||
options[current.key] = current.value
|
options[current.key] = current.value
|
||||||
elif current.item_type == "add":
|
elif current.item_type == "add":
|
||||||
@ -321,16 +322,8 @@ class Sequence(Meta):
|
|||||||
options[current.key] = current.value
|
options[current.key] = current.value
|
||||||
return options
|
return options
|
||||||
|
|
||||||
def __update_pitch(self, current: Item, options: dict) -> dict:
|
def create_pitch(current: Item, options: dict) -> dict:
|
||||||
"""Update pich based on optons
|
"""Create pitch based on values and options"""
|
||||||
|
|
||||||
Args:
|
|
||||||
current (Item): _description_
|
|
||||||
options (dict): _description_
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
dict: _description_
|
|
||||||
"""
|
|
||||||
|
|
||||||
if "modifier" in options:
|
if "modifier" in options:
|
||||||
c_modifier = options["modifier"]
|
c_modifier = options["modifier"]
|
||||||
@ -340,7 +333,6 @@ class Sequence(Meta):
|
|||||||
if hasattr(current, "modifier") and current.modifier is not None:
|
if hasattr(current, "modifier") and current.modifier is not None:
|
||||||
c_modifier += current.modifier
|
c_modifier += current.modifier
|
||||||
|
|
||||||
|
|
||||||
if "octave" in options:
|
if "octave" in options:
|
||||||
c_octave = options["octave"]
|
c_octave = options["octave"]
|
||||||
else:
|
else:
|
||||||
@ -367,36 +359,20 @@ class Sequence(Meta):
|
|||||||
)
|
)
|
||||||
return new_pitch
|
return new_pitch
|
||||||
|
|
||||||
def __update_chord(self, current: Chord, options: dict) -> Chord:
|
def update_chord(current: Chord, options: dict) -> Chord:
|
||||||
"""Update chord based on options
|
"""Update chord based on options"""
|
||||||
|
|
||||||
Args:
|
|
||||||
current (Chord): Current chord object
|
|
||||||
options (dict): Options
|
|
||||||
re (bool, optional): Re-evaluation flag. Defaults to False.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Chord: Returns updated chord
|
|
||||||
"""
|
|
||||||
pcs = current.pitch_classes
|
pcs = current.pitch_classes
|
||||||
notes = [
|
notes = [
|
||||||
pc.set_note(note_from_pc(options["key"], pc.pitch_class, options["scale"]))
|
pc.set_note(
|
||||||
|
note_from_pc(options["key"], pc.pitch_class, options["scale"])
|
||||||
|
)
|
||||||
for pc in pcs
|
for pc in pcs
|
||||||
]
|
]
|
||||||
current.set_notes(notes)
|
current.set_notes(notes)
|
||||||
return current
|
return current
|
||||||
|
|
||||||
def __create_chord_from_roman(self, current: RomanNumeral, options: dict) -> Chord:
|
def create_chord_from_roman(current: RomanNumeral, options: dict) -> Chord:
|
||||||
"""Create chord fom roman numeral
|
"""Create chord fom roman numeral"""
|
||||||
|
|
||||||
Args:
|
|
||||||
current (RomanNumeral): Current roman numeral
|
|
||||||
options (dict): Options
|
|
||||||
re (bool, optional): Re-evaluation flag. Defaults to False.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Chord: New chord created from Roman numeral
|
|
||||||
"""
|
|
||||||
key = options["key"]
|
key = options["key"]
|
||||||
scale = options["scale"]
|
scale = options["scale"]
|
||||||
pitches = [midi_to_pitch_class(note, key, scale) for note in current.notes]
|
pitches = [midi_to_pitch_class(note, key, scale) for note in current.notes]
|
||||||
@ -409,9 +385,45 @@ class Sequence(Meta):
|
|||||||
)
|
)
|
||||||
for pitch in pitches
|
for pitch in pitches
|
||||||
]
|
]
|
||||||
chord = Chord(text="".join(pitches), pitch_classes=pitches, notes=chord_notes)
|
chord = Chord(
|
||||||
|
text="".join(pitches), pitch_classes=pitches, notes=chord_notes
|
||||||
|
)
|
||||||
return chord
|
return chord
|
||||||
|
|
||||||
|
def update_item(item, options):
|
||||||
|
"""Update or create new pitch"""
|
||||||
|
if set(("key", "scale")) <= options.keys():
|
||||||
|
if isinstance(item,Pitch):
|
||||||
|
# TODO: Re-evaluation?
|
||||||
|
# item.check_note(options)
|
||||||
|
pass
|
||||||
|
elif isinstance(item, (RandomPitch, RandomInteger)):
|
||||||
|
item = create_pitch(item, options)
|
||||||
|
elif isinstance(item, Chord):
|
||||||
|
item = update_chord(item, options)
|
||||||
|
elif isinstance(item, RomanNumeral):
|
||||||
|
item = create_chord_from_roman(item, options)
|
||||||
|
item.update_new(options)
|
||||||
|
return item
|
||||||
|
|
||||||
|
# Start of the main function: Evaluate and flatten the Ziffers object tree
|
||||||
|
values = self.evaluated_values if eval_tree else self.values
|
||||||
|
for item in values:
|
||||||
|
yield from resolve_item(item, options)
|
||||||
|
|
||||||
|
def filter(self, keep: tuple):
|
||||||
|
"""Filter out items from sequence.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
keep (tuple): Tuple describing classes to keep
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Sequence: Copy of the sequence with filtered values.
|
||||||
|
"""
|
||||||
|
return replace(
|
||||||
|
self, values=[item for item in self.values if isinstance(item, keep)]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@dataclass(kw_only=True)
|
@dataclass(kw_only=True)
|
||||||
class Ziffers(Sequence):
|
class Ziffers(Sequence):
|
||||||
@ -485,9 +497,7 @@ class Ziffers(Sequence):
|
|||||||
|
|
||||||
def notes(self) -> list[int]:
|
def notes(self) -> list[int]:
|
||||||
"""Return list of midi notes"""
|
"""Return list of midi notes"""
|
||||||
return [
|
return [val.note for val in self.evaluated_values if isinstance(val, Pitch)]
|
||||||
val.note for val in self.evaluated_values if isinstance(val, Pitch)
|
|
||||||
]
|
|
||||||
|
|
||||||
def durations(self) -> list[float]:
|
def durations(self) -> list[float]:
|
||||||
"""Return list of pitch durations as floats"""
|
"""Return list of pitch durations as floats"""
|
||||||
@ -701,3 +711,19 @@ class RepeatedSequence(Sequence):
|
|||||||
repeats: RandomInteger | Integer = field(default_factory=Integer(value=1, text="1"))
|
repeats: RandomInteger | Integer = field(default_factory=Integer(value=1, text="1"))
|
||||||
wrap_start: str = field(default="[:", repr=False)
|
wrap_start: str = field(default="[:", repr=False)
|
||||||
wrap_end: str = field(default=":]", repr=False)
|
wrap_end: str = field(default=":]", repr=False)
|
||||||
|
|
||||||
|
evaluated_values: list = None
|
||||||
|
|
||||||
|
def __post_init__(self):
|
||||||
|
super().__post_init__()
|
||||||
|
self.evaluated_values = list(self.evaluate())
|
||||||
|
|
||||||
|
def evaluate(self):
|
||||||
|
"""Evaluate repeated sequence partially. Leaves Cycles intact."""
|
||||||
|
for item in self.values:
|
||||||
|
if isinstance(item, Sequence):
|
||||||
|
yield from item
|
||||||
|
elif isinstance(item, Cyclic):
|
||||||
|
yield item # Return the cycle
|
||||||
|
elif isinstance(item, (Event, RandomInteger)):
|
||||||
|
yield Pitch(pitch_class=item.get_value())
|
||||||
|
|||||||
@ -267,7 +267,7 @@ class ZiffersTransformer(Transformer):
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
seq = RepeatedListSequence(
|
seq = RepeatedListSequence(
|
||||||
values=items[-2], repeats=Integer(text="1", value=1)
|
values=items[-2], repeats=Integer(text="2", value=2)
|
||||||
)
|
)
|
||||||
seq.update_values(prefixes)
|
seq.update_values(prefixes)
|
||||||
return seq
|
return seq
|
||||||
@ -280,7 +280,7 @@ class ZiffersTransformer(Transformer):
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
seq = RepeatedListSequence(
|
seq = RepeatedListSequence(
|
||||||
values=items[-2], repeats=Integer(text="1", value=1)
|
values=items[-2], repeats=Integer(text="2", value=2)
|
||||||
)
|
)
|
||||||
return seq
|
return seq
|
||||||
|
|
||||||
@ -348,4 +348,4 @@ class ZiffersTransformer(Transformer):
|
|||||||
values=items[0], repeats=items[-1], wrap_end=":" + items[-1].text + "]"
|
values=items[0], repeats=items[-1], wrap_end=":" + items[-1].text + "]"
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
return RepeatedSequence(values=items[0], repeats=Integer(value=1, text="1"))
|
return RepeatedSequence(values=items[0], repeats=Integer(value=2, text="2"))
|
||||||
|
|||||||
Reference in New Issue
Block a user