Ero sivun ”Moduuli:Kitarakirja” versioiden välillä

Poistettu sisältö Lisätty sisältö
Hunsvotti (keskustelu | muokkaukset)
Ei muokkausyhteenvetoa
Hunsvotti (keskustelu | muokkaukset)
Ei muokkausyhteenvetoa
Rivi 1:
--- Pilkkoo merkkijonon osiin.
local NUOTIT = {
function mw.ustring:split(sep)
C = 0,
CISlocal fields = 1,{}
local pattern = mw.ustring.format("([^%s]+)", sep)
D = 2,
self:gsub(pattern, function(c) fields[#fields+1] = c end)
DIS = 3,
Ereturn = 4,fields
end
F = 5,
FIS = 6,
G = 7,
GIS = 8,
A = 9,
AIS = 10,
B = 11,
}
 
--- Tekee matalan kopion taulukosta.
function table.clone(orig)
local orig_type = type(orig)
local copy = {}
for orig_key, orig_value in pairs(orig) do
copy[orig_key] = orig_value
end
return copy
end
 
local viritys = {
NUOTIT.E + 2*12,
NUOTIT.A + 2*12,
NUOTIT.D + 3*12,
NUOTIT.G + 3*12,
NUOTIT.B + 3*12,
NUOTIT.E + 4*12,
}
 
 
-- Tiedot korvataan mahdollisilla annetuilla nimillä.
-- Nuoteista käytetyt nimitykset. Jos parametri nuotit1 tai nuotit2 on annettu korvataan
local nuotinnimet = {
-- taulukon arvot annetuilla arvoilla. Nuotit ovat taulukossa abcdefg-järjestelmän mukaan.
"c",
-- H lisätään vasta tulostusvaiheessa.
"c♯<br/>d♭",
local notenames1 = { "c", "c♯", "d", "d♯", "e", "f", "f♯", "g", "g♯", "a", "a♯", "b" }
"d",
local notenames2 = { "c", "d♭", "d", "e♭", "e", "f", "g♭", "g", "a♭", "a", "b♭", "b" }
"d♯<br/>e♭",
"e",
"f",
"f♯<br/>g♭",
"g",
"g♯<br/>a♭",
"a",
"a♯<br/>b♭<br/>(b)",
"b<br/>(h)"
}
 
 
-- Indeksit nuottinnimet-taulukkoontaulukoihin
local indeces = {
["c"] = 0,
Rivi 77 ⟶ 64:
["b𝄪"] = 1,
["c𝄫"] = 10,
["c♭"] = 11 },
["b𝄫 (b♭)"] = 9,
["b♭ (b)"] = 10,
["b (h)"] = 11,
["b♯ (h♯)"] = 0,
["b𝄪 (h𝄪)"] = 1,
}
 
 
local viritys = {
function lilyoctave(index)
indeces["e"] + 2*12,
indeces["a"] + 2*12,
indeces["d"] + 3*12,
indeces["g"] + 3*12,
indeces["b"] + 3*12,
indeces["e"] + 4*12,
}
 
 
 
--- Lilypondin käyttämä oktaavimerkintä.
-- param index: oktaavi suhteessa keski-c:hen
-- param notename: nuotin nimi
-- return: oktaavi Lilypond-muodossa, esim. ","
function lilyoctave(index, notename)
-- Vastaavat c:tä ja cisiä, mutta merkitään oktaavia alemmas.
if notename == "b♯" or notename == "b𝄪" then
index = index - 1
end
if index < 0 then
return string.rep(",", -index)
Rivi 89 ⟶ 102:
end
 
-- Muuttaa nuotinnimen lilypond-Lilypondin käyttämään muotoon, esim. "e♭" -> "ees"
function lilyfy(notename)
local out = notename
if notename:find("/") then
error("Enharmoniaa ei ole ratkaistu: " .. notename)
end
 
out = string.gsub(out, "♯", "is")
out = string.gsub(out, "𝄪", "isis")
Rivi 105 ⟶ 114:
 
--- Palauttaa nuotin nimen oktaaveineen Lilypond-notaatiolla, esim. "dis''".
-- param noteset: nuottijoukko josta nuotit valitaan
function lilynote(index)
-- param index: absoluuttinen nuotin indeksi (jossa 36 vastaa keski-c:tä)
local octave = math.floor((index - (NUOTIT.C + 3*12)) / 12)
function lilynote(noteset, index)
local success, note = pcall(lilyfy, nuotinnimet[index % 12 + 1])
local octave = math.floor((index - (indeces["c"] + 3*12)) / 12)
local note = noteset[index % 12 + 1]
 
if successindex == -1 then
return note .. lilyoctave(octave)""
end
return lilyfy(note) .. lilyoctave(octave, note)
return ""
end
 
--- Palauttaa tabulatuuriin tarkoitetun nuotin nimen Lilypond-notaatiolla, esim. "dis''".
function notename(index)
-- Tässä ei tarvitse ottaa huomioon enharmonisia nuotteja vaan käytettään vain toista
local name = nuotinnimet[index % 12 + 1]
-- niistä.
if name == "b" then
-- param index: nuotin absoluuttinen indeksi
return "b<br/>(h)"
function lilytabnote(index, stringno)
elseif name == "b♭" then
local octave = math.floor((index - (indeces["c"] + 3*12)) / 12)
return "b♭<br/>(b)"
local note = notenames1[index % 12 + 1]
elseif name == "b♭" then
 
return "b𝄫<br/>(b♭)"
elseifif nameindex == "b♭"-1 then
return "b♯<br/>(h♯)"
elseif name == "b♭" then
return "b𝄪<br/>(h𝄪)"
end
 
return name
return lilyfy(note) .. lilyoctave(octave, note) .. "\\" .. stringno
end
 
-- Taulukko nuotinnimienm muuttamiseen sisäisestä muodosta tulostettavaan muotoon.
function mw.ustring:split(sep)
local sep, fieldssimple_to_output = sep or ":", {}
["b♭"] = "b♭<br/>(b)",
local pattern = mw.ustring.format("([^%s]+)", sep)
["b"] = "b<br/>(h)",
self:gsub(pattern, function(c) fields[#fields+1] = c end)
["b♭"] = "b♭<br/>(b)",
return fields
["b𝄫"] = "b𝄫<br/>(b♭)",
["b♯"] = "b♯<br/>(h♯)",
["b𝄪"] = "b𝄪<br/>(h𝄪)"
}
 
-- Taulukko syötteen muuttamiseen sisäiseen muotoon.
local input_to_simple = {
["b♭ (b)"] = "b♭",
["b (h)"] = "b",
["b♭ (b)"] = "b♭",
["b𝄫 (b♭)"] = "b𝄫",
["b♯ (h♯)"] = "b♯",
["b𝄪 (h𝄪)"] = "b𝄪"
}
 
 
--- Muuttaa nuotin nimen tulostettavaan muotoon ts. lisää h-vaihtoehdon b-nuotteihin.
local function notename_to_print(note)
return simple_to_output[note] or note
end
 
-- Muuttaa mahdollisen "b (h)"-muodossa annetun nuottinimen sisäiseen muotoon.
function table.clone(orig)
local orig_type =function typeinput_to_notename(orignote)
return input_to_simple(note)
local copy = {}
end
 
for orig_key, orig_value in pairs(orig) do
--- Palauttaa otetaulukon nuotinnimiparametrissa näytettävän tekstin.
copy[orig_key] = orig_value
-- param noteset1: ensimmäisen variantin nuotit
-- param noteset2: toisen variantin nuotit
-- param index: nuotin absoluuttinen indeksi
-- return: esim. "g♯<br/>ab".
local function printable_notename(index)
local note1 = notenames1[index % 12 + 1]
local note2 = notenames2[index % 12 + 1]
 
--print("ntoe1: " .. note1 .. ", note2: " .. note2)
if note2 == nil or note1 == note2 then
return notename_to_print(note1)
end
return notename_to_print(note1) .. "<br/>" .. notename_to_print(note2)
return copy
end
 
local p = {}
 
 
--- Palauttaa soinnun nuotit lilypond notaatiolla. Nuotit valitaan notenamesistä.
-- param notenames: jompi kumpi globaaleista taulukoista notenames1 tai notenames2
-- noteindeces: taulukko, jossa on joka kielelle oma nuotti-indeksi
-- return: taulukko, jossa on joka kielelle nuotti Lilypondin käyttämässä muodossa
local function make_lilynotes(notenames, noteindeces)
local lilynotes = { "", "", "", "", "", "" }
print("notenames: " .. table.concat(notenames, " "))
lilynotes[1] = lilynote(notenames, noteindeces[1])
lilynotes[2] = lilynote(notenames, noteindeces[2])
lilynotes[3] = lilynote(notenames, noteindeces[3])
lilynotes[4] = lilynote(notenames, noteindeces[4])
lilynotes[5] = lilynote(notenames, noteindeces[5])
lilynotes[6] = lilynote(notenames, noteindeces[6])
 
return lilynotes
end
 
--- Palauttaa nuottimerkinnän tuottavan Lilypond-koodin (\Staff-lohkon).
-- Jos sointuja on kaksi varianttia, on palautettavan koodilohkon aikayksikkö 2/1. Jos
-- yksi on aikayksikkö 1/1.
-- param lilynotes1: ensimmäisen sointuvariantin nuotit Lilypond-muodossa
-- param lilynotes2: toisen sointuvariantin nuotit Lilypond-muodossa
-- return: \Staff-lohkon koodi
-- return: 1 jos yksi sointu, 2 jos kaksi sointua
local function get_staff_markup(lilynotes1, lilynotes2)
local notes_str1 = table.concat(lilynotes1, " ")
local notes_str2 = table.concat(lilynotes2, " ")
if notes_str2 == notes_str1 then
notes_str2 = ""
elseif notes_str1 == "" then
notes_str1 = notes_str2
notes_str2 = ""
end
 
 
if notes_str1 == "" and notes_str2 == "" then
return "", 1
elseif notes_str1 ~= "" and notes_str2 ~= "" then
return [=[
\new Staff {
\clef "treble_8"
\once \override Staff.TimeSignature #'stencil = ##f
<]=] .. notes_str1 .. [=[>1 | <]=] .. notes_str2 .. [=[>1 |
}
]=], 2
elseif notes_str1 ~= "" then
return [=[
\new Staff {
\clef "treble_8"
\once \override Staff.TimeSignature #'stencil = ##f
<]=] .. notes_str1 .. [=[>1
}
]=], 1
end
 
end
 
--- Muotoilee TabStaff-lohkon.
-- param noteindeces: nuotinnettava sointu absoluuttisina indekseinä
-- param time: aikayksikön nimittäjä (1 tai 2)
-- return: TabStaff-lohkon koodi
local function get_tabstaff_markup(noteindeces, time)
local lilynotestab = { "", "", "", "", "", "" }
 
-- Haetaan nuotit, josta generoidaan tabulatuuri. Nuottien nimillä
-- (onko esim. cis vai des) ei tässä ole väliä.
lilynotestab[1] = lilytabnote(noteindeces[1], 6)
lilynotestab[2] = lilytabnote(noteindeces[2], 5)
lilynotestab[3] = lilytabnote(noteindeces[3], 4)
lilynotestab[4] = lilytabnote(noteindeces[4], 3)
lilynotestab[5] = lilytabnote(noteindeces[5], 2)
lilynotestab[6] = lilytabnote(noteindeces[6], 1)
 
if time == 2 then
return [=[
\new TabStaff {
\override Stem #'transparent = ##t
\override Beam #'transparent = ##t
s2 <]=] .. table.concat(lilynotestab, " ") .. [=[>1 s2
}
]=]
else
return [=[
\new TabStaff {
\override Stem #'transparent = ##t
\override Beam #'transparent = ##t
<]=] .. table.concat(lilynotestab, " ") .. [=[>1
}
]=]
end
end
 
local function get_lilymarkup(noteset1, noteset2, noteindeces)
local lilynotes1 = {}
local lilynotes2 = {}
local notation_markup, tabstaff_markup
local time
local success
 
-- Nuotit, joista generoidaan nuottimerkintä.
success, lilynotes1 = pcall(make_lilynotes, noteset1, noteindeces)
if not success then
lilynotes1 = {}
end
success, lilynotes2 = pcall(make_lilynotes, noteset2, noteindeces)
if not success then
lilynotes2 = {}
end
notation_markup, time = get_staff_markup(lilynotes1, lilynotes2)
 
-- Nuotit, joista generoidaan tabulatuuri.
tabstaff_markup = get_tabstaff_markup(noteindeces, time)
 
return [=[
<<
\override Score.BarLine.break-visibility = ##(#f #t #t)
\time ]=] .. time .. [=[/1
]=] .. notation_markup .. [=[
 
]=] .. tabstaff_markup .. [=[
>>
]=]
end
 
function p.Sointuote(frame)
Rivi 161 ⟶ 327:
local nauha = (tonumber(frame.args.nauhanumero) or 1) - 1
local output = table.clone(frame.args)
local notenamesnotenames1 = {}
local notenames2 = {}
local noteindeces = { -1, -1, -1, -1, -1, -1 }
local noteset1 = { nil, nil, nil, nil, nil, nil }
local noteset2 = { nil, nil, nil, nil, nil, nil }
 
if frame.args.nuotitnuotit1 then
notenamesnotenames1 = mw.text.split(frame.args.nuotitnuotit1, "–", true)
if frame.args.nuotit2 then
notenames2 = mw.text.split(frame.args.nuotit2, "–", true)
end
end
-- Annetaan uudet nimet oletusnuotinnimien tilalle
for i, v in ipairs(notenamesnotenames1) do
local index = indeces[v]
if index > -1 then
nuotinnimetnoteset1[index + 1] = v
notenames1[index + 1] = v
if not frame.args.nuotit2 then
notenames2[index + 1] = v
end
end
end
 
for i, param in ipairs(frame.args) do
for i, v in ipairs(notenames2) do
local index = indeces[v]
if index then
noteset2[index + 1] = v
notenames2[index + 1] = v
end
end
--print("notenames2: " .. table.concat(notenames2, " "))
 
print("noteset1: " .. table.concat(noteset1, " "))
 
 
for i, cur in ipairs(frame.args) do
-- Poistetaan tyhjät alusta ja lopusta.
cur = cur:gsub("^%s*(.-)%s*$", "%1")
-- Kielen ja nauhan numero.
s_idx = (i - 1) % 6 + 1
if s_idx == 1 then
Rivi 182 ⟶ 375:
end
if paramcur == "o" and i >= 0 and i < 7 then -- Vapaat kielet.
frets[s_idx] = f_idx
elseif paramcur == "o" or paramcur == "C" or paramcur == "D" or paramcur == "-" then
frets[s_idx] = f_idx + nauha
end
 
-- Sormitukset.
if i > 36 and i <= 42 then
fingering[s_idx] = paramcur
end
end
 
-- Nuotinnimi-parametrit.
local lilyoutput = {
"",
"",
"",
"",
"",
"" }
 
 
output[43] = ""
output[44] = ""
Rivi 211 ⟶ 397:
if frets[1] ~= -1 then
noteindeces[1] = (viritys[1] + frets[1])
output[43] = notenameprintable_notename(noteindeces[1])
lilyoutput[1] = lilynote(noteindeces[1])
end
if frets[2] ~= -1 then
noteindeces[2] = (viritys[2] + frets[2])
output[44] = notenameprintable_notename(noteindeces[2])
lilyoutput[2] = lilynote(noteindeces[2])
end
if frets[3] ~= -1 then
noteindeces[3] = (viritys[3] + frets[3])
output[45] = notenameprintable_notename(noteindeces[3])
lilyoutput[3] = lilynote(noteindeces[3])
end
if frets[4] ~= -1 then
noteindeces[4] = (viritys[4] + frets[4])
output[46] = notenameprintable_notename(noteindeces[4])
lilyoutput[4] = lilynote(noteindeces[4])
end
if frets[5] ~= -1 then
noteindeces[5] = (viritys[5] + frets[5])
output[47] = notenameprintable_notename(noteindeces[5])
lilyoutput[5] = lilynote(noteindeces[5])
end
if frets[6] ~= -1 then
noteindeces[6] = (viritys[6] + frets[6])
output[48] = notenameprintable_notename(noteindeces[6])
lilyoutput[6] = lilynote(noteindeces[6])
end
 
lilyoutput = get_lilymarkup(noteset1, noteset2, noteindeces)
 
return frame:expandTemplate{ title = "Kitaran sointuote", args = output }
local lilyoutputtab = table.clone(lilyoutput)
.. "<div style=\"display: inline-block; vertical-align: top;\">"
.. frame:extensionTag{ name = "score", content = lilyoutput } .. "</div>"
-- Lisätään kielen numero nuotteihin, joista luodaan tabulatuuri.
for i, v in ipairs(lilyoutputtab) do
if lilyoutputtab[i] ~= "" then
lilyoutputtab[i] = lilyoutputtab[i] .. "\\" .. (6 - i + 1)
end
end
 
lilyout = [=[
<<
\override Score.BarLine.break-visibility = ##(#f #t #t)
\new Staff {
\clef "treble_8"
\once \override Staff.TimeSignature #'stencil = ##f
<]=] .. table.concat(lilyoutput, " ") .. [=[>1
}
\new TabStaff {
\override Stem #'transparent = ##t
\override Beam #'transparent = ##t
<]=] .. table.concat(lilyoutputtab, " ") .. [=[>
}
>>
 
]=]
 
return frame:expandTemplate{ title = "Kitaran sointuote", args = output }
.. "<div style=\"display: inline-block; vertical-align: top;\">"
.. frame:extensionTag{ name = "score", content = lilyout } .. "</div>"
end