Entropy Piano Tuner  1.1.3 (documentation not yet complete) An open-source experimental software for piano tuning by entropy minimization
KeyRecognizer Class Reference

Module for fast recognition of the pressed key. More...

#include <keyrecognizer.h>

Inheritance diagram for KeyRecognizer:
Collaboration diagram for KeyRecognizer:

## 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
Constructor. More...

virtual destructor calls stop More...

virtual void start ()

virtual void stop ()

## 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 log-log 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

KeyRecognizerCallbackmCallback
Pointer to the caller. More...

FFTDataPointer mFFTPtr
Pointer to Fourier transform. More...

double mConcertPitch
Actual frequency of the A-key. More...

const PianomPiano
Pointer to the piano data. More...

int mNumberOfKeys
Number of piano keys. More...

int mKeyNumberOfA
Index of the A-key. 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 M-1. More...

static const double logfmin = log(fmin)
Log of minimal frequency. More...

static const double logfmax = log(fmax)
Log of maximal frequency. More...

Static Public Member Functions inherited from SimpleThreadHandler
Specify the name of the thread. More...

Protected Member Functions inherited from SimpleThreadHandler
virtual void exceptionCaught (const EptException &e)
EPT exception handling. More...

void msleep (double milliseconds)
Sleep function for staying idle. More...

Flag to check if the thread is running. More...

## Detailed Description

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.

## Constructor & Destructor Documentation

 KeyRecognizer::KeyRecognizer ( KeyRecognizerCallback * callback )

Constructor of the KeyRecognizer.

Parameters
 callback : Pointer to the instance of the callback.

Definition at line 52 of file keyrecognizer.cpp.

 KeyRecognizer::~KeyRecognizer ( )
inline

Definition at line 66 of file keyrecognizer.h.

## Member Function Documentation

 void KeyRecognizer::constructLogSpec ( )
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.

Here is the call graph for this function:

 void KeyRecognizer::defineKernel ( )
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.

Here is the call graph for this function:

 double KeyRecognizer::detectForcedFrequency ( )
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.

Returns
Frequency in Hz

Definition at line 188 of file keyrecognizer.cpp.

Here is the call graph for this function:

 double KeyRecognizer::detectFrequencyInTreble ( )
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.

Returns
Frequency in Hz

Definition at line 224 of file keyrecognizer.cpp.

Here is the call graph for this function:

 double KeyRecognizer::estimateFrequency ( )
private

Estimate frequency for a given log-log 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 m-value (slot) where the response of the kernel is maximal is then used as a first estimate of the frequency.

Returns
Estimated frequency in Hz

Definition at line 443 of file keyrecognizer.cpp.

Here is the call graph for this function:

 int KeyRecognizer::findNearestKey ( double f )
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.

Parameters
 f : Frequency in Hz
Returns
Index of the key in the range 0..mNumberOfKeys

Definition at line 468 of file keyrecognizer.cpp.

 int KeyRecognizer::ftom ( double f )
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(f-f_{min})}{\ln(f_{max}-f_{min})}\right],$

where M is the total number of bins. The expression is rounded to an integer.

Parameters
 f : Frequency in Hz
Returns
Integer index

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.

Parameters
 optimize : true if the FFT should be optimized

Definition at line 82 of file keyrecognizer.cpp.

Here is the call graph for this function:

 double KeyRecognizer::mtof ( int m )
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} \,.$

Parameters
 m : the index
Returns
frequency in Hz

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.

Parameters
 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.

Here is the call graph for this function:

 void KeyRecognizer::signalPreprocessing ( )
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 double-logarithmic 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.

Here is the call graph for this function:

 void KeyRecognizer::workerFunction ( )
finaloverrideprivatevirtual

Main worker function for executing the key recognition thread.

Definition at line 146 of file keyrecognizer.cpp.

Here is the call graph for this function:

 void KeyRecognizer::Write ( std::string filename, std::vector< double > & v, bool log = true )
private

Definition at line 485 of file keyrecognizer.cpp.

Here is the call graph for this function:

## Member Data Documentation

 const double KeyRecognizer::fmax = 10000
staticprivate

Frequency of bin M-1.

Definition at line 62 of file keyrecognizer.h.

 const double KeyRecognizer::fmin = 20
staticprivate

Frequency of bin 0.

Definition at line 61 of file keyrecognizer.h.

 const double KeyRecognizer::logfmax = log(fmax)
staticprivate

Log of maximal frequency.

Definition at line 103 of file keyrecognizer.h.

 const double KeyRecognizer::logfmin = log(fmin)
staticprivate

Log of minimal frequency.

Definition at line 102 of file keyrecognizer.h.

 const int KeyRecognizer::M = 1024
staticprivate

Number of bins (powers of 2,3,5)

Definition at line 60 of file keyrecognizer.h.

 KeyRecognizerCallback* KeyRecognizer::mCallback
private

Pointer to the caller.

Definition at line 86 of file keyrecognizer.h.

 double KeyRecognizer::mConcertPitch
private

Actual frequency of the A-key.

Definition at line 88 of file keyrecognizer.h.

 FFTRealVector KeyRecognizer::mConvolution
private

Convolution vector.

Definition at line 97 of file keyrecognizer.h.

 FFT_Implementation KeyRecognizer::mFFT
private

Instance of FFT implementation.

Definition at line 92 of file keyrecognizer.h.

 FFTDataPointer KeyRecognizer::mFFTPtr
private

Pointer to Fourier transform.

Definition at line 87 of file keyrecognizer.h.

 FFTComplexVector KeyRecognizer::mFlatFFT
private

Fourier transform of LogLogSpec.

Definition at line 96 of file keyrecognizer.h.

 std::vector KeyRecognizer::mFlatSpectrum
private

DoubleLogarithmic spectrum (LogLogSpec)

Definition at line 94 of file keyrecognizer.h.

 FFTComplexVector KeyRecognizer::mKernelFFT
private

Fourier transform of the kernel.

Definition at line 95 of file keyrecognizer.h.

 bool KeyRecognizer::mKeyForced
private

Flag indicating that the key is forced.

Definition at line 99 of file keyrecognizer.h.

 int KeyRecognizer::mKeyNumberOfA
private

Index of the A-key.

Definition at line 91 of file keyrecognizer.h.

 std::vector KeyRecognizer::mLogSpec
private

Logarithmic spectrum (LogSpec)

Definition at line 93 of file keyrecognizer.h.

 int KeyRecognizer::mNumberOfKeys
private

Number of piano keys.

Definition at line 90 of file keyrecognizer.h.

 const Piano* KeyRecognizer::mPiano
private

Pointer to the piano data.

Definition at line 89 of file keyrecognizer.h.

 int KeyRecognizer::mSelectedKey
private

Number of the actually selected key.

Definition at line 98 of file keyrecognizer.h.

The documentation for this class was generated from the following files:
• /home/hinrichsen/Dokumente/Aktuell/EPT/Entropy-Piano-Tuner/core/analyzers/keyrecognizer.h
• /home/hinrichsen/Dokumente/Aktuell/EPT/Entropy-Piano-Tuner/core/analyzers/keyrecognizer.cpp