File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -3,7 +3,6 @@ package sound
33import (
44 "os"
55 "sort"
6-
76 "gitlab.com/gomidi/midi/mid"
87)
98
@@ -28,16 +27,19 @@ func midiLoad(f string) []int {
2827 return notes
2928}
3029
31- type markov map [int ]map [int ]uint32
30+ type Transitions map [int ]map [int ]uint32
3231type Markov map [int ][]probability
3332
33+ // Returns a Markov chain
3434func NewMarkov (f string ) Markov {
3535 notes := midiLoad (f )
36- chain := make (markov )
36+ chain := make (Transitions )
3737
38- previous := 0
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+ previous := - 1
3941 for _ , n := range notes {
40- if previous != 0 {
42+ if previous != - 1 {
4143 if chain [previous ] == nil {
4244 chain [previous ] = make (map [int ]uint32 )
4345 }
@@ -85,12 +87,12 @@ func getProbabilities(probs map[int]uint32) []probability {
8587 var pList []probability
8688 var previous uint32
8789 for _ , k := range keys {
88- t := probs [k ]
89- if t == max {
90- // maximum probability is manually set to overcome rounding errors
90+ t := uint32 ( float64 ( probs [k ]) * scale )
91+ // take care of overflow
92+ if t > probMax - previous {
9193 t = probMax
9294 } else {
93- t = uint32 ( float64 ( t ) * scale ) + previous
95+ t += previous
9496 }
9597
9698 previous = t
You can’t perform that action at this time.
0 commit comments