@@ -27,16 +27,21 @@ func midiLoad(f string) []int {
27
27
return notes
28
28
}
29
29
30
+ // Transitions will store for a state (note identifier in midi) the
31
+ // number of transitions to each other note, as observed in the midi file
30
32
type Transitions map [int ]map [int ]uint32
33
+ // A Markov chain is represented as a hash map with assigns state i -> [probability]
34
+ // where the probability contains the state we can transition to as Key and an integer Value.
35
+ // The array of probabilities is ordered increasingly by Value, where each value
36
+ // represents an interval ]Value for prev state, Value] where the length of that
37
+ // interval is proportional to the probability of going from i to Key
31
38
type Markov map [int ][]probability
32
39
33
40
// Returns a Markov chain
34
41
func NewMarkov (f string ) Markov {
35
42
notes := midiLoad (f )
36
43
chain := make (Transitions )
37
44
38
- // Parse the transitions, i.e. store for a state (note identifier in midi)
39
- // the number of transitions to each other note, as observed in the midi file
40
45
previous := - 1
41
46
for _ , n := range notes {
42
47
if previous != - 1 {
@@ -66,6 +71,8 @@ type probability struct {
66
71
67
72
const probMax uint32 = (1 << 32 ) - 1
68
73
74
+ // Parses the transitions for a state and generates
75
+ // the probability itervals associated with it
69
76
func getProbabilities (probs map [int ]uint32 ) []probability {
70
77
var max uint32
71
78
var keys []int
@@ -104,6 +111,9 @@ func getProbabilities(probs map[int]uint32) []probability {
104
111
return pList
105
112
}
106
113
114
+ // Receives the initial state (as prev) and a random number, num,
115
+ // in [0, max uint32]. We compute the state whose probability
116
+ // interval contains num.
107
117
func (m Markov ) Get (prev int , num uint32 ) int {
108
118
chain := m [prev ]
109
119
if chain == nil {
@@ -119,6 +129,8 @@ func (m Markov) Get(prev int, num uint32) int {
119
129
return m .Rand (num )
120
130
}
121
131
132
+ // Returns a random state from the Markov chain, given num
133
+ // a random integer
122
134
func (m Markov ) Rand (num uint32 ) int {
123
135
// if the chain is not initialized return a valid note
124
136
if m == nil {
0 commit comments