# Graph_to_sounds_garbage.py -rw-r--r-- 1.5 KiB View raw
                                                                                
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
# NOTE this is hot garbage ripped from a Jupyter notebook. .. you'll have to work it a bit to make it work.
# Also need a midi player, and to make a .dot file or modify to construct it here.
# (noted below how i played the midi)

def midi_graph_shared(MyMIDI, g, start=None, max_t=1000, duration=1, volume=100, track=0, channel=0):
    cur = random.choice(list(g.iternodes())) if start is None is None else start
    cnts = dict()
    opts = list(g.iterneighbors(cur))  # NOTE: assuming its constant.
    for t in range(max_t):
        cnt = cnts.get(cur, 0)
        cnts[cur] = cnt + 1
        cur = opts[(cnt - 1)%len(opts)]
        next_opts = list(g.iterneighbors(cur))
        n_overlap = len(set(next_opts).intersection(opts))
        MyMIDI.addNote(track, channel, 52 + len(opts) + 8*n_overlap, t, duration, volume)
        opts = next_opts

from midiutil import MIDIFile

MyMIDI = MIDIFile(1)
MyMIDI.addTempo(0, 0, 240)

g = pygraphviz.AGraph(filename="third.dot")
midi_graph(MyMIDI, g)

with open("graph_third.mid", "wb") as output_file:
    MyMIDI.writeFile(output_file)

basic_draw(g)

import graphviz

def basic_draw(g):  # Jupyter will draw those from this package.
    dg = graphviz.Graph()
    for e in g.iteredges():
        dg.edge(*e)
    return dg

# Played the midi with:
# Ran fluidsynth
# `fluidsynth -is -a pulseaudio -m alsa_seq -r 48000 "$SOME_SOUNDFONT.sf"`
# `/usr/share/soundfonts/FluidR3_GM.sf2` was on my disc, not ideal.
# Look for ports with `aplaymidi -l`
# `aplaymidi --port $PORT $FILE.mid`