-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
notemap.go
183 lines (171 loc) · 3.99 KB
/
notemap.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package op
import (
"bytes"
"errors"
"fmt"
"strings"
"github.com/emicklei/melrose/core"
"github.com/emicklei/melrose/notify"
)
const (
formatDotAndBangs = iota
formatNumbers
)
type NoteMap struct {
Target core.HasValue
Indices []int
indicesFormat int
maxIndex int
}
// NewNoteMap returns a NoteMap that creates a sequence from occurrences of a note.
// The format of indices can be one of:
// 1 2 4 ; each number is an index in the sequence where the note is present; rest notes are placed in the gaps.
// ! . ! ; each dot is a rest, each exclamation mark is a presence of a note.
func NewNoteMap(indices string, note core.HasValue) (NoteMap, error) {
idx := []int{}
// check for dots and bangs first
var parsed [][]int
format := formatNumbers
var maxIndex int
if strings.ContainsAny(indices, "!.") {
parsed = parseIndices(convertDotsAndBangs(indices))
format = formatDotAndBangs
maxIndex = len(indices)
} else if strings.ContainsAny(indices, "1234567890 ") { // space is allowed
parsed = parseIndices(indices)
} else {
return NoteMap{}, errors.New("bad syntax NoteMap; must have digits,spaces OR dots and exclamation marks")
}
for _, each := range parsed {
idx = append(idx, each[0])
}
max := sliceMax(idx)
if max > maxIndex {
maxIndex = max
}
return NoteMap{
Target: note,
Indices: idx,
// internal
indicesFormat: format,
maxIndex: maxIndex,
}, nil
}
func convertDotsAndBangs(format string) string {
var b bytes.Buffer
for i, each := range []rune(format) {
if each == '.' {
fmt.Fprintf(&b, " ")
} else {
fmt.Fprintf(&b, "%d ", i+1)
}
}
return b.String()
}
func (n NoteMap) formattedIndices(format int) string {
var b bytes.Buffer
if format == formatDotAndBangs {
for i := 1; i <= n.maxIndex; i++ {
found := false
for _, each := range n.Indices {
if each == i {
found = true
break
}
}
if found {
fmt.Fprintf(&b, "!")
} else {
fmt.Fprintf(&b, ".")
}
}
} else {
for i, each := range n.Indices {
if i > 0 {
fmt.Fprintf(&b, " ")
}
fmt.Fprintf(&b, "%d", each)
}
}
return b.String()
}
func (n NoteMap) Storex() string {
st, ok := n.Target.(core.Storable)
if !ok {
st, ok = n.Target.Value().(core.Storable)
}
if ok {
return fmt.Sprintf("notemap('%s',%s)", n.formattedIndices(n.indicesFormat), st.Storex())
}
return ""
}
// Inspect implements Inspectable
func (n NoteMap) Inspect(i core.Inspection) {
if n.indicesFormat == formatDotAndBangs {
i.Properties["nrs"] = n.formattedIndices(formatNumbers)
} else {
i.Properties["dots"] = n.formattedIndices(formatDotAndBangs)
}
n.S().Inspect(i)
}
func sliceMax(indices []int) int {
max := 0
for _, each := range indices {
if each > max {
max = each
}
}
return max
}
func (n NoteMap) S() core.Sequence {
var note core.Note
notelike, ok := n.Target.Value().(core.NoteConvertable)
if !ok {
// try create sequence first
seq, ok := n.Target.Value().(core.Sequenceable)
if !ok {
notify.Console.Errorf("cannot map %v (%T)", n.Target.Value(), n.Target.Value())
return core.EmptySequence
}
// then take the first note
notes := seq.S()
if len(notes.Notes) == 0 || len(notes.Notes[0]) == 0 {
return core.EmptySequence
}
note = notes.Notes[0][0]
} else {
var err error
note, err = notelike.ToNote()
if err != nil {
notify.Panic(err)
return core.EmptySequence
}
}
notes := make([]core.Note, n.maxIndex)
for i := range notes {
notes[i] = note.ToRest()
}
for _, each := range n.Indices {
notes[each-1] = note
}
return core.BuildSequence(notes)
}
// Replaced is part of Replaceable
func (n NoteMap) Replaced(from, to core.Sequenceable) core.Sequenceable {
if core.IsIdenticalTo(n, from) {
return to
}
notelike, ok := n.Target.Value().(core.NoteConvertable)
if !ok {
return n
}
note, err := notelike.ToNote()
if err != nil {
return n
}
return NoteMap{
Target: core.On(note.Replaced(from, to)),
Indices: n.Indices,
indicesFormat: n.indicesFormat,
maxIndex: n.maxIndex}
}