-
Notifications
You must be signed in to change notification settings - Fork 2
/
mxml.go
299 lines (271 loc) · 6.83 KB
/
mxml.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package main
import (
"encoding/xml"
"fmt"
"io"
"math/big"
"os"
"strconv"
"strings"
"time"
"github.com/sqweek/sqribe/midi"
"github.com/sqweek/sqribe/score"
)
type XMLWriter struct {
stream io.WriteCloser
level int
err error
}
func (writer *XMLWriter) Close() error {
err2 := writer.stream.Close()
if err2 != nil {
return err2
} else if writer.err != nil {
return writer.err
}
return nil
}
func (writer *XMLWriter) Fmt(format string, args... interface{}) {
_, writer.err = fmt.Fprintf(writer.stream, "%s%s\n", strings.Repeat(" ", writer.level), fmt.Sprintf(format, args...))
}
func (writer *XMLWriter) Tag(name string, attrs... interface{}) string {
tag := name
if len(attrs) > 0 {
for i :=0; i < len(attrs); i += 2 {
tag = fmt.Sprintf("%s %v=\"%v\"", tag, attrs[i], attrs[i+1])
}
}
writer.Fmt("<%s>", tag)
writer.level++
return name
}
func (writer *XMLWriter) CloseTag(name string) {
writer.level--
writer.Fmt("</%s>", name)
}
func (writer *XMLWriter) EmptyTag(name string) {
writer.Fmt("<%s />", name)
}
func (writer *XMLWriter) ContentTag(name string, content interface{}) {
writer.Fmt("<%s>%v</%s>", name, content, name)
}
func ExportMXML(filename string) error {
file, err := os.Create(filename)
if err != nil {
return err
}
_, err = io.WriteString(file, xml.Header)
if err != nil {
return err
}
wr := XMLWriter{file, 0, nil}
score := wr.Tag("score-partwise")
mxmlIdent(&wr)
mxmlParts(&wr)
wr.CloseTag(score)
err = wr.Close()
return err
}
func mxmlIdent(wr *XMLWriter) {
defer wr.CloseTag(wr.Tag("identification"))
defer wr.CloseTag(wr.Tag("encoding"))
wr.ContentTag("software", "sqribe")
wr.ContentTag("encoding-date", time.Now().Format("2006-01-02"))
}
func mxmlParts(wr *XMLWriter) {
staves := G.score.Staves()
list := wr.Tag("part-list")
for i, staff := range staves {
mix := Mixer.For(staff)
instName := midi.InstName(mix.Voice)
id := fmt.Sprintf("P%d", i)
xpart := wr.Tag("score-part", "id", id)
wr.ContentTag("part-name", instName)
xinst := wr.Tag("score-instrument", "id", fmt.Sprintf("%s-I1", id))
wr.ContentTag("instrument-name", instName)
wr.CloseTag(xinst)
wr.CloseTag(xpart)
}
wr.CloseTag(list)
for i, staff := range staves {
id := fmt.Sprintf("P%d", i)
mxmlPart(wr, staff, id)
}
}
func rat(n, d int64) *big.Rat {
return big.NewRat(n, d)
}
func flt(rat *big.Rat) float64 {
float, _ := rat.Float64()
return float
}
type NotePosIter struct {
Note *score.Note
notes []*score.Note
iN int // index of next note
beat *score.BeatRef
iB int // index of beat
}
func (iter NotePosIter) done() bool {
return iter.iN >= len(iter.notes)
}
func (iter *NotePosIter) advance() bool {
if iter.done() {
iter.Note = nil
return false
}
iter.Note = iter.notes[iter.iN]
iter.iN++
prevb := iter.beat
iter.beat = iter.Note.Beat
if prevb == nil {
iter.iB = iter.beat.BeatNum() - 1
} else {
iter.iB += iter.beat.Subtract(prevb)
}
return true
}
func (iter NotePosIter) Pos() *big.Rat {
b := big.NewRat(int64(iter.iB), 1)
b.Add(b, iter.Note.Offset)
return b
}
func mxmlPart(wr *XMLWriter, staff *score.Staff, id string) {
iter := &NotePosIter{notes: staff.Notes()}
defer wr.CloseTag(wr.Tag("part", "id", id))
ticks := 384
divisions := ticks/4
i0 := 0 // beat index of measure start
m := 1 // number of measure
for !iter.done() {
meas := wr.Tag("measure", "number", m)
if m == 1 {
iter.advance()
attr := wr.Tag("attributes")
wr.ContentTag("divisions", divisions)
key := wr.Tag("key")
wr.ContentTag("fifths", int(staff.Key()))
wr.ContentTag("mode", "major")
wr.CloseTag(key)
time := wr.Tag("time")
wr.ContentTag("beats", 4)
wr.ContentTag("beat-type", 4)
wr.CloseTag(time)
mxmlClef(wr, staff.Clef().Origin)
wr.CloseTag(attr)
}
iN := i0 + 4
curtick := (m - 1) * ticks
var prevOffset *big.Rat
for !iter.done() && flt(iter.Pos()) < float64(iN) {
tick0 := dur2ticks(rat(1,4).Mul(rat(1,4), iter.Pos()), ticks)
chord := false
if prevOffset != nil && iter.Pos().Cmp(prevOffset) == 0 {
chord = true
} else {
if tick0 < curtick {
backup := wr.Tag("backup")
wr.ContentTag("duration", curtick - tick0)
wr.CloseTag(backup)
} else if tick0 > curtick {
mxmlRest(wr ,tick0 - curtick, divisions)
}
}
prevOffset = iter.Pos()
note := iter.Note
durticks := dur2ticks(note.Duration, divisions)
if durticks <= 0 {
durticks = 1
}
mxmlNote(wr, ¬e.Pitch, note.Duration, durticks, chord)
curtick = tick0 + durticks
iter.advance()
}
if ticks > curtick {
mxmlRest(wr, ticks - curtick, divisions) /* insert rest to finish out the measure */
}
wr.CloseTag(meas)
i0 = iN
m++
}
}
func mxmlClef(wr *XMLWriter, origin uint8) {
defer wr.CloseTag(wr.Tag("clef"))
if origin == midi.PitchB5 {
wr.ContentTag("sign", "G")
wr.ContentTag("line", 2)
} else if origin == midi.PitchD4 {
wr.ContentTag("sign", "F")
wr.ContentTag("line", 4)
} else {
wr.ContentTag("sign", midi.PitchName(origin)[0:1])
wr.ContentTag("line", 3)
}
}
func dur2ticks(duration *big.Rat, divisions int) int {
dur := big.NewRat(int64(divisions), 1)
dur.Mul(dur, duration)
ticks := int(flt(dur) + 0.5)
if ticks < 0 {
ticks = 1
}
return ticks
}
func ticks2dur(ticks, divisions int) *big.Rat {
r := []*big.Rat{rat(1,128), rat(3,256), rat(1,64), rat(3,128), rat(1,32), rat(3,64), rat(1,16), rat(3,32), rat(1,8), rat(3,16), rat(1,4), rat(3,8), rat(1,2), rat(3,4), rat(1,1), rat(3,2), rat(2,1), rat(3,1), rat(4,1)}
d := float64(ticks) / float64(divisions)
for i := 0; i + 1 < len(r); i++ {
if d < (flt(r[i]) + flt(r[i + 1])) / 2 {
return r[i]
}
}
return r[len(r) - 1]
}
func mxmlRest(wr *XMLWriter, ticks, divisions int) {
dur := ticks2dur(ticks, divisions)
mxmlNote(wr, nil, dur, ticks, false)
}
func mxmlNote(wr *XMLWriter, pitch *uint8, duration *big.Rat, ticks int, chord bool) {
defer wr.CloseTag(wr.Tag("note"))
if chord {
wr.EmptyTag("chord")
}
if pitch != nil {
mxmlPitch(wr, *pitch)
} else {
wr.EmptyTag("rest")
}
wr.ContentTag("duration", ticks)
wr.ContentTag("voice", 1)
ntype, dot := mxmlNoteType(duration)
wr.ContentTag("type", ntype)
if dot {
wr.EmptyTag("dot")
}
}
func mxmlPitch(wr *XMLWriter, pitch uint8) {
defer wr.CloseTag(wr.Tag("pitch"))
s := midi.PitchName(pitch)
wr.ContentTag("step", s[0:1])
if s[1] == '#' {
wr.ContentTag("alter", 1)
} else if s[1] == 'b' {
wr.ContentTag("alter", -1)
}
octave, _ := strconv.Atoi(s[len(s) - 1:])
wr.ContentTag("octave", octave - 1)
}
func mxmlNoteType(dur *big.Rat) (string, bool) {
switch dur.RatString() {
case "1/32": return "128th", false
case "1/16": return "64th", false
case "1/8": return "32nd", false
case "1/4": return "16th", false
case "1/2": return "eighth", false
case "1": return "quarter", false
case "2": return "half", false
case "3": return "half", true
case "4": return "whole", false
}
return "quarter", false
}