def parse_kern_octave(note, accs, lineno=1): """Calculate the octave of a note in the kern representation. Since kern uses repetition and case to indicate octave, we need to parse it. The central octave is 4, these are the most common octaves: ccc = 6, cc = 5, c = 4, C = 3, CC = 2, CCC = 1, CCCC = 0 This is a basic example of use: >>> parse_kern_octave('bb', '') 5 And we need to take care of the cases where an accidental will change the octave: >>> parse_kern_octave('bb', '#') 6 """ size = len(note) if size > 0: if note[0].islower(): octave = 3 + size else: octave = -size + 4 assert size <= 4, "octave can't be lower than 0, the value is " + str(octave) else: kern_error("Note can't be empty.", lineno) n = music.string_to_code(note[0], "", "base12") a = music.accidental(accs) return octave + ((n + a) // 12)
def parse_kern_item(item, note_system="base40", lineno=1, itemno=1): tokens = kern_tokenizer(item, lineno) if (not tokens['dur']) and ((not tokens['acciac']) or (not tokens['app'])): kern_error("Duration can't be NULL.", lineno) if (tokens['note'] and tokens['rest']): kern_error("A note can't have a pitch and a rest.", lineno) dur = int("".join(tokens['dur'])) dots = len(tokens['dot']) duration = music.calculate_duration(dur, dots) if tokens['note']: notename = "".join(tokens['note']) acc = "".join(tokens['acc']) name = parse_kern_note(notename, acc) note = Note(name, duration) note.articulations = tokens['art'] note.beams = tokens['beam'] note.octave = parse_kern_octave(notename, acc, lineno) note.code = music.string_to_code(notename[0], acc, note_system, note.octave) note.system = note_system note.type = "kern" return note elif tokens['rest']: wholeNote = not bool(tokens['rest'] or len(tokens['rest']) >= 1) return Rest(duration, wholeNote) else: kern_error("Kern data must have a note or rest.", lineno)
def test_string_to_code_bf_base12(): assert music.string_to_code("b", "-", "base12") == 10
def test_string_to_code_bs_base12(): assert music.string_to_code("b", "#", "base12") == 0
def test_string_to_code_cs_base40(): assert music.string_to_code("c", "#", "base40") == 4