Skip to content

Commit c6277d9

Browse files
committed
Fixes note generation (0 is a note, cannot be used as flag)
Signed-off-by: ncordon <nacho.cordon.castillo@gmail.com>
1 parent 973b44f commit c6277d9

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/sound/markov.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package sound
33
import (
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
3231
type Markov map[int][]probability
3332

33+
// Returns a Markov chain
3434
func 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

0 commit comments

Comments
 (0)