Module:Ups and downs notation: Difference between revisions

From Xenharmonic Wiki
Jump to navigation Jump to search
CompactStar (talk | contribs)
No edit summary
ArrowHead294 (talk | contribs)
mNo edit summary
 
(44 intermediate revisions by 5 users not shown)
Line 1: Line 1:
local ET = require("Module:ET")
-- local u = require("Module:Utils")
local p = {}
local p = {}


 
local ET = require("Module:ET")
local function table_contains(tbl, x)
local utils = require("Module:Utils")
    found = false
local yesno = require("Module:Yesno")
    for _, v in pairs(tbl) do
        if v == x then
            found = true
        end
    end
    return found
end
 


-- Returns a table of note names
-- Returns a table of note names
-- e.g. for 12edo, intended result would be something like:
-- e.g. for 12edo, intended result would be something like:
-- {
-- {
-- [0] = {"C"},
-- [0] = {"D"},
-- [1] = {"C#", "Db"},
-- [1] = {"D#", "Eb"},
-- [2] = {"D"},
-- [2] = {"E"},
-- [3] = {"D#", "Eb"},
-- [3] = {"F"},
-- [4] = {"E"},
-- [4] = {"F#", "Gb"},
-- [5] = {"F"},
-- [5] = {"G"},
-- [6] = {"F#", "Gb"},
-- [6] = {"G#", "Ab"},
--  [7] = {"G"},
--  [7] = {"A"},
--  [8] = {"G#", Ab"},
--  [8] = {"A#", Bb"},
--  [9] = {"A"},
--  [9] = {"B"},
--  [10] = {"A#", "Bb"},
--  [10] = {"C"},
--  [11] = {"B"},
--  [11] = {"C#", "Db"},
--  {12} = {"C"}
--  [12] = {"D"}
-- }
-- }
local function round(x)
-- rounds half-integers towards 0.
return x >= 0 and math.ceil(x - 0.5) or math.floor(x + 0.5)
end


function p.get_note_names_table(et, fifth)
function p.get_note_names_table(et, fifth)
local note_names = {}
local note_names = {}
for i = 0,et.size do
for i = 0, et.size do
note_names[i] = {}
note_names[i] = {}
i = i + 1
end
end
fifth = fifth or math.floor(math.log(3/2)/math.log(2) * et.size + 0.5)
local fourth = et.size - fifth
local chroma = (fifth * 7) % et.size


local major_note_idx = {
fifth = fifth or math.floor(math.log(3 / 2) / math.log(2) * et.size + 0.5)
0,  
local fourth = et.size - fifth -- 4/3 = [2 -1>
local chroma = (fifth * 7) % et.size -- 2187/2048 = [-11 7>
local is_mavila = fifth / et.size < 4 / 7
if is_mavila then
chroma = et.size - chroma
end
 
local diatonic_note_idx = {
0,
(fifth * 2) % et.size,
(fifth * 2) % et.size,
(fifth * 4) % et.size,
(fifth * -3) % et.size,
fourth,
fourth,
fifth,
fifth,
(fifth * 3) % et.size,
(fifth * 3) % et.size,
(fifth * 5) % et.size,
(fifth * -2) % et.size,
et.size
et.size,
}
}
 
-- Add major scale notes
-- Add D dorian scale notes
table.insert(note_names[major_note_idx[1]], "C")
table.insert(note_names[diatonic_note_idx[1]], "D")
table.insert(note_names[major_note_idx[2]], "D")
table.insert(note_names[diatonic_note_idx[2]], "E")
table.insert(note_names[major_note_idx[3]], "E")
table.insert(note_names[diatonic_note_idx[3]], "F")
table.insert(note_names[major_note_idx[4]], "F")
table.insert(note_names[diatonic_note_idx[4]], "G")
table.insert(note_names[major_note_idx[5]], "G")
table.insert(note_names[diatonic_note_idx[5]], "A")
table.insert(note_names[major_note_idx[6]], "A")
table.insert(note_names[diatonic_note_idx[6]], "B")
table.insert(note_names[major_note_idx[7]], "B")
table.insert(note_names[diatonic_note_idx[7]], "C")
table.insert(note_names[major_note_idx[8]], "C")
table.insert(note_names[diatonic_note_idx[8]], "D")
 
local last_diatonic_note = 0


-- Add sharp/up notes
-- Add sharp/up notes
local last_major_note = 0
for i = 0, et.size - 1 do
for i = 0,et.size-1 do
if utils.table_contains(diatonic_note_idx, i) then
if table_contains(major_note_idx, i) then
last_diatonic_note = i
last_major_note = i
else
else
local num_sharps = math.floor((i - last_major_note) / chroma)
local num_double_sharps = 0
local num_ups = (i - last_major_note) % chroma
local num_sharps = round((i - last_diatonic_note) / chroma)
if num_sharps > 1 then
num_double_sharps = math.floor(num_sharps / 2)
num_sharps = num_sharps % 2
end
local num_ups = (i - last_diatonic_note) % chroma
local num_downs = 0
if chroma == 0 then
if chroma == 0 then
num_double_sharps = 0
num_sharps = 0
num_sharps = 0
num_ups = (i - last_major_note)
num_ups = (i - last_diatonic_note)
end
if num_ups > chroma / 2 then
num_downs = chroma - num_ups
num_ups = 0
end
end
local last_major_names = note_names[last_major_note]
local last_diatonic_names = note_names[last_diatonic_note]
for j = 0,#last_major_names-1 do
for j = 1, #last_diatonic_names do
table.insert(note_names[i], string.rep("^", num_ups) .. last_major_names[j] .. string.rep("#", num_sharps))
if num_downs >= 3 then
table.insert(
note_names[i],
"v<sup>"
.. num_downs
.. "</sup>"
.. last_diatonic_names[j]
.. string.rep("♯", num_sharps)
.. string.rep("𝄪", num_double_sharps)
)
elseif num_downs > 0 then
table.insert(
note_names[i],
string.rep("v", num_downs)
.. last_diatonic_names[j]
.. string.rep("♯", num_sharps)
.. string.rep("𝄪", num_double_sharps)
)
elseif num_ups >= 3 then
table.insert(
note_names[i],
"^<sup>"
.. num_ups
.. "</sup>"
.. last_diatonic_names[j]
.. string.rep("♯", num_sharps)
.. string.rep("𝄪", num_double_sharps)
)
else
table.insert(
note_names[i],
string.rep("^", num_ups)
.. last_diatonic_names[j]
.. string.rep("", num_sharps)
.. string.rep("𝄪", num_double_sharps)
)
end
end
end
end
end
end
end
 
last_major_note = et.size - 1
last_diatonic_note = et.size


-- Add flat/down notes
-- Add flat/down notes
for i = et.size-1,0,-1 do
for i = et.size - 1, 0, -1 do
if table_contains(major_note_idx, i) then
if utils.table_contains(diatonic_note_idx, i) then
last_major_note = i
last_diatonic_note = i
else
else
local num_flats = math.floor((last_major_note - i) / chroma)
local num_flats = round((last_diatonic_note - i) / chroma)
local num_downs = (last_major_note - i) % chroma
local num_downs = (last_diatonic_note - i) % chroma
local num_ups = 0
if chroma == 0 then
if chroma == 0 then
num_flats = 0
num_flats = 0
num_downs = (last_major_note - i)
num_downs = (last_diatonic_note - i)
end
end
last_major_names = note_names[last_major_note]  
if num_downs > chroma / 2 then
for j = 0,#last_major_names-1 do
num_ups = chroma - num_downs
name = string.rep("v", num_downs) .. last_major_names[j] .. string.rep("b", num_flats)
num_downs = 0
table.insert(note_names[i], name)
end
local last_diatonic_names = note_names[last_diatonic_note]
for j = 1, #last_diatonic_names do
if num_ups >= 3 then
table.insert(
note_names[i],
"^<sup>" .. num_ups .. "</sup>" .. last_diatonic_names[j] .. string.rep("♭", num_flats)
)
elseif num_ups > 0 then
table.insert(
note_names[i],
string.rep("^", num_ups) .. last_diatonic_names[j] .. string.rep("♭", num_flats)
)
elseif num_downs >= 3 then
table.insert(
note_names[i],
"v<sup>" .. num_downs .. "</sup>" .. last_diatonic_names[j] .. string.rep("", num_flats)
)
else
table.insert(
note_names[i],
string.rep("v", num_downs) .. last_diatonic_names[j] .. string.rep("♭", num_flats)
)
end
end
end
end
end
end
end


return note_names
return note_names
end
end
Line 111: Line 180:
function p.ups_and_downs_note_name(frame)
function p.ups_and_downs_note_name(frame)
local et = ET.parse(frame.args["tuning"]) or ET.parse("12edo")
local et = ET.parse(frame.args["tuning"]) or ET.parse("12edo")
local debugg = yesno(frame.args["debug"])
local fifth = tonumber(frame.args["fifth"])
local step = tonumber(frame.args["step"])
local step = tonumber(frame.args["step"])
return table.concat(p.get_note_names_table(et)[step], ",")
local result = table.concat(p.get_note_names_table(et, fifth)[step], ", "):sub(0, -1)
if debugg == true then
result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>"
end
return frame:preprocess(result)
end
end


return p
return p

Latest revision as of 13:23, 1 June 2025

Module documentation[view] [edit] [history] [purge]
This module should not be invoked directly; use its corresponding template instead: Template:Ups and downs note name.

This module gets the note name of an edo interval in ups and downs notation.

Introspection summary for Module:Ups and downs notation 
Functions provided (2)
Line Function Params
30 get_note_names_table (et, fifth)
180 ups_and_downs_note_name (invokable) (frame)
Lua modules required (3)
Variable Module Functions used
ET Module:ET parse
utils Module:Utils table_contains
yesno Module:Yesno yesno

No function descriptions were provided. The Lua code may have further information.


local p = {}

local ET = require("Module:ET")
local utils = require("Module:Utils")
local yesno = require("Module:Yesno")

-- Returns a table of note names
-- e.g. for 12edo, intended result would be something like:
-- {
--	[0] = {"D"},
--	[1] = {"D#", "Eb"},
--	[2] = {"E"},
--	[3] = {"F"},
--	[4] = {"F#", "Gb"},
--	[5] = {"G"},
--	[6] = {"G#", "Ab"},
--  [7] = {"A"},
--  [8] = {"A#", Bb"},
--  [9] = {"B"},
--  [10] = {"C"},
--  [11] = {"C#", "Db"},
--  [12] = {"D"}
-- }

local function round(x)
	-- rounds half-integers towards 0.
	return x >= 0 and math.ceil(x - 0.5) or math.floor(x + 0.5)
end

function p.get_note_names_table(et, fifth)
	local note_names = {}
	for i = 0, et.size do
		note_names[i] = {}
	end

	fifth = fifth or math.floor(math.log(3 / 2) / math.log(2) * et.size + 0.5)
	local fourth = et.size - fifth -- 4/3 = [2 -1>
	local chroma = (fifth * 7) % et.size -- 2187/2048 = [-11 7>
	local is_mavila = fifth / et.size < 4 / 7
	if is_mavila then
		chroma = et.size - chroma
	end

	local diatonic_note_idx = {
		0,
		(fifth * 2) % et.size,
		(fifth * -3) % et.size,
		fourth,
		fifth,
		(fifth * 3) % et.size,
		(fifth * -2) % et.size,
		et.size,
	}

	-- Add D dorian scale notes
	table.insert(note_names[diatonic_note_idx[1]], "D")
	table.insert(note_names[diatonic_note_idx[2]], "E")
	table.insert(note_names[diatonic_note_idx[3]], "F")
	table.insert(note_names[diatonic_note_idx[4]], "G")
	table.insert(note_names[diatonic_note_idx[5]], "A")
	table.insert(note_names[diatonic_note_idx[6]], "B")
	table.insert(note_names[diatonic_note_idx[7]], "C")
	table.insert(note_names[diatonic_note_idx[8]], "D")

	local last_diatonic_note = 0

	-- Add sharp/up notes
	for i = 0, et.size - 1 do
		if utils.table_contains(diatonic_note_idx, i) then
			last_diatonic_note = i
		else
			local num_double_sharps = 0
			local num_sharps = round((i - last_diatonic_note) / chroma)
			if num_sharps > 1 then
				num_double_sharps = math.floor(num_sharps / 2)
				num_sharps = num_sharps % 2
			end
			local num_ups = (i - last_diatonic_note) % chroma
			local num_downs = 0
			if chroma == 0 then
				num_double_sharps = 0
				num_sharps = 0
				num_ups = (i - last_diatonic_note)
			end
			if num_ups > chroma / 2 then
				num_downs = chroma - num_ups
				num_ups = 0
			end
			local last_diatonic_names = note_names[last_diatonic_note]
			for j = 1, #last_diatonic_names do
				if num_downs >= 3 then
					table.insert(
						note_names[i],
						"v<sup>"
							.. num_downs
							.. "</sup>"
							.. last_diatonic_names[j]
							.. string.rep("♯", num_sharps)
							.. string.rep("𝄪", num_double_sharps)
					)
				elseif num_downs > 0 then
					table.insert(
						note_names[i],
						string.rep("v", num_downs)
							.. last_diatonic_names[j]
							.. string.rep("♯", num_sharps)
							.. string.rep("𝄪", num_double_sharps)
					)
				elseif num_ups >= 3 then
					table.insert(
						note_names[i],
						"^<sup>"
							.. num_ups
							.. "</sup>"
							.. last_diatonic_names[j]
							.. string.rep("♯", num_sharps)
							.. string.rep("𝄪", num_double_sharps)
					)
				else
					table.insert(
						note_names[i],
						string.rep("^", num_ups)
							.. last_diatonic_names[j]
							.. string.rep("♯", num_sharps)
							.. string.rep("𝄪", num_double_sharps)
					)
				end
			end
		end
	end

	last_diatonic_note = et.size

	-- Add flat/down notes
	for i = et.size - 1, 0, -1 do
		if utils.table_contains(diatonic_note_idx, i) then
			last_diatonic_note = i
		else
			local num_flats = round((last_diatonic_note - i) / chroma)
			local num_downs = (last_diatonic_note - i) % chroma
			local num_ups = 0
			if chroma == 0 then
				num_flats = 0
				num_downs = (last_diatonic_note - i)
			end
			if num_downs > chroma / 2 then
				num_ups = chroma - num_downs
				num_downs = 0
			end
			local last_diatonic_names = note_names[last_diatonic_note]
			for j = 1, #last_diatonic_names do
				if num_ups >= 3 then
					table.insert(
						note_names[i],
						"^<sup>" .. num_ups .. "</sup>" .. last_diatonic_names[j] .. string.rep("♭", num_flats)
					)
				elseif num_ups > 0 then
					table.insert(
						note_names[i],
						string.rep("^", num_ups) .. last_diatonic_names[j] .. string.rep("♭", num_flats)
					)
				elseif num_downs >= 3 then
					table.insert(
						note_names[i],
						"v<sup>" .. num_downs .. "</sup>" .. last_diatonic_names[j] .. string.rep("♭", num_flats)
					)
				else
					table.insert(
						note_names[i],
						string.rep("v", num_downs) .. last_diatonic_names[j] .. string.rep("♭", num_flats)
					)
				end
			end
		end
	end

	return note_names
end

function p.ups_and_downs_note_name(frame)
	local et = ET.parse(frame.args["tuning"]) or ET.parse("12edo")
	local debugg = yesno(frame.args["debug"])
	local fifth = tonumber(frame.args["fifth"])
	local step = tonumber(frame.args["step"])
	local result = table.concat(p.get_note_names_table(et, fifth)[step], ", "):sub(0, -1)
	
	if debugg == true then
		result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>"
	end
	
	return frame:preprocess(result)
end

return p