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 
8     This program is free software; you can redistribute it and/or
9     modify it under the terms of the GNU General Public License as
10     published by the Free Software Foundation; either version 2 of the
11     License, or (at your option) any later version.  See the file
12     COPYING included with this distribution for more information.
13 */
14 
15 #include "Pitch.h"
16 
17 #include <math.h>
18 
19 float
getFrequencyForPitch(int midiPitch,float centsOffset,float concertA)20 Pitch::getFrequencyForPitch(int midiPitch,
21 			    float centsOffset,
22 			    float concertA)
23 {
24     float p = float(midiPitch) + (centsOffset / 100);
25     return concertA * powf(2.0, (p - 69.0) / 12.0);
26 }
27 
28 int
getPitchForFrequency(float frequency,float * centsOffsetReturn,float concertA)29 Pitch::getPitchForFrequency(float frequency,
30 			    float *centsOffsetReturn,
31 			    float concertA)
32 {
33     float p = 12.0 * (log(frequency / (concertA / 2.0)) / log(2.0)) + 57.0;
34 
35     int midiPitch = int(p + 0.00001);
36     float centsOffset = (p - midiPitch) * 100.0;
37 
38     if (centsOffset >= 50.0) {
39 	midiPitch = midiPitch + 1;
40 	centsOffset = -(100.0 - centsOffset);
41     }
42 
43     if (centsOffsetReturn) *centsOffsetReturn = centsOffset;
44     return midiPitch;
45 }
46 
47