Added frequency and default key&scale
This commit is contained in:
@ -4,7 +4,7 @@ from itertools import product, islice, cycle
|
|||||||
import operator
|
import operator
|
||||||
import random
|
import random
|
||||||
from .defaults import DEFAULT_OPTIONS
|
from .defaults import DEFAULT_OPTIONS
|
||||||
from .scale import note_from_pc, midi_to_pitch_class
|
from .scale import note_from_pc, midi_to_pitch_class, midi_to_freq
|
||||||
|
|
||||||
|
|
||||||
@dataclass(kw_only=True)
|
@dataclass(kw_only=True)
|
||||||
@ -99,6 +99,7 @@ class Pitch(Event):
|
|||||||
note: int = field(default=None)
|
note: int = field(default=None)
|
||||||
key: str = field(default=None)
|
key: str = field(default=None)
|
||||||
scale: str | list = field(default=None)
|
scale: str | list = field(default=None)
|
||||||
|
freq: float = field(default=None)
|
||||||
|
|
||||||
def __post_init__(self):
|
def __post_init__(self):
|
||||||
super().__post_init__()
|
super().__post_init__()
|
||||||
@ -121,7 +122,8 @@ class Pitch(Event):
|
|||||||
modifier=self.modifier if self.modifier is not None else 0,
|
modifier=self.modifier if self.modifier is not None else 0,
|
||||||
octave=self.octave if self.octave is not None else 0,
|
octave=self.octave if self.octave is not None else 0,
|
||||||
)
|
)
|
||||||
self.set_note(note)
|
self.freq = midi_to_freq(note)
|
||||||
|
self.note = note
|
||||||
|
|
||||||
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.
|
||||||
@ -135,6 +137,9 @@ class Pitch(Event):
|
|||||||
self.note = note
|
self.note = note
|
||||||
return note
|
return note
|
||||||
|
|
||||||
|
def set_freq(self, freq: float):
|
||||||
|
self.freq = freq
|
||||||
|
|
||||||
# pylint: disable=locally-disabled, unused-argument
|
# pylint: disable=locally-disabled, unused-argument
|
||||||
def get_value(self) -> int:
|
def get_value(self) -> int:
|
||||||
"""Returns the pitch class
|
"""Returns the pitch class
|
||||||
@ -323,20 +328,24 @@ class Sequence(Meta):
|
|||||||
Returns:
|
Returns:
|
||||||
dict: _description_
|
dict: _description_
|
||||||
"""
|
"""
|
||||||
if hasattr(current, "modifier"):
|
|
||||||
c_modifier = 0
|
if "modifier" in options:
|
||||||
elif "modifier" in options:
|
|
||||||
c_modifier = options["modifier"]
|
c_modifier = options["modifier"]
|
||||||
else:
|
else:
|
||||||
c_modifier = 0
|
c_modifier = 0
|
||||||
|
|
||||||
if hasattr(current, "octave"):
|
if hasattr(current, "modifier") and current.modifier is not None:
|
||||||
c_octave = 0
|
c_modifier += current.modifier
|
||||||
elif "octave" in options:
|
|
||||||
|
|
||||||
|
if "octave" in options:
|
||||||
c_octave = options["octave"]
|
c_octave = options["octave"]
|
||||||
else:
|
else:
|
||||||
c_octave = 0
|
c_octave = 0
|
||||||
|
|
||||||
|
if hasattr(current, "octave") and current.octave is not None:
|
||||||
|
c_octave += current.octave
|
||||||
|
|
||||||
note = note_from_pc(
|
note = note_from_pc(
|
||||||
root=options["key"],
|
root=options["key"],
|
||||||
pitch_class=current.get_value(),
|
pitch_class=current.get_value(),
|
||||||
@ -348,6 +357,7 @@ class Sequence(Meta):
|
|||||||
pitch_class=current.get_value(),
|
pitch_class=current.get_value(),
|
||||||
text=str(current.get_value()),
|
text=str(current.get_value()),
|
||||||
note=note,
|
note=note,
|
||||||
|
freq=midi_to_freq(note),
|
||||||
octave=c_octave,
|
octave=c_octave,
|
||||||
modifier=c_modifier,
|
modifier=c_modifier,
|
||||||
kwargs=options,
|
kwargs=options,
|
||||||
|
|||||||
@ -41,7 +41,7 @@ DEFAULT_DURS = {
|
|||||||
|
|
||||||
DEFAULT_OCTAVE = 4
|
DEFAULT_OCTAVE = 4
|
||||||
|
|
||||||
DEFAULT_OPTIONS = {"octave": 0, "duration": 0.25}
|
DEFAULT_OPTIONS = {"octave": 0, "duration": 0.25, "key":"C4", "scale":"IONIAN"}
|
||||||
|
|
||||||
OPERATORS = {
|
OPERATORS = {
|
||||||
"+": operator.add,
|
"+": operator.add,
|
||||||
|
|||||||
@ -44,6 +44,10 @@ def note_name_to_interval(name: str) -> int:
|
|||||||
interval = NOTES_TO_INTERVALS[values[0].capitalize()]
|
interval = NOTES_TO_INTERVALS[values[0].capitalize()]
|
||||||
return interval + modifier
|
return interval + modifier
|
||||||
|
|
||||||
|
def midi_to_freq(note: int) -> float:
|
||||||
|
"""Transform midi to frequency"""
|
||||||
|
freq = 440 # Frequency of A
|
||||||
|
return (freq / 32) * (2 ** ((note - 9) / 12))
|
||||||
|
|
||||||
def note_name_to_midi(name: str) -> int:
|
def note_name_to_midi(name: str) -> int:
|
||||||
"""Parse note name to midi
|
"""Parse note name to midi
|
||||||
|
|||||||
Reference in New Issue
Block a user