File tree Expand file tree Collapse file tree 1 file changed +11
-9
lines changed Expand file tree Collapse file tree 1 file changed +11
-9
lines changed Original file line number Diff line number Diff line change @@ -3,7 +3,6 @@ package sound
3
3
import (
4
4
"os"
5
5
"sort"
6
-
7
6
"gitlab.com/gomidi/midi/mid"
8
7
)
9
8
@@ -28,16 +27,19 @@ func midiLoad(f string) []int {
28
27
return notes
29
28
}
30
29
31
- type markov map [int ]map [int ]uint32
30
+ type Transitions map [int ]map [int ]uint32
32
31
type Markov map [int ][]probability
33
32
33
+ // Returns a Markov chain
34
34
func NewMarkov (f string ) Markov {
35
35
notes := midiLoad (f )
36
- chain := make (markov )
36
+ chain := make (Transitions )
37
37
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
39
41
for _ , n := range notes {
40
- if previous != 0 {
42
+ if previous != - 1 {
41
43
if chain [previous ] == nil {
42
44
chain [previous ] = make (map [int ]uint32 )
43
45
}
@@ -85,12 +87,12 @@ func getProbabilities(probs map[int]uint32) []probability {
85
87
var pList []probability
86
88
var previous uint32
87
89
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 {
91
93
t = probMax
92
94
} else {
93
- t = uint32 ( float64 ( t ) * scale ) + previous
95
+ t += previous
94
96
}
95
97
96
98
previous = t
You can’t perform that action at this time.
0 commit comments