Module:Keyboard: Difference between revisions
mNo edit summary |
mNo edit summary |
||
| Line 1: | Line 1: | ||
local ET = require('Module:ET') | |||
local p = {} | local p = {} | ||
function p.ET(frame) | |||
local et = ET.parse(frame.args[1]) | |||
local from = tonumber(frame.args['From']) or 1 | |||
local to = tonumber(frame.args['To']) or et.size | |||
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) | function p.build(frame) | ||
| Line 24: | Line 52: | ||
table.insert(harmonic_spectrum, tonumber(val)) | table.insert(harmonic_spectrum, tonumber(val)) | ||
end | 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 = '' | |||
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 - 1) % et.size + 1 | |||
colours = colours .. layout:sub(j, j) | |||
end | end | ||