comparison base/Pitch.cpp @ 505:930b5b0f707d

Merge branch 'codestyle-and-tidy'
author Chris Cannam <cannam@all-day-breakfast.com>
date Wed, 05 Jun 2019 12:55:15 +0100
parents fdaa63607c15
children
comparison
equal deleted inserted replaced
471:e3335cb213da 505:930b5b0f707d
16 16
17 #include <math.h> 17 #include <math.h>
18 18
19 float 19 float
20 Pitch::getFrequencyForPitch(int midiPitch, 20 Pitch::getFrequencyForPitch(int midiPitch,
21 float centsOffset, 21 float centsOffset,
22 float concertA) 22 float concertA)
23 { 23 {
24 float p = float(midiPitch) + (centsOffset / 100); 24 float p = float(midiPitch) + (centsOffset / 100);
25 return concertA * powf(2.0, (p - 69.0) / 12.0); 25 return concertA * powf(2.0, (p - 69.0) / 12.0);
26 } 26 }
27 27
28 int 28 int
29 Pitch::getPitchForFrequency(float frequency, 29 Pitch::getPitchForFrequency(float frequency,
30 float *centsOffsetReturn, 30 float *centsOffsetReturn,
31 float concertA) 31 float concertA)
32 { 32 {
33 float p = 12.0 * (log(frequency / (concertA / 2.0)) / log(2.0)) + 57.0; 33 float p = 12.0 * (log(frequency / (concertA / 2.0)) / log(2.0)) + 57.0;
34 34
35 int midiPitch = int(p + 0.00001); 35 int midiPitch = int(p + 0.00001);
36 float centsOffset = (p - midiPitch) * 100.0; 36 float centsOffset = (p - midiPitch) * 100.0;
37 37
38 if (centsOffset >= 50.0) { 38 if (centsOffset >= 50.0) {
39 midiPitch = midiPitch + 1; 39 midiPitch = midiPitch + 1;
40 centsOffset = -(100.0 - centsOffset); 40 centsOffset = -(100.0 - centsOffset);
41 } 41 }
42 42
43 if (centsOffsetReturn) *centsOffsetReturn = centsOffset; 43 if (centsOffsetReturn) *centsOffsetReturn = centsOffset;
44 return midiPitch; 44 return midiPitch;
45 } 45 }