commit e3fd305a9a698eeaf77f1966b7b7f55f6d0f632a
parent 34958ac65f6d2e35f294ded5434afdd84b5af3e6
Author: Nihal Jere <nihal@nihaljere.xyz>
Date: Mon, 22 Aug 2022 21:30:04 -0500
basic note placement for treble and bass clefs
Diffstat:
M | smallpond.lua | | | 46 | +++++++++++++++++++++++++++++++++------------- |
1 file changed, 33 insertions(+), 13 deletions(-)
diff --git a/smallpond.lua b/smallpond.lua
@@ -1,12 +1,26 @@
-- parse
local em = 8
-function placement(char)
- assert(char)
- local NOTES = "abcdefg"
- local s, _ = string.find(NOTES, char)
- return s
-end
+local noteheadBlock = 0xE0A4
+local gClef = 0xE050
+local fClef = 0xE062
+local treble = {
+ glyph = gClef,
+ place = function(char)
+ local NOTES = "abcdefg"
+ local s, _ = string.find(NOTES, char)
+ return 6 - s
+ end
+}
+
+local bass = {
+ glyph = fClef,
+ place = function(char)
+ local NOTES = "abcdefg"
+ local s, _ = string.find(NOTES, char)
+ return 8 - s
+ end
+}
commands = {
clef = {
@@ -15,8 +29,10 @@ commands = {
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 at offset %s", string.sub(text, start)))
+ error(string.format("unknown clef %s", string.sub(text, start)))
end
end
}
@@ -49,12 +65,18 @@ local x = 10
staff = {}
command_dispatch = {
newnote = function(data)
- local i = placement(data.note)
+ local i = staff.clef.place(data.note)
table.insert(staff, {kind="notehead", x=x, y=(em*i) / 2})
x = x + 20
end,
changeclef = function(data)
- table.insert(staff, {kind="clef", x=x, y=4*em})
+ if data.kind == "treble" then
+ staff.clef = treble
+ table.insert(staff, {kind="clef", class=treble, x=x, y=3*em})
+ elseif data.kind == "bass" then
+ staff.clef = bass
+ table.insert(staff, {kind="clef", class=bass, x=x, y=em})
+ end
x = x + 40
end
}
@@ -70,16 +92,14 @@ local yoffset = 20
local xoffset = 20
-- draw staff
-for y=0,em*5,em do
+for y=0,em*4,em do
draw_line(xoffset, y + yoffset, staff_width + xoffset, y + yoffset)
end
-local noteheadBlock = 0xE0A4
-local gClef = 0xE050
for i, el in ipairs(staff) do
if el.kind == "notehead" then
draw_glyph(noteheadBlock, xoffset + el.x, yoffset + el.y)
elseif el.kind == "clef" then
- draw_glyph(gClef, xoffset + el.x, yoffset + el.y)
+ draw_glyph(el.class.glyph, xoffset + el.x, yoffset + el.y)
end
end