def testExtractPerformances(self):
    music_testing_lib.add_track_to_sequence(
        self.note_sequence, 0, [(60, 100, 0.0, 4.0)])
    quantized_sequence = sequences_lib.quantize_note_sequence_absolute(
        self.note_sequence, steps_per_second=100)

    perfs, _ = performance_pipeline.extract_performances(quantized_sequence)
    self.assertLen(perfs, 1)

    perfs, _ = performance_pipeline.extract_performances(
        quantized_sequence, min_events_discard=1, max_events_truncate=10)
    self.assertLen(perfs, 1)

    perfs, _ = performance_pipeline.extract_performances(
        quantized_sequence, min_events_discard=8, max_events_truncate=10)
    self.assertEmpty(perfs)

    perfs, _ = performance_pipeline.extract_performances(
        quantized_sequence, min_events_discard=1, max_events_truncate=3)
    self.assertLen(perfs, 1)
    self.assertLen(perfs[0], 3)

    perfs, _ = performance_pipeline.extract_performances(
        quantized_sequence, max_steps_truncate=100)
    self.assertLen(perfs, 1)
    self.assertEqual(100, perfs[0].num_steps)
    def testFromQuantizedNoteSequenceWithQuantizedVelocity(self):
        testing_lib.add_track_to_sequence(self.note_sequence, 0,
                                          [(60, 100, 0.0, 4.0),
                                           (64, 100, 0.0, 3.0),
                                           (67, 127, 1.0, 2.0)])
        quantized_sequence = sequences_lib.quantize_note_sequence_absolute(
            self.note_sequence, steps_per_second=100)
        performance = list(
            performance_lib.Performance(quantized_sequence,
                                        num_velocity_bins=16))

        pe = performance_lib.PerformanceEvent
        expected_performance = [
            pe(pe.VELOCITY, 13),
            pe(pe.NOTE_ON, 60),
            pe(pe.NOTE_ON, 64),
            pe(pe.TIME_SHIFT, 100),
            pe(pe.VELOCITY, 16),
            pe(pe.NOTE_ON, 67),
            pe(pe.TIME_SHIFT, 100),
            pe(pe.NOTE_OFF, 67),
            pe(pe.TIME_SHIFT, 100),
            pe(pe.NOTE_OFF, 64),
            pe(pe.TIME_SHIFT, 100),
            pe(pe.NOTE_OFF, 60),
        ]
        self.assertEqual(expected_performance, performance)
    def testFromQuantizedNoteSequence(self):
        testing_lib.add_track_to_sequence(self.note_sequence, 0,
                                          [(60, 100, 0.0, 4.0),
                                           (64, 100, 0.0, 3.0),
                                           (67, 100, 1.0, 2.0)])
        quantized_sequence = sequences_lib.quantize_note_sequence_absolute(
            self.note_sequence, steps_per_second=100)
        performance = performance_lib.Performance(quantized_sequence)

        self.assertEqual(100, performance.steps_per_second)

        pe = performance_lib.PerformanceEvent
        expected_performance = [
            pe(pe.NOTE_ON, 60),
            pe(pe.NOTE_ON, 64),
            pe(pe.TIME_SHIFT, 100),
            pe(pe.NOTE_ON, 67),
            pe(pe.TIME_SHIFT, 100),
            pe(pe.NOTE_OFF, 67),
            pe(pe.TIME_SHIFT, 100),
            pe(pe.NOTE_OFF, 64),
            pe(pe.TIME_SHIFT, 100),
            pe(pe.NOTE_OFF, 60),
        ]
        self.assertEqual(expected_performance, list(performance))
    def testProgramAndIsDrumFromQuantizedNoteSequence(self):
        testing_lib.add_track_to_sequence(self.note_sequence,
                                          0, [(60, 100, 0.0, 4.0),
                                              (64, 100, 0.0, 3.0),
                                              (67, 100, 1.0, 2.0)],
                                          program=1)
        testing_lib.add_track_to_sequence(self.note_sequence,
                                          1, [(36, 100, 0.0, 4.0),
                                              (48, 100, 0.0, 4.0)],
                                          program=2)
        testing_lib.add_track_to_sequence(self.note_sequence,
                                          2, [(57, 100, 0.0, 0.1)],
                                          is_drum=True)
        quantized_sequence = sequences_lib.quantize_note_sequence_absolute(
            self.note_sequence, steps_per_second=100)

        performance = performance_lib.Performance(quantized_sequence,
                                                  instrument=0)
        self.assertEqual(1, performance.program)
        self.assertFalse(performance.is_drum)

        performance = performance_lib.Performance(quantized_sequence,
                                                  instrument=1)
        self.assertEqual(2, performance.program)
        self.assertFalse(performance.is_drum)

        performance = performance_lib.Performance(quantized_sequence,
                                                  instrument=2)
        self.assertIsNone(performance.program)
        self.assertTrue(performance.is_drum)

        performance = performance_lib.Performance(quantized_sequence)
        self.assertIsNone(performance.program)
        self.assertIsNone(performance.is_drum)
    def testNotePerformanceFromQuantizedNoteSequence(self):
        testing_lib.add_track_to_sequence(self.note_sequence, 0,
                                          [(60, 97, 0.0, 4.0),
                                           (64, 97, 0.0, 3.0),
                                           (67, 121, 1.0, 2.0)])
        quantized_sequence = sequences_lib.quantize_note_sequence_absolute(
            self.note_sequence, steps_per_second=100)
        performance = performance_lib.NotePerformance(quantized_sequence,
                                                      num_velocity_bins=16)

        pe = performance_lib.PerformanceEvent
        expected_performance = [
            (pe(pe.TIME_SHIFT, 0), pe(pe.NOTE_ON,
                                      60), pe(pe.VELOCITY,
                                              13), pe(pe.DURATION, 400)),
            (pe(pe.TIME_SHIFT, 0), pe(pe.NOTE_ON,
                                      64), pe(pe.VELOCITY,
                                              13), pe(pe.DURATION, 300)),
            (pe(pe.TIME_SHIFT, 100), pe(pe.NOTE_ON,
                                        67), pe(pe.VELOCITY,
                                                16), pe(pe.DURATION, 100)),
        ]
        self.assertEqual(expected_performance, list(performance))

        ns = performance.to_sequence(instrument=0)
        self.assertEqual(self.note_sequence, ns)
