-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.go
71 lines (55 loc) · 1.35 KB
/
converter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package sonic
import (
"hash/fnv"
"math"
"github.com/mloncode/sonic/src/sound"
)
const logarithmic = false
func Convert(scale sound.Scale, nodes []sonicNode) []sound.Note {
var max uint32
for _, n := range nodes {
if n.Lenght > max {
max = n.Lenght
}
}
notes := make([]sound.Note, len(nodes))
hash := fnv.New32a()
for i, n := range nodes {
duration := float64(n.Lenght)
duration = toLog(float64(max), duration) * 0.25
hash.Reset()
hash.Write([]byte(n.Token))
note := scale.Get(hash.Sum32())
notes[i] = sound.Note{Note: int64(note), Duration: duration}
}
return notes
}
func ConvertMarkov(m sound.Markov, nodes []sonicNode) []sound.Note {
var max uint32
for _, n := range nodes {
if n.Lenght > max {
max = n.Lenght
}
}
notes := make([]sound.Note, len(nodes))
hash := fnv.New32a()
last := m.Rand(uint32(len(nodes)))
for i, n := range nodes {
var duration float64
if logarithmic {
duration = float64(n.Lenght)
duration = toLog(float64(max), duration) * 0.25
} else {
duration = float64(n.Lenght) / float64(max)
}
hash.Reset()
hash.Write([]byte(n.Token))
note := m.Get(last, hash.Sum32())
last = note
notes[i] = sound.Note{Note: int64(note), Duration: duration}
}
return notes
}
func toLog(max, value float64) float64 {
return (math.Log(value) - math.Log(0.1)) / (math.Log(max) - math.Log(0.1))
}