Exemplo n.º 1
0
    def testExtractMelodiesSimple(self):
        self.quantized_sequence.steps_per_beat = 1
        testing_lib.add_quantized_track(self.quantized_sequence, 0,
                                        [(12, 100, 2, 4), (11, 1, 6, 7)])
        testing_lib.add_quantized_track(self.quantized_sequence, 1,
                                        [(12, 127, 2, 4), (14, 50, 6, 8)])
        expected = [[
            NO_EVENT, NO_EVENT, 12, NO_EVENT, NOTE_OFF, NO_EVENT, 11, NOTE_OFF
        ],
                    [
                        NO_EVENT, NO_EVENT, 12, NO_EVENT, NOTE_OFF, NO_EVENT,
                        14, NO_EVENT, NOTE_OFF
                    ]]
        melodies, _ = melodies_lib.extract_melodies(
            self.quantized_sequence,
            min_bars=1,
            gap_bars=1,
            min_unique_pitches=2,
            ignore_polyphonic_notes=True)

        self.assertEqual(2, len(melodies))
        self.assertTrue(isinstance(melodies[0], melodies_lib.MonophonicMelody))
        self.assertTrue(isinstance(melodies[1], melodies_lib.MonophonicMelody))

        melodies = sorted([list(melody) for melody in melodies])
        self.assertEqual(expected, melodies)
Exemplo n.º 2
0
 def testExtractLeadSheetFragments(self):
     self.quantized_sequence.steps_per_quarter = 1
     testing_lib.add_quantized_track(self.quantized_sequence, 0,
                                     [(12, 100, 2, 4), (11, 1, 6, 11)])
     testing_lib.add_quantized_track(self.quantized_sequence, 1,
                                     [(12, 127, 2, 4), (14, 50, 6, 8),
                                      (50, 100, 33, 37), (52, 100, 34, 37)])
     testing_lib.add_quantized_chords(self.quantized_sequence,
                                      [('C', 2), ('G7', 6), ('Cmaj7', 33)])
     lead_sheets, _ = lead_sheets_lib.extract_lead_sheet_fragments(
         self.quantized_sequence,
         min_bars=1,
         gap_bars=2,
         min_unique_pitches=2,
         ignore_polyphonic_notes=True,
         require_chords=True)
     melodies, _ = melodies_lib.extract_melodies(
         self.quantized_sequence,
         min_bars=1,
         gap_bars=2,
         min_unique_pitches=2,
         ignore_polyphonic_notes=True)
     chord_progressions, _ = chords_lib.extract_chords_for_melodies(
         self.quantized_sequence, melodies)
     self.assertEqual(list(melodies),
                      list(lead_sheet.melody for lead_sheet in lead_sheets))
     self.assertEqual(list(chord_progressions),
                      list(lead_sheet.chords for lead_sheet in lead_sheets))
Exemplo n.º 3
0
  def testMonophonicMelodyExtractor(self):
    quantized_sequence = sequences_lib.QuantizedSequence()
    quantized_sequence.steps_per_beat = 1
    testing_lib.add_quantized_track(
        quantized_sequence, 0,
        [(12, 100, 2, 4), (11, 1, 6, 7)])
    testing_lib.add_quantized_track(
        quantized_sequence, 1,
        [(12, 127, 2, 4), (14, 50, 6, 8)])
    expected_events = [
        [NO_EVENT, NO_EVENT, 12, NO_EVENT, NOTE_OFF, NO_EVENT, 11, NOTE_OFF],
        [NO_EVENT, NO_EVENT, 12, NO_EVENT, NOTE_OFF, NO_EVENT, 14, NO_EVENT,
         NOTE_OFF]]
    expected_melodies = []
    for events_list in expected_events:
      melody = melodies_lib.MonophonicMelody()
      melody.from_event_list(events_list)
      melody.steps_per_bar = 4
      expected_melodies.append(melody)
    expected_melodies[0].end_step = 8
    expected_melodies[1].end_step = 12

    unit = pipelines_common.MonophonicMelodyExtractor(
        min_bars=1, min_unique_pitches=1, gap_bars=1)
    self._unit_transform_test(unit, quantized_sequence, expected_melodies)
Exemplo n.º 4
0
 def testExtractChordsForMelodiesCoincidentChords(self):
     self.quantized_sequence.steps_per_quarter = 1
     testing_lib.add_quantized_track(self.quantized_sequence, 0,
                                     [(12, 100, 2, 4), (11, 1, 6, 11)])
     testing_lib.add_quantized_track(self.quantized_sequence, 1,
                                     [(12, 127, 2, 4), (14, 50, 6, 8),
                                      (50, 100, 33, 37), (52, 100, 34, 37)])
     testing_lib.add_quantized_chords(self.quantized_sequence,
                                      [('C', 2), ('G7', 6), ('E13', 8),
                                       ('Cmaj7', 8)])
     melodies, _ = melodies_lib.extract_melodies(
         self.quantized_sequence,
         min_bars=1,
         gap_bars=2,
         min_unique_pitches=2,
         ignore_polyphonic_notes=True)
     chord_progressions, stats = chords_lib.extract_chords_for_melodies(
         self.quantized_sequence, melodies)
     expected = [[NO_CHORD, NO_CHORD, 'C', 'C', 'C', 'C', 'G7', 'G7'],
                 ['Cmaj7', 'Cmaj7', 'Cmaj7', 'Cmaj7', 'Cmaj7']]
     stats_dict = dict([(stat.name, stat) for stat in stats])
     self.assertIsNone(chord_progressions[0])
     self.assertEqual(expected,
                      [list(chords) for chords in chord_progressions[1:]])
     self.assertEqual(stats_dict['coincident_chords'].count, 1)