Exemplo n.º 6
0
 def transform(self, note_sequence):
     try:
         if self._steps_per_quarter is not None:
             quantized_sequence = sequences_lib.quantize_note_sequence(
                 note_sequence, self._steps_per_quarter)
         else:
             quantized_sequence = sequences_lib.quantize_note_sequence_absolute(
                 note_sequence, self._steps_per_second)
         return [quantized_sequence]
     except sequences_lib.MultipleTimeSignatureError as e:
         tf.logging.warning(
             'Multiple time signatures in NoteSequence %s: %s',
             note_sequence.filename, e)
         self._set_stats([
             statistics.Counter(
                 'sequences_discarded_because_multiple_time_signatures', 1)
         ])
         return []
     except sequences_lib.MultipleTempoError as e:
         tf.logging.warning('Multiple tempos found in NoteSequence %s: %s',
                            note_sequence.filename, e)
         self._set_stats([
             statistics.Counter(
                 'sequences_discarded_because_multiple_tempos', 1)
         ])
         return []
     except sequences_lib.BadTimeSignatureError as e:
         tf.logging.warning('Bad time signature in NoteSequence %s: %s',
                            note_sequence.filename, e)
         self._set_stats([
             statistics.Counter(
                 'sequences_discarded_because_bad_time_signature', 1)
         ])
         return []
  def testExtractPerformancesMultiProgram(self):
    music_testing_lib.add_track_to_sequence(
        self.note_sequence, 0,
        [(60, 100, 0.0, 4.0), (64, 100, 0.0, 3.0), (67, 100, 1.0, 2.0)])
    self.note_sequence.notes[0].program = 2
    quantized_sequence = sequences_lib.quantize_note_sequence_absolute(
        self.note_sequence, steps_per_second=100)

    perfs, _ = performance_pipeline.extract_performances(quantized_sequence)
    self.assertEmpty(perfs)
  def testExtractPerformancesNonZeroStart(self):
    music_testing_lib.add_track_to_sequence(
        self.note_sequence, 0, [(60, 100, 0.0, 4.0)])
    quantized_sequence = sequences_lib.quantize_note_sequence_absolute(
        self.note_sequence, steps_per_second=100)

    perfs, _ = performance_pipeline.extract_performances(
        quantized_sequence, start_step=400, min_events_discard=1)
    self.assertEmpty(perfs)
    perfs, _ = performance_pipeline.extract_performances(
        quantized_sequence, start_step=0, min_events_discard=1)
    self.assertLen(perfs, 1)
    def testToSequence(self):
        testing_lib.add_track_to_sequence(self.note_sequence, 0,
                                          [(60, 100, 0.0, 4.0),
                                           (64, 100, 0.0, 3.0),
                                           (67, 100, 1.0, 2.0)])
        quantized_sequence = sequences_lib.quantize_note_sequence_absolute(
            self.note_sequence, steps_per_second=100)
        performance = performance_lib.Performance(quantized_sequence)
        performance_ns = performance.to_sequence()

        # Make comparison easier by sorting.
        performance_ns.notes.sort(key=lambda n: (n.start_time, n.pitch))
        self.note_sequence.notes.sort(key=lambda n: (n.start_time, n.pitch))

        self.assertEqual(self.note_sequence, performance_ns)
  def testExtractPerformancesSplitInstruments(self):
    music_testing_lib.add_track_to_sequence(
        self.note_sequence, 0, [(60, 100, 0.0, 4.0)])
    music_testing_lib.add_track_to_sequence(
        self.note_sequence, 1, [(62, 100, 0.0, 2.0), (64, 100, 2.0, 4.0)])
    quantized_sequence = sequences_lib.quantize_note_sequence_absolute(
        self.note_sequence, steps_per_second=100)

    perfs, _ = performance_pipeline.extract_performances(
        quantized_sequence, split_instruments=True)
    self.assertLen(perfs, 2)

    perfs, _ = performance_pipeline.extract_performances(
        quantized_sequence, min_events_discard=8, split_instruments=True)
    self.assertLen(perfs, 1)

    perfs, _ = performance_pipeline.extract_performances(
        quantized_sequence, min_events_discard=16, split_instruments=True)
    self.assertEmpty(perfs)