Example #1
0
 def testExtendMelodies(self):
     melody1 = melodies_lib.MonophonicMelody()
     melody1.from_event_list([60])
     melody2 = melodies_lib.MonophonicMelody()
     melody2.from_event_list([60])
     melody3 = melodies_lib.MonophonicMelody()
     melody3.from_event_list([60])
     melody4 = melodies_lib.MonophonicMelody()
     melody4.from_event_list([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.melody_encoder_decoder.extend_melodies(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])
Example #2
0
    def testTranspose(self):
        # MonophonicMelody 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.MonophonicMelody()
        melody.from_event_list(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))

        # MonophonicMelody 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.MonophonicMelody()
        melody.from_event_list(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))

        # MonophonicMelody 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.MonophonicMelody()
        melody.from_event_list(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))
Example #3
0
    def testSquash(self):
        # MonophonicMelody 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.MonophonicMelody()
        melody.from_event_list(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))

        # MonophonicMelody 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.MonophonicMelody()
        melody.from_event_list(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))

        # MonophonicMelody 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.MonophonicMelody()
        melody.from_event_list(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))
Example #4
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.MonophonicMelody()
        melody.from_event_list(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.MonophonicMelody()
        melody.from_event_list(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))
Example #5
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.MonophonicMelody()
        melody.from_event_list(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.MonophonicMelody()
        melody.from_event_list(events)
        self.assertEqual(1, melody.get_major_key())

        # One note in C Major.
        events = [NO_EVENT, 12 * 2 + 11, NOTE_OFF]
        melody = melodies_lib.MonophonicMelody()
        melody.from_event_list(events)
        self.assertEqual(0, melody.get_major_key())
Example #6
0
    def testSetLength(self):
        events = [60]
        melody = melodies_lib.MonophonicMelody()
        melody.from_event_list(events, start_step=9)
        melody.set_length(5)
        self.assertListEqual([60, NOTE_OFF, NO_EVENT, NO_EVENT, NO_EVENT],
                             list(melody))
        self.assertEquals(9, melody.start_step)
        self.assertEquals(14, melody.end_step)

        melody = melodies_lib.MonophonicMelody()
        melody.from_event_list(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.assertEquals(5, melody.start_step)
        self.assertEquals(10, melody.end_step)

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

        melody = melodies_lib.MonophonicMelody()
        melody.from_event_list(events)
        melody.set_length(3, from_left=True)
        self.assertListEqual([NO_EVENT, NO_EVENT, NOTE_OFF], list(melody))
        self.assertEquals(1, melody.start_step)
        self.assertEquals(4, melody.end_step)
    def testSetLength(self):
        events = [60]
        melody = melodies_lib.MonophonicMelody()
        melody.from_event_list(events)
        melody.set_length(5)
        self.assertListEqual([60, -2, -2, -2, -2], melody.events)

        events = [60, -1, -1, -2]
        melody = melodies_lib.MonophonicMelody()
        melody.from_event_list(events)
        melody.set_length(3)
        self.assertListEqual([60, -1, -1], melody.events)
    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.MonophonicMelody()
        melody.from_event_list([
            NO_EVENT, 1, NO_EVENT, NOTE_OFF, NO_EVENT, 2, 3, NOTE_OFF, NO_EVENT
        ])
        chords = chords_lib.ChordProgression()
        chords.from_event_list(
            [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.assertEquals(melody_sequence.ticks_per_quarter,
                          sequence.ticks_per_quarter)
        self.assertProtoEquals(melody_sequence.tempos, sequence.tempos)
        self.assertEquals(melody_sequence.total_time, sequence.total_time)
        self.assertProtoEquals(melody_sequence.notes, sequence.notes)
        self.assertProtoEquals(chords_sequence.text_annotations,
                               sequence.text_annotations)
Example #9
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)
Example #10
0
 def testGetInputsBatch(self):
   events1 = [100, 100, 107, 111, NO_EVENT, 99, 112, NOTE_OFF, NO_EVENT]
   melody1 = melodies_lib.MonophonicMelody()
   melody1.from_event_list(events1)
   events2 = [9, 10, 12, 14, 15, 17, 19, 21, 22]
   melody2 = melodies_lib.MonophonicMelody()
   melody2.from_event_list(events2)
   transpose_amount1 = melody1.squash(
       self.melody_encoder_decoder.min_note,
       self.melody_encoder_decoder.max_note,
       self.melody_encoder_decoder.transpose_to_key)
   transpose_amount2 = melody2.squash(
       self.melody_encoder_decoder.min_note,
       self.melody_encoder_decoder.max_note,
       self.melody_encoder_decoder.transpose_to_key)
   self.assertEqual(transpose_amount1, -40)
   self.assertEqual(transpose_amount2, 50)
   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.melody_encoder_decoder.get_inputs_batch(melodies, True))
   self.assertListEqual(
       expected_last_event_inputs_batch,
       self.melody_encoder_decoder.get_inputs_batch(melodies))
Example #11
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.MonophonicMelody()
    melody.from_event_list(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.MonophonicMelody()
    melody.from_event_list(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.MonophonicMelody()
    expected = [0] * 12
    self.assertEqual(expected, list(melody.get_note_histogram()))
Example #12
0
 def testFromNotesStepsPerBar(self):
   self.quantized_sequence.time_signature = sequences_lib.TimeSignature(7, 8)
   self.quantized_sequence.steps_per_quarter = 12
   self.quantized_sequence.tracks[0] = []
   melody = melodies_lib.MonophonicMelody()
   melody.from_quantized_sequence(self.quantized_sequence,
                                  start_step=0, track=0,
                                  ignore_polyphonic_notes=False)
   self.assertEqual(42, melody.steps_per_bar)
Example #13
0
    def testToSequenceEmpty(self):
        melody = melodies_lib.MonophonicMelody()
        sequence = melody.to_sequence(velocity=10,
                                      instrument=1,
                                      sequence_start_time=2,
                                      bpm=60.0)

        self.assertProtoEquals('ticks_per_beat: 96 '
                               'tempos < bpm: 60.0 > ', sequence)
Example #14
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.MonophonicMelody()
   melody.from_event_list(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))
Example #15
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))
Example #16
0
    def testDeepcopy(self):
        melody = melodies_lib.MonophonicMelody()
        melody.from_event_list([0, 1, 2],
                               start_step=0,
                               steps_per_quarter=4,
                               steps_per_bar=8)
        melody_copy = melody.deepcopy()
        self.assertEqual(melody, melody_copy)

        melody.set_length(2)
        self.assertNotEqual(melody, melody_copy)
 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))
