-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmallpond.lua
1061 lines (946 loc) · 30.5 KB
/
smallpond.lua
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- parse
local em = 8
local Glyph = {
["noteheadWhole"] = 0xE0A2,
["noteheadHalf"] = 0xE0A3,
["noteheadBlack"] = 0xE0A4,
["flag8thDown"] = 0xE241,
["flag8thUp"] = 0xE240,
["accidentalFlat"] = 0xE260,
["accidentalNatural"] = 0xE261,
["accidentalSharp"] = 0xE262,
["gClef"] = 0xE050,
["fClef"] = 0xE062,
}
local Clef = {
["treble"] = {
glyph = Glyph.gClef,
yoff = 3*em,
defoctave = 4,
place = function(char, octave)
local defoctave = 4 -- TODO: how do we use the value above?
local NOTES = "abcdefg"
local s, _ = string.find(NOTES, char)
return (octave - defoctave) * 7 + 2 - s
end
},
["bass"] = {
glyph = Glyph.fClef,
yoff = em,
defoctave = 3,
place = function(char, octave)
local defoctave = 3 -- TODO: how do we use the value above?
local NOTES = "abcdefg"
local s, _ = string.find(NOTES, char)
return (octave - defoctave) * 7 + 4 - s
end
}
}
local numerals = {
['0'] = 0xE080,
['1'] = 0xE081,
['2'] = 0xE082,
['3'] = 0xE083,
['4'] = 0xE084,
['5'] = 0xE085,
['6'] = 0xE086,
['7'] = 0xE087,
['8'] = 0xE088,
['9'] = 0xE089
}
voice_commands = {
staff = {
parse = function(text, start)
-- move past "\staff "
start = start + 7
local text = string.match(text, "(%a+)", start)
return 7 + #text, {command="changestaff", name=text}
end
},
clef = {
parse = function(text, start)
-- move past "\clef "
start = start + 6
if string.match(text, "^treble", start) then
return #"\\clef treble", {command="changeclef", kind="treble"}
elseif string.match(text, "^bass", start) then
return #"\\clef bass", {command="changeclef", kind="bass"}
else
error(string.format("unknown clef %s", string.sub(text, start)))
end
end
},
time = {
parse = function(text, start)
-- move past "\time "
start = start + 6
local num, denom = string.match(text, "^(%d+)/(%d+)", start)
if num == nil or denom == nil then
error(string.format("bad time signature format"))
end
return #"\\time " + #num + #denom + 1, {command="changetime", num=num, denom=denom}
end
}
}
local voices = {}
local stafforder = {}
local commands = {
voice = function (text, start)
local parsenotecolumn = function(text, start)
local s, e, flags, count, dot, beam = string.find(text, "^([v^]?)(%d*)(%.?)([%[%]]?)", start)
local out = {}
if string.find(flags, "v", 1, true) then
out.stemdir = 1
elseif string.find(flags, "^", 1, true) then
out.stemdir = -1
end
if beam == '[' then
out.beam = 1
elseif beam == ']' then
out.beam = -1
end
-- make sure that count is a power of 2
if #count ~= 0 then
assert(math.ceil(math.log(count)/math.log(2)) == math.floor(math.log(count)/math.log(2)), "note count is not a power of 2")
end
out.count = tonumber(count)
out.dot = #dot == 1
return start + e - s + 1, out
end
local parsenote = function(text, start)
-- TODO: should we be more strict about accidentals and stem orientations on rests?
local s, e, time, note, acc, shift, tie = string.find(text, "^(%d*%.?%d*)([abcdefgs])([fns]?)([,']*)(~?)", start)
if note then
local out
if note == 's' then
out = {command='srest'}
else
out = {command="note", time=tonumber(time), note=note, acc=acc}
end
local _, down = string.gsub(shift, ',', '')
local _, up = string.gsub(shift, "'", '')
out.shift = up - down
if #tie > 0 then out.tie = true end
return start + e - s + 1, out
end
error("unknown token")
end
local i = start
voice = {}
while true do
::start::
i = i + #(string.match(text, "^%s*", i) or "")
if i >= #text then return i end
local cmd = string.match(text, "^\\(%a+)", i)
if cmd == "end" then
i = i + 4
break
end
if cmd then
local size, data = voice_commands[cmd].parse(text, i)
i = i + size
table.insert(voice, data)
goto start
end
-- barline
local s, e = string.find(text, "^|", i)
if s then
i = i + e - s + 1
table.insert(voice, {command="barline"})
goto start
end
-- grouping (grace or tuplet)
local s, e, f = string.find(text, "^(%g*)%b{}", i)
if s then
local notes = {}
local grace = false
if string.find(f, 'g', 1, true) then
grace = true
end
local tn, td = string.match(f, 't(%d+)/(%d+)', 1)
assert(not not tn == not not td)
if not tn then
tn = 1
td = 1
end
-- TODO: deal with notegroups
i = i + #f + 1
while i <= e - 2 do
i = i + #(string.match(text, "^%s*", i) or "")
if i >= #text then return i end
i, note = parsenote(text, i)
i, col = parsenotecolumn(text, i)
table.insert(voice, {command="newnotegroup", count=col.count, stemdir=col.stemdir, beam=col.beam, dot=col.dot, tuplet=Q.new(td)/tn, grace=grace, notes={[1] = note}})
end
i = e + 1
goto start
end
-- note column
local s, e = string.find(text, "^%b<>", i)
if s then
i = i + 1
local group = {command="newnotegroup", notes = {}}
while i <= e - 1 do
i = i + #(string.match(text, "^%s*", i) or "")
if i >= #text then return i end
i, out = parsenote(text, i)
table.insert(group.notes, out)
end
i = e + 1
i, out = parsenotecolumn(text, i)
group.count = out.count
group.stemdir = out.stemdir
group.beam = out.beam
group.dot = out.dot
table.insert(voice, group)
goto start
end
i, note = parsenote(text, i)
i, col = parsenotecolumn(text, i)
if note.command == 'srest' then
table.insert(voice, {command='srest', count=col.count})
else
table.insert(voice, {command="newnotegroup", count=col.count, stemdir=col.stemdir, beam=col.beam, dot=col.dot, notes={[1] = note}})
end
end
voices[#voices + 1] = voice
return i
end,
layout = function (text, start)
local i = start
while true do
::start::
i = i + #(string.match(text, "^%s*", i) or "")
if i >= #text then return i end
local cmd = string.match(text, "^\\(%a+)", i)
if cmd == 'end' then
i = i + 4
break
end
if cmd == 'staff' then
i = i + 7
local name = string.match(text, '^(%a+)', i)
table.insert(stafforder, name)
i = i + #name
goto start
end
error('unknown token')
end
return i
end
}
function parse(text)
local i = 1
while true do
i = i + #(string.match(text, "^%s*", i) or "")
if i >= #text then return nil end
local cmd = string.match(text, "^\\(%a+)", i)
if cmd then
i = i + #cmd + 1
i = commands[cmd](text, i)
end
end
end
f = assert(io.open("score.sp"))
parse(f:read("*a"))
local time = Q.new(0)
local octave = 0
local clef = Clef.treble
local lastnote = nil
local staff1 = {}
local points = {}
local pointindices = {}
function point(t)
for k, v in pairs(pointindices) do
if t == k then
return v
end
end
table.insert(points, t)
pointindices[t] = #points
return pointindices[t]
end
local timings = {}
local curname
local inbeam = false
local beam
local beams = {}
local beamednotes
local unterminated_ties = {}
local ties = {}
local lastbarline
-- first-order placement
local dispatch1 = {
newnotegroup = function(data)
local heads = {}
local realbeamcount = math.log(data.count) / math.log(2) - 2
local beamcount
if inbeam then
beamcount = math.min(realbeamcount, beamednotes[#beamednotes].realcount)
else
beamcount = realbeamcount
end
local maxtime, mintime
local lasthead
local flipped = false
for _, note in ipairs(data.notes) do
octave = octave - note.shift
local head = {acc=note.acc, y=clef.place(note.note, octave), time=note.time, flip}
-- avoid overlapping heads by "flipping" head across stem
if lasthead and math.abs(head.y - lasthead.y) == 1 then
flipped = true
if head.y % 2 == 1 then
head.flip = true
else
lasthead.flip = true
end
end
lasthead = head
table.insert(heads, head)
if note.time and not maxtime then maxtime = note.time end
if maxtime and note.time and note.time > maxtime then maxtime = note.time end
if note.time and not mintime then mintime = note.time end
if maxtime and note.time and note.time < maxtime then maxtime = note.time end
if unterminated_ties[curname] and unterminated_ties[curname].y == head.y then
table.insert(ties, {staff=curname, start=unterminated_ties[curname], stop=head})
unterminated_ties[curname] = nil
end
if note.tie then
assert(unterminated_ties[curname] == nil)
unterminated_ties[curname] = head
end
end
local index = point(time)
if flipped and maxtime then timings[index].flipped = true end
if not timings[index].mintime then
timings[index].mintime = mintime
else
timings[index].mintime = math.min(mintime, timings[index].mintime)
end
local incr = Q.new(1) / Q.new(data.count)
if data.dot then
incr = 3*incr / 2
end
if data.tuplet then incr = incr * data.tuplet end
local stemlen
if data.grace then
stemlen = 2.5
else
stemlen = 3.5
end
local note = {kind="notecolumn", stemdir=data.stemdir, stemlen=stemlen, dot=data.dot, grace=data.grace, count=incr, length=data.count, time=maxtime, heads=heads, staff=curname}
if data.beam == 1 then
assert(not inbeam)
beamednotes = {}
table.insert(beams, beamednotes)
table.insert(beamednotes, {note=note, count=beamcount, realcount=realbeamcount})
if data.grace then beamednotes.grace = data.grace end
beamednotes.maxbeams = beamcount
note.beamgroup = beamednotes
inbeam = true
elseif data.beam == -1 then
assert(inbeam)
inbeam = false
table.insert(beamednotes, {note=note, count=beamcount, realcount=realbeamcount})
beamednotes.maxbeams = math.max(beamednotes.maxbeams, beamcount)
note.beamgroup = beamednotes
elseif inbeam then
beamednotes.maxbeams = math.max(beamednotes.maxbeams, beamcount)
table.insert(beamednotes, {note=note, count=beamcount, realcount=realbeamcount})
note.beamgroup = beamednotes
end
table.insert(staff1[curname], note)
local index = point(time)
if note.grace then
table.insert(timings[index].staffs[curname].pre, note)
else
table.insert(timings[index].staffs[curname].on, note)
time = time + incr
end
lastnote = note
end,
changeclef = function(data)
local class = assert(Clef[data.kind])
local clefitem = {kind="clef", class=class}
local index = point(time)
timings[index].staffs[curname].clef = clefitem
table.insert(staff1[curname], clefitem)
clef = class
octave = class.defoctave
end,
changestaff = function(data)
if staff1[data.name] == nil then
staff1[data.name] = {}
end
curname = data.name
-- mark cross staff beams for special treatment later
if inbeam then beamednotes.cross = true end
end,
changetime = function(data)
local timesig = {kind="time", num=data.num, denom=data.denom}
local index = point(time)
timings[index].staffs[curname].timesig = timesig
table.insert(staff1[curname], timesig)
end,
barline = function(data)
local index = point(time)
timings[index].barline = true
lastbarline = index
lastnote = nil
end,
srest = function(data)
table.insert(staff1[curname], {kind='srest', length=data.count, time=time})
time = time + 1 / Q.new(data.count)
end,
}
for _, voice in ipairs(voices) do
time = Q.new(0)
for _, item in ipairs(voice) do
local index = point(time)
if not timings[index] then timings[index] = {staffs={}} end
if curname and not timings[index].staffs[curname] then timings[index].staffs[curname] = {pre={}, on={}, post={}} end
assert(dispatch1[item.command])(item)
end
end
table.sort(points)
for _, beam in pairs(beams) do
-- check which way the stem should point on all the notes in the beam
local ysum = 0
for _, entry in ipairs(beam) do
-- FIXME: note.heads[1].y is wrong
ysum = ysum + entry.note.heads[1].y
end
local stemdir
if ysum >= 0 then
stemdir = -1
else
stemdir = 1
end
-- check that stem direction hasn't been set manually
local unset = true
for _, entry in ipairs(beam) do
if entry.note.stemdir then
unset = false
break
end
end
-- update the stem direction
if unset then
for _, entry in ipairs(beam) do
entry.note.stemdir = stemdir
end
end
end
local staff3 = {}
local extra3 = {}
local x = 10
local lasttime = 0
for staff, _ in pairs(staff1) do
staff3[staff] = {}
end
local staff3ify = function(timing, el, staff)
local xdiff
local tindex = point(timing)
if el.kind == "notecolumn" then
local glyphsize
if el.grace then
glyphsize = 24
else
glyphsize = 32
end
local rx = x
xdiff = 10
rx = rx + xdiff
local glyph
if el.length == 1 then
glyph = Glyph["noteheadWhole"]
elseif el.length == 2 then
glyph = Glyph["noteheadHalf"]
elseif el.length >= 4 then
glyph = Glyph["noteheadBlack"]
end
local w, h = glyph_extents(glyph, glyphsize)
local preoffset = 0
-- TODO: increment on each accidental to reduce overlap
for _, head in ipairs(el.heads) do
if #head.acc then
preoffset = 10
end
end
-- offset of stem if a head is drawn on opposite side of stem
local altoffset = 0
if timings[tindex].flipped then
altoffset = w - 1.2
end
local heightsum = 0
local lowheight
local highheight
for _, head in ipairs(el.heads) do
heightsum = heightsum + head.y
local ry = (em*head.y) / 2 + 2*em
if not lowheight then lowheight = ry end
if not highheight then highheight = ry end
if head.flip then
head.glyph = {kind="glyph", width=w, size=glyphsize, glyph=glyph, x=preoffset + rx, y=ry, time={start=head.time}}
else
head.glyph = {kind="glyph", width=w, size=glyphsize, glyph=glyph, x=preoffset + altoffset + rx, y=ry, time={start=head.time}}
end
table.insert(staff3[staff], head.glyph)
if el.dot then
xdiff = xdiff + 5
table.insert(staff3[staff], {kind="circle", r=1.5, x=preoffset + altoffset + rx + w + 5, y=ry, time={start=head.time}})
end
if head.acc == "s" then
table.insert(staff3[staff], {kind="glyph", size=glyphsize, glyph=Glyph["accidentalSharp"], x=rx, y=ry, time={start=head.time}})
elseif head.acc == "f" then
table.insert(staff3[staff], {kind="glyph", size=glyphsize, glyph=Glyph["accidentalFlat"], x=rx, y=ry, time={start=head.time}})
elseif head.acc == "n" then
table.insert(staff3[staff], {kind="glyph", size=glyphsize, glyph=Glyph["accidentalNatural"], x=rx, y=ry, time={start=head.time}})
end
lowheight = math.min(lowheight, ry)
highheight = math.max(highheight, ry)
local stoptime
if el.time then stoptime = el.time + 1 else stoptime = nil end
-- TODO: only do this once per column
-- leger lines
if head.y <= -6 then
for j = -6, head.y, -2 do
table.insert(staff3[staff], {kind="line", t=1.2, x1=altoffset + preoffset + rx - .2*em, y1=(em * (j + 4)) / 2, x2=altoffset + preoffset + rx + w + .2*em, y2=(em * (j + 4)) / 2, time={start=el.time, stop=stoptime}})
end
end
if head.y >= 6 then
for j = 6, head.y, 2 do
table.insert(staff3[staff], {kind="line", t=1.2, x1=altoffset + preoffset + rx - .2*em, y1=(em * (j + 4)) / 2, x2=altoffset + preoffset + rx + w + .2*em, y2=(em * (j + 4)) / 2, time={start=el.time, stop=stoptime}})
end
end
end
if not el.stemdir and el.length > 1 then
if heightsum <= 0 then
el.stemdir = 1
else
el.stemdir = -1
end
end
-- stem
local stemstoptime
if el.stemdir then
if el.time then stemstoptime = el.time + .25 else stemstoptime = nil end
if el.stemdir == -1 then
-- stem up
-- advance width for bravura is 1.18 - .1 for stem width
el.stemx = w + rx - 1.08 + preoffset + altoffset
local stem = {kind="line", t=1, x1=el.stemx, y1=highheight - .168*em, x2=el.stemx, y2=lowheight -.168*em - el.stemlen*em, time={start=el.time, stop=stemstoptime}}
el.stem = stem
table.insert(staff3[staff], el.stem)
else
el.stemx = rx + .5 + preoffset + altoffset
local stem = {kind="line", t=1, x1=el.stemx, y1=lowheight + .168*em, x2=el.stemx, y2=highheight + el.stemlen*em, time={start=el.time, stop=stemstoptime}}
el.stem = stem
table.insert(staff3[staff], stem)
end
end
-- flag
if el.length == 8 and not el.beamgroup then
if el.stemdir == 1 then
local fx, fy = glyph_extents(Glyph["flag8thDown"], glyphsize)
table.insert(staff3[staff], {kind="glyph", glyph=Glyph["flag8thDown"], size=glyphsize, x=altoffset + preoffset + rx, y=highheight + 3.5*em, time={start=stemstoptime}})
else
-- TODO: move glyph extents to a precalculated table or something
local fx, fy = glyph_extents(Glyph["flag8thUp"], glyphsize)
table.insert(staff3[staff], {kind="glyph", glyph=Glyph["flag8thUp"], size=glyphsize, x=altoffset + el.stemx - .48, y=lowheight -.168*em - 3.5*em, time={start=stemstoptime}})
xdiff = xdiff + fx
end
end
xdiff = xdiff + 100 / el.length + 10
lasttime = el.time
elseif el.kind == "srest" then
xdiff = 0
elseif el.kind == "clef" then
table.insert(staff3[staff], {kind="glyph", glyph=el.class.glyph, size=32, x=x, y=el.class.yoff, time={start=timings[tindex].mintime, stop=timings[tindex].mintime + 1}})
xdiff = 30
elseif el.kind == "time" then
-- TODO: draw multidigit time signatures properly
table.insert(staff3[staff], {kind="glyph", glyph=numerals[el.num], size=32, x=x, y=em, time={start=timings[tindex].mintime, stop=timings[tindex].mintime + 1}})
table.insert(staff3[staff], {kind="glyph", glyph=numerals[el.denom], size=32, x=x, y=3*em, time={start=timings[tindex].mintime, stop=timings[tindex].mintime + 1}})
xdiff = 30
end
return xdiff
end
local rtimings = {}
local snappoints = {}
local curclef = {}
for _, time in ipairs(points) do
local tindex = point(time)
local todraw = timings[tindex].staffs
-- clef
local xdiff = 0
for staff, vals in pairs(todraw) do
if vals.clef and (vals.clef.class ~= curclef[staff]) then
local diff = staff3ify(time, vals.clef, staff)
if diff > xdiff then xdiff = diff end
curclef[staff] = vals.clef.class
end
end
x = x + xdiff
xdiff = 0
-- time signature
local xdiff = 0
for staff, vals in pairs(todraw) do
if vals.timesig then
local diff = staff3ify(time, vals.timesig, staff)
if diff > xdiff then xdiff = diff end
end
end
x = x + xdiff
xdiff = 0
if timings[tindex].barline then
local time = timings[tindex].mintime or 0
if tindex == lastbarline then
table.insert(extra3, {kind='barline', x=x+25, last=true, time={start=time - 1, stop=time}})
else
table.insert(extra3, {kind='barline', x=x+25, time={start=time - 1, stop=time}})
end
x = x + 10
end
-- prebeat
for staff, vals in pairs(todraw) do
if #vals.pre == 0 then goto nextstaff end
for _, el in ipairs(vals.pre) do
local diff = staff3ify(time, el, staff)
if el.beamref then staff3ify(time, el.beamref, staff) end
x = x + diff
end
::nextstaff::
end
xdiff = 0
local maxtime = 0
-- on beat
for staff, vals in pairs(todraw) do
if #vals.on == 0 then goto nextstaff end
local diff
for _, el in ipairs(vals.on) do
-- HACK: don't hardcode staff name
if staff == "low" and el.time and el.time > maxtime then maxtime = el.time end
diff = staff3ify(time, el, staff)
if el.beamref then staff3ify(time, el.beamref, staff) end
end
if xdiff < diff then xdiff = diff end
::nextstaff::
end
x = x + xdiff
rtimings[maxtime] = x
if maxtime ~= 0 then
table.insert(snappoints, maxtime)
end
end
-- calculate extents
local extents = {}
for _, staff in pairs(stafforder) do
local items = staff3[staff]
extents[staff] = {xmin=0, ymin=0, xmax=0, ymax=0}
for i, d in ipairs(items) do
if d.kind == "glyph" then
local w, h = glyph_extents(d.glyph, 32)
if d.x - w < extents[staff].xmin then
extents[staff].xmin = d.x - w
elseif d.x + w > extents[staff].xmax then
extents[staff].xmax = d.x + w
end
if d.y - h < extents[staff].ymin then
extents[staff].ymin = d.y - h
elseif d.y + h > extents[staff].ymax then
extents[staff].ymax = d.y + h
end
elseif d.kind == "line" then
if d.x1 < extents[staff].xmin then
extents[staff].xmin = d.x1
elseif d.x1 > extents[staff].xmax then
extents[staff].xmax = d.x1
end
if d.x2 < extents[staff].xmin then
extents[staff].xmin = d.x2
elseif d.x2 > extents[staff].xmax then
extents[staff].xmax = d.x2
end
if d.y1 < extents[staff].ymin then
extents[staff].ymin = d.y1
elseif d.y1 > extents[staff].ymax then
extents[staff].ymax = d.y1
end
if d.y2 < extents[staff].ymin then
extents[staff].ymin = d.y2
elseif d.y2 > extents[staff].ymax then
extents[staff].ymax = d.y2
end
elseif d.kind == "quad" then
if d.x1 < extents[staff].xmin then
extents[staff].xmin = d.x1
elseif d.x1 > extents[staff].xmax then
extents[staff].xmax = d.x1
end
if d.x2 < extents[staff].xmin then
extents[staff].xmin = d.x2
elseif d.x2 > extents[staff].xmax then
extents[staff].xmax = d.x2
end
if d.y1 < extents[staff].ymin then
extents[staff].ymin = d.y1
elseif d.y1 > extents[staff].ymax then
extents[staff].ymax = d.y1
end
if d.y2 < extents[staff].ymin then
extents[staff].ymin = d.y2
elseif d.y2 > extents[staff].ymax then
extents[staff].ymax = d.y2
end
if d.x3 < extents[staff].xmin then
extents[staff].xmin = d.x3
elseif d.x3 > extents[staff].xmax then
extents[staff].xmax = d.x3
end
if d.x4 < extents[staff].xmin then
extents[staff].xmin = d.x4
elseif d.x4 > extents[staff].xmax then
extents[staff].xmax = d.x4
end
if d.y3 < extents[staff].ymin then
extents[staff].ymin = d.y3
elseif d.y3 > extents[staff].ymax then
extents[staff].ymax = d.y3
end
if d.y4 < extents[staff].ymin then
extents[staff].ymin = d.y4
elseif d.y4 > extents[staff].ymax then
extents[staff].ymax = d.y4
end
end
end
end
local xmax = 0
local yoff = 0
local firstymin, lastymin
local xmin = 0
for i, staff in pairs(stafforder) do
local extent = extents[staff]
if xmin > extent.xmin then
xmin = extent.xmin
end
if xmax < extent.xmax then
xmax = extent.xmax
end
if i == 1 then
firstymin = yoff + extent.ymin
end
if i == #stafforder then
lastymin = yoff - extent.ymin
end
extent.yoff = yoff
yoff = yoff + extent.ymax - extent.ymin
end
scale = frameheight / yoff
for _, tie in pairs(ties) do
local yoff = extents[tie.staff].yoff - extents[tie.staff].ymin
table.insert(extra3, {kind="curve", x0=tie.start.glyph.x + tie.start.glyph.width + 10, y0=tie.start.glyph.y + yoff, x2=tie.stop.glyph.x + 10, y2=tie.stop.glyph.y + yoff, time={start=tie.start.glyph.time.start, stop=tie.stop.glyph.time.start}})
end
-- draw beam (and adjust stems) after all previous notes already have set values
for _, notes in ipairs(beams) do
local beamheight, beamspace
if notes.grace then
beamheight = 3
beamspace = 5
else
beamheight = 5
beamspace = 7
end
local x0 = notes[1].note.stemx + .5
local y0 = notes[1].note.stem.y2 + extents[notes[1].note.staff].yoff - extents[notes[1].note.staff].ymin
local y0s = notes[1].note.stem.y2
local yn = notes[#notes].note.stem.y2 + extents[notes[#notes].note.staff].yoff - extents[notes[#notes].note.staff].ymin
local m = (yn - y0) / (notes[#notes].note.stemx + .5 - x0)
-- THIS IS A HACK: replace with more generic mechanism that detects overlap
-- this only accounts for stems pointing do
local stemextension = 0
if notes[1].count == 3 and notes[1].note.stemdir == -1 then
stemextension = -10
end
if notes.cross then
if notes[1].note.stemdir == -1 then
notes[1].note.stem.y2 = notes[1].note.stem.y2 - beamspace * (notes.maxbeams - 1) + beamheight
end
if notes[1].note.stemdir == 1 and notes[2] and notes[2].note.stemdir == -1 then
notes[1].note.stem.y2 = notes[1].note.stem.y2 + beamspace * notes.maxbeams
end
end
if notes[1].note.stemdir == 1 then
notes[1].note.stem.y2 = y0s + 7*(notes.maxbeams - 2) + beamheight + stemextension
end
for i, entry in ipairs(notes) do
if i == 1 then goto continue end
local note = notes[i].note
local n = entry.count
local x1 = notes[i-1].note.stemx + .5
local prevymin = extents[notes[i-1].note.staff].ymin
local x2 = note.stemx + .5
local extent = extents[note.staff]
-- change layout parameters depending on stem up or stem down
local first, last, inc
if entry.note.stemdir == 1 then
first = beamspace*(notes.maxbeams - 2)
last = beamspace*(notes.maxbeams - n - 1)
if extents[entry.note.staff].yoff < extents[notes[1].note.staff].yoff then
entry.note.stem.y2 = y0 + m*(x2 - x0) + 7*(notes.maxbeams - 2) + beamheight + extents[entry.note.staff].ymin - extents[entry.note.staff].yoff + stemextension
else
entry.note.stem.y2 = y0s + m*(x2 - x0) + 7*(notes.maxbeams - 2) + beamheight + stemextension
end
inc = -beamspace
else
if extents[entry.note.staff].yoff > extents[notes[1].note.staff].yoff then
entry.note.stem.y2 = y0 + m*(x2 - x0) + beamheight - extents[entry.note.staff].yoff + extents[entry.note.staff].ymin + stemextension
else
entry.note.stem.y2 = y0s + m*(x2 - x0) + stemextension
end
first = 0
last = beamspace*(n-1)
inc = beamspace
end
-- draw beams segment by segment
for yoff=first, last, inc do
local starttime, stoptime
if note.time then stoptime = note.time + .25 else stoptime = nil end
if note.time then starttime = notes[i-1].note.time + .25 else starttime = nil end
if entry.note.stemdir ~= 1 and notes.cross then
table.insert(extra3, {kind="beamseg", x1=x1 - 0.5 - extent.xmin, x2=x2 - extent.xmin, y1=y0 + m*(x1 - x0) + yoff + stemextension, y2=y0 + m*(x2 - x0) + yoff + stemextension, h=beamheight, time={start=starttime, stop=stoptime}})
else
table.insert(extra3, {kind="beamseg", x1=x1 - 0.5 - extent.xmin, x2=x2 - extent.xmin, y1=y0 + m*(x1 - x0) + yoff + stemextension, y2=y0 + m*(x2 - x0) + yoff + stemextension, h=beamheight, time={start=starttime, stop=stoptime}})
end
end
::continue::
end
end
for staff, item in ipairs(extra3) do
if item.kind == 'barline' then
if item.x < xmin then
xmin = item.x
elseif item.x > xmax then
if item.last then
xmax = item.x + 7
else
xmax = item.x
end
end
end
end
local lastpoint = 0
for _, point in ipairs(snappoints) do
lastpoint = math.max(point, lastpoint)
end
-- TODO: is there a better way to do this?
snappoints[0] = snappoints[1]
local snapidx = 1
local toff_base = 0
function drawframe(time)
if snappoints[snapidx + 1] and snappoints[snapidx] < time then
snapidx = snapidx + 1
toff_base = -rtimings[snappoints[snapidx - 1]]
end
local xdiff = rtimings[snappoints[snapidx]] - rtimings[snappoints[snapidx - 1]]
local delta = xdiff * (time - snappoints[snapidx - 1]) / (snappoints[snapidx] - snappoints[snapidx - 1])
local toff = toff_base - delta + framewidth / (2*scale)
if time > lastpoint + 10 then
return true
end
for _, staff in ipairs(stafforder) do
local extent = extents[staff]
for i, d in ipairs(staff3[staff]) do
if not d.time.start then goto continue end
if d.time.start < time then
if d.kind == "glyph" then
draw_glyph(scale*d.size, d.glyph, scale*(toff + d.x - extent.xmin), scale*(d.y - extent.ymin + extent.yoff))
elseif d.kind == "line" then
local delta = (time - d.time.start) / (d.time.stop - d.time.start)
local endx, endy
if d.x1 < d.x2 then
endx = math.min(d.x1 + delta*(d.x2 - d.x1), d.x2)
else
endx = math.max(d.x1 + delta*(d.x2 - d.x1), d.x2)
end
if d.y1 < d.y2 then
endy = math.min(d.y1 + delta*(d.y2 - d.y1), d.y2)
else
endy = math.max(d.y1 + delta*(d.y2 - d.y1), d.y2)
end
draw_line(scale*d.t, scale*(toff + d.x1 - extent.xmin), scale*(d.y1 - extent.ymin + extent.yoff), scale*(toff + endx - extent.xmin), scale*(endy - extent.ymin + extent.yoff))
elseif d.kind == "circle" then
draw_circle(scale*d.r, scale*(toff + d.x - extent.xmin), scale*(d.y - extent.ymin + extent.yoff))
elseif d.kind == "vshear" then
local delta = (time - d.time.start) / (d.time.stop - d.time.start)
local endx, endy
if d.x1 < d.x2 then
endx = math.min(d.x1 + delta*(d.x2 - d.x1), d.x2)
else
endx = math.max(d.x1 + delta*(d.x2 - d.x1), d.x2)
end
if d.y1 < d.y2 then
endy = math.min(d.y1 + delta*(d.y2 - d.y1), d.y2)
else
endy = math.max(d.y1 + delta*(d.y2 - d.y1), d.y2)
end
draw_quad(scale*(toff + d.x1 - extent.xmin), scale(d.y1 - extent.ymin + extent.yoff), scale(toff + endx - extent.xmin), scale*(endy - extent.ymin + extent.yoff), scale*(toff + endx - extent.xmin), scale*(endy + d.h - extent.ymin + extent.yoff), scale*(toff + d.x1 - extent.xmin), scale*(d.y1 + d.h - extent.ymin + extent.yoff))
elseif d.kind == "quad" then
draw_quad(scale*(toff + d.x1 - extent.xmin), scale*(d.y1 - extent.ymin + extent.yoff), scale*(toff + d.x2 - extent.xmin), scale*(d.y2 - extent.ymin + extent.yoff), scale*(toff + d.x3 - extent.xmin), scale*(d.y3 - extent.ymin + extent.yoff), scale*(toff + d.x4 - extent.xmin), scale*(d.y4 - extent.ymin + extent.yoff))