Module:Ups and downs notation

From Xenharmonic Wiki
Revision as of 23:07, 7 June 2023 by CompactStar (talk | contribs)
Jump to navigation Jump to search
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 (1)
Line Function Params
25 get_note_names (tuning)
Lua modules required (2)
Variable Module Functions used
et Module:ET approximate
rat Module:Rational new

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


-- WIP

local et = require('Module:ET')
local rat = require('Module:Rational')
local p = {}

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

function p.get_note_names(tuning)
	local note_names = {}
	local i = 0
	while i < tuning.size do
		note_names[i] = {}
	end
	
	local fifth = et.approximate(tuning, rat.new(3, 2))
	local fourth = et.approximate(tuning, rat.new(4, 3))
	local chroma = (fifth * 7) % et.size
	
	-- Add major scale notes
	table.insert(note_names[0], "C")
	table.insert(note_names[(fifth * 2) % tuning.size], "D")
	table.insert(note_names[(fifth * 4) % tuning.size], "E")
	table.insert(note_names[fourth], "F")
	table.insert(note_names[fifth], "G")
	table.insert(note_names[(fifth * 3) % tuning.size], "A")
    table.insert(note_names[(fifth * 5) % tuning.size], "B")
    table.insert(note_names[et.size], "C")
	
	i = 0
	local last_major_note = 0
	while i < tuning.size do
		if #(note_names[i]) == 0 then
			local num_sharps = math.floor((i - last_major_note) / chroma)
			local num_ups = (i - last_major_note) % chroma
			local name = interval_names[last_major_note]
			local j = 0
			while j < num_sharps do
				name = name + "#"
			end
			j = 0
			while j < num_ups do
				name = "^" + name
			end
			table.insert(note_names[i], note_names[last_note])
		else
			last_major_note = i
		end
		i = i + 1
	end
	

end

return p