I am writing a PHP application that represents the six strings of the guitar as a series of ranges. In this way, I can add, or subtract to a set of numbers to systematically change an archetypical structure I define.
The ranges are:
//E: 1-15, A: 26-40, D: 51-65, G: 76-90, b: 101-115, e: 126-140
I am having trouble with the code below.
The function
//the key of the key value pair represents its position on the guitar, and the value
//represents a theoretical function. If the inversion is "0", do nothing. If the
//inversion is "1", all notes that have the value of "r" should have their key
//incremented by 4 and their value changed to '3'.
//example: 1=>"r",28=>"5",52=>"7",77=>"3"
function invChange($pattern, $inversion) {
if ($inversion == 1) {
foreach ($pattern as $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 as $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 as $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;
}
}
return $modScale;
}
As you can see, this is quite repetitive. Just looking at this code makes me think there has got to be a better way. I think what I need is to use an array in an infinite linear fashion:
array("root" => 4, "third" => 3, "fifth" => 4, "seventh" => 1);
Now I need to take any of a predefined $note => $offset pair and jump them across this array by 1, 2, or 3 jumps. For instance, if it starts as a root, and it makes one jump, I need to add 4 to turn it into a "third", and change its value to 'third'. But if it starts as a root and makes two jumps, I need to add 4, add 3, and then change its value to "fifth".