Example #18
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)
Example #19
0
    def testGetKeyHistogram(self):
        # One C.
        events = [NO_EVENT, 12 * 5, NOTE_OFF]
        melody = melodies_lib.MonophonicMelody()
        melody.from_event_list(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.MonophonicMelody()
        melody.from_event_list(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.MonophonicMelody()
        melody.from_event_list(events)
        expected = [2, 2, 2, 2, 1, 2, 1, 2, 2, 2, 2, 1]
        self.assertListEqual(expected, list(melody.get_major_key_histogram()))
Example #20
0
    def testAppendEvent(self):
        melody = melodies_lib.MonophonicMelody()

        melody.append_event(14)
        self.assertListEqual([14], list(melody))
        self.assertEqual(0, melody.start_step)
        self.assertEqual(1, melody.end_step)

        melody.append_event(NOTE_OFF)
        self.assertListEqual([14, NOTE_OFF], list(melody))
        self.assertEqual(0, melody.start_step)
        self.assertEqual(2, melody.end_step)
Example #21
0
 def testSetLength(self):
     # Setting LeadSheet length should agree with setting length on melody and
     # chords separately.
     melody_events = [60]
     melody = melodies_lib.MonophonicMelody()
     melody.from_event_list(melody_events, start_step=9)
     chord_events = ['C7']
     chords = chords_lib.ChordProgression()
     chords.from_event_list(chord_events, start_step=9)
     lead_sheet = lead_sheets_lib.LeadSheet(melody, chords)
     lead_sheet.set_length(5)
     expected_melody = melodies_lib.MonophonicMelody()
     expected_melody.from_event_list(melody_events[:], start_step=9)
     expected_melody.set_length(5)
     expected_chords = chords_lib.ChordProgression()
     expected_chords.from_event_list(chord_events[:], start_step=9)
     expected_chords.set_length(5)
     self.assertEquals(expected_melody, lead_sheet.melody)
     self.assertEquals(expected_chords, lead_sheet.chords)
     self.assertEquals(9, lead_sheet.start_step)
     self.assertEquals(14, lead_sheet.end_step)
Example #22
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))
Example #23
0
 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
     ]
     melody = melodies_lib.MonophonicMelody()
     melody.from_event_list(melody_events)
     chord_events = ['C', 'Am', 'Dm', 'G', 'C', NO_CHORD]
     chords = chords_lib.ChordProgression()
     chords.from_event_list(chord_events)
     lead_sheet = lead_sheets_lib.LeadSheet(melody, chords)
     lead_sheet.squash(min_note=12 * 5, max_note=12 * 6, transpose_to_key=0)
     expected_melody = melodies_lib.MonophonicMelody()
     expected_melody.from_event_list(melody_events[:])
     transpose_amount = expected_melody.squash(min_note=12 * 5,
                                               max_note=12 * 6,
                                               transpose_to_key=0)
     expected_chords = chords_lib.ChordProgression()
     expected_chords.from_event_list(chord_events[:])
     expected_chords.transpose(transpose_amount=transpose_amount)
     self.assertEqual(expected_melody, lead_sheet.melody)
     self.assertEqual(expected_chords, lead_sheet.chords)
