Module:Keyboard: Difference between revisions

Plumtree (talk | contribs)
mNo edit summary
Plumtree (talk | contribs)
mNo edit summary
Line 37: Line 37:
return r_layout .. 'bw'
return r_layout .. 'bw'
end
end
end
function p.auto_position(layout, compact)
local data = {}
local pos = -1
for i = 1, #layout do
if compact then
if layout:sub(i, i) == 'w' then
pos = pos + 1
table.insert(data, { key = 'w', pos = pos, kind = 'full' })
elseif layout:sub(i - 1, i - 1) == 'w' and layout:sub(i + 1, i + 1) == 'w' then
table.insert(data, { key = 'b', pos = pos, kind = 'mid' })
elseif layout:sub(i - 1, i - 1) == 'w' then
table.insert(data, { key = 'b', pos = pos, kind = 'right' })
elseif layout:sub(i + 1, i + 1) == 'w' then
table.insert(data, { key = 'b', pos = pos + 1, kind = 'left' })
else
pos = pos + 1
table.insert(data, { key = 'b', pos = pos, kind = 'full' })
end
else
pos = pos + 1
table.insert(data, { key = layout:sub(i, i), pos = pos, kind = 'full' })
end
end
return data
end
end


Line 128: Line 154:


function p.build_keyboard(freqs, colours, seps, key_width, key_height, dur, gain, instrument, harmonic_spectrum)
function p.build_keyboard(freqs, colours, seps, key_width, key_height, dur, gain, instrument, harmonic_spectrum)
key_width = key_width - (key_width % 6)
key_height = key_height - (key_height % 20)
if #freqs ~= #colours then
if #freqs ~= #colours then
return '<span style="color:red;">Number of keys (' .. (#freqs) .. ') and of colours (' .. (#colours) .. ') are different!</span>'
return '<span style="color:red;">Number of keys (' .. (#freqs) .. ') and of colours (' .. (#colours) .. ') are different!</span>'
end
end
local n = #freqs
local n = #freqs
local positions = p.auto_position(colours, true)
local s = '<div style="display: flex; width: fit-content;">'
local s = '<div style="display: flex; width: fit-content;">'
for i = 1, n do
for i = 1, n do
Line 144: Line 173:
s = s .. ' data-timbre-' .. instrument .. '="' .. table.concat(harmonic_spectrum, ' ') .. '"'
s = s .. ' data-timbre-' .. instrument .. '="' .. table.concat(harmonic_spectrum, ' ') .. '"'
end
end
s = s .. ' style="width: ' .. key_width .. 'px; height: ' .. key_height .. 'px; border: 1px solid #7f7f7f;'
local width = key_width
local height = key_height
if positions[i].kind == 'mid' then
width = 2 * (width / 3)
height = 13 * (height / 20)
elseif positions[i].kind == 'left' or positions[i].kind == 'right' then
width = width / 2
height = 13 * (height / 20)
end
s = s .. ' style="width: ' .. (width - 2) .. 'px; height: ' .. (height - 2) .. 'px; border: 1px solid #7f7f7f;'
if positions[i].kind == 'mid' then
s = s .. ' position: absolute; left: ' .. (positions[i].pos * key_width + key_width - width / 2) .. 'px;'
elseif positions[i].kind == 'left' then
s = s .. ' position: absolute; left: ' .. (positions[i].pos * key_width) .. 'px;'
elseif positions[i].kind == 'right' then
s = s .. ' position: absolute; left: ' .. (positions[i].pos * key_width + key_width - width) .. 'px;'
end
if seps[i] then
if seps[i] then
s = s .. ' border-left: 1px solid #ff0000;'
s = s .. ' border-left: 1px solid #ff0000;'