Exemplo n.º 5
0
    def testQuantizer(self):
        steps_per_quarter = 4
        note_sequence = testing_lib.parse_test_proto(
            music_pb2.NoteSequence, """
        time_signatures: {
          numerator: 4
          denominator: 4}
        tempos: {
          qpm: 60}""")
        testing_lib.add_track(note_sequence, 0, [(12, 100, 0.01, 10.0),
                                                 (11, 55, 0.22, 0.50),
                                                 (40, 45, 2.50, 3.50),
                                                 (55, 120, 4.0, 4.01),
                                                 (52, 99, 4.75, 5.0)])
        expected_quantized_sequence = sequences_lib.QuantizedSequence()
        expected_quantized_sequence.qpm = 60.0
        expected_quantized_sequence.steps_per_quarter = steps_per_quarter
        testing_lib.add_quantized_track(expected_quantized_sequence, 0,
                                        [(12, 100, 0, 40), (11, 55, 1, 2),
                                         (40, 45, 10, 14), (55, 120, 16, 17),
                                         (52, 99, 19, 20)])

        unit = pipelines_common.Quantizer(steps_per_quarter)
        self._unit_transform_test(unit, note_sequence,
                                  [expected_quantized_sequence])
Exemplo n.º 6
0
 def testExtractLeadSheetFragmentsNoChords(self):
     self.quantized_sequence.steps_per_quarter = 1
     testing_lib.add_quantized_track(self.quantized_sequence, 0,
                                     [(12, 100, 2, 4), (11, 1, 6, 11)])
     testing_lib.add_quantized_track(self.quantized_sequence, 1,
                                     [(12, 127, 2, 4), (14, 50, 6, 8),
                                      (50, 100, 33, 37), (52, 100, 34, 37)])
     testing_lib.add_quantized_chords(self.quantized_sequence,
                                      [('C', 2), ('G7', 6), (NO_CHORD, 10)])
     lead_sheets, stats = lead_sheets_lib.extract_lead_sheet_fragments(
         self.quantized_sequence,
         min_bars=1,
         gap_bars=2,
         min_unique_pitches=2,
         ignore_polyphonic_notes=True,
         require_chords=True)
     melodies, _ = melodies_lib.extract_melodies(
         self.quantized_sequence,
         min_bars=1,
         gap_bars=2,
         min_unique_pitches=2,
         ignore_polyphonic_notes=True)
     chord_progressions, _ = chords_lib.extract_chords_for_melodies(
         self.quantized_sequence, melodies)
     stats_dict = dict([(stat.name, stat) for stat in stats])
     # Last lead sheet should be rejected for having no chords.
     self.assertEqual(list(melodies[:2]),
                      list(lead_sheet.melody for lead_sheet in lead_sheets))
     self.assertEqual(list(chord_progressions[:2]),
                      list(lead_sheet.chords for lead_sheet in lead_sheets))
     self.assertEqual(stats_dict['empty_chord_progressions'].count, 1)
Exemplo n.º 7
0
  def testQuantizer(self):
    steps_per_beat = 4
    note_sequence = testing_lib.parse_test_proto(
        music_pb2.NoteSequence,
        """
        time_signatures: {
          numerator: 4
          denominator: 4}
        tempos: {
          bpm: 60}""")
    testing_lib.add_track(
        note_sequence, 0,
        [(12, 100, 0.01, 10.0), (11, 55, 0.22, 0.50), (40, 45, 2.50, 3.50),
         (55, 120, 4.0, 4.01), (52, 99, 4.75, 5.0)])
    expected_quantized_sequence = sequences_lib.QuantizedSequence()
    expected_quantized_sequence.bpm = 60.0
    expected_quantized_sequence.steps_per_beat = steps_per_beat
    testing_lib.add_quantized_track(
        expected_quantized_sequence, 0,
        [(12, 100, 0, 40), (11, 55, 1, 2), (40, 45, 10, 14),
         (55, 120, 16, 17), (52, 99, 19, 20)])

    unit = pipelines_common.Quantizer(steps_per_beat)
    self._unit_transform_test(unit, note_sequence,
                              [expected_quantized_sequence])
Exemplo n.º 8
0
 def testFromNotesPolyphonic(self):
   testing_lib.add_quantized_track(
       self.quantized_sequence, 0,
       [(12, 100, 4, 16), (19, 100, 4, 12)])
   melody = melodies_lib.MonophonicMelody()
   with self.assertRaises(melodies_lib.PolyphonicMelodyException):
     melody.from_quantized_sequence(self.quantized_sequence,
                                    start_step=0, track=0,
                                    ignore_polyphonic_notes=False)
   self.assertFalse(list(melody))
Exemplo n.º 9
0
 def testFromNotesPolyphonic(self):
     testing_lib.add_quantized_track(self.quantized_sequence, 0,
                                     [(12, 100, 4, 16), (19, 100, 4, 12)])
     melody = melodies_lib.MonophonicMelody()
     with self.assertRaises(melodies_lib.PolyphonicMelodyException):
         melody.from_quantized_sequence(self.quantized_sequence,
                                        start_step=0,
                                        track=0,
                                        ignore_polyphonic_notes=False)
     self.assertFalse(list(melody))
Exemplo n.º 10
0
 def testFromNotesPolyphonicWithIgnorePolyphonicNotes(self):
   testing_lib.add_quantized_track(
       self.quantized_sequence, 0,
       [(12, 100, 0, 8), (19, 100, 0, 12),
        (12, 100, 4, 12), (19, 100, 4, 16)])
   melody = melodies_lib.MonophonicMelody()
   melody.from_quantized_sequence(self.quantized_sequence,
                                  start_step=0, track=0,
                                  ignore_polyphonic_notes=True)
   expected = [19] + [NO_EVENT] * 3 + [19] + [NO_EVENT] * 11 + [NOTE_OFF]
   self.assertEqual(expected, list(melody))
