def testGenerateId(self):
        sequence_id_1 = note_sequence_io.generate_id('/my/file/name',
                                                     'my_collection', 'midi')
        self.assertEquals('/id/midi/my_collection/', sequence_id_1[0:23])
        sequence_id_2 = note_sequence_io.generate_id('/my/file/name',
                                                     'your_collection', 'abc')
        self.assertEquals('/id/abc/your_collection/', sequence_id_2[0:24])
        self.assertEquals(sequence_id_1[23:], sequence_id_2[24:])

        sequence_id_3 = note_sequence_io.generate_id('/your/file/name',
                                                     'my_collection', 'abc')
        self.assertNotEquals(sequence_id_3[22:], sequence_id_1[23:])
        self.assertNotEquals(sequence_id_3[22:], sequence_id_2[24:])
  def testGenerateId(self):
    sequence_id_1 = note_sequence_io.generate_id(
        '/my/file/name', 'my_collection', 'midi')
    self.assertEquals('/id/midi/my_collection/', sequence_id_1[0:23])
    sequence_id_2 = note_sequence_io.generate_id(
        '/my/file/name', 'your_collection', 'abc')
    self.assertEquals('/id/abc/your_collection/', sequence_id_2[0:24])
    self.assertEquals(sequence_id_1[23:], sequence_id_2[24:])

    sequence_id_3 = note_sequence_io.generate_id(
        '/your/file/name', 'my_collection', 'abc')
    self.assertNotEquals(sequence_id_3[22:], sequence_id_1[23:])
    self.assertNotEquals(sequence_id_3[22:], sequence_id_2[24:])
  def runTest(self, relative_root, recursive):
    """Tests the output for the given parameters."""
    root_dir = os.path.join(self.root_dir, relative_root)
    expected_filenames = self.expected_dir_midi_contents[relative_root]
    if recursive:
      for sub_dir in self.expected_sub_dirs[relative_root]:
        for filename in self.expected_dir_midi_contents[
            os.path.join(relative_root, sub_dir)]:
          expected_filenames.add(os.path.join(sub_dir, filename))

    with tempfile.NamedTemporaryFile(
        prefix='ConvertMidiDirToSequenesTest') as output_file:
      with note_sequence_io.NoteSequenceRecordWriter(
          output_file.name) as writer:
        convert_midi_dir_to_note_sequences.convert_directory(
            root_dir, '', writer, recursive)
      actual_filenames = set()
      for sequence in note_sequence_io.note_sequence_record_iterator(
          output_file.name):
        self.assertEquals(
            note_sequence_io.generate_id(sequence.filename,
                                         os.path.basename(relative_root),
                                         'midi'),
            sequence.id)
        self.assertEquals(os.path.basename(root_dir), sequence.collection_name)
        self.assertNotEquals(0, len(sequence.notes))
        actual_filenames.add(sequence.filename)

    self.assertEquals(expected_filenames, actual_filenames)
