def note_minus_intvl(note_obj, intvl_obj): from pymusician import Note, Interval if not isinstance(intvl_obj, Interval): raise ValueError("Intervals can only be added to Note objects.") letter = note_obj.letter - intvl_obj.letter_diff if letter < 0: letter += 7 if note_obj.octave != 0: hard_pitch = note_obj.hard_pitch - intvl_obj.diff new_note = Note.from_values(letter, abs(hard_pitch % 12)) new_note.octave = hard_pitch // 12 else: pitch = note_obj.pitch - intvl_obj.diff if intvl_obj._displace > 0: pitch -= intvl_obj._displace * 12 if pitch < 0: pitch += 12 new_note = Note.from_values(letter, pitch) if note_obj.rhythm: print(note_obj.rhythm) new_note.rhythm = note_obj.rhythm.flags return new_note
def note_minus_intvl(note_obj,intvl_obj): from pymusician import Note, Interval if not isinstance(intvl_obj,Interval): raise ValueError("Intervals can only be added to Note objects.") letter = note_obj.letter - intvl_obj.letter_diff if letter < 0: letter += 7 if note_obj.octave != 0: hard_pitch = note_obj.hard_pitch - intvl_obj.diff new_note = Note.from_values(letter,abs(hard_pitch % 12)) new_note.octave = hard_pitch // 12 else: pitch = note_obj.pitch - intvl_obj.diff if intvl_obj._displace > 0: pitch -= intvl_obj._displace * 12 if pitch < 0: pitch += 12 new_note = Note.from_values(letter,pitch) if note_obj.rhythm: print(note_obj.rhythm) new_note.rhythm = note_obj.rhythm.flags return new_note
def enharmonic(note_obj, prefer=None, gross=False): from pymusician import Note if prefer: if prefer not in ("#", "b"): raise ValueError("'prefer' parameter should be set to '#' or 'b'.") if not isinstance(gross, bool): raise ValueError("2nd arg for enharmonic should be Boolean.") if gross and note_obj.name in GROSS_ROOTS: new_name = GROSS_ROOTS[note_obj.name] if "#" in new_name and prefer == 'b': return note_obj elif 'b' in new_name and prefer == '#': return note_obj else: new_note = Note(new_name) elif len(note_obj.name) == 1: return note_obj elif len(note_obj.name) == 2: if "#" in note_obj.name: if prefer == '#': return note_obj new_letter = note_obj.letter + 1 if new_letter > 6: new_letter -= 7 else: if prefer == 'b': return note_obj new_letter = note_obj.letter - 1 if new_letter < 0: new_letter += 7 new_note = Note.from_values(new_letter, note_obj.pitch) else: new_note = note_obj new_letter = note_obj.letter limit = 2 if note_obj.pitch in NON_NATURAL else 1 while len(new_note.name) > limit: if "#" in note_obj.name: new_letter += 1 if new_letter > 6: new_letter -= 7 else: new_letter -= 1 if new_letter < 0: new_letter += 7 new_note = Note.from_values(new_letter, note_obj.pitch) if "#" in new_note.name and prefer == "b": new_note = Note.from_values(new_letter + 1, note_obj.pitch) elif "b" in new_note.name and prefer == "#": new_note = Note.from_values(new_letter - 1, note_obj.pitch) if note_obj.octave: new_note.octave = note_obj.octave if note_obj.rhythm: new_note.rhythm = note_obj.rhythm.flags return new_note
def enharmonic(note_obj,prefer=None,gross=False): from pymusician import Note if prefer: if prefer not in ("#","b"): raise ValueError("'prefer' parameter should be set to '#' or 'b'.") if not isinstance(gross, bool): raise ValueError("2nd arg for enharmonic should be Boolean.") if gross and note_obj.name in GROSS_ROOTS: new_name = GROSS_ROOTS[note_obj.name] if "#" in new_name and prefer == 'b': return note_obj elif 'b' in new_name and prefer == '#': return note_obj else: new_note = Note(new_name) elif len(note_obj.name) == 1: return note_obj elif len(note_obj.name) == 2: if "#" in note_obj.name: if prefer == '#': return note_obj new_letter = note_obj.letter + 1 if new_letter > 6: new_letter -= 7 else: if prefer == 'b': return note_obj new_letter = note_obj.letter - 1 if new_letter < 0: new_letter += 7 new_note = Note.from_values(new_letter,note_obj.pitch) else: new_note = note_obj new_letter = note_obj.letter limit = 2 if note_obj.pitch in NON_NATURAL else 1 while len(new_note.name) > limit: if "#" in note_obj.name: new_letter += 1 if new_letter > 6: new_letter -= 7 else: new_letter -= 1 if new_letter < 0: new_letter += 7 new_note = Note.from_values(new_letter,note_obj.pitch) if "#" in new_note.name and prefer == "b": new_note = Note.from_values(new_letter + 1, note_obj.pitch) elif "b" in new_note.name and prefer == "#": new_note = Note.from_values(new_letter - 1, note_obj.pitch) if note_obj.octave: new_note.octave = note_obj.octave if note_obj.rhythm: new_note.rhythm = note_obj.rhythm.flags return new_note
def mode_speller(root,mode): from pymusician import Note spelling = [root] if type(MODES[mode]) is str: parent_name = MODES[mode][:len(MODES[mode])-1] offset = int(MODES[mode][-1]) - 1 elif type(MODES[mode]) is list: parent_name = mode offset = 0 else: raise ValueError("Invalid Mode pattern. (Check modes json)") parent = MODES[parent_name] for item in parent: if type(item) is not int: raise ValueError("Invalid Mode pattern. (Check modes json)") next_pitch = root.pitch next_letter = root.letter index = offset n = 1 flats = False if "b" not in root.name else True sharps = False if "#" not in root.name else True if parent_name in MODE_LETTER_SPELLINGS: for step in parent: if index == len(parent): index -= len(parent) if n == len(parent): break next_pitch += parent[index] if parent_name in MODE_LETTER_SPELLINGS: next_letter += MODE_LETTER_SPELLINGS[parent_name][index] else: next_letter += 1 if next_pitch < 0: next_pitch += 12 elif next_pitch > 11: next_pitch -= 12 if next_letter < 0: next_letter += 7 elif next_letter > 6: next_letter -= 7 if parent_name in MODE_LETTER_SPELLINGS: next_note = Note.from_values(next_letter,next_pitch) else: next_note = Note.from_values(next_letter,next_pitch) if len(next_note.name) > 2: next_note = next_note.enharmonic() if next_note.name in ("B#","Cb","E#","Fb"): next_note = next_note.enharmonic() if "#" in next_note.name: if flats: next_note = next_note.enharmonic() if not flats and not sharps: sharps = True if "b" in next_note.name: if sharps: next_note = next_note.enharmonic() if not flats and not sharps: flats = True spelling.append(next_note) index += 1 n += 1 else: flats = False if "b" not in root.name else True sharps = False if "#" not in root.name else True for step in parent: if index == len(parent): index -= len(parent) if n == len(parent): break next_pitch += parent[index] next_letter += 1 if next_pitch < 0: next_pitch += 12 elif next_pitch > 11: next_pitch -= 12 if next_letter < 0: next_letter += 7 elif next_letter > 6: next_letter -= 7 next_note = Note.from_values(next_letter,next_pitch) if len(next_note.name) > 2: next_note = next_note.enharmonic() if next_note.name in ("B#","Cb","E#","Fb"): next_note = next_note.enharmonic() if "#" in next_note.name: if flats: next_note = next_note.enharmonic() if not flats and not sharps: sharps = True if "b" in next_note.name: if sharps: next_note = next_note.enharmonic() if not flats and not sharps: flats = True index += 1 n += 1 spelling.append(next_note) return tuple(spelling)
def mode_speller(root, mode): from pymusician import Note spelling = [root] if type(MODES[mode]) is str: parent_name = MODES[mode][:len(MODES[mode]) - 1] offset = int(MODES[mode][-1]) - 1 elif type(MODES[mode]) is list: parent_name = mode offset = 0 else: raise ValueError("Invalid Mode pattern. (Check modes json)") parent = MODES[parent_name] for item in parent: if type(item) is not int: raise ValueError("Invalid Mode pattern. (Check modes json)") next_pitch = root.pitch next_letter = root.letter index = offset n = 1 flats = False if "b" not in root.name else True sharps = False if "#" not in root.name else True if parent_name in MODE_LETTER_SPELLINGS: for step in parent: if index == len(parent): index -= len(parent) if n == len(parent): break next_pitch += parent[index] if parent_name in MODE_LETTER_SPELLINGS: next_letter += MODE_LETTER_SPELLINGS[parent_name][index] else: next_letter += 1 if next_pitch < 0: next_pitch += 12 elif next_pitch > 11: next_pitch -= 12 if next_letter < 0: next_letter += 7 elif next_letter > 6: next_letter -= 7 if parent_name in MODE_LETTER_SPELLINGS: next_note = Note.from_values(next_letter, next_pitch) else: next_note = Note.from_values(next_letter, next_pitch) if len(next_note.name) > 2: next_note = next_note.enharmonic() if next_note.name in ("B#", "Cb", "E#", "Fb"): next_note = next_note.enharmonic() if "#" in next_note.name: if flats: next_note = next_note.enharmonic() if not flats and not sharps: sharps = True if "b" in next_note.name: if sharps: next_note = next_note.enharmonic() if not flats and not sharps: flats = True spelling.append(next_note) index += 1 n += 1 else: flats = False if "b" not in root.name else True sharps = False if "#" not in root.name else True for step in parent: if index == len(parent): index -= len(parent) if n == len(parent): break next_pitch += parent[index] next_letter += 1 if next_pitch < 0: next_pitch += 12 elif next_pitch > 11: next_pitch -= 12 if next_letter < 0: next_letter += 7 elif next_letter > 6: next_letter -= 7 next_note = Note.from_values(next_letter, next_pitch) if len(next_note.name) > 2: next_note = next_note.enharmonic() if next_note.name in ("B#", "Cb", "E#", "Fb"): next_note = next_note.enharmonic() if "#" in next_note.name: if flats: next_note = next_note.enharmonic() if not flats and not sharps: sharps = True if "b" in next_note.name: if sharps: next_note = next_note.enharmonic() if not flats and not sharps: flats = True index += 1 n += 1 spelling.append(next_note) return tuple(spelling)