Exemplo n.º 11
0
 def testFromNotesPolyphonicWithIgnorePolyphonicNotes(self):
     testing_lib.add_quantized_track(self.quantized_sequence, 0,
                                     [(12, 100, 0, 8), (19, 100, 0, 12),
                                      (12, 100, 4, 12), (19, 100, 4, 16)])
     melody = melodies_lib.MonophonicMelody()
     melody.from_quantized_sequence(self.quantized_sequence,
                                    start_step=0,
                                    track=0,
                                    ignore_polyphonic_notes=True)
     expected = [19] + [NO_EVENT] * 3 + [19] + [NO_EVENT] * 11 + [NOTE_OFF]
     self.assertEqual(expected, list(melody))
Exemplo n.º 12
0
 def testFromNotesTrimEmptyMeasures(self):
   testing_lib.add_quantized_track(
       self.quantized_sequence, 0,
       [(12, 100, 6, 7), (11, 100, 8, 9)])
   melody = melodies_lib.MonophonicMelody()
   melody.from_quantized_sequence(self.quantized_sequence,
                                  start_step=0, track=0,
                                  ignore_polyphonic_notes=False)
   expected = [NO_EVENT, NO_EVENT, NO_EVENT, NO_EVENT, NO_EVENT, NO_EVENT, 12,
               NOTE_OFF, 11, NOTE_OFF]
   self.assertEqual(expected, list(melody))
Exemplo n.º 13
0
 def testFromNoteSequence(self):
   testing_lib.add_track(
       self.note_sequence, 0,
       [(12, 100, 0.01, 10.0), (11, 55, 0.22, 0.50), (40, 45, 2.50, 3.50),
        (55, 120, 4.0, 4.01), (52, 99, 4.75, 5.0)])
   testing_lib.add_quantized_track(
       self.expected_quantized_sequence, 0,
       [(12, 100, 0, 40), (11, 55, 1, 2), (40, 45, 10, 14),
        (55, 120, 16, 17), (52, 99, 19, 20)])
   quantized = sequences_lib.QuantizedSequence()
   quantized.from_note_sequence(self.note_sequence, self.steps_per_beat)
   self.assertEqual(self.expected_quantized_sequence, quantized)
Exemplo n.º 14
0
 def testFromNotesTrimEmptyMeasures(self):
   testing_lib.add_quantized_track(
       self.quantized_sequence, 0,
       [(12, 100, 6, 7), (11, 100, 8, 9)])
   melody = melodies_lib.MonophonicMelody()
   melody.from_quantized_sequence(self.quantized_sequence,
                                  start_step=0, track=0,
                                  ignore_polyphonic_notes=False)
   expected = [NO_EVENT, NO_EVENT, NO_EVENT, NO_EVENT, NO_EVENT, NO_EVENT, 12,
               NOTE_OFF, 11]
   self.assertEqual(expected, list(melody))
   self.assertEqual(16, melody.steps_per_bar)
Exemplo n.º 15
0
 def testRounding(self):
   testing_lib.add_track(
       self.note_sequence, 1,
       [(12, 100, 0.01, 0.24), (11, 100, 0.22, 0.55), (40, 100, 0.50, 0.75),
        (41, 100, 0.689, 1.18), (44, 100, 1.19, 1.69), (55, 100, 4.0, 4.01)])
   testing_lib.add_quantized_track(
       self.expected_quantized_sequence, 1,
       [(12, 100, 0, 1), (11, 100, 1, 2), (40, 100, 2, 3),
        (41, 100, 3, 4), (44, 100, 5, 7), (55, 100, 16, 17)])
   quantized = sequences_lib.QuantizedSequence()
   quantized.from_note_sequence(self.note_sequence, self.steps_per_beat)
   self.assertEqual(self.expected_quantized_sequence, quantized)
Exemplo n.º 16
0
 def testFromNoteSequence(self):
   testing_lib.add_track(
       self.note_sequence, 0,
       [(12, 100, 0.01, 10.0), (11, 55, 0.22, 0.50), (40, 45, 2.50, 3.50),
        (55, 120, 4.0, 4.01), (52, 99, 4.75, 5.0)])
   testing_lib.add_quantized_track(
       self.expected_quantized_sequence, 0,
       [(12, 100, 0, 40), (11, 55, 1, 2), (40, 45, 10, 14),
        (55, 120, 16, 17), (52, 99, 19, 20)])
   quantized = sequences_lib.QuantizedSequence()
   quantized.from_note_sequence(self.note_sequence, self.steps_per_quarter)
   self.assertEqual(self.expected_quantized_sequence, quantized)
Exemplo n.º 17
0
 def testRounding(self):
   testing_lib.add_track(
       self.note_sequence, 1,
       [(12, 100, 0.01, 0.24), (11, 100, 0.22, 0.55), (40, 100, 0.50, 0.75),
        (41, 100, 0.689, 1.18), (44, 100, 1.19, 1.69), (55, 100, 4.0, 4.01)])
   testing_lib.add_quantized_track(
       self.expected_quantized_sequence, 1,
       [(12, 100, 0, 1), (11, 100, 1, 2), (40, 100, 2, 3),
        (41, 100, 3, 5), (44, 100, 5, 7), (55, 100, 16, 17)])
   quantized = sequences_lib.QuantizedSequence()
   quantized.from_note_sequence(self.note_sequence, self.steps_per_quarter)
   self.assertEqual(self.expected_quantized_sequence, quantized)
