def testMultiVoice(self): tunes, exceptions = abc_parser.parse_tunebook_file( os.path.join(tf.resource_loader.get_data_files_path(), 'testdata/zocharti_loch.abc')) self.assertEqual(0, len(tunes)) self.assertEqual(1, len(exceptions)) self.assertTrue(isinstance(exceptions[0], abc_parser.MultiVoiceException))
def compare_directory(directory): files_in_dir = tf.gfile.ListDirectory(directory) for file_in_dir in files_in_dir: if not file_in_dir.endswith('.abc'): continue abc = os.path.join(directory, file_in_dir) midis = {} ref_num = 1 while True: midi = re.sub(r'\.abc$', str(ref_num) + '.mid', os.path.join(directory, file_in_dir)) if not tf.gfile.Exists(midi): break midis[ref_num] = midi ref_num += 1 print('parsing {}'.format(abc)) tunes, exceptions = abc_parser.parse_tunebook_file(abc) if len(tunes) != len(midis) - len(exceptions): raise ValueError( 'Different number of tunes and midis for {}'.format(abc)) for tune in tunes.values(): expanded_tune = sequences_lib.expand_section_groups(tune) midi_ns = midi_io.midi_file_to_sequence_proto( midis[tune.reference_number]) # abc2midi adds a 1-tick delay to the start of every note, but we don't. tick_length = ((1 / (midi_ns.tempos[0].qpm / 60)) / midi_ns.ticks_per_quarter) for note in midi_ns.notes: note.start_time -= tick_length if len(midi_ns.notes) != len(expanded_tune.notes): pdb.set_trace()
def compare_directory(self, directory): self.maxDiff = None # pylint: disable=invalid-name files_in_dir = tf.gfile.ListDirectory(directory) files_parsed = 0 for file_in_dir in files_in_dir: if not file_in_dir.endswith('.abc'): continue abc = os.path.join(directory, file_in_dir) midis = {} ref_num = 1 while True: midi = re.sub(r'\.abc$', str(ref_num) + '.mid', os.path.join(directory, file_in_dir)) if not tf.gfile.Exists(midi): break midis[ref_num] = midi ref_num += 1 print('parsing {}: {}'.format(files_parsed, abc)) tunes, exceptions = abc_parser.parse_tunebook_file(abc) files_parsed += 1 self.assertEqual(len(tunes), len(midis) - len(exceptions)) for tune in tunes.values(): expanded_tune = sequences_lib.expand_section_groups(tune) midi_ns = midi_io.midi_file_to_sequence_proto( midis[tune.reference_number]) # abc2midi adds a 1-tick delay to the start of every note, but we don't. tick_length = ((1 / (midi_ns.tempos[0].qpm / 60)) / midi_ns.ticks_per_quarter) for note in midi_ns.notes: note.start_time -= tick_length # For now, don't compare velocities. note.velocity = 90 if len(midi_ns.notes) != len(expanded_tune.notes): pdb.set_trace() self.assertProtoEquals(midi_ns, expanded_tune) for midi_note, test_note in zip(midi_ns.notes, expanded_tune.notes): try: self.assertProtoEquals(midi_note, test_note) except Exception as e: # pylint: disable=broad-except print(e) pdb.set_trace() self.assertEqual(midi_ns.total_time, expanded_tune.total_time)
def testParseEnglishAbc(self): tunes, exceptions = abc_parser.parse_tunebook_file( os.path.join(tf.resource_loader.get_data_files_path(), 'testdata/english.abc')) self.assertEqual(1, len(tunes)) self.assertEqual(2, len(exceptions)) self.assertTrue(isinstance(exceptions[0], abc_parser.VariantEndingException)) self.assertTrue(isinstance(exceptions[1], abc_parser.PartException)) expected_metadata1 = common_testing_lib.parse_test_proto( music_pb2.NoteSequence, """ ticks_per_quarter: 220 source_info: { source_type: SCORE_BASED encoding_type: ABC parser: MAGENTA_ABC } reference_number: 1 sequence_metadata { title: "Dusty Miller, The; Binny's Jig" artist: "Trad." composers: "Trad." } key_signatures { key: G } section_annotations { time: 0.0 section_id: 0 } section_annotations { time: 6.0 section_id: 1 } section_annotations { time: 12.0 section_id: 2 } section_groups { sections { section_id: 0 } num_times: 2 } section_groups { sections { section_id: 1 } num_times: 2 } section_groups { sections { section_id: 2 } num_times: 2 } """) expected_expanded_metadata1 = common_testing_lib.parse_test_proto( music_pb2.NoteSequence, """ ticks_per_quarter: 220 source_info: { source_type: SCORE_BASED encoding_type: ABC parser: MAGENTA_ABC } reference_number: 1 sequence_metadata { title: "Dusty Miller, The; Binny's Jig" artist: "Trad." composers: "Trad." } key_signatures { key: G } section_annotations { time: 0.0 section_id: 0 } section_annotations { time: 6.0 section_id: 0 } section_annotations { time: 12.0 section_id: 1 } section_annotations { time: 18.0 section_id: 1 } section_annotations { time: 24.0 section_id: 2 } section_annotations { time: 30.0 section_id: 2 } """) self.compareToAbc2midiAndMetadata( 'testdata/english1.mid', expected_metadata1, expected_expanded_metadata1, tunes[1])
def testParseEnglishAbc(self): tunes = abc_parser.parse_tunebook_file( os.path.join(tf.resource_loader.get_data_files_path(), 'testdata/english.abc')) self.assertEqual(3, len(tunes)) expected_ns1_metadata = common_testing_lib.parse_test_proto( music_pb2.NoteSequence, """ ticks_per_quarter: 220 source_info: { source_type: SCORE_BASED encoding_type: ABC parser: MAGENTA_ABC } reference_number: 1 sequence_metadata { title: "Dusty Miller, The; Binny's Jig" artist: "Trad." composers: "Trad." } key_signatures { key: G } """) self.compareToAbc2midiAndMetadata('testdata/english1.mid', expected_ns1_metadata, tunes[0]) expected_ns2_metadata = common_testing_lib.parse_test_proto( music_pb2.NoteSequence, """ ticks_per_quarter: 220 source_info: { source_type: SCORE_BASED encoding_type: ABC parser: MAGENTA_ABC } reference_number: 2 sequence_metadata { title: "Old Sir Simon the King" artist: "Trad." composers: "Trad." } key_signatures { key: G } """) self.compareToAbc2midiAndMetadata('testdata/english2.mid', expected_ns2_metadata, tunes[1]) expected_ns3_metadata = common_testing_lib.parse_test_proto( music_pb2.NoteSequence, """ ticks_per_quarter: 220 source_info: { source_type: SCORE_BASED encoding_type: ABC parser: MAGENTA_ABC } reference_number: 3 sequence_metadata { title: "William and Nancy; New Mown Hay; Legacy, The" artist: "Trad." composers: "Trad." } key_signatures { key: G } """) # TODO(fjord): verify chord annotations del tunes[2].text_annotations[:] self.compareToAbc2midiAndMetadata('testdata/english3.mid', expected_ns3_metadata, tunes[2])