def extract_chords_for_melodies(quantized_sequence, melodies): """Extracts a chord progression from the quantized NoteSequence for melodies. This function will extract the underlying chord progression (encoded as text annotations) from `quantized_sequence` for each monophonic melody in `melodies`. Each chord progression will be the same length as its corresponding melody. Args: quantized_sequence: A quantized NoteSequence object. melodies: A python list of Melody instances. Returns: chord_progressions: A python list of ChordProgression instances, the same length as `melodies`. If a progression fails to be extracted for a melody, the corresponding list entry will be None. stats: A dictionary mapping string names to `statistics.Statistic` objects. """ chord_progressions = [] stats = dict([('coincident_chords', statistics.Counter('coincident_chords'))]) for melody in melodies: try: chords = ChordProgression() chords.from_quantized_sequence(quantized_sequence, melody.start_step, melody.end_step) except CoincidentChordsError: stats['coincident_chords'].increment() chords = None chord_progressions.append(chords) return chord_progressions, list(stats.values())
def extract_chords(quantized_sequence, max_steps=None, all_transpositions=False): """Extracts a single chord progression from a quantized NoteSequence. This function will extract the underlying chord progression (encoded as text annotations) from `quantized_sequence`. Args: quantized_sequence: A quantized NoteSequence. max_steps: An integer, maximum length of a chord progression. Chord progressions will be trimmed to this length. If None, chord progressions will not be trimmed. all_transpositions: If True, also transpose the chord progression into all 12 keys. Returns: chord_progressions: If `all_transpositions` is False, a python list containing a single ChordProgression instance. If `all_transpositions` is True, a python list containing 12 ChordProgression instances, one for each transposition. stats: A dictionary mapping string names to `statistics.Statistic` objects. """ sequences_lib.assert_is_relative_quantized_sequence(quantized_sequence) stats = dict([('chords_truncated', statistics.Counter('chords_truncated')) ]) chords = ChordProgression() chords.from_quantized_sequence(quantized_sequence, 0, quantized_sequence.total_quantized_steps) if max_steps is not None: if len(chords) > max_steps: chords.set_length(max_steps) stats['chords_truncated'].increment() if all_transpositions: chord_progressions = [] for amount in range(-6, 6): transposed_chords = copy.deepcopy(chords) transposed_chords.transpose(amount) chord_progressions.append(transposed_chords) return chord_progressions, stats.values() else: return [chords], stats.values()