comparison base/Pitch.cpp @ 373:0a44caddd9fe

* Add pitch range in octaves, semitones and cents to the measurement rect as well as in Hz * Avoid crash when adding time ruler This commit contains a lot of debug output (will be slow)
author Chris Cannam
date Wed, 06 Feb 2008 16:21:29 +0000
parents e412f65884ee
children ad4911230a66
comparison
equal deleted inserted replaced
372:d31b4ccb7ddb 373:0a44caddd9fe
37 { 37 {
38 if (concertA <= 0.0) { 38 if (concertA <= 0.0) {
39 concertA = Preferences::getInstance()->getTuningFrequency(); 39 concertA = Preferences::getInstance()->getTuningFrequency();
40 } 40 }
41 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;
42
43 int midiPitch = int(p + 0.00001);
44 float centsOffset = (p - midiPitch) * 100.0;
45
46 if (centsOffset >= 50.0) {
47 midiPitch = midiPitch + 1;
48 centsOffset = -(100.0 - centsOffset);
49 }
50
51 if (centsOffsetReturn) *centsOffsetReturn = centsOffset;
52 return midiPitch;
53 }
54
55 int
56 Pitch::getPitchForFrequencyDifference(float frequencyA,
57 float frequencyB,
58 float *centsOffsetReturn,
59 float concertA)
60 {
61 if (concertA <= 0.0) {
62 concertA = Preferences::getInstance()->getTuningFrequency();
63 }
64
65 if (frequencyA > frequencyB) {
66 std::swap(frequencyA, frequencyB);
67 }
68
69 float pA = 12.0 * (log(frequencyA / (concertA / 2.0)) / log(2.0)) + 57.0;
70 float pB = 12.0 * (log(frequencyB / (concertA / 2.0)) / log(2.0)) + 57.0;
71
72 float p = pB - pA;
42 73
43 int midiPitch = int(p + 0.00001); 74 int midiPitch = int(p + 0.00001);
44 float centsOffset = (p - midiPitch) * 100.0; 75 float centsOffset = (p - midiPitch) * 100.0;
45 76
46 if (centsOffset >= 50.0) { 77 if (centsOffset >= 50.0) {
99 float centsOffset = 0.0; 130 float centsOffset = 0.0;
100 int midiPitch = getPitchForFrequency(frequency, &centsOffset, concertA); 131 int midiPitch = getPitchForFrequency(frequency, &centsOffset, concertA);
101 return getPitchLabel(midiPitch, centsOffset, useFlats); 132 return getPitchLabel(midiPitch, centsOffset, useFlats);
102 } 133 }
103 134
135 QString
136 Pitch::getLabelForPitchRange(int semis, float cents)
137 {
138 int ic = lrintf(cents);
139
140 if (ic == 0) {
141 if (semis >= 12) {
142 return QString("%1'%2").arg(semis/12).arg(semis - 12*(semis/12));
143 } else {
144 return QString("%1").arg(semis);
145 }
146 } else {
147 if (ic > 0) {
148 if (semis >= 12) {
149 return QString("%1'%2+%3c").arg(semis/12).arg(semis - 12*(semis/12)).arg(ic);
150 } else {
151 return QString("%1+%3c").arg(semis).arg(ic);
152 }
153 } else {
154 if (semis >= 12) {
155 return QString("%1'%2%3c").arg(semis/12).arg(semis - 12*(semis/12)).arg(ic);
156 } else {
157 return QString("%1%3c").arg(semis).arg(ic);
158 }
159 }
160 }
161 }
162
104 bool 163 bool
105 Pitch::isFrequencyInMidiRange(float frequency, 164 Pitch::isFrequencyInMidiRange(float frequency,
106 float concertA) 165 float concertA)
107 { 166 {
108 float centsOffset = 0.0; 167 float centsOffset = 0.0;