Module:Ups and downs notation

From Xenharmonic Wiki
Jump to navigation Jump to search
Icon-Todo.png Todo: Documentation

Documentation transcluded from /doc
Note: Do not invoke this module directly; use the corresponding template instead: Template:Ups and downs note name.

local ET = require("Module:ET")
local utils = require("Module:Utils")
local p = {}

-- 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"}
-- }

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 = math.floor((i - last_diatonic_note) / (2 * chroma))
			local num_sharps = math.floor((i - last_diatonic_note) / chroma) % 2
			local num_ups = (i - last_diatonic_note) % chroma
			if chroma == 0 then
				num_double_sharps = 0
				num_sharps = 0
				num_ups = (i - last_diatonic_note)
			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_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 = math.floor((last_diatonic_note - i) / chroma)
			local num_downs = (last_diatonic_note - i) % chroma
			if chroma == 0 then
				num_flats = 0
				num_downs = (last_diatonic_note - i)
			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_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 fifth = tonumber(frame.args["fifth"])
	local step = tonumber(frame.args["step"])
	return table.concat(p.get_note_names_table(et, fifth)[step], ", "):sub(0, -1)
end

return p