コード例 #1
0
  def __init__(self, quantized_sequence, num_velocity_bins, instrument=0,
               start_step=0, max_shift_steps=1000, max_duration_steps=1000):
    """Construct a NotePerformance.

    Args:
      quantized_sequence: A quantized NoteSequence proto.
      num_velocity_bins: Number of velocity bins to use.
      instrument: If not None, extract only the specified instrument from
          `quantized_sequence`. Otherwise, extract all instruments.
      start_step: The offset of this sequence relative to the beginning of the
          source sequence.
      max_shift_steps: Maximum number of steps for a time-shift event.
      max_duration_steps: Maximum number of steps for a duration event.

    Raises:
      ValueError: If `num_velocity_bins` is larger than the number of MIDI
          velocity values.
    """
    program, is_drum = _program_and_is_drum_from_sequence(
        quantized_sequence, instrument)

    super(NotePerformance, self).__init__(
        start_step=start_step,
        num_velocity_bins=num_velocity_bins,
        max_shift_steps=max_shift_steps,
        program=program,
        is_drum=is_drum)

    self._max_duration_steps = max_duration_steps

    sequences_lib.assert_is_absolute_quantized_sequence(quantized_sequence)
    self._steps_per_second = (
        quantized_sequence.quantization_info.steps_per_second)
    self._events = self._from_quantized_sequence(
        quantized_sequence, instrument)
コード例 #2
0
  def __init__(self, quantized_sequence=None, steps_per_second=None,
               start_step=0, num_velocity_bins=0,
               max_shift_steps=DEFAULT_MAX_SHIFT_STEPS, instrument=None,
               program=None, is_drum=None):
    """Construct a Performance.

    Either quantized_sequence or steps_per_second should be supplied.

    Args:
      quantized_sequence: A quantized NoteSequence proto.
      steps_per_second: Number of quantized time steps per second, if using
          absolute quantization.
      start_step: The offset of this sequence relative to the
          beginning of the source sequence. If a quantized sequence is used as
          input, only notes starting after this step will be considered.
      num_velocity_bins: Number of velocity bins to use. If 0, velocity events
          will not be included at all.
      max_shift_steps: Maximum number of steps for a single time-shift event.
      instrument: If not None, extract only the specified instrument from
          `quantized_sequence`. Otherwise, extract all instruments.
      program: MIDI program used for this performance, or None if not specified.
          Ignored if `quantized_sequence` is provided.
      is_drum: Whether or not this performance consists of drums, or None if not
          specified. Ignored if `quantized_sequence` is provided.

    Raises:
      ValueError: If both or neither of `quantized_sequence` or
          `steps_per_second` is specified.
    """
    if (quantized_sequence, steps_per_second).count(None) != 1:
      raise ValueError(
          'Must specify exactly one of quantized_sequence or steps_per_second')

    if quantized_sequence:
      sequences_lib.assert_is_absolute_quantized_sequence(quantized_sequence)
      self._steps_per_second = (
          quantized_sequence.quantization_info.steps_per_second)
      self._events = self._from_quantized_sequence(
          quantized_sequence, start_step, num_velocity_bins,
          max_shift_steps=max_shift_steps, instrument=instrument)
      program, is_drum = _program_and_is_drum_from_sequence(
          quantized_sequence, instrument)

    else:
      self._steps_per_second = steps_per_second
      self._events = []

    super(Performance, self).__init__(
        start_step=start_step,
        num_velocity_bins=num_velocity_bins,
        max_shift_steps=max_shift_steps,
        program=program,
        is_drum=is_drum)
