comparison base/Pitch.cpp @ 141:4f26f623a8bc

* Finish preferences dialog (as far as it's going at the moment) and connect it up * Fix Parzen window shape (was triangular!) * Various fixes to spectrogram draw coordinates in smoothing mode etc * Draw C keys in grey on the piano
author Chris Cannam
date Fri, 21 Jul 2006 16:03:42 +0000
parents d397ea0a79f5
children e412f65884ee
comparison
equal deleted inserted replaced
140:a35098a9c814 141:4f26f623a8bc
12 License, or (at your option) any later version. See the file 12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information. 13 COPYING included with this distribution for more information.
14 */ 14 */
15 15
16 #include "Pitch.h" 16 #include "Pitch.h"
17 #include "Preferences.h"
17 18
18 #include <cmath> 19 #include <cmath>
19 20
20 float 21 float
21 Pitch::getFrequencyForPitch(int midiPitch, 22 Pitch::getFrequencyForPitch(int midiPitch,
22 float centsOffset, 23 float centsOffset,
23 float concertA) 24 float concertA)
24 { 25 {
26 if (concertA <= 0.0) {
27 concertA = Preferences::getInstance()->getTuningFrequency();
28 }
25 float p = float(midiPitch) + (centsOffset / 100); 29 float p = float(midiPitch) + (centsOffset / 100);
26 return concertA * powf(2.0, (p - 69.0) / 12.0); 30 return concertA * powf(2.0, (p - 69.0) / 12.0);
27 } 31 }
28 32
29 int 33 int
30 Pitch::getPitchForFrequency(float frequency, 34 Pitch::getPitchForFrequency(float frequency,
31 float *centsOffsetReturn, 35 float *centsOffsetReturn,
32 float concertA) 36 float concertA)
33 { 37 {
38 if (concertA <= 0.0) {
39 concertA = Preferences::getInstance()->getTuningFrequency();
40 }
34 float p = 12.0 * (log(frequency / (concertA / 2.0)) / log(2.0)) + 57.0; 41 float p = 12.0 * (log(frequency / (concertA / 2.0)) / log(2.0)) + 57.0;
35 42
36 int midiPitch = int(p + 0.00001); 43 int midiPitch = int(p + 0.00001);
37 float centsOffset = (p - midiPitch) * 100.0; 44 float centsOffset = (p - midiPitch) * 100.0;
38 45
84 QString 91 QString
85 Pitch::getPitchLabelForFrequency(float frequency, 92 Pitch::getPitchLabelForFrequency(float frequency,
86 float concertA, 93 float concertA,
87 bool useFlats) 94 bool useFlats)
88 { 95 {
96 if (concertA <= 0.0) {
97 concertA = Preferences::getInstance()->getTuningFrequency();
98 }
89 float centsOffset = 0.0; 99 float centsOffset = 0.0;
90 int midiPitch = getPitchForFrequency(frequency, &centsOffset, concertA); 100 int midiPitch = getPitchForFrequency(frequency, &centsOffset, concertA);
91 return getPitchLabel(midiPitch, centsOffset, useFlats); 101 return getPitchLabel(midiPitch, centsOffset, useFlats);
92 } 102 }
93 103