Sunday, 15 June 2014

math - Using PHP/HTML to calculate and display inversions of 4-part chords -



math - Using PHP/HTML to calculate and display inversions of 4-part chords -

i writing php application represents 6 strings of guitar series of ranges. in way, can add together or subtract set of numbers systematically alter archetypical construction define.

the ranges are:

//e: 1-15, a: 26-40, d: 51-65, g: 76-90, b: 101-115, e: 126-140

i having problem code below.

i hope getting little help on math used in function, can move myself in right direction solve myself.

the function

//the key of key value pair represents position on guitar, , value //represents theoretical function. if inversion "0", nothing. if //inversion "1", notes have value of "r" should have key //incremented 4 , value changed '3'. //example: 1=>"r",28=>"5",52=>"7",77=>"3" function invchange($pattern, $inversion) { if ($inversion == 1) { foreach ($pattern $position => $function) { if ($function == 'r' ) { $position += 4; $junction = '3'; } if ($function == '3' ) { $position += 3; $junction = '5'; } if ($function == '5' ) { $position += 4; $junction = '7'; } if ($function == '7' ) { $position += 1; $junction = 'r'; } $modscale[$position] = $junction; } } if ($inversion == 2) { foreach ($pattern $position => $function) { if ($function == 'r' ) { $position += 7; $junction = '5';} if ($function == '3' ) { $position += 7; $junction = '7';} if ($function == '5' ) { $position += 5; $junction = 'r';} if ($function == '7' ) { $position += 5; $junction = '3';} $modscale[$position] = $junction; } } if ($inversion == 3) { foreach ($pattern $position => $function) { if ($function == 'r' ) { $position += 11; $junction = '7';} if ($function == '3' ) { $position += 8; $junction = 'r';} if ($function == '5' ) { $position += 9; $junction = '3';} if ($function == '7' ) { $position += 8; $junction = '5';} $modscale[$position] = $junction; } } homecoming $modscale; }

as can see, quite repetitive. looking @ code makes me think there has got improve way. think need utilize array in infinite linear fashion:

array("root" => 4, "third" => 3, "fifth" => 4, "seventh" => 1);

now need take of predefined $note => $offset pair , jump them across array 1, 2 or 3 jumps. instance, if starts root, , makes 1 jump, need add together 4 turn "third", , alter value 'third'. if starts root , makes 2 jumps, need add together 4, add together 3, , alter value "fifth".

any help @ appreciated.

tl;dr;

well.. can math. more, restrictions you've provided, can done with, actually, one function. quadratic function.

how it's done

all need - somehow create math function, take inversion argument, , homecoming position given "function" (how it's called within php function). since have 3 values "inversion", can done quadratic function:

f(x) = ax2 + bx + c

here, you'll need find coefficients a, b, , c given x values ($inversion case) , function values (values "position" in each of "inversion" if blocks).

however, have 4 different position switches each of "function" (that's within foreach block) - that's why, actually, you'll have deal four quadratic functions.

now, junctions. i'm not sure in logical sense, but, certainly, dependence obvious: given array of possible junctions, value "function" junction @ position "current" + "inversion". so, if have junctions array ['r', '3', '5', '7'], then, if inversion 1 , current 3, result 5. if inversion 2, result 7 e t.c.

and code

here go:

function invchangen($pattern, $inversion) { $junc = ['r', '3', '5', '7']; $pos = [ 'r' => [ 1, 7, 14], '3' => [-3, 5, 14], '5' => [ 3, 5, 10], '7' => [-1, 7, 10] ]; $inversion -= 2; $modscale = []; foreach ($pattern $p => $f) { $p += ($pos[$f][0]*pow($inversion,2) + $pos[$f][1]*$inversion + $pos[$f][2])/2; $j = $junc[(array_search($f, $junc)+$inversion+2)%count($junc)]; $modscale[$p] = $j; } homecoming $modscale; }

little about, why doing $inversion -= 2. it's - transpose 3 points 1, 2 , 3 (original inversion values) -1, 0 , 1 - it's much easier calculate our a, b , c.

$pos contains arrays of coefficients quadratic function each "function" (that $f within loop). due resolving linear equations system, "half-integers" (so, have view w/2, denominator of fraction 2). that's why i've multiplied them 2 , added partition 2 position calculation.

tests

i've done tests of function , new "math" analog - successful results, are, of course of study inversion 1, 2 , 3, like:

//derived old function: array(4) { [8]=> string(1) "5" [33]=> string(1) "r" [57]=> string(1) "3" [84]=> string(1) "7" } //derived new function: array(4) { [8]=> string(1) "5" [33]=> string(1) "r" [57]=> string(1) "3" [84]=> string(1) "7" }

results above pattern [1=>"r",28=>"5",52=>"7",77=>"3"] , inversion 2

php math music guitar chord-diagram

No comments:

Post a Comment