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 1: Line 1:
-- WIP
-- WIP


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


Line 35: Line 35:


function p.get_note_names(tuning)
function p.get_note_names(tuning)
tuning = et.parse(tuning or '12edo')
local et = ET.parse("12edo")
local note_names = {}
local note_names = {}
local i = 0
local i = 0
Line 44: Line 44:
local fifth = u.round(math.log(3/2)/math.log(2) * tuning.size)
local fifth = u.round(math.log(3/2)/math.log(2) * tuning.size)
local fourth = u.round(math.log(4/3)/math.log(2) * tuning.size)
local fourth = u.round(math.log(4/3)/math.log(2) * tuning.size)
local chroma = (fifth * 7) % et.size
local chroma = (fifth * 7) % ET.size
local major_note_idx = {
local major_note_idx = {
0,  
0,  
(fifth * 2) % tuning.size,
(fifth * 2) % et.size,
(fifth * 4) % tuning.size,
(fifth * 4) % et.size,
fourth,
fourth,
fifth,
fifth,
(fifth * 3) % tuning.size,
(fifth * 3) % et.size,
(fifth * 5) % tuning.size,
(fifth * 5) % et.size,
tuning.size
tuning.size
}
}
Line 73: Line 73:
i = 0
i = 0
local last_major_note = 0
local last_major_note = 0
while i < tuning.size do
while i < et.size do
if table_contains(major_note_idx, i) then
if table_contains(major_note_idx, i) then
last_major_note = i
last_major_note = i
Line 94: Line 94:


-- Add flat/down notes
-- Add flat/down notes
i = tuning.size - 1
i = et.size - 1
local last_major_note = tuning.size - 1
local last_major_note = tuning.size - 1
while i >= 0 do
while i >= 0 do
Line 101: Line 101:
else
else
local num_flats = math.floor((last_major_note - i) / chroma)
local num_flats = math.floor((last_major_note - i) / chroma)
local num_downs = (last_major_note -i) % chroma
local num_downs = (last_major_note - i) % chroma
local name = interval_names[last_major_note]
local name = interval_names[last_major_note]
local j = 0
local j = 0

Revision as of 23:31, 7 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
36 get_note_names (tuning)
121 get_specific_note_name (tuning, steps)
Lua modules required (2)
Variable Module Functions used
ET Module:ET parse
u Module:Utils round

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


-- WIP

local ET = require("Module:ET")
local u = require("Module:Utils")
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"}
-- }

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


function p.get_note_names(tuning)
	local et = ET.parse("12edo")
	local note_names = {}
	local i = 0
	while i < tuning.size do
		note_names[i] = {}
	end
	
	local fifth = u.round(math.log(3/2)/math.log(2) * tuning.size)
	local fourth = u.round(math.log(4/3)/math.log(2) * tuning.size)
	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,
		tuning.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")
	
	-- temporary
	mw.logObject(note_names)
	
	-- 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
			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], name)
		end
		i = i + 1
	end

	-- Add flat/down notes
	i = et.size - 1
	local last_major_note = tuning.size - 1
	while i >= 0 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
			local name = interval_names[last_major_note]
			local j = 0
			while j < num_flats do
				name = name + "b"
			end
			j = 0
			while j < num_downs do
				name = "v" + name
			end
			table.insert(note_names[i], name)
		end
		i = i - 1
	end
	
	return note_names
end

function p.get_specific_note_name(tuning, steps)
	tbl = p.get_note_names(tuning)[steps]
	result = ''
	i = 0
	while i < #tbl do
		result = result + tbl[i] + ','
	end
	result = result:slice(0, result.len() - 1)
	return result
end

return p