Entropy Piano Tuner  1.1.3 (documentation not yet complete)
An open-source experimental software for piano tuning by entropy minimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
key.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright 2015 Haye Hinrichsen, Christoph Wick
3  *
4  * This file is part of Entropy Piano Tuner.
5  *
6  * Entropy Piano Tuner is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * Entropy Piano Tuner is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * Entropy Piano Tuner. If not, see http://www.gnu.org/licenses/.
18  *****************************************************************************/
19 
20 //=============================================================================
21 // Class describing a single piano key
22 //=============================================================================
23 
24 #include "key.h"
25 
26 #include <cmath>
27 #include "../system/eptexception.h"
28 #include "../system/log.h"
29 #include "../math/mathtools.h"
30 
31 // Constants characterizing the logarighmically bins, do not change!
32 
33 const int Key::NumberOfBins = 10800; // Number of log. bins
34 const int Key::BinsPerOctave = 1200; // bins per octave
35 const double Key::fmin = 20.601722; // frequ. of lowest bin
36 
37 
38 //-----------------------------------------------------------------------------
39 // clear data
40 //-----------------------------------------------------------------------------
41 
42 void Key::clear()
43 {
45  mPeaks.clear();
50  mTunedFrequency = 0;
51  mOverpull = 0;
52  mRecorded = false;
53 }
54 
55 
56 //-----------------------------------------------------------------------------
57 // map frequency to real-valued logspec index
58 //-----------------------------------------------------------------------------
59 
69 
70 double Key::FrequencyToRealIndex (double f)
71 {
72  EptAssert(f > 0 && f < 20000, "Freqency must be in [0,20000]");
73  static const double lnfmin = log(fmin);
74  return BinsPerOctave * (log(f) - lnfmin) / MathTools::LOG2;
75 }
76 
77 //-----------------------------------------------------------------------------
78 // map frequency to integer logspec index
79 //-----------------------------------------------------------------------------
80 
87 
88 int Key::FrequencyToIndex (double f)
90 
91 
92 //-----------------------------------------------------------------------------
93 // map logspec index to frequency
94 //-----------------------------------------------------------------------------
95 
101 
102 double Key::IndexToFrequency (double m)
103 {
104  // m may be negative aswell here
105  return fmin * pow(2, m / BinsPerOctave);
106 }
107 
108 
109 //-----------------------------------------------------------------------------
110 // set and get recorded frequency
111 //-----------------------------------------------------------------------------
112 
113 void Key::setRecordedFrequency (const double f)
114 {
115  EptAssert(f > 0 && f < 20000, "Frequency must in the range (0,20000)");
117 }
118 
120 { return mRecordedFrequency; }
121 
123 { return mRecordedFrequency; }
124 
125 
126 //-----------------------------------------------------------------------------
127 // set and get measured inharmonicity
128 //-----------------------------------------------------------------------------
129 
131 {
132  EptAssert (B >= 0, "The inharmonicity must be positive");
133  if (B>1)
134  {
135  LogW("Inharmonicity larger than 1 ignored");
136  return;
137  }
139 }
140 
142 { return mMeasuredInharmonicity; }
143 
145 { return mMeasuredInharmonicity; }
146 
147 
148 //-----------------------------------------------------------------------------
149 // set and get recognition quality
150 //-----------------------------------------------------------------------------
151 
153 { mRecognitionQuality = Q; }
154 
156 { return mRecognitionQuality; }
157 
159 { return mRecognitionQuality; }
160 
161 
162 //-----------------------------------------------------------------------------
163 // set and get computed frequency
164 //-----------------------------------------------------------------------------
165 
166 // Note: The computed frequency is always stored in a pitch with A4 = 440Hz
167 
169 {
170  EptAssert(f >= 0 && f < 20000, "Freqency must be below 20000 and greater than 0");
172 }
173 
175 { return mComputedFrequency; }
176 
178 { return mComputedFrequency; }
179 
180 
181 //-----------------------------------------------------------------------------
182 // set and get tuned frequency
183 //-----------------------------------------------------------------------------
184 
185 // Note: The computed frequency is stored in the actually tuned pitch
186 
187 void Key::setTunedFrequency (double f)
188 {
189  EptAssert(f >= 0 && f < 20000, "Freqency must be below 20000 and greater than 0");
190  mTunedFrequency=f;
191 }
192 
193 double Key::getTunedFrequency () const
194 { return mTunedFrequency; }
195 
197 { return mTunedFrequency; }
198 
199 
200 //-----------------------------------------------------------------------------
201 // set and get overpull in cents
202 //-----------------------------------------------------------------------------
203 
204 void Key::setOverpull (double cents)
205 {
206  if (cents>25) mOverpull=25;
207  else if (cents<-25) mOverpull=-25;
208  else mOverpull = cents;
209 }
210 
211 double Key::getOverpull () const
212 { return mOverpull; }
213 
215 { return mOverpull; }
216 
217 
218 //-----------------------------------------------------------------------------
219 // copy vector to mSpectrum
220 //-----------------------------------------------------------------------------
221 
223 {
224  EptAssert(s.size() == static_cast<size_t>(NumberOfBins),
225  "Spectrum size must match the total number of bins");
226  mSpectrum = s;
227 }
228 
229 
230 //-----------------------------------------------------------------------------
231 // get spectrum
232 //-----------------------------------------------------------------------------
233 
235 { return mSpectrum; }
236 
238 { return mSpectrum; }
239 
240 
241 //-----------------------------------------------------------------------------
242 // copy list to mPeaks
243 //-----------------------------------------------------------------------------
244 
246 {
247  EptAssert(s.size() < 200, "Peak list should not be unreasonably large");
248  mPeaks = s;
249 }
250 
251 
252 //-----------------------------------------------------------------------------
253 // get peaks
254 //-----------------------------------------------------------------------------
255 
257 { return mPeaks; }
258 
260 { return mPeaks; }
double getRecordedFrequency() const
Get recorded frequency.
Definition: key.cpp:119
void setTunedFrequency(double f)
Set tuned frequency.
Definition: key.cpp:187
void setPeaks(const PeakListType &s)
Copy map of peaks.
Definition: key.cpp:245
const double LOG2
Definition: mathtools.h:39
PeakListType mPeaks
List of identified peaks.
Definition: key.h:107
double mOverpull
Overpull in cents.
Definition: key.h:113
const PeakListType & getPeaks() const
Get a read-only reference to mPeaks.
Definition: key.cpp:259
void setMeasuredInharmonicity(double f)
Set estimated inharmonicity.
Definition: key.cpp:130
#define LogW(...)
Definition: log.h:56
void setComputedFrequency(double f)
Set computed frequency.
Definition: key.cpp:168
void setRecordedFrequency(const double f)
Set recorded frequency.
Definition: key.cpp:113
static const int NumberOfBins
Total number of slots: 9 octaves.
Definition: key.h:50
static const int BinsPerOctave
Number of slots per ocatave (here 1 cent)
Definition: key.h:51
int roundToInteger(T x)
Round a floating point number to an integer.
Definition: mathtools.h:43
double mRecognitionQuality
Accuracy of higher partials (in cents)
Definition: key.h:110
void setOverpull(double cents)
Set overpull in cents.
Definition: key.cpp:204
bool mRecorded
Is the key already recorded?
Definition: key.h:114
static double IndexToFrequency(double m)
Convert continuous slot index to frequency in Hz.
Definition: key.cpp:102
void setSpectrum(const SpectrumType &s)
Copy spectrum to mSpectrum.
Definition: key.cpp:222
double mRecordedFrequency
Recorded frequency of 1st partial in Hz.
Definition: key.h:108
double getComputedFrequency() const
Get computed frequency.
Definition: key.cpp:174
double getMeasuredInharmonicity() const
Get estimated inharmonicity.
Definition: key.cpp:141
static double FrequencyToRealIndex(double f)
Convert frequency to real-valued logbin index.
Definition: key.cpp:70
void setRecognitionQuality(double f)
Set quality of recognition.
Definition: key.cpp:152
void clear()
Clear all data elements of the Key.
Definition: key.cpp:42
double getRecognitionQuality() const
Get quality of recognition.
Definition: key.cpp:155
std::vector< double > SpectrumType
Type of a log-binned spectrum.
Definition: key.h:54
double mMeasuredInharmonicity
Measured inharmonicity of recorded signal.
Definition: key.h:109
SpectrumType mSpectrum
Logarithmically organized spectrum.
Definition: key.h:106
const SpectrumType & getSpectrum() const
Get a read-only reference to mSpectrum.
Definition: key.cpp:237
#define EptAssert(a, b)
Definition: eptexception.h:47
double mComputedFrequency
Computed frequency in Hz (tuning curve)
Definition: key.h:111
static int FrequencyToIndex(double f)
Convert frequency to logbin index.
Definition: key.cpp:88
double getTunedFrequency() const
Get tuned frequency.
Definition: key.cpp:193
std::map< double, double > PeakListType
Type for a peak map.
Definition: key.h:55
double getOverpull() const
Get overpull in cents.
Definition: key.cpp:211
static const double fmin
Mimimal frequency of logbinned spectrum in Hz.
Definition: key.h:52
double mTunedFrequency
Tuned frequency in Hz.
Definition: key.h:112