// Les hauteurs MIDI conservent le registre, tandis que la ligne des quintes mesure un autre coût.
// Une matrice de coûts relie chaque voix source à chaque note cible.
string[] Notes =
{
"C",
"C#",
"D",
"D#",
"E",
"F",
"F#",
"G",
"G#",
"A",
"A#",
"B"
};
string Nom(int midi) => $"{Notes[midi % 12]}{midi / 12 - 1}";
int Quinte(int note)
{
int v = (note % 12 * 7) % 12;
return v <= 6 ? v : v - 12;
}
int[, ] Cout(int[] s, int[] t, bool quintes = false)
{
var m = new int[s.Length, t.Length];
for (int i = 0; i < s.Length; i++)
for (int j = 0; j < t.Length; j++)
m[i, j] = quintes ? Math.Abs(Quinte(s[i]) - Quinte(t[j])) : Math.Abs(s[i] - t[j]);
return m;
}
var accords = new Dictionary<string, int[]>
{
{
"C",
new[]
{
60,
64,
67
}
},
{
"C/1",
new[]
{
64,
67,
72
}
},
{
"C/2",
new[]
{
55,
60,
64
}
},
{
"F",
new[]
{
53,
57,
60
}
},
{
"F/1",
new[]
{
57,
60,
65
}
},
{
"F/2",
new[]
{
60,
65,
69
}
},
{
"G",
new[]
{
55,
59,
62
}
},
{
"G/1",
new[]
{
59,
62,
67
}
},
{
"G/2",
new[]
{
62,
67,
71
}
},
{
"Dm",
new[]
{
62,
65,
69
}
},
{
"Dm/1",
new[]
{
53,
57,
62
}
},
{
"Dm/2",
new[]
{
57,
62,
65
}
},
{
"Am",
new[]
{
57,
60,
64
}
},
{
"Am/1",
new[]
{
60,
64,
69
}
},
{
"Am/2",
new[]
{
52,
57,
60
}
}
};
var S = accords["C"];
var T = accords["F/1"];
var chrom = Cout(S, T);
var fifth = Cout(S, T, true);
foreach (var(nom, m)in new[]
{
("Chromatique", chrom),
("Quintes", fifth)
}
)
{
Console.WriteLine(nom + " :");
for (int i = 0; i < S.Length; i++)
Console.WriteLine($"{Nom(S[i]), 3} : " + string.Join(" ", Enumerable.Range(0, T.Length).Select(j => m[i, j].ToString().PadLeft(2))));
}
Console.WriteLine($"Positions : C={Quinte(60)}, G={Quinte(67)}, E={Quinte(64)}, F={Quinte(65)}");