Exemplo n.º 18
0
 def testFromNotesTimeOverlap(self):
   testing_lib.add_quantized_track(
       self.quantized_sequence, 0,
       [(12, 100, 4, 8), (11, 100, 13, 15),
        (13, 100, 8, 16)])
   melody = melodies_lib.MonophonicMelody()
   melody.from_quantized_sequence(self.quantized_sequence,
                                  start_step=0, track=0,
                                  ignore_polyphonic_notes=False)
   expected = [NO_EVENT, NO_EVENT, NO_EVENT, NO_EVENT, 12, NO_EVENT, NO_EVENT,
               NO_EVENT, 13, NO_EVENT, NO_EVENT, NO_EVENT, NO_EVENT, 11,
               NO_EVENT, NOTE_OFF]
   self.assertEqual(expected, list(melody))
Exemplo n.º 19
0
 def testFromQuantizedSequence(self):
   testing_lib.add_quantized_track(
       self.quantized_sequence, 0,
       [(12, 100, 0, 40), (11, 55, 1, 2), (40, 45, 10, 14),
        (55, 120, 16, 17), (52, 99, 19, 20)])
   melody = melodies_lib.MonophonicMelody()
   melody.from_quantized_sequence(self.quantized_sequence,
                                  start_step=0, track=0)
   expected = ([12, 11, NOTE_OFF, NO_EVENT, NO_EVENT, NO_EVENT, NO_EVENT,
                NO_EVENT, NO_EVENT, NO_EVENT, 40, NO_EVENT, NO_EVENT, NO_EVENT,
                NOTE_OFF, NO_EVENT, 55, NOTE_OFF, NO_EVENT, 52])
   self.assertEqual(expected, list(melody))
   self.assertEqual(16, melody.steps_per_bar)
Exemplo n.º 20
0
 def testFromNotesTimeOverlap(self):
   testing_lib.add_quantized_track(
       self.quantized_sequence, 0,
       [(12, 100, 4, 8), (11, 100, 13, 15),
        (13, 100, 8, 16)])
   melody = melodies_lib.MonophonicMelody()
   melody.from_quantized_sequence(self.quantized_sequence,
                                  start_step=0, track=0,
                                  ignore_polyphonic_notes=False)
   expected = [NO_EVENT, NO_EVENT, NO_EVENT, NO_EVENT, 12, NO_EVENT, NO_EVENT,
               NO_EVENT, 13, NO_EVENT, NO_EVENT, NO_EVENT, NO_EVENT, 11,
               NO_EVENT]
   self.assertEqual(expected, list(melody))
Exemplo n.º 21
0
 def testFromQuantizedSequence(self):
   testing_lib.add_quantized_track(
       self.quantized_sequence, 0,
       [(12, 100, 0, 40), (11, 55, 1, 2), (40, 45, 10, 14),
        (55, 120, 16, 17), (52, 99, 19, 20)])
   melody = melodies_lib.MonophonicMelody()
   melody.from_quantized_sequence(self.quantized_sequence,
                                  start_step=0, track=0)
   expected = [12, 11, NOTE_OFF, NO_EVENT, NO_EVENT, NO_EVENT, NO_EVENT,
               NO_EVENT, NO_EVENT, NO_EVENT, 40, NO_EVENT, NO_EVENT, NO_EVENT,
               NOTE_OFF, NO_EVENT, 55, NOTE_OFF, NO_EVENT, 52, NOTE_OFF]
   self.assertEqual(expected, list(melody))
   self.assertEqual(16, melody.steps_per_bar)
Exemplo n.º 22
0
 def testFromNotesStartAndEndStep(self):
   testing_lib.add_quantized_track(
       self.quantized_sequence, 0,
       [(12, 100, 4, 8), (11, 100, 9, 10), (13, 100, 13, 15),
        (14, 100, 19, 20), (15, 100, 21, 27)])
   melody = melodies_lib.MonophonicMelody()
   melody.from_quantized_sequence(self.quantized_sequence,
                                  start_step=18, track=0,
                                  ignore_polyphonic_notes=False)
   expected = [NO_EVENT, NO_EVENT, NO_EVENT, 14, NOTE_OFF, 15, NO_EVENT,
               NO_EVENT, NO_EVENT, NO_EVENT, NO_EVENT, NOTE_OFF]
   self.assertEqual(expected, list(melody))
   self.assertEqual(32, melody.end_step)
Exemplo n.º 23
0
  def testExtractMelodiesStatistics(self):
    self.quantized_sequence.steps_per_beat = 1
    testing_lib.add_quantized_track(
        self.quantized_sequence, 0,
        [(12, 100, 2, 4), (11, 1, 6, 7), (10, 100, 8, 10), (9, 100, 11, 14),
         (8, 100, 16, 40), (7, 100, 41, 42)])
    testing_lib.add_quantized_track(
        self.quantized_sequence, 1,
        [(12, 127, 2, 4), (14, 50, 2, 8)])
    testing_lib.add_quantized_track(
        self.quantized_sequence, 2,
        [(12, 127, 0, 1)])
    testing_lib.add_quantized_track(
        self.quantized_sequence, 3,
        [(12, 127, 2, 4), (12, 50, 6, 8)])
    _, stats = melodies_lib.extract_melodies(
        self.quantized_sequence, min_bars=1, gap_bars=1, min_unique_pitches=2,
        ignore_polyphonic_notes=False)

    stats_dict = dict([(stat.name, stat) for stat in stats])
    self.assertEqual(stats_dict['polyphonic_tracks_discarded'].count, 1)
    self.assertEqual(stats_dict['melodies_discarded_too_short'].count, 1)
    self.assertEqual(stats_dict['melodies_discarded_too_few_pitches'].count, 1)
    self.assertEqual(
        stats_dict['melody_lengths_in_bars'].counters,
        {float('-inf'): 0, 0: 1, 1: 0, 2: 1, 10: 1, 20: 0, 30: 0, 40: 0, 50: 0,
         100: 0, 200: 0, 500: 0})
