Ejemplo n.º 1
0
    def augment(self, note_sequence):
        """Python implementation that augments the NoteSequence."""
        trans_amt = (random.randint(
            *self._transpose_range) if self._transpose_range else 0)
        stretch_factor = (random.uniform(
            *self._stretch_range) if self._stretch_range else 1.0)
        augmented_ns = copy.deepcopy(note_sequence)
        del augmented_ns.notes[:]
        for note in note_sequence.notes:
            aug_pitch = note.pitch
            if not note.is_drum:
                aug_pitch += trans_amt
            if MIN_MIDI_PITCH <= aug_pitch <= MAX_MIDI_PITCH:
                augmented_ns.notes.add().CopyFrom(note)
                augmented_ns.notes[-1].pitch = aug_pitch

        for ta in augmented_ns.text_annotations:
            if ta.annotation_type == CHORD_SYMBOL and ta.text != mm.NO_CHORD:
                try:
                    figure = chord_symbols_lib.transpose_chord_symbol(
                        ta.text, trans_amt)
                except chord_symbols_lib.ChordSymbolException:
                    tf.logging.warning('Unable to transpose chord symbol: %s',
                                       ta.text)
                    figure = mm.NO_CHORD
                ta.text = figure

        augmented_ns = sequences_lib.stretch_note_sequence(
            augmented_ns, stretch_factor)
        return augmented_ns
Ejemplo n.º 2
0
  def augment(self, note_sequence):
    """Python implementation that augments the NoteSequence."""
    trans_amt = (random.randint(*self._transpose_range)
                 if self._transpose_range else 0)
    stretch_factor = (random.uniform(*self._stretch_range)
                      if self._stretch_range else 1.0)
    augmented_ns = copy.deepcopy(note_sequence)
    del augmented_ns.notes[:]
    for note in note_sequence.notes:
      aug_pitch = note.pitch
      if not note.is_drum:
        aug_pitch += trans_amt
      if MIN_MIDI_PITCH <= aug_pitch <= MAX_MIDI_PITCH:
        augmented_ns.notes.add().CopyFrom(note)
        augmented_ns.notes[-1].pitch = aug_pitch

    for ta in augmented_ns.text_annotations:
      if ta.annotation_type == CHORD_SYMBOL and ta.text != mm.NO_CHORD:
        try:
          figure = chord_symbols_lib.transpose_chord_symbol(ta.text, trans_amt)
        except chord_symbols_lib.ChordSymbolException:
          tf.logging.warning('Unable to transpose chord symbol: %s', ta.text)
          figure = mm.NO_CHORD
        ta.text = figure

    augmented_ns = sequences_lib.stretch_note_sequence(
        augmented_ns, stretch_factor)
    return augmented_ns
  def testStretchPipeline(self):
    note_sequence = common_testing_lib.parse_test_proto(
        music_pb2.NoteSequence,
        """
        time_signatures: {
          time: 1.0
          numerator: 4
          denominator: 4}
        tempos: {
          qpm: 60}""")
    testing_lib.add_track_to_sequence(
        note_sequence, 0,
        [(11, 55, 0.22, 0.50), (40, 45, 2.50, 3.50), (55, 120, 4.0, 4.01)])

    expected_sequences = [
        sequences_lib.stretch_note_sequence(note_sequence, 0.5),
        sequences_lib.stretch_note_sequence(note_sequence, 1.0),
        sequences_lib.stretch_note_sequence(note_sequence, 1.5)]

    unit = note_sequence_pipelines.StretchPipeline(
        stretch_factors=[0.5, 1.0, 1.5])
    self._unit_transform_test(unit, note_sequence, expected_sequences)
  def testStretchPipeline(self):
    note_sequence = common_testing_lib.parse_test_proto(
        music_pb2.NoteSequence,
        """
        time_signatures: {
          time: 1.0
          numerator: 4
          denominator: 4}
        tempos: {
          qpm: 60}""")
    testing_lib.add_track_to_sequence(
        note_sequence, 0,
        [(11, 55, 0.22, 0.50), (40, 45, 2.50, 3.50), (55, 120, 4.0, 4.01)])

    expected_sequences = [
        sequences_lib.stretch_note_sequence(note_sequence, 0.5),
        sequences_lib.stretch_note_sequence(note_sequence, 1.0),
        sequences_lib.stretch_note_sequence(note_sequence, 1.5)]

    unit = note_sequence_pipelines.StretchPipeline(
        stretch_factors=[0.5, 1.0, 1.5])
    self._unit_transform_test(unit, note_sequence, expected_sequences)
Ejemplo n.º 5
0
 def augment_note_sequence(ns, stretch_factor, transpose_amount):
   """Augment a NoteSequence by time stretch and pitch transposition."""
   augmented_ns = sequences_lib.stretch_note_sequence(
       ns, stretch_factor, in_place=False)
   try:
     _, num_deleted_notes = sequences_lib.transpose_note_sequence(
         augmented_ns, transpose_amount,
         min_allowed_pitch=MIN_PITCH, max_allowed_pitch=MAX_PITCH,
         in_place=True)
   except chord_symbols_lib.ChordSymbolError:
     raise datagen_beam.DataAugmentationError(
         'Transposition of chord symbol(s) failed.')
   if num_deleted_notes:
     raise datagen_beam.DataAugmentationError(
         'Transposition caused out-of-range pitch(es).')
   return augmented_ns
