示例#1
0
    def testSquashCenterOctaves(self):
        # Move up an octave.
        events = [
            12 * 4, NO_EVENT, 12 * 4 + 2, NOTE_OFF, 12 * 4 + 4, NO_EVENT,
            12 * 4 + 5, 12 * 5 + 2, 12 * 4 - 1, NOTE_OFF
        ]
        melody = melodies_lib.Melody(events)
        melody.squash(min_note=12 * 4, max_note=12 * 7, transpose_to_key=0)
        expected = [
            12 * 5, NO_EVENT, 12 * 5 + 2, NOTE_OFF, 12 * 5 + 4, NO_EVENT,
            12 * 5 + 5, 12 * 6 + 2, 12 * 5 - 1, NOTE_OFF
        ]
        self.assertEqual(expected, list(melody))

        # Move down an octave.
        events = [
            12 * 6, NO_EVENT, 12 * 6 + 2, NOTE_OFF, 12 * 6 + 4, NO_EVENT,
            12 * 6 + 5, 12 * 7 + 2, 12 * 6 - 1, NOTE_OFF
        ]
        melody = melodies_lib.Melody(events)
        melody.squash(min_note=12 * 4, max_note=12 * 7, transpose_to_key=0)
        expected = [
            12 * 5, NO_EVENT, 12 * 5 + 2, NOTE_OFF, 12 * 5 + 4, NO_EVENT,
            12 * 5 + 5, 12 * 6 + 2, 12 * 5 - 1, NOTE_OFF
        ]
        self.assertEqual(expected, list(melody))
示例#2
0
    def testSetLength(self):
        events = [60]
        melody = melodies_lib.Melody(events, start_step=9)
        melody.set_length(5)
        self.assertListEqual([60, NOTE_OFF, NO_EVENT, NO_EVENT, NO_EVENT],
                             list(melody))
        self.assertEqual(9, melody.start_step)
        self.assertEqual(14, melody.end_step)

        melody = melodies_lib.Melody(events, start_step=9)
        melody.set_length(5, from_left=True)
        self.assertListEqual([NO_EVENT, NO_EVENT, NO_EVENT, NO_EVENT, 60],
                             list(melody))
        self.assertEqual(5, melody.start_step)
        self.assertEqual(10, melody.end_step)

        events = [60, NO_EVENT, NO_EVENT, NOTE_OFF]
        melody = melodies_lib.Melody(events)
        melody.set_length(3)
        self.assertListEqual([60, NO_EVENT, NO_EVENT], list(melody))
        self.assertEqual(0, melody.start_step)
        self.assertEqual(3, melody.end_step)

        melody = melodies_lib.Melody(events)
        melody.set_length(3, from_left=True)
        self.assertListEqual([NO_EVENT, NO_EVENT, NOTE_OFF], list(melody))
        self.assertEqual(1, melody.start_step)
        self.assertEqual(4, melody.end_step)
示例#3
0
    def testTranspose(self):
        # Melody transposed down 5 half steps. 2 octave range.
        events = [12 * 5 + 4, NO_EVENT, 12 * 5 + 5, NOTE_OFF, 12 * 6, NO_EVENT]
        melody = melodies_lib.Melody(events)
        melody.transpose(transpose_amount=-5, min_note=12 * 5, max_note=12 * 7)
        expected = [
            12 * 5 + 11, NO_EVENT, 12 * 5, NOTE_OFF, 12 * 5 + 7, NO_EVENT
        ]
        self.assertEqual(expected, list(melody))

        # Melody transposed up 19 half steps. 2 octave range.
        events = [12 * 5 + 4, NO_EVENT, 12 * 5 + 5, NOTE_OFF, 12 * 6, NO_EVENT]
        melody = melodies_lib.Melody(events)
        melody.transpose(transpose_amount=19, min_note=12 * 5, max_note=12 * 7)
        expected = [
            12 * 6 + 11, NO_EVENT, 12 * 6, NOTE_OFF, 12 * 6 + 7, NO_EVENT
        ]
        self.assertEqual(expected, list(melody))

        # Melody transposed zero half steps. 1 octave range.
        events = [12 * 4 + 11, 12 * 5, 12 * 5 + 11, NOTE_OFF, 12 * 6, NO_EVENT]
        melody = melodies_lib.Melody(events)
        melody.transpose(transpose_amount=0, min_note=12 * 5, max_note=12 * 6)
        expected = [
            12 * 5 + 11, 12 * 5, 12 * 5 + 11, NOTE_OFF, 12 * 5, NO_EVENT
        ]
        self.assertEqual(expected, list(melody))
