@@ -27,16 +27,21 @@ func midiLoad(f string) []int {
2727 return notes
2828}
2929
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
3032type 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
3138type Markov map [int ][]probability
3239
3340// Returns a Markov chain
3441func NewMarkov (f string ) Markov {
3542 notes := midiLoad (f )
3643 chain := make (Transitions )
3744
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
4045 previous := - 1
4146 for _ , n := range notes {
4247 if previous != - 1 {
@@ -66,6 +71,8 @@ type probability struct {
6671
6772const probMax uint32 = (1 << 32 ) - 1
6873
74+ // Parses the transitions for a state and generates
75+ // the probability itervals associated with it
6976func getProbabilities (probs map [int ]uint32 ) []probability {
7077 var max uint32
7178 var keys []int
@@ -104,6 +111,9 @@ func getProbabilities(probs map[int]uint32) []probability {
104111 return pList
105112}
106113
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.
107117func (m Markov ) Get (prev int , num uint32 ) int {
108118 chain := m [prev ]
109119 if chain == nil {
@@ -119,6 +129,8 @@ func (m Markov) Get(prev int, num uint32) int {
119129 return m .Rand (num )
120130}
121131
132+ // Returns a random state from the Markov chain, given num
133+ // a random integer
122134func (m Markov ) Rand (num uint32 ) int {
123135 // if the chain is not initialized return a valid note
124136 if m == nil {
0 commit comments