Exemplo n.º 24
0
 def testFromNotesStartAndEndStep(self):
   testing_lib.add_quantized_track(
       self.quantized_sequence, 0,
       [(12, 100, 4, 8), (11, 100, 9, 10), (13, 100, 13, 15),
        (14, 100, 19, 20), (15, 100, 21, 27)])
   melody = melodies_lib.MonophonicMelody()
   melody.from_quantized_sequence(self.quantized_sequence,
                                  start_step=18, track=0,
                                  ignore_polyphonic_notes=False)
   expected = [NO_EVENT, NO_EVENT, NO_EVENT, 14, NOTE_OFF, 15, NO_EVENT,
               NO_EVENT, NO_EVENT, NO_EVENT, NO_EVENT]
   self.assertEqual(expected, list(melody))
   self.assertEqual(16, melody.start_step)
   self.assertEqual(27, melody.end_step)
Exemplo n.º 25
0
  def testExtractMelodiesStatistics(self):
    self.quantized_sequence.steps_per_quarter = 1
    testing_lib.add_quantized_track(
        self.quantized_sequence, 0,
        [(12, 100, 2, 4), (11, 1, 6, 7), (10, 100, 8, 10), (9, 100, 11, 14),
         (8, 100, 16, 40), (7, 100, 41, 42)])
    testing_lib.add_quantized_track(
        self.quantized_sequence, 1,
        [(12, 127, 2, 4), (14, 50, 2, 8)])
    testing_lib.add_quantized_track(
        self.quantized_sequence, 2,
        [(12, 127, 0, 1)])
    testing_lib.add_quantized_track(
        self.quantized_sequence, 3,
        [(12, 127, 2, 4), (12, 50, 6, 8)])
    _, stats = melodies_lib.extract_melodies(
        self.quantized_sequence, min_bars=1, gap_bars=1, min_unique_pitches=2,
        ignore_polyphonic_notes=False)

    stats_dict = dict([(stat.name, stat) for stat in stats])
    self.assertEqual(stats_dict['polyphonic_tracks_discarded'].count, 1)
    self.assertEqual(stats_dict['melodies_discarded_too_short'].count, 1)
    self.assertEqual(stats_dict['melodies_discarded_too_few_pitches'].count, 1)
    self.assertEqual(
        stats_dict['melody_lengths_in_bars'].counters,
        {float('-inf'): 0, 0: 1, 1: 0, 2: 1, 10: 1, 20: 0, 30: 0, 40: 0, 50: 0,
         100: 0, 200: 0, 500: 0})
Exemplo n.º 26
0
 def testExtractMelodiesMelodyTooShort(self):
   self.quantized_sequence.steps_per_beat = 1
   testing_lib.add_quantized_track(
       self.quantized_sequence, 0,
       [(12, 127, 2, 4), (14, 50, 6, 7)])
   testing_lib.add_quantized_track(
       self.quantized_sequence, 1,
       [(12, 127, 2, 4), (14, 50, 6, 8)])
   expected = [[NO_EVENT, NO_EVENT, 12, NO_EVENT, NOTE_OFF, NO_EVENT, 14,
                NO_EVENT, NOTE_OFF]]
   melodies, _ = melodies_lib.extract_melodies(
       self.quantized_sequence, min_bars=2, gap_bars=1, min_unique_pitches=2,
       ignore_polyphonic_notes=True)
   melodies = [list(melody) for melody in melodies]
   self.assertEqual(expected, melodies)
Exemplo n.º 27
0
 def testExtractMelodiesLateStart(self):
   self.quantized_sequence.steps_per_quarter = 1
   testing_lib.add_quantized_track(
       self.quantized_sequence, 0,
       [(12, 100, 102, 103), (13, 100, 104, 106)])
   testing_lib.add_quantized_track(
       self.quantized_sequence, 1,
       [(12, 100, 100, 101), (13, 100, 102, 105)])
   expected = [[NO_EVENT, NO_EVENT, 12, NOTE_OFF, 13, NO_EVENT],
               [12, NOTE_OFF, 13, NO_EVENT, NO_EVENT]]
   melodies, _ = melodies_lib.extract_melodies(
       self.quantized_sequence, min_bars=1, gap_bars=1, min_unique_pitches=2,
       ignore_polyphonic_notes=True)
   melodies = sorted([list(melody) for melody in melodies])
   self.assertEqual(expected, melodies)
Exemplo n.º 28
0
 def testExtractMelodiesMelodyTooShort(self):
   self.quantized_sequence.steps_per_quarter = 1
   testing_lib.add_quantized_track(
       self.quantized_sequence, 0,
       [(12, 127, 2, 4), (14, 50, 6, 7)])
   testing_lib.add_quantized_track(
       self.quantized_sequence, 1,
       [(12, 127, 2, 4), (14, 50, 6, 9)])
   expected = [[NO_EVENT, NO_EVENT, 12, NO_EVENT, NOTE_OFF, NO_EVENT, 14,
                NO_EVENT, NO_EVENT]]
   melodies, _ = melodies_lib.extract_melodies(
       self.quantized_sequence, min_bars=2, gap_bars=1, min_unique_pitches=2,
       ignore_polyphonic_notes=True)
   melodies = [list(melody) for melody in melodies]
   self.assertEqual(expected, melodies)
