Added some helper methods
This commit is contained in:
@ -77,9 +77,10 @@ class Item(Meta):
|
|||||||
Returns:
|
Returns:
|
||||||
dict: Options as a dict
|
dict: Options as a dict
|
||||||
"""
|
"""
|
||||||
keys = ["octave","modifier","key","scale","duration"]
|
keys = ["octave", "modifier", "key", "scale", "duration"]
|
||||||
return {key: getattr(self, key) for key in keys if hasattr(self, key)}
|
return {key: getattr(self, key) for key in keys if hasattr(self, key)}
|
||||||
|
|
||||||
|
|
||||||
@dataclass(kw_only=True)
|
@dataclass(kw_only=True)
|
||||||
class Whitespace:
|
class Whitespace:
|
||||||
"""Class for whitespace"""
|
"""Class for whitespace"""
|
||||||
@ -147,6 +148,7 @@ class Pitch(Event):
|
|||||||
scale: str | list = field(default=None)
|
scale: str | list = field(default=None)
|
||||||
freq: float = field(default=None)
|
freq: float = field(default=None)
|
||||||
beat: float = field(default=None)
|
beat: float = field(default=None)
|
||||||
|
|
||||||
def __post_init__(self):
|
def __post_init__(self):
|
||||||
super().__post_init__()
|
super().__post_init__()
|
||||||
if self.text is None:
|
if self.text is None:
|
||||||
@ -171,7 +173,7 @@ class Pitch(Event):
|
|||||||
self.freq = midi_to_freq(note)
|
self.freq = midi_to_freq(note)
|
||||||
self.note = note
|
self.note = note
|
||||||
if self.duration is not None:
|
if self.duration is not None:
|
||||||
self.beat = self.duration*4
|
self.beat = self.duration * 4
|
||||||
|
|
||||||
def check_note(self, options: dict):
|
def check_note(self, options: dict):
|
||||||
"""Check for note modification"""
|
"""Check for note modification"""
|
||||||
@ -209,6 +211,26 @@ class Pitch(Event):
|
|||||||
"""
|
"""
|
||||||
return self.pitch_class
|
return self.pitch_class
|
||||||
|
|
||||||
|
def get_notes(self) -> int:
|
||||||
|
"""Return notes"""
|
||||||
|
return self.note
|
||||||
|
|
||||||
|
def get_octaves(self) -> int:
|
||||||
|
"""Return octave"""
|
||||||
|
return self.octave
|
||||||
|
|
||||||
|
def get_beats(self) -> float:
|
||||||
|
"""Return beats"""
|
||||||
|
return self.beat
|
||||||
|
|
||||||
|
def get_durations(self) -> float:
|
||||||
|
"""Return duration"""
|
||||||
|
return self.duration
|
||||||
|
|
||||||
|
def get_freqs(self) -> float:
|
||||||
|
"""Return frequencies"""
|
||||||
|
return self.freq
|
||||||
|
|
||||||
|
|
||||||
@dataclass(kw_only=True)
|
@dataclass(kw_only=True)
|
||||||
class RandomPitch(Event):
|
class RandomPitch(Event):
|
||||||
@ -241,6 +263,9 @@ class Chord(Event):
|
|||||||
pitch_classes: list[Pitch] = field(default=None)
|
pitch_classes: list[Pitch] = field(default=None)
|
||||||
notes: list[int] = field(default=None)
|
notes: list[int] = field(default=None)
|
||||||
freqs: list[float] = field(default=None, init=False)
|
freqs: list[float] = field(default=None, init=False)
|
||||||
|
octaves: list[int] = field(default=None, init=False)
|
||||||
|
durations: list[float] = field(default=None, init=False)
|
||||||
|
beats: list[float] = field(default=None, init=False)
|
||||||
|
|
||||||
def set_notes(self, notes: list[int]):
|
def set_notes(self, notes: list[int]):
|
||||||
"""Set notes to the class"""
|
"""Set notes to the class"""
|
||||||
@ -250,16 +275,44 @@ class Chord(Event):
|
|||||||
"""Update notes"""
|
"""Update notes"""
|
||||||
notes = []
|
notes = []
|
||||||
freqs = []
|
freqs = []
|
||||||
|
octaves = []
|
||||||
|
durations = []
|
||||||
|
beats = []
|
||||||
|
|
||||||
for pitch in self.pitch_classes:
|
for pitch in self.pitch_classes:
|
||||||
pitch.update_options(options)
|
pitch.update_options(options)
|
||||||
pitch.update_note()
|
pitch.update_note()
|
||||||
notes.append(pitch.note)
|
notes.append(pitch.note)
|
||||||
freqs.append(pitch.freq)
|
freqs.append(pitch.freq)
|
||||||
|
octaves.append(pitch.octave)
|
||||||
|
durations.append(pitch.duration)
|
||||||
|
beats.append(pitch.beat)
|
||||||
|
|
||||||
self.notes = notes
|
self.notes = notes
|
||||||
self.freqs = freqs
|
self.freqs = freqs
|
||||||
|
self.octaves = octaves
|
||||||
|
self.duration = durations
|
||||||
|
self.beats = beats
|
||||||
|
|
||||||
|
def get_notes(self) -> int:
|
||||||
|
"""Return notes"""
|
||||||
|
return self.notes
|
||||||
|
|
||||||
|
def get_octaves(self) -> int:
|
||||||
|
"""Return octave"""
|
||||||
|
return self.octaves
|
||||||
|
|
||||||
|
def get_beats(self) -> float:
|
||||||
|
"""Return beats"""
|
||||||
|
return self.beats
|
||||||
|
|
||||||
|
def get_durations(self) -> float:
|
||||||
|
"""Return duration"""
|
||||||
|
return self.durations
|
||||||
|
|
||||||
|
def get_freqs(self) -> float:
|
||||||
|
"""Return frequencies"""
|
||||||
|
return self.freqs
|
||||||
|
|
||||||
@dataclass(kw_only=True)
|
@dataclass(kw_only=True)
|
||||||
class RomanNumeral(Event):
|
class RomanNumeral(Event):
|
||||||
@ -338,7 +391,9 @@ class Sequence(Meta):
|
|||||||
for obj in self.values:
|
for obj in self.values:
|
||||||
if isinstance(obj, Event):
|
if isinstance(obj, Event):
|
||||||
if obj.local_options:
|
if obj.local_options:
|
||||||
obj.local_options = obj.local_options | self.local_options.copy()
|
obj.local_options = (
|
||||||
|
obj.local_options | self.local_options.copy()
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
obj.local_options = self.local_options.copy()
|
obj.local_options = self.local_options.copy()
|
||||||
|
|
||||||
@ -656,20 +711,32 @@ class Ziffers(Sequence):
|
|||||||
def pitch_classes(self) -> list[int]:
|
def pitch_classes(self) -> list[int]:
|
||||||
"""Return list of pitch classes as ints"""
|
"""Return list of pitch classes as ints"""
|
||||||
return [
|
return [
|
||||||
val.pitch_class for val in self.evaluated_values if isinstance(val, Pitch)
|
val.get_pitches()
|
||||||
|
for val in self.evaluated_values
|
||||||
|
if isinstance(val, (Pitch, Chord))
|
||||||
]
|
]
|
||||||
|
|
||||||
def notes(self) -> list[int]:
|
def notes(self) -> list[int]:
|
||||||
"""Return list of midi notes"""
|
"""Return list of midi notes"""
|
||||||
return [val.note for val in self.evaluated_values if isinstance(val, Pitch)]
|
return [
|
||||||
|
val.get_notes()
|
||||||
|
for val in self.evaluated_values
|
||||||
|
if isinstance(val, (Pitch, Chord))
|
||||||
|
]
|
||||||
|
|
||||||
def durations(self) -> list[float]:
|
def durations(self) -> list[float]:
|
||||||
"""Return list of pitch durations as floats"""
|
"""Return list of pitch durations as floats"""
|
||||||
return [val.duration for val in self.evaluated_values if isinstance(val, Event)]
|
return [
|
||||||
|
val.get_durations()
|
||||||
|
for val in self.evaluated_values
|
||||||
|
if isinstance(val, Event)
|
||||||
|
]
|
||||||
|
|
||||||
def beats(self) -> list[float]:
|
def beats(self) -> list[float]:
|
||||||
"""Return list of pitch durations as floats"""
|
"""Return list of pitch durations as floats"""
|
||||||
return [val.beat for val in self.evaluated_values if isinstance(val, Event)]
|
return [
|
||||||
|
val.get_beats() for val in self.evaluated_values if isinstance(val, Event)
|
||||||
|
]
|
||||||
|
|
||||||
def pairs(self) -> list[tuple]:
|
def pairs(self) -> list[tuple]:
|
||||||
"""Return list of pitches and durations"""
|
"""Return list of pitches and durations"""
|
||||||
@ -681,7 +748,19 @@ class Ziffers(Sequence):
|
|||||||
|
|
||||||
def octaves(self) -> list[int]:
|
def octaves(self) -> list[int]:
|
||||||
"""Return list of octaves"""
|
"""Return list of octaves"""
|
||||||
return [val.octave for val in self.evaluated_values if isinstance(val, Pitch)]
|
return [
|
||||||
|
val.get_octaves()
|
||||||
|
for val in self.evaluated_values
|
||||||
|
if isinstance(val, (Pitch, Chord))
|
||||||
|
]
|
||||||
|
|
||||||
|
def freqs(self) -> list[int]:
|
||||||
|
"""Return list of octaves"""
|
||||||
|
return [
|
||||||
|
val.get_freqs()
|
||||||
|
for val in self.evaluated_values
|
||||||
|
if isinstance(val, (Pitch, Chord))
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
@dataclass(kw_only=True)
|
@dataclass(kw_only=True)
|
||||||
@ -870,7 +949,7 @@ class ListOperation(Sequence):
|
|||||||
left = [
|
left = [
|
||||||
Pitch(
|
Pitch(
|
||||||
pitch_class=operation(x.get_value(options), y.get_value(options)),
|
pitch_class=operation(x.get_value(options), y.get_value(options)),
|
||||||
kwargs=y.get_options()
|
kwargs=y.get_options(),
|
||||||
)
|
)
|
||||||
for (x, y) in pairs
|
for (x, y) in pairs
|
||||||
]
|
]
|
||||||
|
|||||||
@ -112,12 +112,12 @@ class ZiffersTransformer(Transformer):
|
|||||||
prefixes = sum_dict(items[0:-1]) # If there are prefixes
|
prefixes = sum_dict(items[0:-1]) # If there are prefixes
|
||||||
text_prefix = prefixes.pop("text")
|
text_prefix = prefixes.pop("text")
|
||||||
prefixes["prefix"] = text_prefix
|
prefixes["prefix"] = text_prefix
|
||||||
p = Pitch(
|
pitch = Pitch(
|
||||||
pitch_class=items[-1]["pitch_class"],
|
pitch_class=items[-1]["pitch_class"],
|
||||||
text=text_prefix + items[-1]["text"],
|
text=text_prefix + items[-1]["text"],
|
||||||
local_options=prefixes,
|
local_options=prefixes,
|
||||||
)
|
)
|
||||||
return p
|
return pitch
|
||||||
|
|
||||||
val = items[0]
|
val = items[0]
|
||||||
return Pitch(**val)
|
return Pitch(**val)
|
||||||
|
|||||||
Reference in New Issue
Block a user