Entropy Piano Tuner
1.1.3 (documentation not yet complete)
An opensource experimental software for piano tuning by entropy minimization

Module for fast recognition of the pressed key. More...
#include <keyrecognizer.h>
Public Member Functions  
KeyRecognizer (KeyRecognizerCallback *callback)  
Constructor of the KeyRecognizer. More...  
~KeyRecognizer ()  
void  init (bool optimize) 
Initialization of the KeyRecognizer. More...  
void  recognizeKey (bool forceRestart, const Piano *piano, FFTDataPointer fftPointer, int selectedKey, bool keyForced) 
Start key recognition. More...  
Public Member Functions inherited from SimpleThreadHandler  
SimpleThreadHandler ()  
Constructor. More...  
virtual  ~SimpleThreadHandler () 
virtual destructor calls stop More...  
virtual void  start () 
Start the thread. More...  
virtual void  stop () 
Stop the thread. More...  
Private Member Functions  
void  workerFunction () overridefinal 
Main worker function for executing the key recognition thread. More...  
double  detectForcedFrequency () 
Detect forced key. More...  
double  detectFrequencyInTreble () 
Detect keys in the treble. More...  
void  constructLogSpec () 
Construct logarithmic spectrum. More...  
void  signalPreprocessing () 
Preprocessing of the signal. More...  
void  defineKernel () 
Define the kernel vector for key recognition. More...  
double  estimateFrequency () 
Estimate frequency for a given loglog spectrum. More...  
int  findNearestKey (double f) 
Find the nearest key for a given frequency. More...  
double  mtof (int m) 
Map bin index to frequency. More...  
int  ftom (double f) 
Map frequency to bin index. More...  
void  Write (std::string filename, std::vector< double > &v, bool log=true) 
Private Attributes  
KeyRecognizerCallback *  mCallback 
Pointer to the caller. More...  
FFTDataPointer  mFFTPtr 
Pointer to Fourier transform. More...  
double  mConcertPitch 
Actual frequency of the Akey. More...  
const Piano *  mPiano 
Pointer to the piano data. More...  
int  mNumberOfKeys 
Number of piano keys. More...  
int  mKeyNumberOfA 
Index of the Akey. More...  
FFT_Implementation  mFFT 
Instance of FFT implementation. More...  
std::vector< double >  mLogSpec 
Logarithmic spectrum (LogSpec) More...  
std::vector< double >  mFlatSpectrum 
DoubleLogarithmic spectrum (LogLogSpec) More...  
FFTComplexVector  mKernelFFT 
Fourier transform of the kernel. More...  
FFTComplexVector  mFlatFFT 
Fourier transform of LogLogSpec. More...  
FFTRealVector  mConvolution 
Convolution vector. More...  
int  mSelectedKey 
Number of the actually selected key. More...  
bool  mKeyForced 
Flag indicating that the key is forced. More...  
Static Private Attributes  
static const int  M = 1024 
Number of bins (powers of 2,3,5) More...  
static const double  fmin = 20 
Frequency of bin 0. More...  
static const double  fmax = 10000 
Frequency of bin M1. More...  
static const double  logfmin = log(fmin) 
Log of minimal frequency. More...  
static const double  logfmax = log(fmax) 
Log of maximal frequency. More...  
Additional Inherited Members  
Static Public Member Functions inherited from SimpleThreadHandler  
static void  setThreadName (std::string s) 
Specify the name of the thread. More...  
Protected Member Functions inherited from SimpleThreadHandler  
virtual void  exceptionCaught (const EptException &e) 
EPT exception handling. More...  
void  setCancelThread (bool b) 
Cancelflag setter method, threadsafe. More...  
bool  cancelThread () const 
Cancelflag getter method, threadsafe. More...  
void  msleep (double milliseconds) 
Sleep function for staying idle. More...  
bool  isThreadRunning () const 
Flag to check if the thread is running. More...  
Module for fast recognition of the pressed key.
When a key is pressed, the SignalAnalyzer calls the function recognizeKey to start an independent thread for key recognition on the basis of the current Fourier transform. The key recognizer transmits the estimated frequency and the corresponding key number via a callback function.
Definition at line 57 of file keyrecognizer.h.
KeyRecognizer::KeyRecognizer  (  KeyRecognizerCallback *  callback  ) 
Constructor of the KeyRecognizer.
callback  : Pointer to the instance of the callback. 
Definition at line 52 of file keyrecognizer.cpp.

inline 
Definition at line 66 of file keyrecognizer.h.

private 
Construct logarithmic spectrum.
This function maps the FFT spectrum, which is linear in the frequency, to a logarithmically binned spectrum. To this end the amplitudes of the FFT power spectrum are evently distributed into bins of the logarithmic spectrum (mLogSpec).
Definition at line 304 of file keyrecognizer.cpp.

private 
Define the kernel vector for key recognition.
The tuner needs to recognize the key that is pressed. The essential part in the recognition procedure is a kernel function which is convolved linearly with the actually measured spectrum. It has positive peaks in those positions where partials are expected and negative peaks in the place of wrong partials. It is stored in a in a STL map.
Definition at line 388 of file keyrecognizer.cpp.

private 
Detect forced key.
If a key is forced we search for the maximum of the FFT in the vicinity of the forced key with a width of a bit less than a half tone.
Definition at line 188 of file keyrecognizer.cpp.

