Exemplo n.º 1
0
    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_abc_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 compare_to_abc2midi_and_metadata(
      self, midi_path, expected_metadata, expected_expanded_metadata, test):
    """Compare parsing results to the abc2midi "reference" implementation."""
    # Compare section annotations and groups before expanding.
    self.compare_proto_list(expected_metadata.section_annotations,
                            test.section_annotations)
    self.compare_proto_list(expected_metadata.section_groups,
                            test.section_groups)

    expanded_test = sequences_lib.expand_section_groups(test)

    abc2midi = midi_io.midi_file_to_sequence_proto(
        os.path.join(testing_lib.get_testdata_dir(), midi_path))

    # abc2midi adds a 1-tick delay to the start of every note, but we don't.
    tick_length = ((1 / (abc2midi.tempos[0].qpm / 60)) /
                   abc2midi.ticks_per_quarter)

    for note in abc2midi.notes:
      # For now, don't compare velocities.
      note.velocity = 90
      note.start_time -= tick_length

    self.compare_proto_list(abc2midi.notes, expanded_test.notes)

    self.assertEqual(abc2midi.total_time, expanded_test.total_time)

    self.compare_proto_list(abc2midi.time_signatures,
                            expanded_test.time_signatures)

    # We've checked the notes and time signatures, now compare the rest of the
    # proto to the expected proto.
    expanded_test_copy = copy.deepcopy(expanded_test)
    del expanded_test_copy.notes[:]
    expanded_test_copy.ClearField('total_time')
    del expanded_test_copy.time_signatures[:]

    self.assertProtoEquals(expected_expanded_metadata, expanded_test_copy)