示例#1
0
def process(msd_id: str, counter: AtomicCounter) -> Optional[dict]:
    """
  Processes the given MSD id and increments the counter. The
  method will call the get_tags and the extract_pianos method and write
  the resulting MIDI files to disk.

  :param msd_id: the MSD id to process
  :param counter: the counter to increment
  :return: the dictionary containing the MSD id, the PrettyMIDI pianos and
  the matching tags, raises an exception if the file cannot be processed
  """
    try:
        with tables.open_file(msd_id_to_h5(msd_id,
                                           args.path_dataset_dir)) as h5:
            tags = get_tags(h5)
            matching_tags = [tag for tag in tags if tag in TAGS]
            if not matching_tags:
                return
            pm_pianos = extract_pianos(msd_id)
            for index, pm_piano in enumerate(pm_pianos):
                pm_piano.write(
                    os.path.join(args.path_output_dir,
                                 f"{msd_id}_{index}.mid"))
            return {
                "msd_id": msd_id,
                "pm_pianos": pm_pianos,
                "tags": matching_tags
            }
    except Exception as e:
        print(f"Exception during processing of {msd_id}: {e}")
    finally:
        counter.increment()
def process(midi_path: str, counter: AtomicCounter) -> Optional[dict]:
    """
  Processes the MIDI file at the given path and increments the counter. The
  method will call the extract_drums method and the get_bass_drums_on_beat
  method, and write the resulting drum file if the bass drum ratio is over
  the threshold.

  :param midi_path: the MIDI file path to process
  :param counter: the counter to increment
  :return: the dictionary containing the MIDI path, the PrettyMIDI instance
  and the ratio of bass drum on beat, raises an exception if the file cannot
  be processed
  """
    try:
        pm_drums = extract_drums(midi_path)
        bass_drums_on_beat = get_bass_drums_on_beat(pm_drums)
        if bass_drums_on_beat >= args.bass_drums_on_beat_threshold:
            midi_filename = os.path.basename(midi_path)
            pm_drums.write(
                os.path.join(args.path_output_dir, f"{midi_filename}.mid"))
        else:
            raise Exception(f"Not on beat {midi_path}: {bass_drums_on_beat}")
        return {
            "midi_path": midi_path,
            "pm_drums": pm_drums,
            "bass_drums_on_beat": bass_drums_on_beat
        }
    except Exception as e:
        if "Not on beat" not in str(e):
            print(f"Exception during processing of {midi_path}: {e}")
    finally:
        counter.increment()
示例#3
0
def process(msd_id: str, counter: AtomicCounter) -> Optional[dict]:
    try:
        # TODO
        pass
    except Exception as e:
        print(f"Exception during processing of {msd_id}: {e}")
    finally:
        counter.increment()
示例#4
0
def process(msd_id: str, counter: AtomicCounter) -> Optional[dict]:
  """
  Processes the given MSD id and increments the counter. The
  method will find and return the artist.

  :param msd_id: the MSD id to process
  :param counter: the counter to increment
  :return: the dictionary containing the MSD id and the artist, raises an
  exception if the file cannot be processed
  """
  try:
    with tables.open_file(msd_id_to_h5(msd_id, args.path_dataset_dir)) as h5:
      artist = h5.root.metadata.songs.cols.artist_name[0].decode("utf-8")
      return {"msd_id": msd_id, "artist": artist}
  except Exception as e:
    print(f"Exception during processing of {msd_id}: {e}")
  finally:
    counter.increment()
def process(msd_id: str, counter: AtomicCounter) -> Optional[dict]:
  """
  Processes the given MSD id and increments the counter. The
  method will call the get_instrument_classes method.

  :param msd_id: the MSD id to process
  :param counter: the counter to increment
  :return: the dictionary containing the MSD id and the classes, raises an
  exception if the file cannot be processed
  """
  try:
    with tables.open_file(msd_id_to_h5(msd_id, args.path_dataset_dir)) as h5:
      classes = get_instrument_classes(msd_id)
      return {"msd_id": msd_id, "classes": classes}
  except Exception as e:
    print(f"Exception during processing of {msd_id}: {e}")
  finally:
    counter.increment()
def process(msd_id: str, counter: AtomicCounter) -> Optional[dict]:
  """
  Processes the given MSD id and increments the counter. The
  method will call the extract_drums method and write the resulting MIDI
  files to disk.

  :param msd_id: the MSD id to process
  :param counter: the counter to increment
  :return: the dictionary containing the MSD id and the PrettyMIDI drums;
  raises an exception if the file cannot be processed
  """
  try:
    with tables.open_file(msd_id_to_h5(msd_id, args.path_dataset_dir)) as h5:
      pm_drums = extract_drums(msd_id)
      pm_drums.write(os.path.join(args.path_output_dir, f"{msd_id}.mid"))
      return {"msd_id": msd_id, "pm_drums": pm_drums}
  except Exception as e:
    print(f"Exception during processing of {msd_id}: {e}")
  finally:
    counter.increment()