You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
compact_sets_sandbox/compact_sets_extended_simpl...

1185 lines
213 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 682,
"id": "806f6f69-1e0b-4d34-aac9-695c8531cdb1",
"metadata": {},
"outputs": [],
"source": [
"from itertools import chain, combinations, permutations, product, pairwise\n",
"from math import prod, log\n",
"from copy import deepcopy\n",
"import networkx as nx\n",
"from fractions import Fraction\n",
"import json\n",
"from operator import add\n",
"\n",
"def hs_array_to_fr(hs_array):\n",
" return prod([pow(dims[d], hs_array[d]) for d in range(len(dims))])\n",
"\n",
"def hs_array_to_cents(hs_array):\n",
" return (1200 * log(hs_array_to_fr(hs_array), 2))\n",
"\n",
"def expand_pitch(hs_array):\n",
" expanded_pitch = list(hs_array)\n",
" frequency_ratio = hs_array_to_fr(hs_array)\n",
" if frequency_ratio < 1:\n",
" while frequency_ratio < 1:\n",
" frequency_ratio *= 2\n",
" expanded_pitch[0] += 1\n",
" elif frequency_ratio >= 2:\n",
" while frequency_ratio >= 2:\n",
" frequency_ratio *= 1/2\n",
" expanded_pitch[0] += -1\n",
" return tuple(expanded_pitch)\n",
"\n",
"def expand_chord(chord):\n",
" return tuple(expand_pitch(p) for p in chord)\n",
"\n",
"def collapse_pitch(hs_array):\n",
" collapsed_pitch = list(hs_array)\n",
" collapsed_pitch[0] = 0\n",
" return tuple(collapsed_pitch)\n",
"\n",
"def collapse_chord(chord):\n",
" return tuple(collapse_pitch(p) for p in chord)\n",
"\n",
"def transpose_pitch(pitch, trans):\n",
" return tuple(map(add, pitch, trans))\n",
"\n",
"def transpose_chord(chord, trans):\n",
" return tuple(transpose_pitch(p, trans) for p in chord)\n",
"\n",
"def cent_difference(hs_array1, hs_array2):\n",
" return hs_array_to_cents(hs_array2) - hs_array_to_cents(hs_array1)\n",
"\n",
"def pitch_difference(hs_array1, hs_array2):\n",
" return transpose_pitch(hs_array1, [p * -1 for p in hs_array2])\n",
"\n",
"# this is modified for different chord sizes like original version\n",
"def grow_chords(chord, root, min_chord_size, max_chord_size):\n",
" #this could use the tranpose_pitch function\n",
" branches = [branch for alt in [-1, 1] for d in range(1, len(root)) if (branch:=(*(r:=root)[:d], r[d] + alt, *r[(d + 1):])) not in chord]\n",
" subsets = chain.from_iterable(combinations(branches, r) for r in range(1, max_chord_size - len(chord) + 1))\n",
" for subset in subsets:\n",
" extended_chord = chord + subset\n",
" if(len(extended_chord) < max_chord_size):\n",
" for branch in subset:\n",
" yield from grow_chords(extended_chord, branch, min_chord_size, max_chord_size)\n",
" if(len(extended_chord) >= min_chord_size):\n",
" yield tuple(sorted(extended_chord, key=hs_array_to_fr))\n",
"\n",
"def chords(chord, root, min_chord_size, max_chord_size):\n",
" # this will filter out the 4x dups of paths that are loops, there might be a faster way to test this\n",
" return set(grow_chords(chord, root, min_chord_size, max_chord_size))\n",
"\n",
"# this is very slow, I have an idea in mind that my be faster by simply growing the chords to max_chord_size + max_sim_diff\n",
"# technically at that point you have generated both chords and can get the second chord from the first\n",
"def edges(chords, min_symdiff, max_symdiff, max_chord_size): \n",
"\n",
" def reverse_movements(movements):\n",
" return {value['destination']:{'destination':key, 'cent_difference':value['cent_difference'] * -1} for key, value in movements.items()}\n",
"\n",
" def is_directly_tunable(intersection, diff):\n",
" # this only works for now when intersection if one element - need to fix that\n",
" return max([sum(abs(p) for p in collapse_pitch(pitch_difference(d, list(intersection)[0]))) for d in diff]) == 1\n",
"\n",
" for combination in combinations(chords, 2):\n",
" [expanded_base, expanded_comp] = [expand_chord(chord) for chord in combination]\n",
" edges = []\n",
" transpositions = set(pitch_difference(pair[0], pair[1]) for pair in set(product(expanded_base, expanded_comp)))\n",
" for trans in transpositions:\n",
" expanded_comp_transposed = transpose_chord(expanded_comp, trans)\n",
" intersection = set(expanded_base) & set(expanded_comp_transposed)\n",
" symdiff_len = sum([len(chord) - len(intersection) for chord in [expanded_base, expanded_comp_transposed]])\n",
" if (min_symdiff <= symdiff_len <= max_symdiff):\n",
" rev_trans = tuple(t * -1 for t in trans)\n",
" [diff1, diff2] = [list(set(chord) - intersection) for chord in [expanded_base, expanded_comp_transposed]]\n",
" base_map = {val: {'destination':transpose_pitch(val, rev_trans), 'cent_difference': 0} for val in intersection}\n",
" base_map_rev = reverse_movements(base_map)\n",
" maps = []\n",
" diff1 += [None] * (max_chord_size - len(diff1) - len(intersection))\n",
" perms = [list(perm) + [None] * (max_chord_size - len(perm) - len(intersection)) for perm in set(permutations(diff2))]\n",
" for p in perms:\n",
" appended_map = {\n",
" diff1[index]:\n",
" {\n",
" 'destination': transpose_pitch(val, rev_trans) if val != None else None, \n",
" 'cent_difference': cent_difference(diff1[index], val) if None not in [diff1[index], val] else None\n",
" } for index, val in enumerate(p)}\n",
" yield (tuple(expanded_base), tuple(expanded_comp), {\n",
" 'transposition': trans,\n",
" 'symmetric_difference': symdiff_len, \n",
" 'is_directly_tunable': is_directly_tunable(intersection, diff2),\n",
" 'movements': base_map | appended_map\n",
" },)\n",
" yield (tuple(expanded_comp), tuple(expanded_base), {\n",
" 'transposition': rev_trans,\n",
" 'symmetric_difference': symdiff_len, \n",
" 'is_directly_tunable': is_directly_tunable(intersection, diff1),\n",
" 'movements': base_map_rev | reverse_movements(appended_map)\n",
" },)\n",
"\n",
"def graph_from_edges(edges):\n",
" g = nx.MultiDiGraph()\n",
" g.add_edges_from(edges)\n",
" return g\n",
"\n",
"def generate_graph(chord_set, min_symdiff, max_symdiff, max_chord_size):\n",
" #chord_set = chords(pitch_set, min_chord_size, max_chord_size)\n",
" edge_set = edges(chord_set, min_symdiff, max_symdiff, max_chord_size)\n",
" res_graph = graph_from_edges(edge_set)\n",
" return res_graph\n",
"\n",
"def display_graph(graph):\n",
" show_graph = nx.Graph(graph)\n",
" pos = nx.draw_spring(show_graph, node_size=5, width=0.1)\n",
" plt.figure(1, figsize=(12,12)) \n",
" nx.draw(show_graph, pos, node_size=5, width=0.1)\n",
" plt.show()\n",
" #plt.savefig('compact_sets.png', dpi=150)\n",
"\n",
"def path_to_chords(path, start_root):\n",
" current_root = start_root\n",
" start_chord = tuple(sorted(path[0][0], key=hs_array_to_fr))\n",
" chords = ((start_chord, start_chord,),)\n",
" for edge in path:\n",
" trans = edge[2]['transposition']\n",
" movements = edge[2]['movements']\n",
" current_root = transpose_pitch(current_root, trans)\n",
" current_ref_chord = chords[-1][0]\n",
" next_ref_chord = tuple(movements[pitch]['destination'] for pitch in current_ref_chord)\n",
" next_transposed_chord = tuple(transpose_pitch(pitch, current_root) for pitch in next_ref_chord)\n",
" chords += ((next_ref_chord, next_transposed_chord,),)\n",
" return tuple(chord[1] for chord in chords)\n",
"\n",
"def write_chord_sequence(path):\n",
" file = open(\"seq.txt\", \"w+\")\n",
" content = json.dumps(path)\n",
" content = content.replace(\"[[[\", \"[\\n\\t[[\")\n",
" content = content.replace(\", [[\", \",\\n\\t[[\")\n",
" content = content.replace(\"]]]\", \"]]\\n]\")\n",
" file.write(content)\n",
" file.close()"
]
},
{
"cell_type": "code",
"execution_count": 683,
"id": "aea5215c-8551-4685-b761-11c2dc74cf22",
"metadata": {},
"outputs": [],
"source": [
"from random import choice, choices\n",
"\n",
"# This is for the static version\n",
"def stochastic_hamiltonian(graph):\n",
" \n",
" def movement_size_weights(edges):\n",
" \n",
" def max_cent_diff(edge):\n",
" res = max([abs(v) for val in edge[2]['movements'].values() if (v:=val['cent_difference']) is not None])\n",
" return res\n",
" \n",
" def min_cent_diff(edge):\n",
" res = [abs(v) for val in edge[2]['movements'].values() if (v:=val['cent_difference']) is not None]\n",
" res.remove(0)\n",
" return min(res)\n",
" \n",
" for e in edges:\n",
" yield 1000 if ((max_cent_diff(e) < 200) and (min_cent_diff(e)) > 50) else 1\n",
"\n",
" def hamiltonian_weights(edges):\n",
" for e in edges:\n",
" yield 10 if e[1] not in [path_edge[0] for path_edge in path] else 1 / graph.nodes[e[1]]['count']\n",
" \n",
" def contrary_motion_weights(edges):\n",
"\n",
" def is_contrary(edge):\n",
" cent_diffs = [v for val in edge[2]['movements'].values() if (v:=val['cent_difference']) is not None]\n",
" cent_diffs.sort()\n",
" return (cent_diffs[0] < 0) and (cent_diffs[1] == 0) and (cent_diffs[2] > 0)\n",
"\n",
" for e in edges:\n",
" yield 10 if is_contrary(e) else 1\n",
" \n",
" def is_directly_tunable_weights(edges):\n",
" for e in edges:\n",
" yield 10 if e[2]['is_directly_tunable'] else 0\n",
"\n",
" def voice_crossing_weights(edges):\n",
" \n",
" def has_voice_crossing(edge):\n",
" source = list(edge[0])\n",
" ordered_source = sorted(source, key=hs_array_to_fr) \n",
" source_order = [ordered_source.index(p) for p in source]\n",
" destination = [transpose_pitch(edge[2]['movements'][p]['destination'], edge[2]['transposition']) for p in source]\n",
" ordered_destination = sorted(destination, key=hs_array_to_fr)\n",
" destination_order = [ordered_destination.index(p) for p in destination]\n",
" return source_order != destination_order\n",
"\n",
" for e in edges:\n",
" yield 10 if not has_voice_crossing(e) else 0\n",
"\n",
" def is_bass_rooted(chord):\n",
" return max([sum(abs(p) for p in collapse_pitch(pitch_difference(chord[0], p))) for p in chord[1:]]) == 1\n",
" \n",
" check_graph = graph.copy()\n",
" next_node = choice([node for node in graph.nodes() if is_bass_rooted(node)])\n",
" check_graph.remove_node(next_node)\n",
" for node in graph.nodes(data=True):\n",
" node[1]['count'] = 1\n",
" path = []\n",
" while (nx.number_of_nodes(check_graph) > 0) and (len(path) < 5000):\n",
" out_edges = list(graph.out_edges(next_node, data=True))\n",
" #print([l for l in zip(movement_size_weights(out_edges), hamiltonian_weights(out_edges))])\n",
" factors = [\n",
" movement_size_weights(out_edges), \n",
" hamiltonian_weights(out_edges), \n",
" contrary_motion_weights(out_edges), \n",
" is_directly_tunable_weights(out_edges),\n",
" voice_crossing_weights(out_edges)\n",
" ]\n",
" weights = [prod(a) for a in zip(*factors)]\n",
" edge = choices(out_edges, weights=weights)[0]\n",
" #edge = random.choice(out_edges)\n",
" next_node = edge[1]\n",
" node[1]['count'] += 1\n",
" path.append(edge)\n",
" if next_node in check_graph.nodes:\n",
" check_graph.remove_node(next_node)\n",
" return path"
]
},
{
"cell_type": "code",
"execution_count": 684,
"id": "4e3ef738-7f64-47c3-9129-0450fd031375",
"metadata": {},
"outputs": [],
"source": [
"dims = (2, 3, 5, 7, 11)\n",
"root = (0, 0, 0, 0, 0)\n",
"chord = (root,)\n",
"chord_set = chords(chord, root, 3, 3)\n",
"graph = generate_graph(chord_set, 4, 4, 3)"
]
},
{
"cell_type": "code",
"execution_count": 685,
"id": "6e0de0e5-7973-4a15-ae90-5bbb4a1ef49d",
"metadata": {},
"outputs": [],
"source": [
"path = stochastic_hamiltonian(graph)\n",
"#for edge in path:\n",
"# print(edge)\n",
"write_chord_sequence(path_to_chords(path, root))"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "7f2d356f-6377-46cf-bbb1-32111be90f4f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The line_profiler extension is already loaded. To reload it, use:\n",
" %reload_ext line_profiler\n"
]
}
],
"source": [
"%load_ext line_profiler"
]
},
{
"cell_type": "code",
"execution_count": 134,
"id": "7f141bf5-fdcb-4c01-a10b-3e86d3d1a7b4",
"metadata": {},
"outputs": [],
"source": [
"chord_set = chords(chord, root, 3, 3)"
]
},
{
"cell_type": "code",
"execution_count": 136,
"id": "88850b8c-a743-44d0-b863-7cd9066690d9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Timer unit: 1e-09 s\n",
"\n",
"Total time: 0.112228 s\n",
"File: /tmp/ipykernel_515812/2679284550.py\n",
"Function: edge_data at line 74\n",
"\n",
"Line # Hits Time Per Hit % Time Line Contents\n",
"==============================================================\n",
" 74 def edge_data(chords, min_symdiff, max_symdiff, max_chord_size):\n",
" 75 990 29603044.0 29902.1 26.4 [expanded_base, expanded_comp] = [expand_chord(chord) for chord in chords]\n",
" 76 990 229527.0 231.8 0.2 edges = []\n",
" 77 990 23648371.0 23887.2 21.1 transpositions = set(pitch_difference(pair[0], pair[1]) for pair in set(product(expanded_base, expanded_comp)))\n",
" 78 9193 2315267.0 251.9 2.1 for trans in transpositions:\n",
" 79 8203 33386775.0 4070.1 29.7 expanded_comp_transposed = transpose_chord(expanded_comp, trans)\n",
" 80 8203 8393773.0 1023.3 7.5 intersection = set(expanded_base) & set(expanded_comp_transposed)\n",
" 81 8203 11812057.0 1440.0 10.5 symdiff_len = sum([len(chord) - len(intersection) for chord in [expanded_base, expanded_comp_transposed]])\n",
" 82 8203 2530596.0 308.5 2.3 if (min_symdiff <= symdiff_len <= max_symdiff):\n",
" 83 rev_trans = tuple(t * -1 for t in trans)\n",
" 84 [diff1, diff2] = [list(set(chord) - intersection) for chord in [expanded_base, expanded_comp_transposed]]\n",
" 85 base_map = {val: {'destination':transpose_pitch(val, rev_trans), 'cent_difference': 0} for val in intersection}\n",
" 86 base_map_rev = reverse_movements(base_map)\n",
" 87 tunability = is_directly_tunable(intersection, diff2)\n",
" 88 maps = []\n",
" 89 diff1 += [None] * (max_chord_size - len(diff1) - len(intersection))\n",
" 90 perms = [list(perm) + [None] * (max_chord_size - len(perm) - len(intersection)) for perm in set(permutations(diff2))]\n",
" 91 for p in perms:\n",
" 92 appended_map = {\n",
" 93 diff1[index]:\n",
" 94 {\n",
" 95 'destination': transpose_pitch(val, rev_trans) if val != None else None, \n",
" 96 'cent_difference': cent_difference(diff1[index], val) if None not in [diff1[index], val] else None\n",
" 97 } for index, val in enumerate(p)}\n",
" 98 edges.append((tuple(expanded_base), tuple(expanded_comp), {\n",
" 99 'transposition': trans,\n",
" 100 'symmetric_difference': symdiff_len, \n",
" 101 'is_directly_tunable': tunability,\n",
" 102 'movements': base_map | appended_map\n",
" 103 }))\n",
" 104 edges.append((tuple(expanded_comp), tuple(expanded_base), {\n",
" 105 'transposition': rev_trans,\n",
" 106 'symmetric_difference': symdiff_len, \n",
" 107 'is_directly_tunable': tunability,\n",
" 108 'movements': base_map_rev | reverse_movements(appended_map)\n",
" 109 }))\n",
" 110 990 308812.0 311.9 0.3 return edges if edges != [] else None"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"lprun -f edge_data edges(chord_set, 3, 3, 4)"
]
},
{
"cell_type": "code",
"execution_count": 688,
"id": "6e4ecb10-344b-4721-b2f4-68de91d712db",
"metadata": {},
"outputs": [],
"source": [
"from random import choice, choices\n",
"\n",
"# This is for the rising version / yitgadal\n",
"def stochastic_hamiltonian(graph):\n",
" \n",
" def movement_size_weights(edges):\n",
" \n",
" def max_cent_diff(edge):\n",
" res = max([v for val in edge[2]['movements'].values() if (v:=val['cent_difference']) is not None])\n",
" return res\n",
" \n",
" def min_cent_diff(edge):\n",
" res = [v for val in edge[2]['movements'].values() if (v:=val['cent_difference']) is not None]\n",
" res.remove(0)\n",
" return min(res)\n",
" \n",
" for e in edges:\n",
" yield 1000 if ((max_cent_diff(e) < 175) and (min_cent_diff(e)) >= 0) else 1\n",
"\n",
" def hamiltonian_weights(edges):\n",
" for e in edges:\n",
" yield 10 if e[1] not in [path_edge[0] for path_edge in path] else 1 / graph.nodes[e[1]]['count']\n",
" \n",
" def contrary_motion_weights(edges):\n",
"\n",
" def is_contrary(edge):\n",
" cent_diffs = [v for val in edge[2]['movements'].values() if (v:=val['cent_difference']) is not None]\n",
" cent_diffs.sort()\n",
" return (cent_diffs[0] < 0) and (cent_diffs[1] == 0) and (cent_diffs[2] > 0)\n",
"\n",
" for e in edges:\n",
" yield 2 if is_contrary(e) else 1\n",
" \n",
" def is_directly_tunable_weights(edges):\n",
" for e in edges:\n",
" yield 10 if e[2]['is_directly_tunable'] else 0\n",
"\n",
" def transposition_weight(edges):\n",
" for e in edges:\n",
" yield 1000 if 0 <= hs_array_to_cents(e[2]['transposition']) < 100 else 0\n",
"\n",
" def is_sustained_voice(edges, voice):\n",
" \n",
" def is_sustained(edge):\n",
" source = list(edge[0])\n",
" ordered_source = sorted(source, key=hs_array_to_fr) \n",
" destination = [transpose_pitch(edge[2]['movements'][p]['destination'], edge[2]['transposition']) for p in source]\n",
" ordered_destination = sorted(destination, key=hs_array_to_fr)\n",
" return ordered_source[voice] == ordered_destination[voice]\n",
"\n",
" for e in edges:\n",
" yield 10 if is_sustained(e) else 0\n",
"\n",
" def voice_crossing_weights(edges):\n",
" \n",
" def has_voice_crossing(edge):\n",
" source = list(edge[0])\n",
" ordered_source = sorted(source, key=hs_array_to_fr) \n",
" source_order = [ordered_source.index(p) for p in source]\n",
" destination = [transpose_pitch(edge[2]['movements'][p]['destination'], edge[2]['transposition']) for p in source]\n",
" ordered_destination = sorted(destination, key=hs_array_to_fr)\n",
" destination_order = [ordered_destination.index(p) for p in destination]\n",
" return source_order != destination_order\n",
"\n",
" for e in edges:\n",
" yield 10 if not has_voice_crossing(e) else 0\n",
"\n",
" def is_bass_rooted(chord):\n",
" return max([sum(abs(p) for p in collapse_pitch(pitch_difference(chord[0], p))) for p in chord[1:]]) == 1\n",
" \n",
" check_graph = graph.copy()\n",
" #next_node = choice([node for node in graph.nodes() if is_bass_rooted(node)])\n",
" next_node = choice(list(graph.nodes()))\n",
" print(next_node)\n",
" check_graph.remove_node(next_node)\n",
" for node in graph.nodes(data=True):\n",
" node[1]['count'] = 1\n",
" path = []\n",
" while (nx.number_of_nodes(check_graph) > 0) and (len(path) < 500):\n",
" out_edges = list(graph.out_edges(next_node, data=True))\n",
" #print([l for l in zip(movement_size_weights(out_edges), hamiltonian_weights(out_edges))])\n",
" factors = [\n",
" movement_size_weights(out_edges), \n",
" hamiltonian_weights(out_edges), \n",
" #contrary_motion_weights(out_edges), \n",
" #is_directly_tunable_weights(out_edges),\n",
" voice_crossing_weights(out_edges),\n",
" #transposition_weight(out_edges)\n",
" #is_sustained_voice(out_edges, 0)\n",
" ]\n",
" weights = [prod(a) for a in zip(*factors)]\n",
" #print(weights)\n",
" edge = choices(out_edges, weights=weights)[0]\n",
" #print(edge)\n",
" #edge = random.choice(out_edges)\n",
" next_node = edge[1]\n",
" node[1]['count'] += 1\n",
" path.append(edge)\n",
" if next_node in check_graph.nodes:\n",
" check_graph.remove_node(next_node)\n",
" print(len(check_graph.nodes()))\n",
" return path"
]
},
{
"cell_type": "code",
"execution_count": 689,
"id": "a76dc0f3-02e2-4739-9014-b53d3a590e3d",
"metadata": {},
"outputs": [],
"source": [
"dims = (2, 3, 5, 7, 11)\n",
"root = (0, 0, 0, 0, 0)\n",
"chord = (root,)\n",
"chord_set = chords(chord, root, 3, 3)\n",
"graph = generate_graph(chord_set, 2, 2, 3)"
]
},
{
"cell_type": "code",
"execution_count": 690,
"id": "7b76d848-fe53-4b60-b414-46cfe570f78b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"((3, 0, 0, -1, 0), (0, 0, 0, 0, 0), (-2, 0, 0, 1, 0))\n",
"0\n"
]
},
{
"data": {
"text/plain": [
"163"
]
},
"execution_count": 690,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"path = stochastic_hamiltonian(graph)\n",
"#for edge in path:\n",
"# print(edge)\n",
"write_chord_sequence(path_to_chords(path, root))\n",
"len(path)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "91fd1700-6f41-4a4c-98cb-8355075d44d2",
"metadata": {},
"outputs": [],
"source": [
"#ffmpeg -i video.mkv -acodec pcm_s16le -ar 16000 -ac 2 bereshit_chant.wav\n",
"#sox bereshit_chant.wav bereshit_chant_snippet.wav trim 4 60"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "51be5ff2-2b7f-4350-878f-09e79d4bff1f",
"metadata": {},
"outputs": [],
"source": [
"import crepe\n",
"from scipy.io import wavfile"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "9b8e1e21-c5f5-4d77-8d1c-094b073b8322",
"metadata": {},
"outputs": [],
"source": [
"sr, audio = wavfile.read('/home/mwinter/Downloads/bereshit_chant_snippet.wav')"
]
},
{
"cell_type": "code",
"execution_count": 180,
"id": "01f8f969-fc3c-46a2-acfd-b1f956e438bc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1m76/76\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m54s\u001b[0m 714ms/step\n"
]
}
],
"source": [
"time, frequency, confidence, activation = crepe.predict(audio, sr, step_size = 25)\n",
"#time, frequency, confidence = [list(time), list(frequency), list(confidence)]"
]
},
{
"cell_type": "code",
"execution_count": 786,
"id": "a89c32ee-6413-400a-b3c9-a875f2768954",
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAiMAAAGdCAYAAADAAnMpAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy80BEi2AAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOx9d5wlRbX/t/ummdm8ywbCknNOiigKPFFUxBxBEVSe+sDE+71nRH2KoL4nZsX4TOAzKwqigIKKIElyzizL5jS7M3Pv7e76/dFdVaeqTnW4M7OzuPd8PrB37u1Q3V1ddep7vud7AiGEQN/61re+9a1vfevbFFk41Q3oW9/61re+9a1vW7f1nZG+9a1vfetb3/o2pdZ3RvrWt771rW9969uUWt8Z6Vvf+ta3vvWtb1NqfWekb33rW9/61re+Tan1nZG+9a1vfetb3/o2pdZ3RvrWt771rW9969uUWt8Z6Vvf+ta3vvWtb1Nq9aluQBlLkgRLly7FjBkzEATBVDenb33rW9/61re+lTAhBIaHh7HddtshDP34x1PCGVm6dCkWL1481c3oW9/61re+9a1vPdjjjz+OHXbYwfv7U8IZmTFjBoD0YmbOnDnFrelb3/rWt771rW9lbMOGDVi8eLGax332lHBGZGhm5syZfWekb33rW9/61renmBVRLPoE1r71rW9961vf+jal1ndG+ta3vvWtb33r25Ra3xnpW9/61re+9a1vU2p9Z6Rvfetb3/rWt75NqfWdkb71rW9961vf+jal1ndG+ta3vvWtb33r25Ra3xnpW9/61re+9a1vU2p9Z6Rvfetb3/rWt75Nq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+7BV7/6VfzkJz/Be9/73om5gom08SAjE22UGCptvO1btUp/pgqW4zH6YpWdwOhANNEDBnW4JgK+p8eo6ox0Oim6td9+5fg0dP+xMfPeVllJdrspmfXYY9NzSytakT1VwzRf+EJfG8hn//qvad859tjq+557LnDkkb0LiNH3PAhcZ3s8dued/Pf0HFuSM0LbMh5n5Ec/Ap73PGDFivG15/rrgXe9a4vKgKzsjNx444045JBDcMghhwAAzjrrLBxyyCH4SFZ2/Mknn1SOCQDssssuuOSSS3D55ZfjoIMOwmc/+1l861vfwvHHHz9BlzCBNpke6xVXAIsXA5deWm57DjEo44zkTTiT8aKOFxmZ6AGDhn0mAiqvstKy7y8NszzwQPG57MGa3ttrrineX1qnozVPqNmrU9ueSs6I/Sx82WZJApx0ki4m+VSyFSumdrLIxnR885u97W/XdaL9ebwLK0+If4t1RuhCZjyo0EknpXPJF74wvvYccQTwpS+lfLQtxCoXyjvmmGMgciBRTl31mGOOwT8mg6g40TaZyMiLX5w6OyecUA5S5lbSPmeJHq/bNaWyqU3GZDNeZGSiJz3ani98Afj858d3vLLIiBDAX/+q/44iE6VZtgw46KD8c9nOCP37He9ICa5vfnNxmzsd0+GTheSKnJEqAm9TbfZE4+MeXXGFzl44++yJOfcTTwDbblt8P8djjz4K7LknsOOOwP33T955JtPoe97pmOPreFFL31ho81S2FLNRz/HaRC2cb755Yo4zAbZFZtNMmRU94I0b0/TIovg9Z1UdHc4ZKXOMvNUAdUaKVg0jI+lkXpRO3AsyMpmrl4kegMoiI1/7GvB//2duSydIGiIrca540yb3WvJUbik/pt02BzzZr4sEoSbCWbX3m6zVqX0e6ZA/8ECaRSWvhWagTQSv5LLLUrn/k08e/7Hy7E9/St+tBx6YmD49FWJgNhJC/x5vv/CNhU8FZOSaa9JEh83Jc3rkkXTusu/JFuSw9Z0RatQZ4TrKmWc
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"[(1.05, 51.0, 0.0),\n",
" (0.3, 53.0, 200.0),\n",
" (0.5, 51.0, -200.0),\n",
" (0.5, 51.0, 0.0),\n",
" (0.4, 49.0, -200.0),\n",
" (0.5, 48.0, -100.0),\n",
" (0.5, 48.0, 0.0),\n",
" (0.5, 49.0, 100.0),\n",
" (0.5, 49.0, 0.0),\n",
" (0.5, 51.0, 200.0),\n",
" (0.5, 51.0, 0.0),\n",
" (0.025, 49.0, -200.0),\n",
" (0.5, 48.0, -100.0),\n",
" (0.5, 48.0, 0.0),\n",
" (0.5, 51.0, 300.0),\n",
" (0.5, 51.0, 0.0),\n",
" (0.475, 53.0, 200.0),\n",
" (0.5, 51.0, -200.0),\n",
" (0.5, 51.0, 0.0),\n",
" (0.5, 48.0, -300.0),\n",
" (0.5, 48.0, 0.0),\n",
" (0.5, 53.0, 500.0),\n",
" (0.5, 53.0, 0.0),\n",
" (0.5, 49.0, -400.0),\n",
" (0.5, 49.0, 0.0),\n",
" (0.125, 52.0, 300.0),\n",
" (0.1, 53.0, 100.0),\n",
" (0.425, 52.0, -100.0),\n",
" (0.475, 51.0, -100.0),\n",
" (0.1, 52.0, 100.0),\n",
" (0.175, 51.0, -100.0),\n",
" (0.5, 49.0, -200.0),\n",
" (0.5, 49.0, 0.0),\n",
" (0.5, 46.0, -300.0),\n",
" (0.5, 46.0, 0.0),\n",
" (0.5, 51.0, 500.0),\n",
" (0.5, 51.0, 0.0),\n",
" (0.05, 56.0, 500.0),\n",
" (0.5, 57.0, 100.0),\n",
" (0.5, 57.0, 0.0),\n",
" (0.5, 53.0, -400.0),\n",
" (0.5, 53.0, 0.0),\n",
" (0.075, 54.0, 100.0),\n",
" (0.375, 53.0, -100.0),\n",
" (0.025, 56.0, 300.0),\n",
" (0.5, 57.0, 100.0),\n",
" (0.5, 57.0, 0.0),\n",
" (0.5, 51.0, -600.0),\n",
" (0.5, 51.0, 0.0),\n",
" (0.5, 52.0, 100.0),\n",
" (0.5, 52.0, 0.0),\n",
" (0.5, 53.0, 100.0),\n",
" (0.5, 53.0, 0.0),\n",
" (0.5, 51.0, -200.0),\n",
" (0.5, 51.0, 0.0),\n",
" (0.5, 48.0, -300.0),\n",
" (0.5, 48.0, 0.0),\n",
" (0.5, 51.0, 300.0),\n",
" (0.5, 51.0, 0.0),\n",
" (0.45, 53.0, 200.0),\n",
" (0.125, 52.0, -100.0),\n",
" (0.45, 51.0, -100.0),\n",
" (0.025, 56.0, 500.0),\n",
" (0.2, 57.0, 100.0),\n",
" (0.5, 56.0, -100.0),\n",
" (0.5, 56.0, 0.0),\n",
" (0.5, 57.0, 100.0),\n",
" (0.5, 57.0, 0.0),\n",
" (0.3, 53.0, -400.0),\n",
" (0.5, 51.0, -200.0),\n",
" (0.5, 51.0, 0.0),\n",
" (0.125, 52.0, 100.0),\n",
" (0.5, 51.0, -100.0),\n",
" (0.5, 51.0, 0.0),\n",
" (0.425, 52.0, 100.0),\n",
" (0.5, 51.0, -100.0),\n",
" (0.5, 51.0, 0.0),\n",
" (0.5, 48.0, -300.0),\n",
" (0.5, 48.0, 0.0),\n",
" (0.5, 53.0, 500.0),\n",
" (0.5, 53.0, 0.0),\n",
" (0.5, 49.0, -400.0),\n",
" (0.5, 49.0, 0.0),\n",
" (0.5, 51.0, 200.0),\n",
" (0.5, 51.0, 0.0),\n",
" (0.5, 48.0, -300.0),\n",
" (0.5, 48.0, 0.0),\n",
" (0.075, 51.0, 300.0),\n",
" (0.15, 52.0, 100.0),\n",
" (0.5, 51.0, -100.0),\n",
" (0.5, 51.0, 0.0),\n",
" (0.2, 53.0, 200.0),\n",
" (0.5, 52.0, -100.0),\n",
" (0.5, 52.0, 0.0),\n",
" (0.05, 50.0, -200.0),\n",
" (0.5, 49.0, -100.0),\n",
" (0.5, 49.0, 0.0),\n",
" (0.5, 52.0, 300.0),\n",
" (0.5, 52.0, 0.0),\n",
" (0.5, 51.0, -100.0),\n",
" (0.5, 51.0, 0.0),\n",
" (0.45, 53.0, 200.0),\n",
" (0.5, 49.0, -400.0),\n",
" (0.5, 49.0, 0.0),\n",
" (0.5, 56.0, 700.0),\n",
" (0.5, 56.0, 0.0),\n",
" (0.075, 55.0, -100.0),\n",
" (0.3, 54.0, -100.0),\n",
" (0.2, 53.0, -100.0),\n",
" (0.35, 51.0, -200.0),\n",
" (0.25, 53.0, 200.0),\n",
" (0.5, 51.0, -200.0),\n",
" (0.5, 51.0, 0.0),\n",
" (0.45, 49.0, -200.0),\n",
" (0.3, 48.0, -100.0),\n",
" (0.2, 49.0, 100.0),\n",
" (0.5, 51.0, 200.0),\n",
" (0.5, 51.0, 0.0),\n",
" (0.3, 53.0, 200.0),\n",
" (0.05, 52.0, -100.0)]"
]
},
"execution_count": 786,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAh8AAAGdCAYAAACyzRGfAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy80BEi2AAAACXBIWXMAAA9hAAAPYQGoP6dpAACsbUlEQVR4nO2dd5xcVfn/P+fOzM723fTegJBKQmghdAkiRURAwQiCCAiIVEHAFvgqBisqXRTBn4WmVAHpASSEEFoCJKSRXkjbvrMzc8/vj9vOmbl1Zu7M7M7zfr2Snbn1zC3nPOepjHPOQRAEQRAEUSSUUjeAIAiCIIjKgoQPgiAIgiCKCgkfBEEQBEEUFRI+CIIgCIIoKiR8EARBEARRVEj4IAiCIAiiqJDwQRAEQRBEUSHhgyAIgiCIohItdQMyUVUVmzZtQkNDAxhjpW4OQRAEQRA+4Jyjra0Nw4cPh6K46zbKTvjYtGkTRo0aVepmEARBEASRA+vXr8fIkSNdtyk74aOhoQGA1vjGxsYSt4YgCIIgCD+0trZi1KhR5jjuRtkJH4appbGxkYQPgiAIguhl+HGZIIdTgiAIgiCKCgkfBEEQBEEUFRI+CIIgCIIoKiR8EARBEARRVEj4IAiCIAiiqJDwQRAEQRBEUSHhgyAIgiCI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OWMlM5b2RvAFYDiP+LCclfgYEoMUGLCygI0sBdiaJO43+soXjOCyIFY1Br+qqqiUJS49oUzpNMk0IZNXsJHe3s7Vq1ahWHDhmHs2LEYPnw4li+XZy2ffPIJxowZk1cjCaIcYeCAHslljpk+wj7TovA
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import librosa \n",
"import numpy as np\n",
"from scipy.signal import savgol_filter\n",
"import matplotlib.pyplot as plt\n",
"\n",
"def filter_data(time, frequency, confidence):\n",
" data = tuple(data for data in zip(list(time), list(frequency), list(confidence)) if data[2] > 0.1)\n",
" dev = 0\n",
" dur = 0\n",
" for (p1, p2) in pairwise(data):\n",
" t_diff = (p2[0] - p1[0])\n",
" dur = round(dur + (p2[0] - p1[0]), 2)\n",
" if(True):\n",
" freq1, conf1 = p1[1:3]\n",
" midi1 = librosa.hz_to_midi(freq1)\n",
" r_midi1 = np.around(midi1)\n",
" freq2, conf2 = p2[1:3]\n",
" midi2 = librosa.hz_to_midi(freq2)\n",
" r_midi2 = np.around(midi2)\n",
" #if(r_midi2 != r_midi1):\n",
" if(True):\n",
" yield(dur, freq1, conf1, midi1, r_midi1, dev * 100)\n",
" dev = r_midi2 - r_midi1\n",
" dur = 0\n",
" \n",
"def filter_data(time, frequency, confidence):\n",
" data = tuple(data for data in zip(list(time), list(frequency), list(confidence)) if data[2] > 0.9)\n",
" #durs = tuple(round(y[0]-x[0], 2) for (x, y) in pairwise(data)) + ((1,))\n",
" #data = tuple((d,) + data[i][1:] for i, d in enumerate(durs) if d > 0.01)\n",
" #print(data)\n",
" m, rm = [librosa.hz_to_midi(data[0][1]), np.around(librosa.hz_to_midi(data[0][1]))]\n",
" yield(data[0] + (m, rm, 0,))\n",
" for i, [t, f, c] in enumerate(data):\n",
" nm, nrm = [librosa.hz_to_midi(f), np.around(librosa.hz_to_midi(f))]\n",
" if((0 < i < len(data) - 1) and (nrm != rm)):\n",
" m, rm, d = [nm, nrm, nrm - rm]\n",
" yield(t, f, c, m, rm, d * 100)\n",
" \n",
"t_min = 50\n",
"t_max = 600\n",
"f_time, f_frequency, f_confidence = [time[t_min:], frequency[t_min:], confidence[t_min:]]\n",
"s_confidence = savgol_filter(f_confidence, 30, 4)\n",
"plt.plot(f_time, f_confidence)\n",
"plt.plot(f_time, s_confidence, color='red')\n",
"plt.show()\n",
"\n",
"def filter_midi(f_time, f_frequency, s_confidence):\n",
" last_confident_value = 51\n",
" for t, f, c in zip(f_time, f_frequency, s_confidence):\n",
" if c > 0.9:\n",
" last_confident_value = librosa.hz_to_midi(f)\n",
" yield last_confident_value\n",
" \n",
"#m_time, midi = zip(*[[t, librosa.hz_to_midi(f)] for t, f, c in zip(f_time, f_frequency, s_confidence) if c > 0.0])\n",
"#nr_midi = np.interp(f_time, m_time, midi)\n",
"#r_midi = np.interp(f_time, m_time, np.around(midi))\n",
"#midi\n",
"\n",
"midi = list(filter_midi(f_time, f_frequency, s_confidence))\n",
"\n",
"def snap_midi(midi):\n",
" r_midi = np.around(midi)\n",
" s_midi = np.around(savgol_filter(midi, 30, 5))\n",
" last_val = s_midi[0]\n",
" for i, (r1, r2) in enumerate(pairwise(r_midi)):\n",
" if r1 != r2:\n",
" last_val = s_midi[i+5]\n",
" yield last_val\n",
" yield last_val\n",
"\n",
"def filter_midi(midi):\n",
" last_val = midi[0]\n",
" dur = 1\n",
" dev = 0.0\n",
" first_flag = True\n",
" for i, (m1, m2) in enumerate(pairwise(midi)):\n",
" if m1 != m2:\n",
" #yield (dur / 40, last_val, dev * 100)\n",
" if first_flag or dur < 20:\n",
" yield (dur / 40, last_val, dev * 100)\n",
" first_flag = False\n",
" else:\n",
" size = int(dur / 20)\n",
" yield (20 / 40.0, last_val, dev * 100)\n",
" yield (20 / 40.0, last_val, 0.0)\n",
" last_val = m2\n",
" dur = 1\n",
" dev = m2 - m1\n",
" else:\n",
" dur += 1\n",
" \n",
" \n",
"\n",
"plt.plot(f_time, midi)\n",
"plt.plot(f_time, savgol_filter(midi, 30, 5), color='yellow')\n",
"plt.plot(f_time, np.around(savgol_filter(midi, 30, 5)), color='red')\n",
"plt.plot(f_time, np.around(midi), color='green')\n",
"plt.plot(f_time, list(snap_midi(midi)), color='blue')\n",
"#plt.show()\n",
"\n",
"target_melody_data = list(filter_midi(list(snap_midi(midi))))\n",
"target_melody_data\n",
"\n",
"#target_melody_data = list(filter_data(time, frequency, confidence))\n",
"#target_melody_data\n",
"#durs = tuple(round(y[0]-x[0], 2) for (x, y) in pairwise(target_melody_data)) + ((1,))\n",
"#print(durs)\n",
"#target_melody_data = tuple((d,) + target_melody_data[i][1:] for i, d in enumerate(durs) if d > 0.1)\n",
"#list(target_melody_data)"
]
},
{
"cell_type": "code",
"execution_count": 750,
"id": "aa9c7525-4d17-4b87-9d92-60096867cd49",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"40.0"
]
},
"execution_count": 750,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1/0.025"
]
},
{
"cell_type": "code",
"execution_count": 822,
"id": "b0d17cc2-a181-4212-aba5-72b90cab2a84",
"metadata": {},
"outputs": [],
"source": [
"from random import choice, choices\n",
"\n",
"# This is for the beginning / breysheet\n",
"def stochastic_hamiltonian(graph):\n",
"\n",
" #try making this omit the moving voice\n",
" def movement_size_weights(edges):\n",
" \n",
" def max_cent_diff(edge):\n",
" res = max([abs(v) for val in edge[2]['movements'].values() if (v:=val['cent_difference']) is not None])\n",
" return res\n",
" \n",
" def min_cent_diff(edge):\n",
" res = [abs(v) for val in edge[2]['movements'].values() if (v:=val['cent_difference']) is not None]\n",
" res.remove(0)\n",
" return min(res)\n",
" \n",
" for e in edges:\n",
" yield 200 if ((max_cent_diff(e) < 100) and (min_cent_diff(e)) >= 0) else 1\n",
"\n",
" def hamiltonian_weights(edges):\n",
" for e in edges:\n",
" yield 10 if e[1] not in [path_edge[0] for path_edge in path] else 1 / graph.nodes[e[1]]['count']\n",
" \n",
" def contrary_motion_weights(edges):\n",
"\n",
" def is_contrary(edge):\n",
" cent_diffs = [v for val in edge[2]['movements'].values() if (v:=val['cent_difference']) is not None]\n",
" cent_diffs.sort()\n",
" return (cent_diffs[0] < 0) and (cent_diffs[1] == 0) and (cent_diffs[2] > 0)\n",
"\n",
" for e in edges:\n",
" yield 2 if is_contrary(e) else 1\n",
" \n",
" def is_directly_tunable_weights(edges):\n",
" for e in edges:\n",
" yield 10 if e[2]['is_directly_tunable'] else 1\n",
"\n",
" def symdiff_weights(edges):\n",
" for e in edges:\n",
" yield 1000 if e[2]['symmetric_difference'] == 2 else 1\n",
"\n",
" def transposition_weight(edges):\n",
" for e in edges:\n",
" yield 100 if 0 <= hs_array_to_cents(e[2]['transposition']) < 100 else 1\n",
"\n",
" def is_sustained_voice(edges, voice):\n",
" \n",
" def is_sustained(edge):\n",
" source = list(edge[0])\n",
" ordered_source = sorted(source, key=hs_array_to_fr) \n",
" destination = [transpose_pitch(edge[2]['movements'][p]['destination'], edge[2]['transposition']) for p in source]\n",
" ordered_destination = sorted(destination, key=hs_array_to_fr)\n",
" return ordered_source[voice] == ordered_destination[voice]\n",
"\n",
" for e in edges:\n",
" yield 10 if is_sustained(e) else 1\n",
"\n",
" def voice_crossing_weights(edges):\n",
" \n",
" def has_voice_crossing(edge):\n",
" source = list(edge[0])\n",
" ordered_source = sorted(source, key=hs_array_to_fr) \n",
" source_order = [ordered_source.index(p) for p in source]\n",
" destination = [transpose_pitch(edge[2]['movements'][p]['destination'], edge[2]['transposition']) for p in source]\n",
" ordered_destination = sorted(destination, key=hs_array_to_fr)\n",
" destination_order = [ordered_destination.index(p) for p in destination]\n",
" return source_order != destination_order\n",
"\n",
" for e in edges:\n",
" yield 10 if not has_voice_crossing(e) else 0\n",
"\n",
" def is_bass_rooted(chord):\n",
" return max([sum(abs(p) for p in collapse_pitch(pitch_difference(chord[0], p))) for p in chord[1:]]) == 1\n",
"\n",
" def target_melody_weights(edges, target, c_devs, voice):\n",
"\n",
" def target_weight(edge, target, c_devs, voice):\n",
" candidate_diffs = []\n",
" for idx, dev in enumerate(c_devs):\n",
" if(idx == 2):\n",
" source = list(edge[0])\n",
" ordered_source = sorted(source, key=hs_array_to_fr) \n",
" candidate_diff = edge[2]['movements'][ordered_source[idx]]['cent_difference']\n",
" candidate_diffs += [abs(dev + candidate_diff - target)]\n",
" #return 1/pow(1.1, min(candidate_diffs))\n",
" return 1 if min(candidate_diffs) < 40 else 1/pow(1.1, min(candidate_diffs))\n",
" \n",
" for e in edges:\n",
" yield target_weight(e, target, c_devs, voice)\n",
" \n",
" check_graph = graph.copy()\n",
" next_node = choice(list(graph.nodes()))\n",
" check_graph.remove_node(next_node)\n",
" for node in graph.nodes(data=True):\n",
" node[1]['count'] = 1\n",
" path = []\n",
" s_next_node = sorted(next_node, key=hs_array_to_fr)\n",
" c_devs = (cent_difference(s_next_node[2], s_next_node[0]), cent_difference(s_next_node[2], s_next_node[1]), 0,)\n",
" #c_devs = (0, cent_difference(s_next_node[0], s_next_node[1]), cent_difference(s_next_node[0], s_next_node[2]),)\n",
" print(c_devs)\n",
" while (nx.number_of_nodes(check_graph) > 0) and (len(path) < len(target_melody_data)-1):\n",
" out_edges = list(graph.out_edges(next_node, data=True))\n",
" factors = [\n",
" movement_size_weights(out_edges), \n",
" hamiltonian_weights(out_edges), \n",
" contrary_motion_weights(out_edges), \n",
" is_directly_tunable_weights(out_edges),\n",
" voice_crossing_weights(out_edges),\n",
" #transposition_weight(out_edges),\n",
" #is_sustained_voice(out_edges, 0),\n",
" target_melody_weights(out_edges, target_melody_data[len(path)+1][-1], c_devs, 2),\n",
" symdiff_weights(out_edges)\n",
" ]\n",
" weights = [prod(a) for a in zip(*factors)]\n",
" edge = choices(out_edges, weights=weights)[0]\n",
" next_node = edge[1]\n",
" node[1]['count'] += 1\n",
" path.append(edge)\n",
" s_chord = tuple(sorted(edge[0], key=hs_array_to_fr))\n",
" c_devs = tuple(c_devs[pdx] + edge[2]['movements'][pitch]['cent_difference'] - target_melody_data[len(path)][-1] for pdx, pitch in enumerate(s_chord))\n",
" print(c_devs)\n",
" if next_node in check_graph.nodes:\n",
" check_graph.remove_node(next_node)\n",
" return path"
]
},
{
"cell_type": "code",
"execution_count": 824,
"id": "a8592bc9-7e9e-4b6a-9eaa-4c4e3b69ce91",
"metadata": {},
"outputs": [],
"source": [
"dims = (2, 3, 5, 7, 11, 13)\n",
"root = (0, 0, 0, 0, 0, 0)\n",
"chord = (root,)\n",
"chord_set = chords(chord, root, 3, 3)\n",
"graph = generate_graph(chord_set, 4, 4, 3)"
]
},
{
"cell_type": "code",
"execution_count": 825,
"id": "be01e4ae-e629-42ff-9d95-f77d510c13bf",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(-359.4723382306895, -231.17409353087487, 0)\n",
"(-929.2190926654878, -431.17409353087487, -44.86037966604022)\n",
"(-729.2190926654878, -177.9011503007306, -27.26409180010012)\n",
"(-826.5832079359744, -177.9011503007306, -12.896921800808741)\n",
"(-626.5832079359744, -240.26949407113915, -9.09540054026445)\n",
"(-526.5832079359744, -206.43935910209234, 24.734734428782644)\n",
"(-526.5832079359744, -167.11086970528447, -28.538208801361634)\n",
"(-1107.6385314745955, -267.1108697052845, -35.936776174409715)\n",
"(-1004.7626826435351, -587.2547185391661, -35.936776174409715)\n",
"(-1018.4288120700417, -787.2547185391661, 26.431567595998786)\n",
"(-942.3943388731266, -814.0960941733115, 26.431567595998786)\n",
"(-742.3943388731266, -511.22024534225113, -13.175246207638565)\n",
"(-642.3943388731266, -353.1846194685721, 6.287718762117265)\n",
"(-712.6569576992622, -353.1846194685721, 33.12909439626287)\n",
"(-1151.2296186031854, -653.1846194685721, -4.5025618333286275)\n",
"(-1107.138446562843, -555.8205041980855, -4.5025618333286275)\n",
"(-986.9945977289614, -755.8205041980855, -18.168691259835214)\n",
"(-1053.8655033326986, -555.8205041980855, -4.5025618333284)\n",
"(-973.3284683024546, -613.8561300717643, -4.5025618333284)\n",
"(-673.3284683024546, -122.01052593769703, -24.64641066720992)\n",
"(-575.9643530319679, -189.65063916713234, -24.64641066720992)\n",
"(-920.8247326980081, -689.6506391671323, 12.304361698255548)\n",
"(-828.2233000710562, -636.3776959369881, 12.304361698255548)\n",
"(-885.0597535722322, -236.37769593698806, -5.203602406112964)\n",
"(-974.029508875239, -160.34322274007286, -5.203602406112964)\n",
"(-819.8155609707633, -460.34322274007286, 37.701776394539706)\n",
"(-946.6569366049085, -560.3432227400729, -9.025280375316015)\n",
"(-846.6569366049085, -392.4429887004319, -6.129274835597016)\n",
"(-651.915326931122, -292.4429887004319, -3.233269295877676)\n",
"(-778.7567025652673, -392.4429887004319, -32.97065046974228)\n",
"(-678.7567025652673, -30.07464493002334, 23.19829830012077)\n",
"(-478.75670256526735, -247.58260903439174, 19.28829656934579)\n",
"(-532.0296457954114, -340.18404166134366, 19.28829656934579)\n",
"(-538.2290407959566, -40.184041661343656, 13.088901568800509)\n",
"(-373.2248122960349, -218.08519196207467, 13.088901568800509)\n",
"(-873.2248122960349, -59.53852616086908, -32.6971505267237)\n",
"(-846.3834366618896, -263.8712440575988, -32.6971505267237)\n",
"(-1123.3435822882886, -763.8712440575988, 49.815042077566545)\n",
"(-1095.045337588474, -863.8712440575988, -23.343582288288076)\n",
"(-672.0256399235316, -409.65729615312296, -23.343582288288076)\n",
"(-369.12963438381234, -9.657296153122957, 17.184079481022536)\n",
"(-823.3435822882882, -213.99001404985268, 17.184079481022536)\n",
"(-923.3435822882882, -372.02563992353157, 14.288073941303537)\n",
"(-823.3435822882882, -174.66152465304492, 17.184079481022536)\n",
"(-972.7065237876574, -474.6615246530449, 23.383474481567873)\n",
"(-1125.9794670178014, -574.6615246530449, -23.343582288287962)\n",
"(-1072.7065237876573, -521.3885814229009, -23.343582288287962)\n",
"(-472.70652378765726, -205.83561818392036, 25.338475346955192)\n",
"(-525.9794670178013, -166.50712878711244, 25.338475346955192)\n",
"(-625.9794670178013, -239.6657531529671, 22.702590617441842)\n",
"(-599.1380913836559, -239.6657531529671, -8.491659622091902)\n",
"(-725.9794670178012, -339.6657531529671, -24.024466152413993)\n",
"(-672.706523787657, -255.19855968328955, -24.024466152413993)\n",
"(-472.70652378765703, -334.13386288373465, 25.33847534695508)\n",
"(-525.9794670178011, -360.97523851788, 25.33847534695508)\n",
"(-874.6615246530446, -60.97523851787997, -34.13386288373431)\n",
"(-1002.9597693528591, -393.60620111442415, -34.13386288373431)\n",
"(-1053.0785393451129, -693.6062011144242, 8.348799750963735)\n",
"(-960.4771067181607, -640.3332578842799, 8.348799750963735)\n",
"(-1160.4771067181607, -346.79082058299616, 12.681517647693681)\n",
"(-798.1087629477522, -246.79082058299616, -15.616727052120837)\n",
"(-698.1087629477522, -200.06376381314044, 3.8462379176348804)\n",
"(-1086.3774776779744, -700.0637638131404, 1.8912370522474475)\n",
"(-1186.3774776779744, -826.9051394472858, -13.218853312119904)\n",
"(-993.7760450510223, -726.9051394472858, -24.950138581897704)\n",
"(-904.8062897480155, -673.6321962171418, -24.950138581897704)\n",
"(-1004.8062897480155, -849.666669414057, -35.980383278891054)\n",
"(-626.626815040455, -395.4527215095811, -35.980383278891054)\n",
"(-354.9250597402696, 4.547278490418876, 31.388654124564994)\n",
"(-470.5663467408225, -319.92928824019305, 231.388654124565)\n",
"(-470.5663467408225, -239.39225320994865, 27.478652393790014)\n",
"(-570.5663467408225, -378.72074260675635, -19.248404376065707)\n",
"(-470.5663467408225, -239.39225320994865, 27.478652393790014)\n",
"(-523.8392899709665, -292.6651964400927, 27.478652393790014)\n",
"(-708.3064834406446, -392.6651964400927, -6.351482575257307)\n",
"(-608.3064834406446, -377.1323899097706, 9.181323955064897)\n",
"(-639.5007336801782, -350.2910142756253, 9.181323955064897)\n",
"(-339.5007336801782, -108.3266401493043, 19.971604550511017)\n",
"(-366.34210931432364, -135.16801578344962, 19.971604550511017)\n",
"(-866.3421093143236, -164.38710844893717, -25.814447545013195)\n",
"(-839.5007336801783, -256.98854107588943, -25.814447545013195)\n",
"(-439.5007336801783, -208.3266401493047, 22.847453381571427)\n",
"(-817.6802083877388, -528.4704889831863, 22.847453381571427)\n",
"(-1087.9428272138744, -728.4704889831863, -26.5154881177981)\n",
"(-1034.6698839837302, -675.197545753042, -26.5154881177981)\n",
"(-606.3716392839157, -375.19754575304205, 11.116168111793286)\n",
"(-690.8388327535935, -348.3561701188967, 11.116168111793286)\n",
"(-990.8388327535935, -439.520890388838, -22.012926284469245)\n",
"(-1188.2029480240801, -539.520890388838, -41.47589125422485)\n",
"(-1088.2029480240801, -386.24794715869393, -26.775608928003635)\n",
"(-995.601515397128, -413.0893227928393, -26.775608928003635)\n",
"(-999.4030366576725, -613.0893227928393, 35.59273484240509)\n",
"(-872.5616610235271, -513.0893227928393, 38.22861957191844)\n",
"(-775.4576665632463, -544.283573032373, 38.22861957191844)\n",
"(-575.4576665632463, -189.14395269841322, -24.13972419849017)\n",
"(-475.45766656324633, -115.98532833255854, 22.58733257136555)\n",
"(-363.7263812934686, -336.885005659325, 22.58733257136555)\n",
"(-663.7263812934686, -432.55228776259537, -15.044323658225892)\n",
"(-716.9993245236128, -513.0893227928398, -15.044323658225892)\n",
"(-616.9993245236128, -466.36226602298404, 31.682733111630057)\n",
"(-670.2722677537569, -439.0981742228837, 31.682733111630057)\n",
"(-870.2722677537569, -372.22726861914623, -29.744605984446764)\n",
"(-470.27226775375686, -239.0981742228837, -7.924080692007408)\n",
"(-559.2420230567636, -199.7696848260759, -7.924080692007408)\n",
"(-926.6110604602196, -899.769684826076, -86.08339869090946)\n",
"(-926.6110604602196, -224.6560595948339, 6.5180339360422295)\n",
"(-826.6110604602196, -217.2574922217857, 13.916601309090538)\n",
"(-668.5754345865408, -117.2574922217857, 33.379566278846255)\n",
"(-665.9395498570275, -17.257492221785697, 36.015451008359605)\n",
"(-465.9395498570275, -79.62583599219425, 32.105449277584626)\n",
"(-665.9395498570275, -306.4672116263396, 36.015451008359605)\n",
"(-465.9395498570275, -234.7654563261541, 32.105449277584626)\n",
"(-519.2124930871717, -199.06864425329172, 32.105449277584626)\n",
"(-812.7549303884553, 0.9313557467082774, 27.772731380854907)\n",
"(-712.7549303884553, -161.43698802370034, -64.07287275321232)\n",
"(-812.7549303884553, -453.28259215776757, 27.772731380854907)\n",
"(-1012.7549303884553, -199.06864425329172, 32.10544927758451)\n",
"(-936.7204571915402, -288.0383995562985, 32.10544927758451)\n",
"(-874.3521134211317, -488.0383995562985, 10.006599578315445)\n",
"(-774.3521134211317, -346.9795411508029, 39.33417271403346)\n"
]
}
],
"source": [
"path = stochastic_hamiltonian(graph)\n",
"#durs = tuple(round(y[0]-x[0], 2) for (x, y) in pairwise(target_melody_data)) + ((1,))\n",
"durs = tuple(d[0] for d in target_melody_data)\n",
"path_to_chords(path, root)\n",
"write_chord_sequence(list(zip(durs, path_to_chords(path, root))))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python (compact sets)",
"language": "python",
"name": "compact_sets"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}