Module:Ups and downs notation: Difference between revisions

From Xenharmonic Wiki
Jump to navigation Jump to search
CompactStar (talk | contribs)
No edit summary
CompactStar (talk | contribs)
No edit summary
Line 43: Line 43:
local fourth = et.size - fifth
local fourth = et.size - fifth
local chroma = (fifth * 7) % et.size
local chroma = (fifth * 7) % et.size
local sharp_flat_swap = fifth/et.size < 4/7 and (sharp_lowers == nil or sharp_lowers == "")
local is_mavila = fifth/et.size < 4/7
if is_mavila then
chroma = et.size - chroma
end
local sharp_flat_swap = is_mavila and (sharp_lowers == nil or sharp_lowers == "")
 
local major_note_idx = {
local major_note_idx = {

Revision as of 01:16, 12 June 2023

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
35 get_note_names_table (et, fifth, sharp_lowers)
124 ups_and_downs_note_name (invokable) (frame)
Lua modules required (1)
Variable Module Functions used
ET Module:ET parse

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


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


local function table_contains(tbl, x)
    found = false
    for _, v in pairs(tbl) do
        if v == x then 
            found = true 
        end
    end
    return found
end


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

function p.get_note_names_table(et, fifth, sharp_lowers)
	local note_names = {}
	for i = 0,et.size do
		note_names[i] = {}
		i = i + 1
	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 is_mavila = fifth/et.size < 4/7
	if is_mavila then
		chroma = et.size - chroma
	end
	local sharp_flat_swap = is_mavila and (sharp_lowers == nil or sharp_lowers == "")

	
	local major_note_idx = {
		0, 
		(fifth * 2) % et.size,
		(fifth * 4) % et.size,
		fourth,
		fifth,
		(fifth * 3) % et.size,
		(fifth * 5) % et.size,
		et.size
	}

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

	local last_major_note = 0
	
	-- Add sharp/up notes
	for i = 0,et.size-1 do
		if table_contains(major_note_idx, i) then
			last_major_note = i % 8
		else
			local num_sharps = math.floor((i - last_major_note) / chroma)
			local num_ups = (i - last_major_note) % chroma
			if chroma == 0 then
				num_sharps = 0
				num_ups = (i - last_major_note)
			end
			local last_major_names = note_names[last_major_note]
			for j = 1,(#last_major_names) do
				if sharp_flat_swap then
					table.insert(note_names[i], string.rep("^", num_ups) .. last_major_names[j] .. string.rep("b", num_sharps))
				else
					table.insert(note_names[i], string.rep("^", num_ups) .. last_major_names[j] .. string.rep("#", num_sharps))
				end
			end
		end
	end
	
	last_major_note = et.size - 1

	-- Add flat/down notes
	for i = et.size-1,0,-1 do
		if table_contains(major_note_idx, i) then
			last_major_note = i
		else
			local num_flats = math.floor((last_major_note - i) / chroma)
			local num_downs = (last_major_note - i) % chroma
			if chroma == 0 then
				num_flats = 0
				num_downs = (last_major_note - i)
			end
			last_major_names = note_names[last_major_note] 
			for j = 1,(#last_major_names) do
				if sharp_flat_swap then
					table.insert(note_names[i], string.rep("v", num_downs) .. last_major_names[j] .. string.rep("#", num_flats))
				else
					table.insert(note_names[i], string.rep("v", num_downs) .. last_major_names[j] .. string.rep("b", 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"])
	local sharp_lowers = tonumber(frame.args["sharp_lowers"])
	return table.concat(p.get_note_names_table(et, fifth, sharp_lowers)[step], ",")
end

return p