1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-06-28 14:18:41 +02:00

Steve Daulton's fix for PitchIndex() to round in the correct direction for negative MIDI numbers.

This commit is contained in:
v.audacity 2013-06-26 00:18:01 +00:00
parent 08dd4d9813
commit 7d7b0a3e00

View File

@ -36,11 +36,14 @@ double MIDInoteToFreq(const double dMIDInote)
unsigned int PitchIndex(const double dMIDInote)
{
int nPitchIndex = ((int)(dMIDInote + 0.5) % 12);
// MIDI numbers can be negative.
// Because of the modulo, we know we're within 12 of positive.
// MIDI numbers can be negative. Round in the right direction.
double dRound = (dMIDInote < 0.0) ? -0.5 : 0.5;
int nPitchIndex = ((int)(dMIDInote + dRound) % 12);
// Because of the modulo, we know we're within 12 of positive, if dMIDInote is negative.
if (nPitchIndex < 0)
nPitchIndex += 12;
return nPitchIndex;
}