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
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  *****************************************************************************/
20 //=============================================================================
21 // Key recognizer
22 //=============================================================================
27 #include "../system/simplethreadhandler.h"
28 #include "../messages/messagelistener.h"
29 #include "../piano/piano.h"
30 #include "../math/fftimplementation.h"
41 {
42 public:
43  virtual void keyRecognized(int keyIndex, double frequency) = 0;
44 };
58 {
59 private:
60  static const int M;
61  static const double fmin;
62  static const double fmax;
64 public:
65  KeyRecognizer (KeyRecognizerCallback *callback); // Constructor
66  ~KeyRecognizer(){} // Empty destructor
68  void init(bool optimize); // Initialize (optimize FFT)
69  void recognizeKey(bool forceRestart, // Recognize a key:
70  const Piano *piano, // This function is called by
71  FFTDataPointer fftPointer, // the SignalAnalyzer
72  int selectedKey, bool keyForced);
74 private:
75  void workerFunction() override final; // Thread execution function
77  double detectForcedFrequency(); // Handle forced keys
78  double detectFrequencyInTreble(); // Handle keys in the treble
79  void constructLogSpec(); // Construct logarithmic spectrum
80  void signalPreprocessing(); // Preprocessing of the signal
81  void defineKernel(); // Define convolution kernel
82  double estimateFrequency(); // Frequency recognition
83  int findNearestKey (double f); // Find nearest key, 0 if none
85 private:
88  double mConcertPitch;
89  const Piano *mPiano;
93  std::vector<double> mLogSpec;
94  std::vector<double> mFlatSpectrum;
99  bool mKeyForced;
101 private:
102  static const double logfmin;
103  static const double logfmax;
104  double mtof (int m);
105  int ftom (double f);
107  void Write(std::string filename, std::vector<double> &v, bool log=true); // only for development
108 };
110 #endif // KEYRECOGNIZER_H
FFTDataPointer mFFTPtr
Pointer to Fourier transform.
Definition: keyrecognizer.h:87
double detectForcedFrequency()
Detect forced key.
FFT_Implementation mFFT
Instance of FFT implementation.
Definition: keyrecognizer.h:92
std::vector< double > mFlatSpectrum
DoubleLogarithmic spectrum (LogLogSpec)
Definition: keyrecognizer.h:94
std::vector< FFTComplexType > FFTComplexVector
Definition: fftadapter.h:36
void signalPreprocessing()
Preprocessing of the signal.
Callback class for KeyRecognizer.
Definition: keyrecognizer.h:40
void init(bool optimize)
Initialization of the KeyRecognizer.
void constructLogSpec()
Construct logarithmic spectrum.
int mKeyNumberOfA
Index of the A-key.
Definition: keyrecognizer.h:91
static const int M
Number of bins (powers of 2,3,5)
Definition: keyrecognizer.h:60
Definition: piano.h:40
KeyRecognizer(KeyRecognizerCallback *callback)
Constructor of the KeyRecognizer.
int ftom(double f)
Map frequency to bin index.
void Write(std::string filename, std::vector< double > &v, bool log=true)
FFTComplexVector mFlatFFT
Fourier transform of LogLogSpec.
Definition: keyrecognizer.h:96
double mConcertPitch
Actual frequency of the A-key.
Definition: keyrecognizer.h:88
bool mKeyForced
Flag indicating that the key is forced.
Definition: keyrecognizer.h:99
void defineKernel()
Define the kernel vector for key recognition.
double estimateFrequency()
Estimate frequency for a given log-log spectrum.
void workerFunction() overridefinal
Main worker function for executing the key recognition thread.
std::vector< double > mLogSpec
Logarithmic spectrum (LogSpec)
Definition: keyrecognizer.h:93
static const double logfmax
Log of maximal frequency.
double detectFrequencyInTreble()
Detect keys in the treble.
const Piano * mPiano
Pointer to the piano data.
Definition: keyrecognizer.h:89
void recognizeKey(bool forceRestart, const Piano *piano, FFTDataPointer fftPointer, int selectedKey, bool keyForced)
Start key recognition.
virtual void keyRecognized(int keyIndex, double frequency)=0
int mNumberOfKeys
Number of piano keys.
Definition: keyrecognizer.h:90
std::vector< FFTRealType > FFTRealVector
Definition: fftadapter.h:35
static const double fmax
Frequency of bin M-1.
Definition: keyrecognizer.h:62
int mSelectedKey
Number of the actually selected key.
Definition: keyrecognizer.h:98
Module for fast recognition of the pressed key.
Definition: keyrecognizer.h:57
FFTComplexVector mKernelFFT
Fourier transform of the kernel.
Definition: keyrecognizer.h:95
KeyRecognizerCallback * mCallback
Pointer to the caller.
Definition: keyrecognizer.h:86
std::shared_ptr< FFTData > FFTDataPointer
Shared pointer of FFTData.
Definition: fftadapter.h:86
FFTRealVector mConvolution
Convolution vector.
Definition: keyrecognizer.h:97
static const double fmin
Frequency of bin 0.
Definition: keyrecognizer.h:61
static const double logfmin
Log of minimal frequency.
Simple thread handler.
double mtof(int m)
Map bin index to frequency.
int findNearestKey(double f)
Find the nearest key for a given frequency.
Thread-safe implementation of fftw3.