local p = {}
function p.build(frame)
local freqs_arg = frame.args[1]
local freqs = {}
for freq in freqs_arg:gmatch('%S+') do
table.insert(freqs, tonumber(freq))
end
local colours = frame.args[2]
local key_width = tonumber(frame.args['Key width']) or 30
local key_height = tonumber(frame.args['Key height']) or 60
local dur = tonumber(frame.args['Duration']) or 500
local gain = tonumber(frame.args['Gain']) or 0.1
local instrument = frame.args['Instrument'] or 'sine'
local spectrum_arg = frame.args['Harmonic spectrum']
local harmonic_spectrum = nil
if spectrum_arg then
harmonic_spectrum = {}
for val in spectrum_arg:gmatch('%S+') do
table.insert(harmonic_spectrum, tonumber(val))
end
end
return p.build_keyboard(freqs, colours, key_width, key_height, dur, gain, instrument, harmonic_spectrum)
end
function p.build_keyboard(freqs, colours, key_width, key_height, dur, gain, instrument, harmonic_spectrum)
if #freqs ~= #colours then
return '<span style="color:red;">Number of keys (' .. (#freqs) .. ') and of colours (' .. (#colours) .. ') are different!</span>'
end
local n = #freqs
local s = '<div style="display: flex; width: fit-content; border: 1px solid black;">'
for i = 1, n do
s = s .. '<div class="sequence-audio sequence-audio-button'
if colours:sub(i, i) == 'w' then
s = s .. ' white-key'
elseif colours:sub(i, i) == 'b' then
s = s .. ' black-key'
end
s = s .. '" data-sequence="' .. freqs[i] .. ':' .. dur .. ':' .. gain .. ':0:' .. instrument .. '"'
if harmonic_spectrum then
s = s .. ' data-timbre-' .. instrument .. '="' .. table.concat(harmonic_spectrum, ' ') .. '"'
end
s = s .. ' style="width: ' .. key_width .. 'px; height: ' .. key_height .. 'px"></div>'
end
s = s .. '</div>'
return s
end
return p