コード例 #3
0
ファイル: performance_lib.py プロジェクト: vnsavitri/magenta
    def __init__(self,
                 quantized_sequence=None,
                 steps_per_second=None,
                 start_step=0,
                 num_velocity_bins=0,
                 max_shift_steps=DEFAULT_MAX_SHIFT_STEPS):
        """Construct a Performance.

    Either quantized_sequence or steps_per_second should be supplied.

    Args:
      quantized_sequence: A quantized NoteSequence proto.
      steps_per_second: Number of quantized time steps per second, if using
          absolute quantization.
      start_step: The offset of this sequence relative to the
          beginning of the source sequence. If a quantized sequence is used as
          input, only notes starting after this step will be considered.
      num_velocity_bins: Number of velocity bins to use. If 0, velocity events
          will not be included at all.
      max_shift_steps: Maximum number of steps for a single time-shift event.

    Raises:
      ValueError: If both or neither of `quantized_sequence` or
          `steps_per_second` is specified.
    """
        if (quantized_sequence, steps_per_second).count(None) != 1:
            raise ValueError(
                'Must specify exactly one of quantized_sequence or steps_per_second'
            )

        if quantized_sequence:
            sequences_lib.assert_is_absolute_quantized_sequence(
                quantized_sequence)
            self._steps_per_second = (
                quantized_sequence.quantization_info.steps_per_second)
            self._events = self._from_quantized_sequence(
                quantized_sequence,
                start_step,
                num_velocity_bins,
                max_shift_steps=max_shift_steps)

        else:
            self._steps_per_second = steps_per_second
            self._events = []

        super(Performance, self).__init__(start_step=start_step,
                                          num_velocity_bins=num_velocity_bins,
                                          max_shift_steps=max_shift_steps)
コード例 #4
0
    def __init__(self,
                 quantized_sequence=None,
                 steps_per_second=None,
                 start_step=0,
                 num_velocity_bins=0):
        """Construct a Performance.

    Either quantized_sequence or steps_per_second should be supplied.

    Args:
      quantized_sequence: A quantized NoteSequence proto.
      steps_per_second: Number of quantized time steps per second.
      start_step: The offset of this sequence relative to the
          beginning of the source sequence. If a quantized sequence is used as
          input, only notes starting after this step will be considered.
      num_velocity_bins: Number of velocity bins to use. If 0, velocity events
          will not be included at all.

    Raises:
      ValueError: If `num_velocity_bins` is larger than the number of MIDI
          velocity values.
    """
        if (quantized_sequence, steps_per_second).count(None) != 1:
            raise ValueError(
                'Must specify exactly one of quantized_sequence or steps_per_second'
            )

        if num_velocity_bins > MAX_MIDI_VELOCITY - MIN_MIDI_VELOCITY + 1:
            raise ValueError('Number of velocity bins is too large: %d' %
                             num_velocity_bins)

        if quantized_sequence:
            sequences_lib.assert_is_absolute_quantized_sequence(
                quantized_sequence)
            self._events = self._from_quantized_sequence(
                quantized_sequence, start_step, num_velocity_bins)
            self._steps_per_second = (
                quantized_sequence.quantization_info.steps_per_second)
        else:
            self._events = []
            self._steps_per_second = steps_per_second

        self._start_step = start_step
        self._num_velocity_bins = num_velocity_bins
コード例 #5
0
ファイル: performance_lib.py プロジェクト: wyn314/magenta
  def __init__(self, quantized_sequence=None, steps_per_second=None,
               start_step=0, num_velocity_bins=0):
    """Construct a Performance.

    Either quantized_sequence or steps_per_second should be supplied.

    Args:
      quantized_sequence: A quantized NoteSequence proto.
      steps_per_second: Number of quantized time steps per second.
      start_step: The offset of this sequence relative to the
          beginning of the source sequence. If a quantized sequence is used as
          input, only notes starting after this step will be considered.
      num_velocity_bins: Number of velocity bins to use. If 0, velocity events
          will not be included at all.

    Raises:
      ValueError: If `num_velocity_bins` is larger than the number of MIDI
          velocity values.
    """
    if (quantized_sequence, steps_per_second).count(None) != 1:
      raise ValueError(
          'Must specify exactly one of quantized_sequence or steps_per_second')

    if num_velocity_bins > MAX_MIDI_VELOCITY - MIN_MIDI_VELOCITY + 1:
      raise ValueError(
          'Number of velocity bins is too large: %d' % num_velocity_bins)

    if quantized_sequence:
      sequences_lib.assert_is_absolute_quantized_sequence(quantized_sequence)
      self._events = self._from_quantized_sequence(
          quantized_sequence, start_step, num_velocity_bins)
      self._steps_per_second = (
          quantized_sequence.quantization_info.steps_per_second)
    else:
      self._events = []
      self._steps_per_second = steps_per_second

    self._start_step = start_step
    self._num_velocity_bins = num_velocity_bins