Ejemplo n.º 6
0
 def augment_note_sequence(ns, stretch_factor, transpose_amount):
   """Augment a NoteSequence by time stretch and pitch transposition."""
   augmented_ns = sequences_lib.stretch_note_sequence(
       ns, stretch_factor, in_place=False)
   try:
     _, num_deleted_notes = sequences_lib.transpose_note_sequence(
         augmented_ns, transpose_amount,
         min_allowed_pitch=MIN_PITCH, max_allowed_pitch=MAX_PITCH,
         in_place=True)
   except chord_symbols_lib.ChordSymbolError:
     raise datagen_beam.DataAugmentationError(
         'Transposition of chord symbol(s) failed.')
   if num_deleted_notes:
     raise datagen_beam.DataAugmentationError(
         'Transposition caused out-of-range pitch(es).')
   return augmented_ns
Ejemplo n.º 7
0
  def augment(self, note_sequence):
    """Python implementation that augments the NoteSequence."""
    trans_amt = (random.randint(*self._transpose_range)
                 if self._transpose_range else 0)
    stretch_factor = (random.uniform(*self._stretch_range)
                      if self._stretch_range else 1.0)
    augmented_ns = copy.deepcopy(note_sequence)
    del augmented_ns.notes[:]
    for note in note_sequence.notes:
      aug_pitch = note.pitch + trans_amt
      if MIN_MIDI_PITCH <= aug_pitch <= MAX_MIDI_PITCH:
        augmented_ns.notes.add().CopyFrom(note)
        augmented_ns.notes[-1].pitch = aug_pitch

    augmented_ns = sequences_lib.stretch_note_sequence(
        augmented_ns, stretch_factor)
    return augmented_ns
Ejemplo n.º 8
0
  def augment(self, note_sequence):
    """Python implementation that augments the NoteSequence."""
    trans_amt = (random.randint(*self._transpose_range)
                 if self._transpose_range else 0)
    stretch_factor = (random.uniform(*self._stretch_range)
                      if self._stretch_range else 1.0)
    augmented_ns = copy.deepcopy(note_sequence)
    del augmented_ns.notes[:]
    for note in note_sequence.notes:
      aug_pitch = note.pitch + trans_amt
      if MIN_MIDI_PITCH <= aug_pitch <= MAX_MIDI_PITCH:
        augmented_ns.notes.add().CopyFrom(note)
        augmented_ns.notes[-1].pitch = aug_pitch

    augmented_ns = sequences_lib.stretch_note_sequence(
        augmented_ns, stretch_factor)
    return augmented_ns
Ejemplo n.º 9
0
  def testStretchNoteSequence(self):
    expected_stretched_sequence = copy.deepcopy(self.note_sequence)
    expected_stretched_sequence.tempos[0].qpm = 40

    testing_lib.add_track_to_sequence(
        self.note_sequence, 0,
        [(12, 100, 0.0, 10.0), (11, 55, 0.2, 0.5), (40, 45, 2.5, 3.5)])
    testing_lib.add_track_to_sequence(
        expected_stretched_sequence, 0,
        [(12, 100, 0.0, 15.0), (11, 55, 0.3, 0.75), (40, 45, 3.75, 5.25)])

    testing_lib.add_chords_to_sequence(
        self.note_sequence, [('B7', 0.5), ('Em9', 2.0)])
    testing_lib.add_chords_to_sequence(
        expected_stretched_sequence, [('B7', 0.75), ('Em9', 3.0)])

    stretched_sequence = sequences_lib.stretch_note_sequence(
        self.note_sequence, stretch_factor=1.5)
    self.assertProtoEquals(expected_stretched_sequence, stretched_sequence)
Ejemplo n.º 10
0
  def testStretchNoteSequence(self):
    expected_stretched_sequence = copy.deepcopy(self.note_sequence)
    expected_stretched_sequence.tempos[0].qpm = 40

    testing_lib.add_track_to_sequence(
        self.note_sequence, 0,
        [(12, 100, 0.0, 10.0), (11, 55, 0.2, 0.5), (40, 45, 2.5, 3.5)])
    testing_lib.add_track_to_sequence(
        expected_stretched_sequence, 0,
        [(12, 100, 0.0, 15.0), (11, 55, 0.3, 0.75), (40, 45, 3.75, 5.25)])

    testing_lib.add_chords_to_sequence(
        self.note_sequence, [('B7', 0.5), ('Em9', 2.0)])
    testing_lib.add_chords_to_sequence(
        expected_stretched_sequence, [('B7', 0.75), ('Em9', 3.0)])

    stretched_sequence = sequences_lib.stretch_note_sequence(
        self.note_sequence, stretch_factor=1.5)
    self.assertProtoEquals(expected_stretched_sequence, stretched_sequence)
Ejemplo n.º 11
0
 def transform(self, note_sequence):
   return [sequences_lib.stretch_note_sequence(note_sequence, stretch_factor)
           for stretch_factor in self._stretch_factors]
Ejemplo n.º 12
0
 def transform(self, note_sequence):
     return [
         sequences_lib.stretch_note_sequence(note_sequence, stretch_factor)
         for stretch_factor in self._stretch_factors
     ]