Exemplo n.º 29
0
 def testExtractMelodiesLateStart(self):
   self.quantized_sequence.steps_per_beat = 1
   testing_lib.add_quantized_track(
       self.quantized_sequence, 0,
       [(12, 100, 102, 103), (13, 100, 104, 106)])
   testing_lib.add_quantized_track(
       self.quantized_sequence, 1,
       [(12, 100, 100, 101), (13, 100, 102, 104)])
   expected = [[NO_EVENT, NO_EVENT, 12, NOTE_OFF, 13, NO_EVENT, NOTE_OFF],
               [12, NOTE_OFF, 13, NO_EVENT, NOTE_OFF]]
   melodies, _ = melodies_lib.extract_melodies(
       self.quantized_sequence, min_bars=1, gap_bars=1, min_unique_pitches=2,
       ignore_polyphonic_notes=True)
   melodies = sorted([list(melody) for melody in melodies])
   self.assertEqual(expected, melodies)
Exemplo n.º 30
0
 def testExtractMelodiesMelodyTooLongWithPad(self):
   self.quantized_sequence.steps_per_quarter = 1
   testing_lib.add_quantized_track(
       self.quantized_sequence, 0,
       [(12, 127, 2, 4), (14, 50, 6, 15)])
   testing_lib.add_quantized_track(
       self.quantized_sequence, 1,
       [(12, 127, 2, 4), (14, 50, 6, 18)])
   expected = [[NO_EVENT, NO_EVENT, 12, NO_EVENT, NOTE_OFF, NO_EVENT, 14,
                NO_EVENT, NO_EVENT, NO_EVENT, NO_EVENT, NO_EVENT]]
   melodies, _ = melodies_lib.extract_melodies(
       self.quantized_sequence, min_bars=1, max_steps_truncate=14,
       max_steps_discard=18, gap_bars=1, min_unique_pitches=2,
       ignore_polyphonic_notes=True, pad_end=True)
   melodies = [list(melody) for melody in melodies]
   self.assertEqual(expected, melodies)
Exemplo n.º 31
0
 def testExtractMelodiesTooFewPitches(self):
   # Test that extract_melodies discards melodies with too few pitches where
   # pitches are equivalent by octave.
   self.quantized_sequence.steps_per_quarter = 1
   testing_lib.add_quantized_track(
       self.quantized_sequence, 0,
       [(12, 100, 0, 1), (13, 100, 1, 2), (18, 100, 2, 3),
        (24, 100, 3, 4), (25, 100, 4, 5)])
   testing_lib.add_quantized_track(
       self.quantized_sequence, 1,
       [(12, 100, 0, 1), (13, 100, 1, 2), (18, 100, 2, 3),
        (25, 100, 3, 4), (26, 100, 4, 5)])
   expected = [[12, 13, 18, 25, 26]]
   melodies, _ = melodies_lib.extract_melodies(
       self.quantized_sequence, min_bars=1, gap_bars=1, min_unique_pitches=4,
       ignore_polyphonic_notes=True)
   melodies = [list(melody) for melody in melodies]
   self.assertEqual(expected, melodies)
Exemplo n.º 32
0
    def testDeepcopy(self):
        quantized = sequences_lib.QuantizedSequence()
        testing_lib.add_track(self.note_sequence, 0, [(12, 100, 0.01, 10.0),
                                                      (11, 55, 0.22, 0.50),
                                                      (40, 45, 2.50, 3.50),
                                                      (55, 120, 4.0, 4.01),
                                                      (52, 99, 4.75, 5.0)])
        quantized.from_note_sequence(self.note_sequence,
                                     self.steps_per_quarter)

        quantized_copy = quantized.deepcopy()
        self.assertEqual(quantized, quantized_copy)

        testing_lib.add_quantized_track(self.quantized, 1, [(12, 100, 4, 20),
                                                            (19, 100, 8, 16),
                                                            (24, 100, 12, 14)])

        self.assertNotEqual(quantized, quantized_copy)
Exemplo n.º 33
0
 def testFromQuantizedSequenceNotCommonTimeSig(self):
     self.quantized_sequence.time_signature = sequences_lib.TimeSignature(
         7, 8)
     testing_lib.add_quantized_track(self.quantized_sequence, 0,
                                     [(12, 100, 0, 40), (11, 55, 1, 2),
                                      (40, 45, 10, 14), (55, 120, 16, 17),
                                      (52, 99, 19, 20)])
     melody = melodies_lib.MonophonicMelody()
     melody.from_quantized_sequence(self.quantized_sequence,
                                    start_step=0,
                                    track=0)
     expected = [
         12, 11, NOTE_OFF, NO_EVENT, NO_EVENT, NO_EVENT, NO_EVENT, NO_EVENT,
         NO_EVENT, NO_EVENT, 40, NO_EVENT, NO_EVENT, NO_EVENT, NOTE_OFF,
         NO_EVENT, 55, NOTE_OFF, NO_EVENT, 52, NOTE_OFF
     ]
     self.assertEqual(expected, list(melody))
     self.assertEqual(14, melody.steps_per_bar)
