Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
Constructs a SpellingPolicy from several possible input string formats
:param string_initializer: one of the following:
- a key center (case insensitive), such as "C#" or "f" or "Gb"
- a key center followed by a mode, such as "g minor" or "Bb locrian". Most modes to not alter the
way spelling is done, but certain modes like phrygian and locrian do.
- "flat"/"b" or "sharp"/"#", indicating that any note, even a white key, is to be expressed with the
specified accidental. Most useful for spelling known individual notes
- "flats"/"sharps" indicating that black keys will be spelled with the specified accidental, but white
keys will remain unaltered. (Turns out "flats" is equivalent to "Bb" and "sharps" is equivalent to "A".)
"""
if string_initializer in ("flat", "b"):
return SpellingPolicy.all_flats(including_white_keys=True)
elif string_initializer in ("sharp", "#"):
return SpellingPolicy.all_sharps(including_white_keys=True)
elif string_initializer == "flats":
return SpellingPolicy.all_flats(including_white_keys=False)
elif string_initializer == "sharps":
return SpellingPolicy.all_sharps(including_white_keys=False)
else:
# most modes don't change anything about how spelling is done, since we default to flat-3, sharp-4,
# flat-6, and flat-7. The only exceptions are phrygian and locrian, since they have a flat-2 instead of
# a sharp-1. As a result, we have to use a different template for them.
string_initializer_processed = string_initializer.lower().replace(" ", "").replace("-", "").\
replace("_", "").replace("major", "").replace("minor", "").replace("ionian", "").\
replace("dorian", "").replace("lydian", "").replace("mixolydian", "").replace("aeolean", "")
if "phrygian" in string_initializer_processed:
string_initializer_processed = string_initializer_processed.replace("phrygian", "")
template = _c_phrygian_spellings
elif "locrian" in string_initializer_processed:
def from_string(cls, string_initializer: str) -> 'SpellingPolicy':
"""
Constructs a SpellingPolicy from several possible input string formats
:param string_initializer: one of the following:
- a key center (case insensitive), such as "C#" or "f" or "Gb"
- a key center followed by a mode, such as "g minor" or "Bb locrian". Most modes to not alter the
way spelling is done, but certain modes like phrygian and locrian do.
- "flat"/"b" or "sharp"/"#", indicating that any note, even a white key, is to be expressed with the
specified accidental. Most useful for spelling known individual notes
- "flats"/"sharps" indicating that black keys will be spelled with the specified accidental, but white
keys will remain unaltered. (Turns out "flats" is equivalent to "Bb" and "sharps" is equivalent to "A".)
"""
if string_initializer in ("flat", "b"):
return SpellingPolicy.all_flats(including_white_keys=True)
elif string_initializer in ("sharp", "#"):
return SpellingPolicy.all_sharps(including_white_keys=True)
elif string_initializer == "flats":
return SpellingPolicy.all_flats(including_white_keys=False)
elif string_initializer == "sharps":
return SpellingPolicy.all_sharps(including_white_keys=False)
else:
# most modes don't change anything about how spelling is done, since we default to flat-3, sharp-4,
# flat-6, and flat-7. The only exceptions are phrygian and locrian, since they have a flat-2 instead of
# a sharp-1. As a result, we have to use a different template for them.
string_initializer_processed = string_initializer.lower().replace(" ", "").replace("-", "").\
replace("_", "").replace("major", "").replace("minor", "").replace("ionian", "").\
replace("dorian", "").replace("lydian", "").replace("mixolydian", "").replace("aeolean", "")
if "phrygian" in string_initializer_processed:
string_initializer_processed = string_initializer_processed.replace("phrygian", "")
def default_spelling_policy(self, value: Union[SpellingPolicy, str]):
if value is None or isinstance(value, SpellingPolicy):
self._default_spelling_policy = value
elif isinstance(value, str):
self._default_spelling_policy = SpellingPolicy.from_string(value)
else:
raise ValueError("Spelling policy not understood.")
def default_spelling_policy(self, value):
if value is None or isinstance(value, SpellingPolicy):
self._default_spelling_policy = value
elif isinstance(value, str):
self._default_spelling_policy = SpellingPolicy.from_string(value)
else:
raise ValueError("Spelling policy not understood.")
def __init__(self, default_soundfont: str = "default", default_audio_driver: str = "default",
default_midi_output_device: str = "default",
default_spelling_policy: Union[SpellingPolicy, str, tuple] = None,
instruments: Sequence['ScampInstrument'] = None):
self.default_soundfont = default_soundfont
self.default_audio_driver = default_audio_driver
self.default_midi_output_device = default_midi_output_device
self._default_spelling_policy = SpellingPolicy.interpret(default_spelling_policy) \
if default_spelling_policy is not None else None
self.instruments = list(instruments) if instruments is not None else []
self.shared_resources = {}
template = _flat_spellings
else:
template = _c_standard_spellings
try:
num_sharps_or_flats = \
_step_circle_of_fifths_positions[_step_names.index(string_initializer_processed[0])]
except ValueError:
raise ValueError("Bad spelling policy initialization string. Use only 'sharp', 'flat', "
"or the name of the desired key center (e.g. 'G#' or 'Db') with optional mode.")
if string_initializer_processed[1:].startswith(("b", "flat", "f")):
num_sharps_or_flats -= 7
elif string_initializer_processed[1:].startswith(("#", "sharp", "s")):
num_sharps_or_flats += 7
return SpellingPolicy.from_circle_of_fifths_position(num_sharps_or_flats, template=template)
def default_spelling_policy(self, value):
if value is None or isinstance(value, SpellingPolicy):
self._default_spelling_policy = value
elif isinstance(value, str):
self._default_spelling_policy = SpellingPolicy.from_string(value)
else:
raise ValueError("Spelling policy not understood.")