Moduuli:Kitarakirja/Lily

Wikikirjastosta

Tämän moduulin ohjeistuksen voi tehdä sivulle Moduuli:Kitarakirja/Lily/ohje

--- Funktioita nuottien Lilypond-muotoon muuttamiseksi.
local p = {}

local notedata = require "Moduuli:Kitarakirja/Nuottidata"


--- Lilypondin käyttämä oktaavimerkintä.
-- param index:     oktaavi suhteessa keski-c:hen
-- param notename:  nuotin nimi
-- return:          oktaavi Lilypond-muodossa, esim. ","
function p.getOctaveString(index, notename)
   -- Merkitään eri oktaaviin kuin soivat
   if notename == "b♯" or notename == "b𝄪" then
      index = index - 1
   elseif notename == "c♭" or notename == "c𝄫" then
      index = index + 1
   end
   
   if index < 0 then
      return string.rep(",", -index)
   elseif index > 0 then
      return string.rep("'", index)
   end
   return ""
end

--- Muuttaa annetut nuotinnimet Lilypondin käyttämään muotoon.
-- Esim. { "c", "e♭", "g" } -> "c", "ees", "g"
-- param args: Nuotinnimitaulukko.
function p.lilyfy(arg)
   local output = {}

   for i, notename in ipairs(arg) do
      local out = notename
      out = string.gsub(out, "♯", "is")
      out = string.gsub(out, "𝄪", "isis")
      out = string.gsub(out, "♭", "es")
      out = string.gsub(out, "𝄫", "eses")
      table.insert(output, out)
   end

   return unpack(output)
end


--- Palauttaa nuotinnimen oktaaveineen Lilypond-notaatiolla, esim. "dis''".
-- param noteset:  nuottijoukko josta nuotinnimi valitaan
-- param index:    absoluuttinen nuotin indeksi (jossa 36 vastaa keski-c:tä)
function p.toLilynote(noteset, index)
   local octave = math.floor((index - (notedata.getNoteIndex("c", 0))) / 12)
   local note   = noteset[index % 12 + 1]

   if index == -1 then
      return ""
   end
   
   return p.lilyfy{note} .. p.getOctaveString(octave, note)
end

--- Palauttaa nuotinnimen oktaaveineen Lilypond-notaatiolla.
-- Esim. { { "", 2} } -> "dis''".
-- param args: taulukko nuotti–oktaavi-pareja
function p.toLilyformat(args)
   local output = {}

   for i, note in ipairs(args) do
      local notename = note[1]
      local octave = note[2]
      table.insert(output, p.lilyfy{notename} .. p.getOctaveString(octave, notename))
   end

   return output
end


return p