示例#4
0
    def testEventListChordsWithMelodies(self):
        note_sequence = music_pb2.NoteSequence(ticks_per_quarter=220)
        note_sequence.tempos.add(qpm=60.0)
        testing_lib.add_chords_to_sequence(note_sequence,
                                           [('N.C.', 0), ('C', 2), ('G7', 6)])
        note_sequence.total_time = 8.0

        melodies = [
            melodies_lib.Melody([60, -2, -2, -1],
                                start_step=0,
                                steps_per_quarter=1,
                                steps_per_bar=4),
            melodies_lib.Melody([62, -2, -2, -1],
                                start_step=4,
                                steps_per_quarter=1,
                                steps_per_bar=4),
        ]

        quantized_sequence = sequences_lib.quantize_note_sequence(
            note_sequence, steps_per_quarter=1)
        chords = chords_lib.event_list_chords(quantized_sequence, melodies)

        expected_chords = [[NO_CHORD, NO_CHORD, 'C', 'C'],
                           ['C', 'C', 'G7', 'G7']]

        self.assertEqual(expected_chords, chords)
 def testMelodyExtractor(self):
     note_sequence = common_testing_lib.parse_test_proto(
         music_pb2.NoteSequence, """
     time_signatures: {
       numerator: 4
       denominator: 4}
     tempos: {
       qpm: 60}""")
     music_testing_lib.add_track_to_sequence(note_sequence,
                                             0, [(12, 100, 2, 4),
                                                 (11, 1, 6, 7)])
     music_testing_lib.add_track_to_sequence(note_sequence,
                                             1, [(12, 127, 2, 4),
                                                 (14, 50, 6, 8)])
     quantized_sequence = sequences_lib.quantize_note_sequence(
         note_sequence, steps_per_quarter=1)
     expected_events = [[
         NO_EVENT, NO_EVENT, 12, NO_EVENT, NOTE_OFF, NO_EVENT, 11
     ], [
         NO_EVENT, NO_EVENT, 12, NO_EVENT, NOTE_OFF, NO_EVENT, 14, NO_EVENT
     ]]
     expected_melodies = []
     for events_list in expected_events:
         melody = melodies_lib.Melody(events_list,
                                      steps_per_quarter=1,
                                      steps_per_bar=4)
         expected_melodies.append(melody)
     unit = melody_pipelines.MelodyExtractor(min_bars=1,
                                             min_unique_pitches=1,
                                             gap_bars=1)
     self._unit_transform_test(unit, quantized_sequence, expected_melodies)
  def testToSequence(self):
    # Sequence produced from lead sheet should contain notes from melody
    # sequence and chords from chord sequence as text annotations.
    melody = melodies_lib.Melody(
        [NO_EVENT, 1, NO_EVENT, NOTE_OFF, NO_EVENT, 2, 3, NOTE_OFF, NO_EVENT])
    chords = chords_lib.ChordProgression(
        [NO_CHORD, 'A', 'A', 'C#m', 'C#m', 'D', 'B', 'B', 'B'])
    lead_sheet = lead_sheets_lib.LeadSheet(melody, chords)

    sequence = lead_sheet.to_sequence(
        velocity=10,
        instrument=1,
        sequence_start_time=2,
        qpm=60.0)
    melody_sequence = melody.to_sequence(
        velocity=10,
        instrument=1,
        sequence_start_time=2,
        qpm=60.0)
    chords_sequence = chords.to_sequence(
        sequence_start_time=2,
        qpm=60.0)

    self.assertEqual(melody_sequence.ticks_per_quarter,
                     sequence.ticks_per_quarter)
    self.assertProtoEquals(melody_sequence.tempos, sequence.tempos)
    self.assertEqual(melody_sequence.total_time, sequence.total_time)
    self.assertProtoEquals(melody_sequence.notes, sequence.notes)
    self.assertProtoEquals(chords_sequence.text_annotations,
                           sequence.text_annotations)
