-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
275 lines (257 loc) · 6.17 KB
/
config.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"math/rand"
"strings"
"time"
)
const (
figurePrefix rune = '+'
seqPrefix rune = ':'
patternPause rune = '.'
patternBeat rune = 'x'
laneSplitStr string = " "
choiceSplitStr string = "|"
defaultStep int = 8
defaultSet string = "default"
)
type part struct {
name string
set noteMap
figures map[string]figure
step int
bpm string
fx []map[string]string
lanes []string
}
type seq []string
type seqMap map[string]seq
type drums struct {
Sets []struct {
Name string
Kit []struct {
Key string
Channel int
Note int
}
}
Figures []struct {
Name string
Key string
Velocity int
Pattern string
}
Parts []struct {
Name string
Set string
Step int
Bpm string
Fx []map[string]string
Lanes []string
Genlanes []map[string]map[string]string
}
Seqs []struct {
Name string
Parts []string
}
}
func (d *drums) dump() {
fmt.Println(*d)
}
func translateKit(set noteMap, s string) (channel, note int) {
// pick a random note from choiceSplitStr separated choices
ss := strings.Split(s, choiceSplitStr)
pick := ss[rand.Intn(len(ss))]
channel = set[pick].Channel
note = set[pick].Note
return
}
// renders the pattern string of a figure into individual midi events,
// translating the kit keys on the way.
func translateFigure(set noteMap, figures map[string]figure, s string) midiFigure {
var fig midiFigure
f := figures[s]
c, n := translateKit(set, f.key)
for _, elem := range f.pattern {
switch elem {
case patternPause:
fig = append(fig, midiEvent{})
case patternBeat:
fig = append(fig, midiEvent{c, n, f.velocity})
}
}
return fig
}
// text2matrix takes the lanes as strings, looks up the channel
// and note number of the events and returns them as separate
// matrices.
// It also returns a map of micro pattern figures. The index of the
// map is the position in the lane, so the player can fire it
// at the right time later.
func text2matrix(set noteMap, figures map[string]figure, txt []string) (channels, notes matrix, figuremap map[int][]midiFigure) {
figuremap = make(map[int][]midiFigure)
for _, line := range txt {
var chanV, noteV []int
lane := strings.Split(strings.TrimSpace(line), laneSplitStr)
// use our own index because we need to be able to decrease it
// if we encounter unwanted (uncounted) characters
var i = -1
for _, elem := range lane {
i++
if elem == "" {
i--
continue
}
first, rest := splitFirstRune(elem)
if first == figurePrefix {
// generate a micro pattern
figuremap[i] = append(figuremap[i], translateFigure(set, figures, rest))
// also generate an empty single note
chanV = append(chanV, 0)
noteV = append(noteV, 0)
} else {
// generate a normal note
c, n := translateKit(set, elem)
chanV = append(chanV, c)
noteV = append(noteV, n)
}
}
channels = append(channels, row(chanV))
notes = append(notes, row(noteV))
}
debugf("text2matrix(): figuremap: %v", figuremap)
return
}
func (d *drums) getSeqs() seqMap {
seqs := make(seqMap)
for _, s := range d.Seqs {
var seqparts []string
for _, partname := range s.Parts {
seqparts = append(seqparts, partname)
}
seqs[s.Name] = seqparts
}
return seqs
}
// take on UTF-8 safe string splitting
func splitFirstRune(s string) (first rune, rest string) {
r := []rune(s)
first = r[0]
rest = string(r[1:])
return
}
// chains up all seqs by looking up references to other
// seqs or parts and returns a list of part names.
func (sm seqMap) flatten(startAt string) []string {
seen := make(map[string]bool)
return sm.flattenRecursive(startAt, seen)
}
func (sm seqMap) flattenRecursive(startAt string, seen map[string]bool) []string {
var l []string
for _, p := range sm[startAt] {
ok := false
first, rest := splitFirstRune(p)
if first == seqPrefix {
_, ok = sm[rest]
}
if ok {
s, ok := seen[rest]
if ok && s {
logger.Fatalf("flattenRecursive(): loop in sequence \"%v\". You can not make circular seqs.", rest)
} else {
seen[rest] = true
}
l = append(l, sm.flattenRecursive(rest, seen)...)
seen[rest] = false
} else {
l = append(l, p)
}
}
return l
}
func (d *drums) getParts(sets map[string]noteMap, figures map[string]figure) map[string]part {
parts := make(map[string]part)
var partsetname string
var partstep int
for _, inp := range d.Parts {
if inp.Set == "" {
partsetname = defaultSet
} else {
partsetname = inp.Set
}
partset, ok := sets[partsetname]
if inp.Step == 0 {
partstep = defaultStep
} else {
partstep = inp.Step
}
if !ok {
logger.Printf("unknown set \"%s\"", partsetname)
}
genlanes, err := renderGenlanes(inp.Genlanes)
if err != nil {
logger.Print(err)
}
lanes := inp.Lanes
lanes = append(lanes, genlanes...)
debugf("getParts(): %#v", lanes)
parts[inp.Name] = part{
name: inp.Name,
set: partset,
figures: figures,
step: partstep,
bpm: inp.Bpm,
fx: inp.Fx,
lanes: lanes,
}
}
return parts
}
func (d *drums) getSets() map[string]noteMap {
// Make sure we have a different random sequence for each invocation
rand.Seed(time.Now().Unix())
sets := make(map[string]noteMap)
for _, set := range d.Sets {
debugf("getSets(): %+v", set)
notes := make(noteMap)
for _, note := range set.Kit {
notes[note.Key] = midiNote{
Channel: note.Channel,
Note: note.Note,
}
debugf("getSets(): %+v", note)
}
sets[set.Name] = notes
}
return sets
}
func (d *drums) getFigures() map[string]figure {
var figurevelocity int
figures := make(map[string]figure)
for _, f := range d.Figures {
debugf("getFigures(): %+v", f)
if f.Velocity == 0 {
figurevelocity = midiVmax
} else {
figurevelocity = f.Velocity
}
figures[f.Name] = figure{
key: f.Key,
velocity: figurevelocity,
pattern: f.Pattern,
}
}
return figures
}
func (d *drums) loadFromFile(fn string) {
data, err := ioutil.ReadFile(fn)
if err != nil {
logger.Fatalf("%v", err)
}
err = yaml.Unmarshal(data, d)
if err != nil {
logger.Fatalf("Syntax error when reading file \"%s\". Maybe it is not proper YAML format.\n%v", fn, err)
}
}