Exemplo n.º 34
0
 def testExtractMelodiesTooFewPitches(self):
   # Test that extract_melodies discards melodies with too few pitches where
   # pitches are equivalent by octave.
   self.quantized_sequence.steps_per_beat = 1
   testing_lib.add_quantized_track(
       self.quantized_sequence, 0,
       [(12, 100, 0, 1), (13, 100, 1, 2), (18, 100, 2, 3),
        (24, 100, 3, 4), (25, 100, 4, 5)])
   testing_lib.add_quantized_track(
       self.quantized_sequence, 1,
       [(12, 100, 0, 1), (13, 100, 1, 2), (18, 100, 2, 3),
        (25, 100, 3, 4), (26, 100, 4, 5)])
   expected = [[12, 13, 18, 25, 26, NOTE_OFF]]
   melodies, _ = melodies_lib.extract_melodies(
       self.quantized_sequence, min_bars=1, gap_bars=1, min_unique_pitches=4,
       ignore_polyphonic_notes=True)
   melodies = [list(melody) for melody in melodies]
   self.assertEqual(expected, melodies)
Exemplo n.º 35
0
 def testExtractMultipleMelodiesFromSameTrack(self):
   self.quantized_sequence.steps_per_quarter = 1
   testing_lib.add_quantized_track(
       self.quantized_sequence, 0,
       [(12, 100, 2, 4), (11, 1, 6, 11)])
   testing_lib.add_quantized_track(
       self.quantized_sequence, 1,
       [(12, 127, 2, 4), (14, 50, 6, 8),
        (50, 100, 33, 37), (52, 100, 34, 37)])
   expected = [[NO_EVENT, NO_EVENT, 12, NO_EVENT, NOTE_OFF, NO_EVENT, 11,
                NO_EVENT, NO_EVENT, NO_EVENT, NO_EVENT],
               [NO_EVENT, NO_EVENT, 12, NO_EVENT, NOTE_OFF, NO_EVENT, 14,
                NO_EVENT],
               [NO_EVENT, 50, 52, NO_EVENT, NO_EVENT]]
   melodies, _ = melodies_lib.extract_melodies(
       self.quantized_sequence, min_bars=1, gap_bars=2, min_unique_pitches=2,
       ignore_polyphonic_notes=True)
   melodies = sorted([list(melody) for melody in melodies])
   self.assertEqual(expected, melodies)
Exemplo n.º 36
0
 def testExtractMultipleMelodiesFromSameTrack(self):
   self.quantized_sequence.steps_per_beat = 1
   testing_lib.add_quantized_track(
       self.quantized_sequence, 0,
       [(12, 100, 2, 4), (11, 1, 6, 7)])
   testing_lib.add_quantized_track(
       self.quantized_sequence, 1,
       [(12, 127, 2, 4), (14, 50, 6, 8),
        (50, 100, 33, 37), (52, 100, 34, 36)])
   expected = [[NO_EVENT, NO_EVENT, 12, NO_EVENT, NOTE_OFF, NO_EVENT, 11,
                NOTE_OFF],
               [NO_EVENT, NO_EVENT, 12, NO_EVENT, NOTE_OFF, NO_EVENT, 14,
                NO_EVENT, NOTE_OFF],
               [NO_EVENT, 50, 52, NO_EVENT, NOTE_OFF]]
   melodies, _ = melodies_lib.extract_melodies(
       self.quantized_sequence, min_bars=1, gap_bars=2, min_unique_pitches=2,
       ignore_polyphonic_notes=True)
   melodies = sorted([list(melody) for melody in melodies])
   self.assertEqual(expected, melodies)
Exemplo n.º 37
0
  def testExtractMelodiesSimple(self):
    self.quantized_sequence.steps_per_beat = 1
    testing_lib.add_quantized_track(
        self.quantized_sequence, 0,
        [(12, 100, 2, 4), (11, 1, 6, 7)])
    testing_lib.add_quantized_track(
        self.quantized_sequence, 1,
        [(12, 127, 2, 4), (14, 50, 6, 8)])
    expected = [[NO_EVENT, NO_EVENT, 12, NO_EVENT, NOTE_OFF, NO_EVENT, 11,
                 NOTE_OFF],
                [NO_EVENT, NO_EVENT, 12, NO_EVENT, NOTE_OFF, NO_EVENT, 14,
                 NO_EVENT, NOTE_OFF]]
    melodies, _ = melodies_lib.extract_melodies(
        self.quantized_sequence, min_bars=1, gap_bars=1, min_unique_pitches=2,
        ignore_polyphonic_notes=True)

    self.assertEqual(2, len(melodies))
    self.assertTrue(isinstance(melodies[0], melodies_lib.MonophonicMelody))
    self.assertTrue(isinstance(melodies[1], melodies_lib.MonophonicMelody))

    melodies = sorted([list(melody) for melody in melodies])
    self.assertEqual(expected, melodies)
Exemplo n.º 38
0
 def testExtractChordsForMelodies(self):
     self.quantized_sequence.steps_per_quarter = 1
     testing_lib.add_quantized_track(self.quantized_sequence, 0,
                                     [(12, 100, 2, 4), (11, 1, 6, 11)])
     testing_lib.add_quantized_track(self.quantized_sequence, 1,
                                     [(12, 127, 2, 4), (14, 50, 6, 8),
                                      (50, 100, 33, 37), (52, 100, 34, 37)])
     testing_lib.add_quantized_chords(self.quantized_sequence,
                                      [('C', 2), ('G7', 6), ('Cmaj7', 33)])
     melodies, _ = melodies_lib.extract_melodies(
         self.quantized_sequence,
         min_bars=1,
         gap_bars=2,
         min_unique_pitches=2,
         ignore_polyphonic_notes=True)
     chord_progressions, _ = chords_lib.extract_chords_for_melodies(
         self.quantized_sequence, melodies)
     expected = [[
         NO_CHORD, NO_CHORD, 'C', 'C', 'C', 'C', 'G7', 'G7', 'G7', 'G7',
         'G7'
     ], [NO_CHORD, NO_CHORD, 'C', 'C', 'C', 'C', 'G7', 'G7'],
                 ['G7', 'Cmaj7', 'Cmaj7', 'Cmaj7', 'Cmaj7']]
     self.assertEqual(expected,
                      [list(chords) for chords in chord_progressions])
