Module:ET: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
mNo edit summary |
||
| Line 65: | Line 65: | ||
steps = steps or 1 | steps = steps or 1 | ||
return 1200 * steps / et.size * math.log(rat.as_float(et.equave)) / math.log(2) | return 1200 * steps / et.size * math.log(rat.as_float(et.equave)) / math.log(2) | ||
end | |||
-- convert ratio to steps | |||
-- ratio is a float! | |||
-- towards is one of: -1 (floor), 0 (nearest), 1 (ceil) | |||
function p.approximate(et, ratio, towards) | |||
towards = towards or 0 | |||
if et.size == 0 then | |||
return 0 | |||
end | |||
local exact = math.log(ratio) / math.log(rat.as_float(et.equave)) * et.size | |||
if towards < 0 then | |||
return math.floor(exact) | |||
elseif towards > 0 then | |||
return math.ceil(exact) | |||
else | |||
return math.floor(exact + 0.5) | |||
end | |||
end | end | ||
return p | return p | ||
Revision as of 18:47, 3 October 2022
- This module primarily serves as a library for other modules and has no corresponding template.
This module provides helper functions for equal-step tunings.
| Introspection summary for Module:ET | |||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
| ||||||||||||||||||||||||||||||||||
local rat = require('Module:Rational')
local p = {}
local common_suffix = {
['3/2'] = 'f',
['2'] = 'o',
['2/1'] = 'o',
['3'] = 't',
['3/1'] = 't',
}
local common_ratio = {
['f'] = rat.new(3, 2),
['o'] = 2,
['t'] = 3
}
-- create a ET structure <size>ed<equave>
function p.new(size, equave, suffix)
size = size or 12
equave = equave or 2
if suffix == nil then
local equave_n, equave_m = rat.as_pair(equave)
local equave_ratio = rat.as_ratio(equave)
suffix = size .. 'ed'
if common_suffix[equave_ratio] then
suffix = suffix .. common_suffix[equave_ratio]
elseif equave_m == 1 then
suffix = suffix .. equave_n
else
suffix = suffix .. equave_ratio
end
end
return { size = size, equave = equave, suffix = suffix }
end
-- parse a ET structure
function p.parse(unparsed)
local size, suffix, equave = unparsed:match('^(%d+)([Ee][Dd](.+))$')
if equave == nil then
return nil
end
size = tonumber(size)
equave = common_ratio[equave] or rat.parse(equave)
if size == nil or equave == nil then
return nil
end
return p.new(size, equave, suffix)
end
-- construct a string representation for a ET structure
function p.as_string(et)
return et.size .. et.suffix
end
-- convert steps to a proper ratio (except that it is a float approximation)
function p.backslash_ratio(et, steps)
if et.size == 0 then
return 1
end
return rat.as_float(et.equave) ^ (steps / et.size)
end
-- convert steps to cents
function p.cents(et, steps)
steps = steps or 1
return 1200 * steps / et.size * math.log(rat.as_float(et.equave)) / math.log(2)
end
-- convert ratio to steps
-- ratio is a float!
-- towards is one of: -1 (floor), 0 (nearest), 1 (ceil)
function p.approximate(et, ratio, towards)
towards = towards or 0
if et.size == 0 then
return 0
end
local exact = math.log(ratio) / math.log(rat.as_float(et.equave)) * et.size
if towards < 0 then
return math.floor(exact)
elseif towards > 0 then
return math.ceil(exact)
else
return math.floor(exact + 0.5)
end
end
return p