comparison base/Pitch.cpp @ 0:d7116e3183f8

* Queen Mary C++ DSP library
author cannam
date Wed, 05 Apr 2006 17:35:59 +0000
parents
children 054c384d860d
comparison
equal deleted inserted replaced
-1:000000000000 0:d7116e3183f8
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 QM DSP library
5 Centre for Digital Music, Queen Mary, University of London.
6 This file Copyright 2006 Chris Cannam.
7 All rights reserved.
8 */
9
10 #include "Pitch.h"
11
12 #include <cmath>
13
14 float
15 Pitch::getFrequencyForPitch(int midiPitch,
16 float centsOffset,
17 float concertA)
18 {
19 float p = float(midiPitch) + (centsOffset / 100);
20 return concertA * powf(2.0, (p - 69.0) / 12.0);
21 }
22
23 int
24 Pitch::getPitchForFrequency(float frequency,
25 float *centsOffsetReturn,
26 float concertA)
27 {
28 float p = 12.0 * (log(frequency / (concertA / 2.0)) / log(2.0)) + 57.0;
29
30 int midiPitch = int(p + 0.00001);
31 float centsOffset = (p - midiPitch) * 100.0;
32
33 if (centsOffset >= 50.0) {
34 midiPitch = midiPitch + 1;
35 centsOffset = -(100.0 - centsOffset);
36 }
37
38 if (centsOffsetReturn) *centsOffsetReturn = centsOffset;
39 return midiPitch;
40 }
41