コード例 #6
0
ファイル: performance_lib.py プロジェクト: wyn314/magenta
def extract_performances(
    quantized_sequence, start_step=0, min_events_discard=None,
    max_events_truncate=None, num_velocity_bins=0):
  """Extracts a performance from the given quantized NoteSequence.

  Currently, this extracts only one performance from a given track.

  Args:
    quantized_sequence: A quantized NoteSequence.
    start_step: Start extracting a sequence at this time step.
    min_events_discard: Minimum length of tracks in events. Shorter tracks are
        discarded.
    max_events_truncate: Maximum length of tracks in events. Longer tracks are
        truncated.
    num_velocity_bins: Number of velocity bins to use. If 0, velocity events
        will not be included at all.

  Returns:
    performances: A python list of Performance instances.
    stats: A dictionary mapping string names to `statistics.Statistic` objects.
  """
  sequences_lib.assert_is_absolute_quantized_sequence(quantized_sequence)

  stats = dict([(stat_name, statistics.Counter(stat_name)) for stat_name in
                ['performances_discarded_too_short',
                 'performances_truncated',
                 'performances_discarded_more_than_1_program']])

  steps_per_second = quantized_sequence.quantization_info.steps_per_second

  # Create a histogram measuring lengths (in bars not steps).
  stats['performance_lengths_in_seconds'] = statistics.Histogram(
      'performance_lengths_in_seconds',
      [5, 10, 20, 30, 40, 60, 120])

  # Allow only 1 program.
  programs = set()
  for note in quantized_sequence.notes:
    programs.add(note.program)
  if len(programs) > 1:
    stats['performances_discarded_more_than_1_program'].increment()
    return [], stats.values()

  performances = []

  # Translate the quantized sequence into a Performance.
  performance = Performance(quantized_sequence, start_step=start_step,
                            num_velocity_bins=num_velocity_bins)

  if (max_events_truncate is not None and
      len(performance) > max_events_truncate):
    performance.truncate(max_events_truncate)
    stats['performances_truncated'].increment()

  if min_events_discard is not None and len(performance) < min_events_discard:
    stats['performances_discarded_too_short'].increment()
  else:
    performances.append(performance)
    stats['performance_lengths_in_seconds'].increment(
        performance.num_steps // steps_per_second)

  return performances, stats.values()
コード例 #7
0
def extract_performances(quantized_sequence,
                         start_step=0,
                         min_events_discard=None,
                         max_events_truncate=None,
                         num_velocity_bins=0):
    """Extracts a performance from the given quantized NoteSequence.

  Currently, this extracts only one performance from a given track.

  Args:
    quantized_sequence: A quantized NoteSequence.
    start_step: Start extracting a sequence at this time step.
    min_events_discard: Minimum length of tracks in events. Shorter tracks are
        discarded.
    max_events_truncate: Maximum length of tracks in events. Longer tracks are
        truncated.
    num_velocity_bins: Number of velocity bins to use. If 0, velocity events
        will not be included at all.

  Returns:
    performances: A python list of Performance instances.
    stats: A dictionary mapping string names to `statistics.Statistic` objects.
  """
    sequences_lib.assert_is_absolute_quantized_sequence(quantized_sequence)

    stats = dict([(stat_name, statistics.Counter(stat_name)) for stat_name in [
        'performances_discarded_too_short', 'performances_truncated',
        'performances_discarded_more_than_1_program'
    ]])

    steps_per_second = quantized_sequence.quantization_info.steps_per_second

    # Create a histogram measuring lengths (in bars not steps).
    stats['performance_lengths_in_seconds'] = statistics.Histogram(
        'performance_lengths_in_seconds', [5, 10, 20, 30, 40, 60, 120])

    # Allow only 1 program.
    programs = set()
    for note in quantized_sequence.notes:
        programs.add(note.program)
    if len(programs) > 1:
        stats['performances_discarded_more_than_1_program'].increment()
        return [], stats.values()

    performances = []

    # Translate the quantized sequence into a Performance.
    performance = Performance(quantized_sequence,
                              start_step=start_step,
                              num_velocity_bins=num_velocity_bins)

    if (max_events_truncate is not None
            and len(performance) > max_events_truncate):
        performance.truncate(max_events_truncate)
        stats['performances_truncated'].increment()

    if min_events_discard is not None and len(
            performance) < min_events_discard:
        stats['performances_discarded_too_short'].increment()
    else:
        performances.append(performance)
        stats['performance_lengths_in_seconds'].increment(
            performance.num_steps // steps_per_second)

    return performances, stats.values()