# microtone = n.pitch.microtone.cents ##actual = C0 * 2**octave * 2**(pitchClass/12) * 2**(microtone/1200) ##actual = C0 * pow(2, octave) * pow(2, pitchClass/12) * pow(2, microtone/1200) # actual = n.pitch.frequency # print('Goal: {}'.format(goal)) # print('Actual: {}'.format(actual)) # print(round(goal / actual, 3)) # print('-----------------------') # exit() test = parse_file("example.ma") print(test) from to_music21 import construct_music21 m21_result = construct_music21(test) for n in m21_result: automatic = n.frequency manual = C0 * pow(2, n.pitch.octave) * pow(2, (n.pitch.pitchClass) / 12) * pow(2, n.pitch.microtone.cents / 1200) print("Manual: {}".format(manual)) print("Automatic: {}".format(automatic)) automatic = n.pitch.ps manual = 12 * (n.pitch.octave + 1) + n.pitch.pitchClass + n.pitch.microtone.cents / 100 print("Manual: {}".format(manual)) print("Automatic: {}".format(automatic)) print() # exit() m21_result.show("text") # m21_result.show('midi') # m21_result.show('vexflow') # m21_result.show('lily.png')
comment = '#' + restOfLine musicobject.ignore(comment) #fraction = Regex(r'(\d*[./]?\d*)') number = Regex(r'[\d./]+') number.setParseAction(lambda s, l, t: [float(eval(t[0]))]) frequency_symbol = Regex(r'[abcdefg_]\d?[#-]?') frequency_number = number frequency = frequency_number ^ frequency_symbol duration = number tone = frequency ^ (Suppress('(') + frequency + Suppress(',') + duration + Suppress(')')) tone.setParseAction(lambda s, l, t: Tone(*t)) group = Suppress('{') + delimitedList(Grp(OneOrMore(musicobject)), ',') + Suppress('}') group.setParseAction(lambda s, l, t: Group(t)) transformed = tone + '*' + musicobject transformed.setParseAction(lambda s, l, t: Transformed(t[0], t[2])) musicobject << (tone ^ group ^ transformed) result = musicobject.parseFile('example.music') print(result[0]) from to_music21 import construct_music21 construct_music21(result[0]).write('musicxml', 'foo.xml') construct_music21(result[0]).show('text')