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 18: Line 18:
-- e.g. for 12edo, intended result would be something like:
-- e.g. for 12edo, intended result would be something like:
-- {
-- {
-- 0 = "C",
-- [0] = {"C"},
-- 1 = "Db",
-- [1] = {"C#", "Db"},
-- 2 = "D",
-- [2] = {"D"},
-- 3 = "Eb",
-- [3] = {"D#", "Eb"},
-- 4 = "E",
-- [4] = {"E"},
-- 5 = "F",
-- [5] = {"F"},
-- 6 = "F#, Gb",
-- [6] = {"F#", "Gb"},
-- 7 = "G",
-- [7] = {"G"},
--  8 = "Ab",
--  [8] = {"Ab"},
--  9 = "A",
--  [9] = {"A"},
--  10 = "Bb",
--  [10] = {"Bb"},
--  11 = "B",
--  [11] = {"B"},
--  12 = "C",
--  {12} = {"C"},
-- }
-- }


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


-- Add sharp/up notes
-- Add sharp/up notes
Line 79: Line 79:
num_ups = (i - last_major_note)
num_ups = (i - last_major_note)
end
end
local name = note_names[last_major_note]
local names = note_names[last_major_note]
local j = 0
local j = 0
while j < num_sharps do
while j < #(note_names[last_major_note]) do
name = name .. "#"
local k = 0
j = j + 1
while k < num_sharps do
end
names[j] = names[j] .. "#"
j = 0
k = k + 1
while j < num_ups do
end
name = "^" .. name
k = 0
while k < num_ups do
names[j] = "^" .. names[j]
k = k + 1
end
j = j + 1
j = j + 1
end
end
Line 132: Line 136:
local et = ET.parse(frame.args['tuning']) or ET.parse('12edo')
local et = ET.parse(frame.args['tuning']) or ET.parse('12edo')
local step = tonumber(frame.args['step'])
local step = tonumber(frame.args['step'])
return p.get_note_names_table(et)[step]
return table.concat(p.get_note_names_table(et)[step], ",")
end
end


return p
return p

Revision as of 07:21, 8 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)
135 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] = {"Ab"},
--  [9] = {"A"},
--  [10] = {"Bb"},
--  [11] = {"B"},
--  {12} = {"C"},
-- }

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

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

	-- Add sharp/up notes
	i = 0
	local last_major_note = 0
	while i < et.size do
		if table_contains(major_note_idx, i) then
			last_major_note = i
		else
			local num_sharps = math.floor((i - last_major_note) / chroma)
			local num_ups = (i - last_major_note) % chroma
			if et.size % 7 == 0 then
				num_sharps = 0
				num_ups = (i - last_major_note)
			end
			local names = note_names[last_major_note]
			local j = 0
			while j < #(note_names[last_major_note]) do
				local k = 0
				while k < num_sharps do
					names[j] = names[j] .. "#"
					k = k + 1
				end
				k = 0
				while k < num_ups do
					names[j] = "^" .. names[j]
					k = k + 1
				end
				j = j + 1
			end
			note_names[i] = name
		end
		i = i + 1
	end

	-- Add flat/down notes
	i = et.size - 1
	local last_major_note = et.size - 1
	while i >= 0 do
		if table_contains(major_note_idx, i) then
			last_major_note = i
			
			local num_flats = math.floor((last_major_note - i) / chroma)
			local num_downs = (last_major_note - i) % chroma
				
			if et.size % 7 == 0 then
				num_flats = 0
				num_downs = (last_major_note - i)
			end
			
			local name = note_names[last_major_note]
			local j = 0
			while j < num_flats do
				name = name .. "b"
				j = j + 1
			end
			j = 0
			while j < num_downs do
				name = "v" .. name
				j = j + 1
			end
			note_names[i] = note_names[i] .. ", " .. name
		end
		i = i - 1
	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 step = tonumber(frame.args['step'])
	return table.concat(p.get_note_names_table(et)[step], ",")
end

return p