-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
velocity_map.go
79 lines (71 loc) · 1.78 KB
/
velocity_map.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
72
73
74
75
76
77
78
79
package op
import (
"bytes"
"fmt"
"github.com/emicklei/melrose/core"
)
type VelocityMap struct {
Target core.Sequenceable
IndexVelocities []int2int // one-based
}
func NewVelocityMap(target core.Sequenceable, indices string) VelocityMap {
return VelocityMap{
Target: target,
IndexVelocities: parseIndexOffsets(indices),
}
}
func (v VelocityMap) S() core.Sequence {
return core.Sequence{Notes: v.Notes()}
}
func (v VelocityMap) Notes() [][]core.Note {
source := v.Target.S().Notes
target := [][]core.Note{}
for _, entry := range v.IndexVelocities {
if entry.from <= 0 || entry.from > len(source) {
// invalid offset, skip
continue
}
eachGroup := source[entry.from-1] // from is one-based
if entry.to == 0 {
// no offset, use as is
target = append(target, eachGroup)
continue
}
newGroup := []core.Note{}
for _, eachNote := range eachGroup {
newGroup = append(newGroup, eachNote.WithVelocity(entry.to))
}
target = append(target, newGroup)
}
return target
}
func (v VelocityMap) Storex() string {
s, ok := v.Target.(core.Storable)
if !ok {
return ""
}
var b bytes.Buffer
fmt.Fprintf(&b, "velocitymap('")
for i, each := range v.IndexVelocities {
if i > 0 {
fmt.Fprintf(&b, ",")
}
fmt.Fprintf(&b, "%d:%d", each.from, each.to)
}
fmt.Fprintf(&b, "',%s", s.Storex())
fmt.Fprintf(&b, ")")
return b.String()
}
// Replaced is part of Replaceable
func (v VelocityMap) Replaced(from, to core.Sequenceable) core.Sequenceable {
if core.IsIdenticalTo(v, from) {
return to
}
if core.IsIdenticalTo(v.Target, from) {
return OctaveMap{Target: to, IndexOffsets: v.IndexVelocities}
}
if rep, ok := v.Target.(core.Replaceable); ok {
return VelocityMap{Target: rep.Replaced(from, to), IndexVelocities: v.IndexVelocities}
}
return v
}