File size: 1,770 Bytes
9a55333 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import json
import numpy as np
def generate_consonance_matrix():
"""
Generates a 12x12 matrix representing the consonance/dissonance
between the 12 notes of the chromatic scale.
"""
# Scores based on music theory (higher is more consonant)
# These values are chosen to represent relative consonance and dissonance.
interval_scores = {
0: 10, # Unison
1: -8, # Minor Second (dissonant)
2: -4, # Major Second (dissonant)
3: 5, # Minor Third (consonant)
4: 6, # Major Third (consonant)
5: 4, # Perfect Fourth (context-dependent, generally consonant)
6: -10, # Tritone (highly dissonant)
7: 8, # Perfect Fifth (highly consonant)
8: 5, # Minor Sixth (consonant)
9: 6, # Major Sixth (consonant)
10: -3, # Minor Seventh (dissonant)
11: -7 # Major Seventh (dissonant)
}
matrix = np.zeros((12, 12), dtype=int)
for i in range(12):
for j in range(12):
interval = abs(i - j)
# We only care about the shortest distance between notes on the circle
if interval > 6:
interval = 12 - interval
matrix[i, j] = interval_scores[interval]
return matrix
def save_matrix_to_json(matrix, path):
"""
Saves the matrix to a JSON file.
"""
with open(path, 'w') as f:
json.dump(matrix.tolist(), f, indent=2)
if __name__ == '__main__':
consonance_matrix = generate_consonance_matrix()
file_path = '/home/KidIkaros/Documents/code/Ikaros/musick/chord_detector_extension/consonance_.json'
save_matrix_to_json(consonance_matrix, file_path)
print(f"Consonance matrix saved to {file_path}")
print(consonance_matrix)
|