Example #24
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)
Example #25
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)
Example #26
0
    def testToSequenceEndsWithNonzeroStart(self):
        melody = melodies_lib.MonophonicMelody()
        melody.from_event_list([NO_EVENT, 1, NO_EVENT], start_step=4)
        sequence = melody.to_sequence(velocity=100,
                                      instrument=0,
                                      sequence_start_time=0.5,
                                      bpm=60.0)

        self.assertProtoEquals(
            'ticks_per_beat: 96 '
            'tempos < bpm: 60.0 > '
            'total_time: 2.25 '
            'notes < pitch: 1 velocity: 100 start_time: 1.75 end_time: 2.25 > ',
            sequence)
Example #27
0
 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
     ]
     melody = melodies_lib.MonophonicMelody()
     melody.from_event_list(melody_events)
     chord_events = [NO_CHORD, 'C', 'F', 'Dm', 'D', 'G']
     chords = chords_lib.ChordProgression()
     chords.from_event_list(chord_events)
     lead_sheet = lead_sheets_lib.LeadSheet(melody, chords)
     lead_sheet.transpose(transpose_amount=-5,
                          min_note=12 * 5,
                          max_note=12 * 7)
     expected_melody = melodies_lib.MonophonicMelody()
     expected_melody.from_event_list(melody_events[:])
     expected_melody.transpose(transpose_amount=-5,
                               min_note=12 * 5,
                               max_note=12 * 7)
     expected_chords = chords_lib.ChordProgression()
     expected_chords.from_event_list(chord_events[:])
     expected_chords.transpose(transpose_amount=-5)
     self.assertEqual(expected_melody, lead_sheet.melody)
     self.assertEqual(expected_chords, lead_sheet.chords)
Example #28
0
 def testEncode(self):
   events = [100, 100, 107, 111, NO_EVENT, 99, 112, NOTE_OFF, NO_EVENT]
   melody = melodies_lib.MonophonicMelody()
   melody.from_event_list(events)
   sequence_example = self.melody_encoder_decoder.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]
   expected_sequence_example = sequence_example_lib.make_sequence_example(
       expected_inputs, expected_labels)
   self.assertEqual(sequence_example, expected_sequence_example)
 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)
Example #30
0
    def testToSequenceEndsWithSustainedNote(self):
        melody = melodies_lib.MonophonicMelody()
        melody.from_event_list([
            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,
                                      bpm=60.0)

        self.assertProtoEquals(
            'ticks_per_beat: 96 '
            'tempos < bpm: 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)