comparison base/Pitch.cpp @ 1429:48e9f538e6e9

Untabify
author Chris Cannam
date Thu, 01 Mar 2018 18:02:22 +0000
parents cc27f35aa75c
children
comparison
equal deleted inserted replaced
1428:87ae75da6527 1429:48e9f538e6e9
19 19
20 #include <cmath> 20 #include <cmath>
21 21
22 double 22 double
23 Pitch::getFrequencyForPitch(int midiPitch, 23 Pitch::getFrequencyForPitch(int midiPitch,
24 double centsOffset, 24 double centsOffset,
25 double concertA) 25 double concertA)
26 { 26 {
27 if (concertA <= 0.0) { 27 if (concertA <= 0.0) {
28 concertA = Preferences::getInstance()->getTuningFrequency(); 28 concertA = Preferences::getInstance()->getTuningFrequency();
29 } 29 }
30 double p = double(midiPitch) + (centsOffset / 100); 30 double p = double(midiPitch) + (centsOffset / 100);
31 return concertA * pow(2.0, (p - 69.0) / 12.0); 31 return concertA * pow(2.0, (p - 69.0) / 12.0);
32 } 32 }
33 33
34 int 34 int
35 Pitch::getPitchForFrequency(double frequency, 35 Pitch::getPitchForFrequency(double frequency,
36 double *centsOffsetReturn, 36 double *centsOffsetReturn,
37 double concertA) 37 double concertA)
38 { 38 {
39 if (concertA <= 0.0) { 39 if (concertA <= 0.0) {
40 concertA = Preferences::getInstance()->getTuningFrequency(); 40 concertA = Preferences::getInstance()->getTuningFrequency();
41 } 41 }
42 double p = 12.0 * (log(frequency / (concertA / 2.0)) / log(2.0)) + 57.0; 42 double p = 12.0 * (log(frequency / (concertA / 2.0)) / log(2.0)) + 57.0;
43 43
44 int midiPitch = int(round(p)); 44 int midiPitch = int(round(p));
45 double centsOffset = (p - midiPitch) * 100.0; 45 double centsOffset = (p - midiPitch) * 100.0;
46 46
47 if (centsOffset >= 50.0) { 47 if (centsOffset >= 50.0) {
48 midiPitch = midiPitch + 1; 48 midiPitch = midiPitch + 1;
49 centsOffset = -(100.0 - centsOffset); 49 centsOffset = -(100.0 - centsOffset);
50 } 50 }
51 if (centsOffset < -50.0) { 51 if (centsOffset < -50.0) {
52 midiPitch = midiPitch - 1; 52 midiPitch = midiPitch - 1;
53 centsOffset = (100.0 + centsOffset); 53 centsOffset = (100.0 + centsOffset);
54 } 54 }
55 55
56 if (centsOffsetReturn) *centsOffsetReturn = centsOffset; 56 if (centsOffsetReturn) *centsOffsetReturn = centsOffset;
57 return midiPitch; 57 return midiPitch;
58 } 58 }
78 78
79 int midiPitch = int(p + 0.00001); 79 int midiPitch = int(p + 0.00001);
80 double centsOffset = (p - midiPitch) * 100.0; 80 double centsOffset = (p - midiPitch) * 100.0;
81 81
82 if (centsOffset >= 50.0) { 82 if (centsOffset >= 50.0) {
83 midiPitch = midiPitch + 1; 83 midiPitch = midiPitch + 1;
84 centsOffset = -(100.0 - centsOffset); 84 centsOffset = -(100.0 - centsOffset);
85 } 85 }
86 86
87 if (centsOffsetReturn) *centsOffsetReturn = centsOffset; 87 if (centsOffsetReturn) *centsOffsetReturn = centsOffset;
88 return midiPitch; 88 return midiPitch;
89 } 89 }
118 // boundaries because Cb is enharmonic with B (not B#) and B# is 118 // boundaries because Cb is enharmonic with B (not B#) and B# is
119 // enharmonic with C (not Cb). So neither B# nor Cb will be 119 // enharmonic with C (not Cb). So neither B# nor Cb will be
120 // spelled from a MIDI pitch + flats flag in isolation. 120 // spelled from a MIDI pitch + flats flag in isolation.
121 121
122 if (midiPitch < 0) { 122 if (midiPitch < 0) {
123 while (midiPitch < 0) { 123 while (midiPitch < 0) {
124 midiPitch += 12; 124 midiPitch += 12;
125 --octave; 125 --octave;
126 } 126 }
127 } else { 127 } else {
128 octave = midiPitch / 12 + baseOctave; 128 octave = midiPitch / 12 + baseOctave;
129 } 129 }
130 130
131 note = midiPitch % 12; 131 note = midiPitch % 12;
132 } 132 }
133 133
134 QString 134 QString
135 Pitch::getPitchLabel(int midiPitch, 135 Pitch::getPitchLabel(int midiPitch,
136 double centsOffset, 136 double centsOffset,
137 bool useFlats) 137 bool useFlats)
138 { 138 {
139 int note, octave; 139 int note, octave;
140 getNoteAndOctaveForPitch(midiPitch, note, octave); 140 getNoteAndOctaveForPitch(midiPitch, note, octave);
141 141
142 QString plain = (useFlats ? flatNotes : notes)[note].arg(octave); 142 QString plain = (useFlats ? flatNotes : notes)[note].arg(octave);
147 else return QString("%1%2c").arg(plain).arg(ic); 147 else return QString("%1%2c").arg(plain).arg(ic);
148 } 148 }
149 149
150 QString 150 QString
151 Pitch::getPitchLabelForFrequency(double frequency, 151 Pitch::getPitchLabelForFrequency(double frequency,
152 double concertA, 152 double concertA,
153 bool useFlats) 153 bool useFlats)
154 { 154 {
155 if (concertA <= 0.0) { 155 if (concertA <= 0.0) {
156 concertA = Preferences::getInstance()->getTuningFrequency(); 156 concertA = Preferences::getInstance()->getTuningFrequency();
157 } 157 }
158 double centsOffset = 0.0; 158 double centsOffset = 0.0;