local ET = require('Module:ET')
local p = {}
function p.ET(frame)
local et = ET.parse(frame.args[1])
local from = tonumber(frame.args['From']) or 0
local to = tonumber(frame.args['To']) or et.size - 1
local base_freq = tonumber(frame.args['Base frequency']) or 440
local layout = 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_ET_keyboard(et, from, to, base_freq, layout, key_width, key_height, dur, gain, instrument, harmonic_spectrum)
end
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_ET_keyboard(et, from, to, base_freq, layout, key_width, key_height, dur, gain, instrument, harmonic_spectrum)
if #layout ~= et.size then
return '<span style="color:red;">Layout for ' .. ET.as_string(et) .. ' should have exactly ' .. et.size .. ' elements!</span>'
end
local freqs = {}
local colours = ''
local seps = {}
for i = from, to do
local cents = ET.cents(et, i)
local hz = 2 ^ (math.log(base_freq)/math.log(2) + cents/1200)
table.insert(freqs, hz)
local j = (i % et.size) + 1
colours = colours .. layout:sub(j, j)
if j == 1 and i > from then
seps[i - from + 1] = true
end
end
return p.build_keyboard(freqs, colours, seps, key_width, key_height, dur, gain, instrument, harmonic_spectrum)
end
function p.build_keyboard(freqs, colours, seps, 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;">'
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; border: 1px solid #7f7f7f;'
if seps[i] then
s = s .. ' border-left: 1px solid #ff0000;'
end
if seps[i + 1] then
s = s .. ' border-right: 1px solid #ff0000;'
end
s = s .. '"></div>'
end
s = s .. '</div>'
return s
end
return p