|
import json |
|
import networkx as nx |
|
import numpy as np |
|
|
|
def create_music_graph(): |
|
""" |
|
Creates a directed graph representing the 12-tone chromatic scale |
|
with edges for major and minor thirds, and perfect fifths. |
|
""" |
|
notes = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'] |
|
G = nx.DiGraph() |
|
for i, note in enumerate(notes): |
|
G.add_node(i, label=note) |
|
|
|
for i in range(12): |
|
|
|
G.add_edge(i, (i + 4) % 12, interval='M3') |
|
|
|
G.add_edge(i, (i + 3) % 12, interval='m3') |
|
|
|
G.add_edge(i, (i + 7) % 12, interval='P5') |
|
|
|
return G |
|
|
|
def get_adjacency_matrix(G): |
|
""" |
|
Returns the adjacency matrix of the graph. |
|
""" |
|
return nx.to_numpy_array(G, dtype=int) |
|
|
|
def save_matrix_to_json(matrix, path='matrix.json'): |
|
""" |
|
Saves the matrix to a JSON file. |
|
""" |
|
with open(path, 'w') as f: |
|
json.dump(matrix.tolist(), f) |
|
|
|
if __name__ == '__main__': |
|
graph = create_music_graph() |
|
adj_matrix = get_adjacency_matrix(graph) |
|
save_matrix_to_json(adj_matrix, '/home/KidIkaros/Documents/code/Ikaros/musick/chord-detector-extension/matrix.json') |
|
print("Adjacency matrix saved to matrix.json") |
|
print(adj_matrix) |
|
|