화성 12음계로 중복을 제외한 152,448개의 조합을 출력하는 파이썬 코드

from itertools import combinations


# 음계 정의

notes = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']


# 코드 타입 정의

chord_types = {

    '': ['', 'm', 'dim', 'aug'],

    '7': ['7', 'm7', 'maj7', 'm7b5', 'dim7'],

    '9': ['9', 'm9', 'maj9'],

    '11': ['11', 'm11'],

    '13': ['13', 'm13']

}


# 인터벌 정의

intervals = {

    '': [0, 4, 7],

    'm': [0, 3, 7],

    'dim': [0, 3, 6],

    'aug': [0, 4, 8],

    '7': [0, 4, 7, 10],

    'm7': [0, 3, 7, 10],

    'maj7': [0, 4, 7, 11],

    'm7b5': [0, 3, 6, 10],

    'dim7': [0, 3, 6, 9],

    '9': [0, 4, 7, 10, 14],

    'm9': [0, 3, 7, 10, 14],

    'maj9': [0, 4, 7, 11, 14],

    '11': [0, 4, 7, 10, 14, 17],

    'm11': [0, 3, 7, 10, 14, 17],

    '13': [0, 4, 7, 10, 14, 17, 21],

    'm13': [0, 3, 7, 10, 14, 17, 21]

}


# 텍스트 파일로 출력

with open('piano_chords_all.txt', 'w') as file:

    # 모든 코드 조합 출력

    for root_note in notes:

        for extension in chord_types:

            for chord_type in chord_types[extension]:

                chord = root_note + chord_type + extension

                chord_intervals = intervals[chord_type]

                chord_notes = [notes[(notes.index(root_note) + i) % 12] for i in chord_intervals]

                file.write(f"조성: {root_note}, 구성음: {chord_notes}, 화음 기호: {chord}\n")


                # 추가 음을 포함한 코드 조합 생성

                for num_extra_notes in range(1, 5):

                    for extra_notes in combinations(notes, num_extra_notes):

                        extended_chord_notes = chord_notes + list(extra_notes)

                        extended_chord = chord + '(' + ','.join(extra_notes) + ')'

                        file.write(f"조성: {root_note}, 구성음: {extended_chord_notes}, 화음 기호: {extended_chord}\n")


print("피아노 코드 조합이 'piano_chords_all.txt' 파일로 저장되었습니다.")