示例#7
0
    def testFromQuantizedNoteSequenceNotCommonTimeSig(self):
        self.note_sequence.time_signatures[0].numerator = 7
        self.note_sequence.time_signatures[0].denominator = 8

        testing_lib.add_track_to_sequence(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_sequence = sequences_lib.quantize_note_sequence(
            self.note_sequence, self.steps_per_quarter)

        melody = melodies_lib.Melody()
        melody.from_quantized_sequence(quantized_sequence,
                                       search_start_step=0,
                                       instrument=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(14, melody.steps_per_bar)
示例#8
0
    def testGetKeyHistogram(self):
        # One C.
        events = [NO_EVENT, 12 * 5, NOTE_OFF]
        melody = melodies_lib.Melody(events)
        expected = [1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0]
        self.assertListEqual(expected, list(melody.get_major_key_histogram()))

        # One C and one C#.
        events = [NO_EVENT, 12 * 5, NOTE_OFF, 12 * 7 + 1, NOTE_OFF]
        melody = melodies_lib.Melody(events)
        expected = [1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1]
        self.assertListEqual(expected, list(melody.get_major_key_histogram()))

        # One C, one C#, and one D.
        events = [NO_EVENT, 12 * 5, NOTE_OFF, 12 * 7 + 1, NO_EVENT, 12 * 9 + 2]
        melody = melodies_lib.Melody(events)
        expected = [2, 2, 2, 2, 1, 2, 1, 2, 2, 2, 2, 1]
        self.assertListEqual(expected, list(melody.get_major_key_histogram()))
  def testEventListKeysWithMelodies(self):
    note_sequence = music_pb2.NoteSequence(ticks_per_quarter=220)
    note_sequence.tempos.add(qpm=60.0)
    testing_lib.add_key_signatures_to_sequence(note_sequence, [(6, 2), (0, 6)])
    note_sequence.total_time = 8.0

    melodies = [
        melodies_lib.Melody([60, -2, -2, -1],
                            start_step=0, steps_per_quarter=1, steps_per_bar=4),
        melodies_lib.Melody([62, -2, -2, -1],
                            start_step=4, steps_per_quarter=1, steps_per_bar=4),
    ]

    keys = chords_lib.event_list_keys(
        note_sequence, melodies, steps_per_second=1)
    expected_keys = [[6, 6, 6, 6], [6, 6, 0, 0]]

    self.assertEqual(expected_keys, keys)
示例#10
0
    def testToSequenceEmpty(self):
        melody = melodies_lib.Melody()
        sequence = melody.to_sequence(velocity=10,
                                      instrument=1,
                                      sequence_start_time=2,
                                      qpm=60.0)

        self.assertProtoEquals(
            'ticks_per_quarter: 220 '
            'tempos < qpm: 60.0 > ', sequence)
示例#11
0
 def testGetInputsBatch(self):
   events1 = [100, 100, 107, 111, NO_EVENT, 99, 112, NOTE_OFF, NO_EVENT]
   melody1 = melodies_lib.Melody(events1)
   events2 = [9, 10, 12, 14, 15, 17, 19, 21, 22]
   melody2 = melodies_lib.Melody(events2)
   melody1.squash(
       self.min_note,
       self.max_note,
       self.transpose_to_key)
   melody2.squash(
       self.min_note,
       self.max_note,
       self.transpose_to_key)
   melodies = [melody1, melody2]
   expected_inputs1 = [
       [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
       [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
       [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0],
       [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
       [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
       [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
       [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
       [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
       [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]
   expected_inputs2 = [
       [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
       [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
       [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
       [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
       [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
       [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0],
       [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0],
       [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
       [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]
   expected_full_length_inputs_batch = [expected_inputs1, expected_inputs2]
   expected_last_event_inputs_batch = [expected_inputs1[-1:],
                                       expected_inputs2[-1:]]
   self.assertListEqual(
       expected_full_length_inputs_batch,
       self.med.get_inputs_batch(melodies, True))
   self.assertListEqual(
       expected_last_event_inputs_batch,
       self.med.get_inputs_batch(melodies))
示例#12
0
    def testGetNoteHistogram(self):
        events = [
            NO_EVENT, NOTE_OFF, 12 * 2 + 1, 12 * 3, 12 * 5 + 11, 12 * 6 + 3,
            12 * 4 + 11
        ]
        melody = melodies_lib.Melody(events)
        expected = [1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2]
        self.assertEqual(expected, list(melody.get_note_histogram()))

        events = [
            0, 1, NO_EVENT, NOTE_OFF, 12 * 2 + 1, 12 * 3, 12 * 6 + 3,
            12 * 5 + 11, NO_EVENT, 12 * 4 + 11, 12 * 7 + 1
        ]
        melody = melodies_lib.Melody(events)
        expected = [2, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2]
        self.assertEqual(expected, list(melody.get_note_histogram()))

        melody = melodies_lib.Melody()
        expected = [0] * 12
        self.assertEqual(expected, list(melody.get_note_histogram()))
示例#13
0
 def testExtendMelodies(self):
   melody1 = melodies_lib.Melody([60])
   melody2 = melodies_lib.Melody([60])
   melody3 = melodies_lib.Melody([60])
   melody4 = melodies_lib.Melody([60])
   melodies = [melody1, melody2, melody3, melody4]
   softmax = [[
       [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
   ], [
       [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0]
   ], [
       [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
   ], [
       [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
   ]]
   self.med.extend_event_sequences(melodies, softmax)
   self.assertListEqual(list(melody1), [60, 60])
   self.assertListEqual(list(melody2), [60, 71])
   self.assertListEqual(list(melody3), [60, NO_EVENT])
   self.assertListEqual(list(melody4), [60, NOTE_OFF])
示例#14
0
    def testFromNotesStepsPerBar(self):
        self.note_sequence.time_signatures[0].numerator = 7
        self.note_sequence.time_signatures[0].denominator = 8
        quantized_sequence = sequences_lib.quantize_note_sequence(
            self.note_sequence, steps_per_quarter=12)

        melody = melodies_lib.Melody()
        melody.from_quantized_sequence(quantized_sequence,
                                       search_start_step=0,
                                       instrument=0,
                                       ignore_polyphonic_notes=False)
        self.assertEqual(42, melody.steps_per_bar)
示例#15
0
 def testSquashMaxNote(self):
     events = [
         12 * 5, 12 * 5 + 2, 12 * 5 + 4, 12 * 5 + 5, 12 * 5 + 11, 12 * 6,
         12 * 6 + 1
     ]
     melody = melodies_lib.Melody(events)
     melody.squash(min_note=12 * 5, max_note=12 * 6, transpose_to_key=0)
     expected = [
         12 * 5, 12 * 5 + 2, 12 * 5 + 4, 12 * 5 + 5, 12 * 5 + 11, 12 * 5,
         12 * 5 + 1
     ]
     self.assertEqual(expected, list(melody))
示例#16
0
    def testToSequenceEndsWithNonzeroStart(self):
        melody = melodies_lib.Melody([NO_EVENT, 1, NO_EVENT], start_step=4)
        sequence = melody.to_sequence(velocity=100,
                                      instrument=0,
                                      sequence_start_time=0.5,
                                      qpm=60.0)

        self.assertProtoEquals(
            'ticks_per_quarter: 220 '
            'tempos < qpm: 60.0 > '
            'total_time: 2.25 '
            'notes < pitch: 1 velocity: 100 start_time: 1.75 end_time: 2.25 > ',
            sequence)
示例#17
0
    def testGetMajorKey(self):
        # D Major.
        events = [
            NO_EVENT, 12 * 2 + 2, 12 * 3 + 4, 12 * 5 + 1, 12 * 6 + 6,
            12 * 4 + 11, 12 * 3 + 9, 12 * 5 + 7, NOTE_OFF
        ]
        melody = melodies_lib.Melody(events)
        self.assertEqual(2, melody.get_major_key())

        # C# Major with accidentals.
        events = [
            NO_EVENT, 12 * 2 + 1, 12 * 4 + 8, 12 * 5 + 5, 12 * 6 + 6,
            12 * 3 + 3, 12 * 2 + 11, 12 * 3 + 10, 12 * 5, 12 * 2 + 8,
            12 * 4 + 1, 12 * 3 + 5, 12 * 5 + 9, 12 * 4 + 3, NOTE_OFF
        ]
        melody = melodies_lib.Melody(events)
        self.assertEqual(1, melody.get_major_key())

        # One note in C Major.
        events = [NO_EVENT, 12 * 2 + 11, NOTE_OFF]
        melody = melodies_lib.Melody(events)
        self.assertEqual(0, melody.get_major_key())
示例#18
0
    def testSquash(self):
        # Melody in C, transposed to C, and squashed to 1 octave.
        events = [12 * 5, NO_EVENT, 12 * 5 + 2, NOTE_OFF, 12 * 6 + 4, NO_EVENT]
        melody = melodies_lib.Melody(events)
        melody.squash(min_note=12 * 5, max_note=12 * 6, transpose_to_key=0)
        expected = [
            12 * 5, NO_EVENT, 12 * 5 + 2, NOTE_OFF, 12 * 5 + 4, NO_EVENT
        ]
        self.assertEqual(expected, list(melody))

        # Melody in D, transposed to C, and squashed to 1 octave.
        events = [12 * 5 + 2, 12 * 5 + 4, 12 * 6 + 7, 12 * 6 + 6, 12 * 5 + 1]
        melody = melodies_lib.Melody(events)
        melody.squash(min_note=12 * 5, max_note=12 * 6, transpose_to_key=0)
        expected = [12 * 5, 12 * 5 + 2, 12 * 5 + 5, 12 * 5 + 4, 12 * 5 + 11]
        self.assertEqual(expected, list(melody))

        # Melody in D, transposed to E, and squashed to 1 octave.
        events = [12 * 5 + 2, 12 * 5 + 4, 12 * 6 + 7, 12 * 6 + 6, 12 * 4 + 11]
        melody = melodies_lib.Melody(events)
        melody.squash(min_note=12 * 5, max_note=12 * 6, transpose_to_key=4)
        expected = [12 * 5 + 4, 12 * 5 + 6, 12 * 5 + 9, 12 * 5 + 8, 12 * 5 + 1]
        self.assertEqual(expected, list(melody))
示例#19
0
    def testFromNotesPolyphonic(self):
        testing_lib.add_track_to_sequence(self.note_sequence, 0,
                                          [(12, 100, 0.0, 10.0),
                                           (11, 55, 0.0, 0.50)])
        quantized_sequence = sequences_lib.quantize_note_sequence(
            self.note_sequence, self.steps_per_quarter)

        melody = melodies_lib.Melody()
        with self.assertRaises(melodies_lib.PolyphonicMelodyError):
            melody.from_quantized_sequence(quantized_sequence,
                                           search_start_step=0,
                                           instrument=0,
                                           ignore_polyphonic_notes=False)
        self.assertFalse(list(melody))
 def testSquash(self):
   # LeadSheet squash should agree with melody squash & chords transpose.
   melody_events = [12 * 5, NO_EVENT, 12 * 5 + 2,
                    NOTE_OFF, 12 * 6 + 4, NO_EVENT]
   chord_events = ['C', 'Am', 'Dm', 'G', 'C', NO_CHORD]
   melody = melodies_lib.Melody(melody_events)
   chords = chords_lib.ChordProgression(chord_events)
   expected_melody = copy.deepcopy(melody)
   expected_chords = copy.deepcopy(chords)
   lead_sheet = lead_sheets_lib.LeadSheet(melody, chords)
   lead_sheet.squash(min_note=12 * 5, max_note=12 * 6, transpose_to_key=0)
   transpose_amount = expected_melody.squash(
       min_note=12 * 5, max_note=12 * 6, transpose_to_key=0)
   expected_chords.transpose(transpose_amount=transpose_amount)
   self.assertEqual(expected_melody, lead_sheet.melody)
   self.assertEqual(expected_chords, lead_sheet.chords)
 def testTranspose(self):
   # LeadSheet transposition should agree with melody & chords transpositions.
   melody_events = [12 * 5 + 4, NO_EVENT, 12 * 5 + 5,
                    NOTE_OFF, 12 * 6, NO_EVENT]
   chord_events = [NO_CHORD, 'C', 'F', 'Dm', 'D', 'G']
   melody = melodies_lib.Melody(melody_events)
   chords = chords_lib.ChordProgression(chord_events)
   expected_melody = copy.deepcopy(melody)
   expected_chords = copy.deepcopy(chords)
   lead_sheet = lead_sheets_lib.LeadSheet(melody, chords)
   lead_sheet.transpose(transpose_amount=-5, min_note=12 * 5, max_note=12 * 7)
   expected_melody.transpose(
       transpose_amount=-5, min_note=12 * 5, max_note=12 * 7)
   expected_chords.transpose(transpose_amount=-5)
   self.assertEqual(expected_melody, lead_sheet.melody)
   self.assertEqual(expected_chords, lead_sheet.chords)
示例#22
0
    def testToSequenceEndsWithSustainedNote(self):
        melody = melodies_lib.Melody([
            NO_EVENT, 1, NO_EVENT, NOTE_OFF, NO_EVENT, 2, 3, NO_EVENT, NO_EVENT
        ])
        sequence = melody.to_sequence(velocity=100,
                                      instrument=0,
                                      sequence_start_time=0,
                                      qpm=60.0)

        self.assertProtoEquals(
            'ticks_per_quarter: 220 '
            'tempos < qpm: 60.0 > '
            'total_time: 2.25 '
            'notes < pitch: 1 velocity: 100 start_time: 0.25 end_time: 0.75 > '
            'notes < pitch: 2 velocity: 100 start_time: 1.25 end_time: 1.5 > '
            'notes < pitch: 3 velocity: 100 start_time: 1.5 end_time: 2.25 > ',
            sequence)
 def testLeadSheetExtractor(self):
     note_sequence = common_testing_lib.parse_test_proto(
         music_pb2.NoteSequence, """
     time_signatures: {
       numerator: 4
       denominator: 4}
     tempos: {
       qpm: 60}""")
     music_testing_lib.add_track_to_sequence(note_sequence,
                                             0, [(12, 100, 2, 4),
                                                 (11, 1, 6, 7)])
     music_testing_lib.add_track_to_sequence(note_sequence,
                                             1, [(12, 127, 2, 4),
                                                 (14, 50, 6, 8)])
     music_testing_lib.add_chords_to_sequence(note_sequence, [('Cm7', 2),
                                                              ('F9', 4),
                                                              ('G7b9', 6)])
     quantized_sequence = sequences_lib.quantize_note_sequence(
         note_sequence, steps_per_quarter=1)
     expected_melody_events = [[
         NO_EVENT, NO_EVENT, 12, NO_EVENT, NOTE_OFF, NO_EVENT, 11
     ], [
         NO_EVENT, NO_EVENT, 12, NO_EVENT, NOTE_OFF, NO_EVENT, 14, NO_EVENT
     ]]
     expected_chord_events = [[
         NO_CHORD, NO_CHORD, 'Cm7', 'Cm7', 'F9', 'F9', 'G7b9'
     ], [NO_CHORD, NO_CHORD, 'Cm7', 'Cm7', 'F9', 'F9', 'G7b9', 'G7b9']]
     expected_lead_sheets = []
     for melody_events, chord_events in zip(expected_melody_events,
                                            expected_chord_events):
         melody = melodies_lib.Melody(melody_events,
                                      steps_per_quarter=1,
                                      steps_per_bar=4)
         chords = chords_lib.ChordProgression(chord_events,
                                              steps_per_quarter=1,
                                              steps_per_bar=4)
         lead_sheet = lead_sheets_lib.LeadSheet(melody, chords)
         expected_lead_sheets.append(lead_sheet)
     unit = lead_sheet_pipelines.LeadSheetExtractor(
         min_bars=1,
         min_unique_pitches=1,
         gap_bars=1,
         all_transpositions=False)
     self._unit_transform_test(unit, quantized_sequence,
                               expected_lead_sheets)
示例#24
0
    def testFromNotesPolyphonicWithIgnorePolyphonicNotes(self):
        testing_lib.add_track_to_sequence(self.note_sequence, 0,
                                          [(12, 100, 0.0, 2.0),
                                           (19, 100, 0.0, 3.0),
                                           (12, 100, 1.0, 3.0),
                                           (19, 100, 1.0, 4.0)])
        quantized_sequence = sequences_lib.quantize_note_sequence(
            self.note_sequence, self.steps_per_quarter)

        melody = melodies_lib.Melody()
        melody.from_quantized_sequence(quantized_sequence,
                                       search_start_step=0,
                                       instrument=0,
                                       ignore_polyphonic_notes=True)
        expected = ([19] + [NO_EVENT] * 3 + [19] + [NO_EVENT] * 11)

        self.assertEqual(expected, list(melody))
        self.assertEqual(16, melody.steps_per_bar)
 def testSetLength(self):
   # Setting LeadSheet length should agree with setting length on melody and
   # chords separately.
   melody_events = [60]
   chord_events = ['C7']
   melody = melodies_lib.Melody(melody_events, start_step=9)
   chords = chords_lib.ChordProgression(chord_events, start_step=9)
   expected_melody = copy.deepcopy(melody)
   expected_chords = copy.deepcopy(chords)
   lead_sheet = lead_sheets_lib.LeadSheet(melody, chords)
   lead_sheet.set_length(5)
   expected_melody.set_length(5)
   expected_chords.set_length(5)
   self.assertEqual(expected_melody, lead_sheet.melody)
   self.assertEqual(expected_chords, lead_sheet.chords)
   self.assertEqual(9, lead_sheet.start_step)
   self.assertEqual(14, lead_sheet.end_step)
   self.assertListEqual([9, 10, 11, 12, 13], lead_sheet.steps)
示例#26
0
    def testFromNotesTrimEmptyMeasures(self):
        testing_lib.add_track_to_sequence(self.note_sequence, 0,
                                          [(12, 100, 1.5, 1.75),
                                           (11, 100, 2, 2.25)])
        quantized_sequence = sequences_lib.quantize_note_sequence(
            self.note_sequence, self.steps_per_quarter)

        melody = melodies_lib.Melody()
        melody.from_quantized_sequence(quantized_sequence,
                                       search_start_step=0,
                                       instrument=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)
示例#27
0
    def testFromNotesTimeOverlap(self):
        testing_lib.add_track_to_sequence(self.note_sequence, 0,
                                          [(12, 100, 1, 2),
                                           (11, 100, 3.25, 3.75),
                                           (13, 100, 2, 4)])
        quantized_sequence = sequences_lib.quantize_note_sequence(
            self.note_sequence, self.steps_per_quarter)

        melody = melodies_lib.Melody()
        melody.from_quantized_sequence(quantized_sequence,
                                       search_start_step=0,
                                       instrument=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))
示例#28
0
 def testEncode(self):
   events = [100, 100, 107, 111, NO_EVENT, 99, 112, NOTE_OFF, NO_EVENT]
   melody = melodies_lib.Melody(events)
   melody.squash(
       self.min_note,
       self.max_note,
       self.transpose_to_key)
   inputs, labels = self.med.encode(melody)
   expected_inputs = [
       [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
       [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
       [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0],
       [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
       [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
       [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
       [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
       [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]
   expected_labels = [2, 9, 13, 0, 13, 2, 1, 0]
   self.assertEqual(inputs, expected_inputs)
   self.assertEqual(labels, expected_labels)
示例#29
0
    def testToSequenceSimple(self):
        melody = melodies_lib.Melody([
            NO_EVENT, 1, NO_EVENT, NOTE_OFF, NO_EVENT, 2, 3, NOTE_OFF, NO_EVENT
        ])
        sequence = melody.to_sequence(velocity=10,
                                      instrument=1,
                                      sequence_start_time=2,
                                      qpm=60.0)

        self.assertProtoEquals(
            'ticks_per_quarter: 220 '
            'tempos < qpm: 60.0 > '
            'total_time: 3.75 '
            'notes < '
            '  pitch: 1 velocity: 10 instrument: 1 start_time: 2.25 end_time: 2.75 '
            '> '
            'notes < '
            '  pitch: 2 velocity: 10 instrument: 1 start_time: 3.25 end_time: 3.5 '
            '> '
            'notes < '
            '  pitch: 3 velocity: 10 instrument: 1 start_time: 3.5 end_time: 3.75 '
            '> ', sequence)
示例#30
0
    def testSlice(self):
        testing_lib.add_track_to_sequence(self.note_sequence, 0,
                                          [(12, 100, 1, 3), (11, 100, 5, 7),
                                           (13, 100, 9, 10)])
        quantized_sequence = sequences_lib.quantize_note_sequence(
            self.note_sequence, steps_per_quarter=1)

        melody = melodies_lib.Melody()
        melody.from_quantized_sequence(quantized_sequence,
                                       search_start_step=0,
                                       instrument=0,
                                       ignore_polyphonic_notes=False)
        expected = [
            NO_EVENT, 12, NO_EVENT, NOTE_OFF, NO_EVENT, 11, NO_EVENT, NOTE_OFF,
            NO_EVENT, 13
        ]
        self.assertEqual(expected, list(melody))

        expected_slice = [
            NO_EVENT, NO_EVENT, NO_EVENT, 11, NO_EVENT, NOTE_OFF, NO_EVENT
        ]
        self.assertEqual(expected_slice, list(melody[2:-1]))