我正在编写一个 PHP 应用程序,它将吉他的六个琴弦表示为一系列范围。通过这种方式,我可以添加或减去一组数字,以系统地更改我定义的原型结构。
范围是:
//E: 1-15, A: 26-40, D: 51-65, G: 76-90, b: 101-115, e: 126-140
我在使用下面的代码时遇到问题。
功能
//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;
}
如您所见,这是相当重复的。只看这段代码就让我觉得一定有更好的方法。我认为我需要以无限线性方式使用数组:
array("root" => 4, "third" => 3, "fifth" => 4, "seventh" => 1);
现在我需要使用任何预定义的 $note => $offset 对,并将它们在这个数组中跳跃 1、2 或 3 次。例如,如果它从根开始,并且跳了一次,我需要添加 4 以将其变为“第三”,并将其值更改为“第三”。但是如果它以root开始并进行两次跳转,我需要加4,加3,然后将其值更改为“第五”。