Moduuli:Musiikin taulukot/Skaala

Wikikirjastosta

Tämän moduulin ohjeistuksen voi tehdä sivulle Moduuli:Musiikin taulukot/Skaala/ohje

local nuottidata = require("Moduuli:Kitarakirja/Nuottidata")

local m = {}

local function sign(num)
    if num > 0 then
        return 1
    elseif num < 1 then
        return -1
    end
    
    return 0
end

local function formatDiff(chromatic_diff)
    local whole = math.floor(math.abs(chromatic_diff) / 2)

    local sign_chr = ""
    if sign(chromatic_diff) == -1 then
        sign_chr = "−"
    end

    local half_chr = ""
    if chromatic_diff % 2 == 1 then
        half_chr = "½"
    end

    local whole_str = ""
    if whole ~= 0 then
        whole_str = tostring(whole)
    end
    
    return sign_chr .. whole_str .. half_chr

end


function m.Skaalataulukko(frame)
    local names = {}
    local diffs = {}
    local prev = nil
    
    for index, intervalName in ipairs(frame:getParent().args) do
        local chromaticDiff, _, _ = nuottidata.intervalToDiffs(intervalName)
        if not chromaticDiff then
            error('Virheellinen intervallin nimi: "' .. intervalName .. '"')
        end

        table.insert(names, intervalName)
        
        if index > 1 then
            table.insert(diffs, formatDiff(chromaticDiff - prev))
        end

        prev = chromaticDiff
    end

    return [=[
{| class="skaalataulukko"
|- class="intervallit"
| ]=] .. table.concat(names, " ||–|| ") .. [=[ 
|- class="valit"
| || ]=] .. table.concat(diffs, " || || ") .. [=[ || 
|-
|}
]=]
    
end

return m