Rithmos / harmony_matrix_generator.py
Ikaros
Initial commit of server files
9a55333
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)