Exemple #4
0
def convert_directory(root_dir, sub_dir, sequence_writer, recursive=False):
    """Converts MIDIs to NoteSequences and writes to `sequence_writer`.

  MIDI files found in the specified directory specified by the combination of
  `root_dir` and `sub_dir` and converted to NoteSequence protos with the
  basename of `root_dir` as the collection_name, and the relative path to the
  MIDI file from `root_dir` as the filename. If `recursive` is true, recursively
  converts any subdirectories of the specified directory.

  Args:
    root_dir: A string specifying a root directory.
    sub_dir: A string specifying a path to a directory under `root_dir` in which
        to convert MIDI contents.
    sequence_writer: A NoteSequenceRecordWriter to write the resulting
        NoteSequence protos to.
    recursive: A boolean specifying whether or not recursively convert MIDIs
        contained in subdirectories of the specified directory.

  Returns:
    The number of NoteSequence protos written as an integer.
  """
    dir_to_convert = os.path.join(root_dir, sub_dir)
    tf.logging.info("Converting MIDI files in '%s'.", dir_to_convert)
    files_in_dir = tf.gfile.ListDirectory(os.path.join(dir_to_convert))
    recurse_sub_dirs = []
    sequences_written = 0
    sequences_skipped = 0
    for file_in_dir in files_in_dir:
        full_file_path = os.path.join(dir_to_convert, file_in_dir)
        if tf.gfile.IsDirectory(full_file_path):
            if recursive:
                recurse_sub_dirs.append(os.path.join(sub_dir, file_in_dir))
            continue
        try:
            sequence = midi_io.midi_to_sequence_proto(
                tf.gfile.FastGFile(full_file_path).read())
        except midi_io.MIDIConversionError as e:
            tf.logging.warning(
                'Could not parse MIDI file %s. It will be skipped. Error was: %s',
                full_file_path, e)
            sequences_skipped += 1
            continue
        sequence.collection_name = os.path.basename(root_dir)
        sequence.filename = os.path.join(sub_dir, file_in_dir)
        sequence.id = note_sequence_io.generate_id(sequence.filename,
                                                   sequence.collection_name,
                                                   'midi')
        sequence_writer.write(sequence)
        sequences_written += 1
    tf.logging.info("Converted %d MIDI files in '%s'.", sequences_written,
                    dir_to_convert)
    tf.logging.info('Could not parse %d MIDI files.', sequences_skipped)
    for recurse_sub_dir in recurse_sub_dirs:
        sequences_written += convert_directory(root_dir, recurse_sub_dir,
                                               sequence_writer, recursive)
    return sequences_written
def convert_directory(root_dir, sub_dir, sequence_writer, recursive=False):
  """Converts MIDIs to NoteSequences and writes to `sequence_writer`.

  MIDI files found in the specified directory specified by the combination of
  `root_dir` and `sub_dir` and converted to NoteSequence protos with the
  basename of `root_dir` as the collection_name, and the relative path to the
  MIDI file from `root_dir` as the filename. If `recursive` is true, recursively
  converts any subdirectories of the specified directory.

  Args:
    root_dir: A string specifying a root directory.
    sub_dir: A string specifying a path to a directory under `root_dir` in which
        to convert MIDI contents.
    sequence_writer: A NoteSequenceRecordWriter to write the resulting
        NoteSequence protos to.
    recursive: A boolean specifying whether or not recursively convert MIDIs
        contained in subdirectories of the specified directory.

  Returns:
    The number of NoteSequence protos written as an integer.
  """
  dir_to_convert = os.path.join(root_dir, sub_dir)
  tf.logging.info("Converting MIDI files in '%s'.", dir_to_convert)
  files_in_dir = tf.gfile.ListDirectory(os.path.join(dir_to_convert))
  recurse_sub_dirs = []
  sequences_written = 0
  sequences_skipped = 0
  for file_in_dir in files_in_dir:
    full_file_path = os.path.join(dir_to_convert, file_in_dir)
    if tf.gfile.IsDirectory(full_file_path):
      if recursive:
        recurse_sub_dirs.append(os.path.join(sub_dir, file_in_dir))
      continue
    try:
      sequence = midi_io.midi_to_sequence_proto(
          tf.gfile.FastGFile(full_file_path).read())
    except midi_io.MIDIConversionError as e:
      tf.logging.warning(
          'Could not parse MIDI file %s. It will be skipped. Error was: %s',
          full_file_path, e)
      sequences_skipped += 1
      continue
    sequence.collection_name = os.path.basename(root_dir)
    sequence.filename = os.path.join(sub_dir, file_in_dir)
    sequence.id = note_sequence_io.generate_id(sequence.filename,
                                               sequence.collection_name, 'midi')
    sequence_writer.write(sequence)
    sequences_written += 1
  tf.logging.info("Converted %d MIDI files in '%s'.", sequences_written,
                  dir_to_convert)
  tf.logging.info('Could not parse %d MIDI files.', sequences_skipped)
  for recurse_sub_dir in recurse_sub_dirs:
    sequences_written += convert_directory(
        root_dir, recurse_sub_dir, sequence_writer, recursive)
  return sequences_written