private 
Detect keys in the treble.
For very high notes the kernel method is not suitable. This function detects whether one of the very high keys has been hit. This is done by comparing the second and the first moment of the FFT. If the ratio is above the threshold, indicating a high key, the frequency is determined simply by searching for the maximal peak in the range from 1..5 kHz.
Definition at line 224 of file keyrecognizer.cpp.

private 
Estimate frequency for a given loglog spectrum.
This function estimates the frequency of a pressed piano key. To this end the logarithmically binned log spectrum is convolved with the recognition kernel for all frequencies. For speedup this convolution product is carried out by two Fourier transformations (in log space instead of real space) with an ordinary multiplication in between. The mvalue (slot) where the response of the kernel is maximal is then used as a first estimate of the frequency.
Definition at line 443 of file keyrecognizer.cpp.

private 
Find the nearest key for a given frequency.
This function determines the most likely index of a key for a given frequency, referring to the actual concert pitch.
f  : Frequency in Hz 
Definition at line 468 of file keyrecognizer.cpp.

private 
Map frequency to bin index.
Convert frequency to logspec index.
The key recognizer performs a logarithmic binning independent of the one carried out in the FFTAnalyzer. To this end the frequency f is mapped to an integer index m. The formula reads:
\[ m(f) = \left[M \frac{\ln(ff_{min})}{\ln(f_{max}f_{min})}\right], \]
where M is the total number of bins. The expression is rounded to an integer.
f  : Frequency in Hz 
Definition at line 271 of file keyrecognizer.cpp.
void KeyRecognizer::init  (  bool  optimize  ) 
Initialization of the KeyRecognizer.
This function defines the kernel and optimizes the plan for the FFT. Optimization will take a while to be finished.
optimize  : true if the FFT should be optimized 
Definition at line 82 of file keyrecognizer.cpp.

private 
Map bin index to frequency.
Convert logspec index to frequency.
This function maps the logarithmic binning index back to the frequency. It is the inverse function of ftom:
\[ f(m) = f_{min} + (f_{max}f_{min})^{m/M} \,. \]
m  : the index 
Definition at line 286 of file keyrecognizer.cpp.
void KeyRecognizer::recognizeKey  (  bool  forceRestart, 
const Piano *  piano,  
FFTDataPointer  fftPointer,  
int  selectedKey,  
bool  keyForced  
) 
Start key recognition.
Function to start the key recognition thread. This function is called by the SignalAnalyzer.
forceRestart  : true if restart of the thread is forced 
piano  : pointer to the piano data 
fftPointer  : pointer to the actual FFT 
selectedKey  : Number of the selected key (1 if none) 
keyForced  : Boolean flag saying whether the selected key is forced 
Definition at line 111 of file keyrecognizer.cpp.

private 
Preprocessing of the signal.
The purpose of this function is to convert the logarithmically binned spectrum (called logspec), which is logarithmic in the frequency, also logarithmically in the amplitude. The result is a doublelogarithmic spectrum, i.e., logarithmic in its binning and also in its value. Values <= 0 are mapped to zero.
Some spectra, in particular those of the high keys, show a broad noise background which varies only smoothly with the frequency. The second part of the function substracts a gliding average over a certain window, centereing the loglog spectrum around the zero line.
Definition at line 332 of file keyrecognizer.cpp.

finaloverrideprivatevirtual 
Main worker function for executing the key recognition thread.
Implements SimpleThreadHandler.
Definition at line 146 of file keyrecognizer.cpp.

private 

staticprivate 
Frequency of bin M1.
Definition at line 62 of file keyrecognizer.h.

staticprivate 
Frequency of bin 0.
Definition at line 61 of file keyrecognizer.h.

staticprivate 
Log of maximal frequency.
Definition at line 103 of file keyrecognizer.h.

staticprivate 
Log of minimal frequency.
Definition at line 102 of file keyrecognizer.h.

staticprivate 
Number of bins (powers of 2,3,5)
Definition at line 60 of file keyrecognizer.h.

private 
Pointer to the caller.
Definition at line 86 of file keyrecognizer.h.

private 
Actual frequency of the Akey.
Definition at line 88 of file keyrecognizer.h.

private 
Convolution vector.
Definition at line 97 of file keyrecognizer.h.

private 
Instance of FFT implementation.
Definition at line 92 of file keyrecognizer.h.

private 
Pointer to Fourier transform.
Definition at line 87 of file keyrecognizer.h.

private 
Fourier transform of LogLogSpec.
Definition at line 96 of file keyrecognizer.h.

private 
DoubleLogarithmic spectrum (LogLogSpec)
Definition at line 94 of file keyrecognizer.h.

private 
Fourier transform of the kernel.
Definition at line 95 of file keyrecognizer.h.

private 
Flag indicating that the key is forced.
Definition at line 99 of file keyrecognizer.h.

private 
Index of the Akey.
Definition at line 91 of file keyrecognizer.h.

private 
Logarithmic spectrum (LogSpec)
Definition at line 93 of file keyrecognizer.h.

private 
Number of piano keys.
Definition at line 90 of file keyrecognizer.h.

private 
Pointer to the piano data.
Definition at line 89 of file keyrecognizer.h.

private 
Number of the actually selected key.
Definition at line 98 of file keyrecognizer.h.