Added some support for cycles

This commit is contained in:
2023-02-12 01:09:54 +02:00
parent d91e9d230a
commit bd911b96cf

View File

@ -137,7 +137,7 @@ class Pitch(Event):
return note return note
# pylint: disable=locally-disabled, unused-argument # pylint: disable=locally-disabled, unused-argument
def get_value(self, re_eval=False) -> int: def get_value(self) -> int:
"""Returns the pitch class """Returns the pitch class
Returns: Returns:
@ -153,7 +153,7 @@ class RandomPitch(Event):
pitch_class: int = field(default=None) pitch_class: int = field(default=None)
# pylint: disable=locally-disabled, unused-argument # pylint: disable=locally-disabled, unused-argument
def get_value(self, re_eval=False) -> int: def get_value(self) -> int:
"""Return random value """Return random value
Returns: Returns:
@ -251,14 +251,14 @@ class Sequence(Meta):
text = text + self.wrap_end text = text + self.wrap_end
return text return text
def evaluate_tree(self, options=None, re_eval=False): def evaluate_tree(self, options=None):
"""Evaluates and flattens the Ziffers object tree""" """Evaluates and flattens the Ziffers object tree"""
for item in self.values: for item in self.values:
if isinstance(item, Sequence): if isinstance(item, Sequence):
if item.evaluation: if item.evaluation:
yield from item.evaluate(options, re_eval) yield from item.evaluate(options)
else: else:
yield from item.evaluate_tree(options, re_eval) yield from item.evaluate_tree(options)
else: else:
# Get value / generated value from the item # Get value / generated value from the item
current = item.get_item() current = item.get_item()
@ -268,8 +268,10 @@ class Sequence(Meta):
options = self.__update_options(current, options) options = self.__update_options(current, options)
else: else:
if set(("key", "scale")) <= options.keys(): if set(("key", "scale")) <= options.keys():
if isinstance(current,Cyclic):
current = current.get_value()
if isinstance(current, (Pitch, RandomPitch, RandomInteger)): if isinstance(current, (Pitch, RandomPitch, RandomInteger)):
current = self.__update_pitch(current, options, re_eval) current = self.__update_pitch(current, options)
elif isinstance(current, Chord): elif isinstance(current, Chord):
current = self.__update_chord(current, options) current = self.__update_chord(current, options)
elif isinstance(current, RomanNumeral): elif isinstance(current, RomanNumeral):
@ -311,7 +313,7 @@ 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, re_eval: bool = False) -> dict: def __update_pitch(self, current: Item, options: dict) -> dict:
"""Update pich based on optons """Update pich based on optons
Args: Args:
@ -337,14 +339,14 @@ class Sequence(Meta):
note = note_from_pc( note = note_from_pc(
root=options["key"], root=options["key"],
pitch_class=current.get_value(re_eval), pitch_class=current.get_value(),
intervals=options["scale"], intervals=options["scale"],
modifier=c_modifier, modifier=c_modifier,
octave=c_octave, octave=c_octave,
) )
new_pitch = Pitch( new_pitch = Pitch(
pitch_class=current.get_value(re_eval), pitch_class=current.get_value(),
text=str(current.get_value(re_eval)), text=str(current.get_value()),
note=note, note=note,
octave=c_octave, octave=c_octave,
modifier=c_modifier, modifier=c_modifier,
@ -415,14 +417,21 @@ class Ziffers(Sequence):
self.loop_i += 1 self.loop_i += 1
return self.current return self.current
def init_opts(self, options): # pylint: disable=locally-disabled, dangerous-default-value
def init_opts(self, options=None):
"""Evaluate the Ziffers tree using the options""" """Evaluate the Ziffers tree using the options"""
self.options = options if options is None:
self.options = DEFAULT_OPTIONS
else:
self.options.update(options)
self.iterator = iter(self.evaluate_tree(self.options)) self.iterator = iter(self.evaluate_tree(self.options))
def re_eval(self, options): def re_eval(self, options=None):
"""Re-evaluate the iterator""" """Re-evaluate the iterator"""
self.iterator = iter(self.evaluate_tree(options, True)) if options is not None:
self.options.update(options)
self.iterator = iter(self.evaluate_tree(self.options))
def get_list(self): def get_list(self):
"""Return list""" """Return list"""
@ -486,7 +495,7 @@ class Integer(Item):
value: int value: int
# pylint: disable=locally-disabled, unused-argument # pylint: disable=locally-disabled, unused-argument
def get_value(self, re_eval=False): def get_value(self):
"""Return value of the integer""" """Return value of the integer"""
return self.value return self.value
@ -506,7 +515,7 @@ class RandomInteger(Item):
self.max = new_max self.max = new_max
# pylint: disable=locally-disabled, unused-argument # pylint: disable=locally-disabled, unused-argument
def get_value(self, re_eval=False): def get_value(self):
"""Evaluate the random value for the generator""" """Evaluate the random value for the generator"""
return random.randint(self.min, self.max) return random.randint(self.min, self.max)
@ -550,13 +559,10 @@ class Cyclic(Item):
text = text + self.wrap_end text = text + self.wrap_end
return text return text
def get_value(self, re_eval=False): def get_value(self):
"""Get the value for the current cycle""" """Get the value for the current cycle"""
value = self.values[self.cycle % len(self.values)] value = self.values[self.cycle % len(self.values)]
if re_eval: # If re-evaluated self.cycle += 1
self.cycle = 0
else:
self.cycle += 1
return value return value
@ -592,7 +598,7 @@ class ListOperation(Sequence):
elif isinstance(item, keep): elif isinstance(item, keep):
yield item yield item
def evaluate(self, options: dict, re_eval=False): def evaluate(self, options: dict):
"""Evaluates the operation""" """Evaluates the operation"""
operators = self.values[1::2] # Fetch every second operator element operators = self.values[1::2] # Fetch every second operator element
values = self.values[::2] # Fetch every second list element values = self.values[::2] # Fetch every second list element
@ -604,7 +610,7 @@ class ListOperation(Sequence):
if isinstance(right_value, Sequence): if isinstance(right_value, Sequence):
result = [ result = [
Pitch( Pitch(
pitch_class=operation(x.get_value(re_eval), y.get_value(re_eval)), pitch_class=operation(x.get_value(), y.get_value()),
kwargs=options, kwargs=options,
) )
for x in result for x in result
@ -614,7 +620,7 @@ class ListOperation(Sequence):
result = [ result = [
Pitch( Pitch(
pitch_class=operation( pitch_class=operation(
x.get_value(re_eval), right_value.get_value(re_eval) x.get_value(), right_value.get_value()
), ),
kwargs=options, kwargs=options,
) )