Exemplo n.º 39
0
 def testMultiTrack(self):
     testing_lib.add_track(self.note_sequence, 0, [(12, 100, 1.0, 4.0),
                                                   (19, 100, 0.95, 3.0)])
     testing_lib.add_track(self.note_sequence, 3, [(12, 100, 1.0, 4.0),
                                                   (19, 100, 2.0, 5.0)])
     testing_lib.add_track(self.note_sequence, 7, [(12, 100, 1.0, 5.0),
                                                   (19, 100, 2.0, 4.0),
                                                   (24, 100, 3.0, 3.5)])
     testing_lib.add_quantized_track(self.expected_quantized_sequence, 0,
                                     [(12, 100, 4, 16), (19, 100, 4, 12)])
     testing_lib.add_quantized_track(self.expected_quantized_sequence, 3,
                                     [(12, 100, 4, 16), (19, 100, 8, 20)])
     testing_lib.add_quantized_track(self.expected_quantized_sequence, 7,
                                     [(12, 100, 4, 20), (19, 100, 8, 16),
                                      (24, 100, 12, 14)])
     quantized = sequences_lib.QuantizedSequence()
     quantized.from_note_sequence(self.note_sequence, self.steps_per_beat)
     self.assertEqual(self.expected_quantized_sequence, quantized)
Exemplo n.º 40
0
 def testMultiTrack(self):
   testing_lib.add_track(
       self.note_sequence, 0,
       [(12, 100, 1.0, 4.0), (19, 100, 0.95, 3.0)])
   testing_lib.add_track(
       self.note_sequence, 3,
       [(12, 100, 1.0, 4.0), (19, 100, 2.0, 5.0)])
   testing_lib.add_track(
       self.note_sequence, 7,
       [(12, 100, 1.0, 5.0), (19, 100, 2.0, 4.0), (24, 100, 3.0, 3.5)])
   testing_lib.add_quantized_track(
       self.expected_quantized_sequence, 0,
       [(12, 100, 4, 16), (19, 100, 4, 12)])
   testing_lib.add_quantized_track(
       self.expected_quantized_sequence, 3,
       [(12, 100, 4, 16), (19, 100, 8, 20)])
   testing_lib.add_quantized_track(
       self.expected_quantized_sequence, 7,
       [(12, 100, 4, 20), (19, 100, 8, 16), (24, 100, 12, 14)])
   quantized = sequences_lib.QuantizedSequence()
   quantized.from_note_sequence(self.note_sequence, self.steps_per_beat)
   self.assertEqual(self.expected_quantized_sequence, quantized)
Exemplo n.º 41
0
 def testEq(self):
     left_hand = sequences_lib.QuantizedSequence()
     left_hand.bpm = 123.0
     left_hand.steps_per_beat = 7
     left_hand.time_signature = sequences_lib.TimeSignature(7, 8)
     testing_lib.add_quantized_track(left_hand, 0, [(12, 100, 0, 40),
                                                    (11, 100, 1, 2)])
     testing_lib.add_quantized_track(left_hand, 2, [(55, 100, 4, 6),
                                                    (14, 120, 4, 10)])
     testing_lib.add_quantized_track(left_hand, 3, [(1, 10, 0, 6),
                                                    (2, 50, 20, 21),
                                                    (0, 101, 17, 21)])
     right_hand = sequences_lib.QuantizedSequence()
     right_hand.bpm = 123.0
     right_hand.steps_per_beat = 7
     right_hand.time_signature = sequences_lib.TimeSignature(7, 8)
     testing_lib.add_quantized_track(right_hand, 0, [(11, 100, 1, 2),
                                                     (12, 100, 0, 40)])
     testing_lib.add_quantized_track(right_hand, 2, [(14, 120, 4, 10),
                                                     (55, 100, 4, 6)])
     testing_lib.add_quantized_track(right_hand, 3, [(0, 101, 17, 21),
                                                     (2, 50, 20, 21),
                                                     (1, 10, 0, 6)])
     self.assertEqual(left_hand, right_hand)
Exemplo n.º 42
0
 def testEq(self):
   left_hand = sequences_lib.QuantizedSequence()
   left_hand.bpm = 123.0
   left_hand.steps_per_beat = 7
   left_hand.time_signature = sequences_lib.TimeSignature(7, 8)
   testing_lib.add_quantized_track(
       left_hand, 0,
       [(12, 100, 0, 40), (11, 100, 1, 2)])
   testing_lib.add_quantized_track(
       left_hand, 2,
       [(55, 100, 4, 6), (14, 120, 4, 10)])
   testing_lib.add_quantized_track(
       left_hand, 3,
       [(1, 10, 0, 6), (2, 50, 20, 21), (0, 101, 17, 21)])
   right_hand = sequences_lib.QuantizedSequence()
   right_hand.bpm = 123.0
   right_hand.steps_per_beat = 7
   right_hand.time_signature = sequences_lib.TimeSignature(7, 8)
   testing_lib.add_quantized_track(
       right_hand, 0,
       [(11, 100, 1, 2), (12, 100, 0, 40)])
   testing_lib.add_quantized_track(
       right_hand, 2,
       [(14, 120, 4, 10), (55, 100, 4, 6)])
   testing_lib.add_quantized_track(
       right_hand, 3,
       [(0, 101, 17, 21), (2, 50, 20, 21), (1, 10, 0, 6)])
   self.assertEqual(left_hand, right_hand)