Mercurial > hg > svcore
diff base/Pitch.cpp @ 1025:88b54a185a0a
Use double instead of float for frequencies in Pitch, just for confidence
author | Chris Cannam |
---|---|
date | Mon, 08 Dec 2014 15:37:12 +0000 |
parents | d1ce7a4a920b |
children | cc27f35aa75c |
line wrap: on
line diff
--- a/base/Pitch.cpp Tue Dec 02 17:53:17 2014 +0000 +++ b/base/Pitch.cpp Mon Dec 08 15:37:12 2014 +0000 @@ -19,45 +19,49 @@ #include <cmath> -float +double Pitch::getFrequencyForPitch(int midiPitch, - float centsOffset, - float concertA) + double centsOffset, + double concertA) { if (concertA <= 0.0) { concertA = Preferences::getInstance()->getTuningFrequency(); } - float p = float(midiPitch) + (centsOffset / 100); - return concertA * powf(2.0, (p - 69.0) / 12.0); + double p = double(midiPitch) + (centsOffset / 100); + return concertA * pow(2.0, (p - 69.0) / 12.0); } int -Pitch::getPitchForFrequency(float frequency, - float *centsOffsetReturn, - float concertA) +Pitch::getPitchForFrequency(double frequency, + double *centsOffsetReturn, + double concertA) { if (concertA <= 0.0) { concertA = Preferences::getInstance()->getTuningFrequency(); } - float p = 12.0 * (log(frequency / (concertA / 2.0)) / log(2.0)) + 57.0; + double p = 12.0 * (log(frequency / (concertA / 2.0)) / log(2.0)) + 57.0; - int midiPitch = int(p + 0.00001); - float centsOffset = (p - midiPitch) * 100.0; + int midiPitch = int(round(p)); + double centsOffset = (p - midiPitch) * 100.0; if (centsOffset >= 50.0) { midiPitch = midiPitch + 1; centsOffset = -(100.0 - centsOffset); } + if (centsOffset < -50.0) { + midiPitch = midiPitch - 1; + centsOffset = (100.0 + centsOffset); + } if (centsOffsetReturn) *centsOffsetReturn = centsOffset; return midiPitch; } int -Pitch::getPitchForFrequencyDifference(float frequencyA, - float frequencyB, - float *centsOffsetReturn, - float concertA) +Pitch::getPitchForFrequencyDifference(double frequencyA, + double frequencyB, + double *centsOffsetReturn, + double concertA) { if (concertA <= 0.0) { concertA = Preferences::getInstance()->getTuningFrequency(); @@ -67,13 +71,13 @@ std::swap(frequencyA, frequencyB); } - float pA = 12.0 * (log(frequencyA / (concertA / 2.0)) / log(2.0)) + 57.0; - float pB = 12.0 * (log(frequencyB / (concertA / 2.0)) / log(2.0)) + 57.0; + double pA = 12.0 * (log(frequencyA / (concertA / 2.0)) / log(2.0)) + 57.0; + double pB = 12.0 * (log(frequencyB / (concertA / 2.0)) / log(2.0)) + 57.0; - float p = pB - pA; + double p = pB - pA; int midiPitch = int(p + 0.00001); - float centsOffset = (p - midiPitch) * 100.0; + double centsOffset = (p - midiPitch) * 100.0; if (centsOffset >= 50.0) { midiPitch = midiPitch + 1; @@ -129,7 +133,7 @@ QString Pitch::getPitchLabel(int midiPitch, - float centsOffset, + double centsOffset, bool useFlats) { int note, octave; @@ -137,42 +141,42 @@ QString plain = (useFlats ? flatNotes : notes)[note].arg(octave); - int ic = lrintf(centsOffset); + int ic = lrint(centsOffset); if (ic == 0) return plain; else if (ic > 0) return QString("%1+%2c").arg(plain).arg(ic); else return QString("%1%2c").arg(plain).arg(ic); } QString -Pitch::getPitchLabelForFrequency(float frequency, - float concertA, +Pitch::getPitchLabelForFrequency(double frequency, + double concertA, bool useFlats) { if (concertA <= 0.0) { concertA = Preferences::getInstance()->getTuningFrequency(); } - float centsOffset = 0.0; + double centsOffset = 0.0; int midiPitch = getPitchForFrequency(frequency, ¢sOffset, concertA); return getPitchLabel(midiPitch, centsOffset, useFlats); } QString -Pitch::getLabelForPitchRange(int semis, float cents) +Pitch::getLabelForPitchRange(int semis, double cents) { if (semis > 0) { - while (cents < 0.f) { + while (cents < 0.0) { --semis; - cents += 100.f; + cents += 100.0; } } if (semis < 0) { - while (cents > 0.f) { + while (cents > 0.0) { ++semis; - cents -= 100.f; + cents -= 100.0; } } - int ic = lrintf(cents); + int ic = lrint(cents); if (ic == 0) { if (semis >= 12) { @@ -198,10 +202,10 @@ } bool -Pitch::isFrequencyInMidiRange(float frequency, - float concertA) +Pitch::isFrequencyInMidiRange(double frequency, + double concertA) { - float centsOffset = 0.0; + double centsOffset = 0.0; int midiPitch = getPitchForFrequency(frequency, ¢sOffset, concertA); return (midiPitch >= 0 && midiPitch < 128); }