def testFromQuantizedNoteSequenceWithinSingleChord(self): testing_lib.add_chords_to_sequence(self.note_sequence, [('F', 0), ('Gm', 8)]) quantized_sequence = sequences_lib.quantize_note_sequence( self.note_sequence, self.steps_per_quarter) chords = chords_lib.ChordProgression() chords.from_quantized_sequence(quantized_sequence, start_step=4, end_step=6) expected = ['F'] * 2 self.assertEqual(expected, list(chords))
def testFromQuantizedSequence(self): testing_lib.add_quantized_chords_to_sequence( self.quantized_sequence, [('Am', 4), ('D7', 8), ('G13', 12), ('Csus', 14)]) chords = chords_lib.ChordProgression() chords.from_quantized_sequence( self.quantized_sequence, start_step=0, end_step=16) expected = [NO_CHORD, NO_CHORD, NO_CHORD, NO_CHORD, 'Am', 'Am', 'Am', 'Am', 'D7', 'D7', 'D7', 'D7', 'G13', 'G13', 'Csus', 'Csus'] self.assertEqual(expected, list(chords))
def testFromQuantizedNoteSequenceWithCoincidentChords(self): testing_lib.add_chords_to_sequence(self.note_sequence, [('Am', 4), ('D7', 8), ('G13', 12), ('Csus', 12)]) quantized_sequence = sequences_lib.quantize_note_sequence( self.note_sequence, self.steps_per_quarter) chords = chords_lib.ChordProgression() with self.assertRaises(chords_lib.CoincidentChordsException): chords.from_quantized_sequence(quantized_sequence, start_step=0, end_step=16)
def testTranspose(self): # Transpose ChordProgression with basic triads. events = ['Cm', 'F', 'B-', 'E-'] chords = chords_lib.ChordProgression(events) chords.transpose(transpose_amount=7) expected = ['Gm', 'C', 'F', 'B-'] self.assertEqual(expected, list(chords)) # Transpose ChordProgression with more complex chords. events = ['Esus2', 'B13', 'A7/B', 'F#dim'] chords = chords_lib.ChordProgression(events) chords.transpose(transpose_amount=-2) expected = ['Dsus2', 'A13', 'G7/A', 'Edim'] self.assertEqual(expected, list(chords)) # Transpose ChordProgression containing NO_CHORD. events = ['C', 'B-', NO_CHORD, 'F', 'C'] chords = chords_lib.ChordProgression(events) chords.transpose(transpose_amount=4) expected = ['E', 'D', NO_CHORD, 'A', 'E'] self.assertEqual(expected, list(chords))
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)
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.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)
def testChordsExtractor(self): quantized_sequence = sequences_lib.QuantizedSequence() quantized_sequence.steps_per_quarter = 1 testing_lib.add_quantized_chords_to_sequence(quantized_sequence, [('C', 2), ('Am', 4), ('F', 5)]) quantized_sequence.total_steps = 8 expected_events = [[NO_CHORD, NO_CHORD, 'C', 'C', 'Am', 'F', 'F', 'F']] expected_chord_progressions = [] for events_list in expected_events: chords = chords_lib.ChordProgression(events_list, steps_per_quarter=1, steps_per_bar=4) expected_chord_progressions.append(chords) unit = pipelines_common.ChordsExtractor(all_transpositions=False) self._unit_transform_test(unit, quantized_sequence, expected_chord_progressions)
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)
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] lead_sheet = lead_sheets_lib.LeadSheet() lead_sheet.from_event_list(zip(melody_events, chord_events)) 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)
def testSetLength(self): # Setting LeadSheet length should agree with setting length on melody and # chords separately. melody_events = [60] chord_events = ['C7'] lead_sheet = lead_sheets_lib.LeadSheet() lead_sheet.from_event_list(zip(melody_events, chord_events), start_step=9) 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)
def testToSequence(self): chords = chords_lib.ChordProgression( [NO_CHORD, 'C7', 'C7', 'C7', 'C7', 'Am7b5', 'F6', 'F6', NO_CHORD]) sequence = chords.to_sequence(sequence_start_time=2, qpm=60.0) self.assertProtoEquals( 'ticks_per_quarter: 220 ' 'tempos < qpm: 60.0 > ' 'text_annotations < ' ' text: "C7" time: 2.25 annotation_type: CHORD_SYMBOL ' '> ' 'text_annotations < ' ' text: "Am7b5" time: 3.25 annotation_type: CHORD_SYMBOL ' '> ' 'text_annotations < ' ' text: "F6" time: 3.5 annotation_type: CHORD_SYMBOL ' '> ' 'text_annotations < ' ' text: "N.C." time: 4.0 annotation_type: CHORD_SYMBOL ' '> ', sequence)
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'] lead_sheet = lead_sheets_lib.LeadSheet() lead_sheet.from_event_list(zip(melody_events, chord_events)) 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)
def testChordsExtractor(self): note_sequence = common_testing_lib.parse_test_proto( music_pb2.NoteSequence, """ time_signatures: { numerator: 4 denominator: 4} tempos: { qpm: 60}""") testing_lib.add_chords_to_sequence(note_sequence, [('C', 2), ('Am', 4), ('F', 5)]) quantized_sequence = sequences_lib.quantize_note_sequence( note_sequence, steps_per_quarter=1) quantized_sequence.total_quantized_steps = 8 expected_events = [[NO_CHORD, NO_CHORD, 'C', 'C', 'Am', 'F', 'F', 'F']] expected_chord_progressions = [] for events_list in expected_events: chords = chords_lib.ChordProgression(events_list, steps_per_quarter=1, steps_per_bar=4) expected_chord_progressions.append(chords) unit = chord_pipelines.ChordsExtractor(all_transpositions=False) self._unit_transform_test(unit, quantized_sequence, expected_chord_progressions)
def testTransposeUnknownChordSymbol(self): # Attempt to transpose ChordProgression with unknown chord symbol. events = ['Cm', 'G7', 'P#13', 'F'] chords = chords_lib.ChordProgression(events) with self.assertRaises(chord_symbols_lib.ChordSymbolException): chords.transpose(transpose_amount=-4)
def testFromQuantizedSequenceWithNoChords(self): chords = chords_lib.ChordProgression() chords.from_quantized_sequence( self.quantized_sequence, start_step=0, end_step=16) expected = [NO_CHORD] * 16 self.assertEqual(expected, list(chords))
def _reset(self): """Clear events and reset object state.""" self._melody = melodies_lib.Melody() self._chords = chords_lib.ChordProgression()