Entropy Piano Tuner
1.1.3 (documentation not yet complete)
An open-source experimental software for piano tuning by entropy minimization
|
Signal analyzer: Fourier transformation of the recorded audio signal. More...
#include <signalanalyzer.h>
Public Member Functions | |
SignalAnalyzer (AudioRecorderAdapter *recorder) | |
Constructor of the SignalAnalyzer. More... | |
~SignalAnalyzer () | |
void | init () |
Initializes the SignalAnalyzer and its components. More... | |
void | exit () |
virtual void | stop () override |
Stop the thread. More... | |
Public Member Functions inherited from SimpleThreadHandler | |
SimpleThreadHandler () | |
Constructor. More... | |
virtual | ~SimpleThreadHandler () |
virtual destructor calls stop More... | |
virtual void | start () |
Start the thread. More... | |
Static Public Attributes | |
static const int | AUDIO_BUFFER_SIZE_IN_SECONDS = 60 |
Maximal size of the audio buffer. More... | |
static const int | MINIMAL_FFT_INTERVAL_IN_MILLISECONDS = 150 |
Time interval for at most one FFT. More... | |
Private Types | |
enum | AnalyzerRole { ROLE_IDLE, ROLE_RECORD_KEYSTROKE, ROLE_ROLLING_FFT } |
Private Member Functions | |
void | handleMessage (MessagePtr m) overridefinal |
Message receiver and dispatcer. More... | |
void | changeRole (AnalyzerRole role) |
Changes the current role of the signal analyzer. More... | |
void | updateDataBufferSize () |
void | workerFunction () overridefinal |
Thread function of the SignalAnalyzer. More... | |
void | recordSignal () |
Record signal and perform FFT's in regular intervals. More... | |
void | analyzeSignal () |
Analyze the final signal (in recording mode only) More... | |
void | recordPostprocessing () |
Process the singal after recording has finsihed. More... | |
void | updateOverpull () |
double | signalPreprocessing (FFTWVector &signal) |
Function for signal preprocessing. More... | |
void | signalProcessing (FFTWVector &signal, int samplingrate) |
Function for signal processing. More... | |
bool | detectClipping (FFTWVector signal) |
Clipping detector. More... | |
void | PerformFFT (FFTWVector &signal, FFTWVector &powerspec) |
Perform fast Fourier transform. More... | |
void | createPolygon (const FFTWVector &powerspec, FFTPolygon &poly) const |
Create a polygon for drawing. More... | |
int | identifySelectedKey () |
identify final key More... | |
virtual void | keyRecognized (int keyIndex, double frequency) overridefinal |
void | WriteSignal (std::string filename, const FFTWVector &signal) |
void | WriteFFT (std::string filename, const FFTWVector &fft) |
Private Attributes | |
const Piano * | mPiano |
Pointer to the piano. More... | |
CircularBuffer< FFTWType > | mDataBuffer |
Local audio buffer. More... | |
std::mutex | mDataBufferMutex |
The data buffer might change its size during recording and key selection, lock it. More... | |
AudioRecorderAdapter * | mAudioRecorder |
Pointer to the audio recorder. More... | |
std::atomic< bool > | mRecording |
Flag indicating ongoing recording. More... | |
FFTWVector | mProprocessedSignal |
the current signal (after preprocessing) More... | |
FFTDataPointer | mPowerspectrum |
the last recorded powerspectrum More... | |
FFT_Implementation | mFFT |
Instance of the Fourier transformer. More... | |
FFTAnalyzer | mFFTAnalyser |
Instance of the FFT analyzer. More... | |
KeyRecognizer | mKeyRecognizer |
Instance of the Key recognizer. More... | |
OverpullEstimator | mOverpull |
Instance of the overpull estimator. More... | |
std::map< int, int > | mKeyCountStatistics |
Count which key is selected how often. More... | |
std::mutex | mKeyCountStatisticsMutex |
Corresponding mutex. More... | |
int | mSelectedKey |
The selected key by the user. More... | |
bool | mKeyForced |
Is the key selection forced. More... | |
std::atomic< AnalyzerRole > | mAnalyzerRole |
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) |
Cancel-flag setter method, thread-safe. More... | |
bool | cancelThread () const |
Cancel-flag getter method, thread-safe. More... | |
void | msleep (double milliseconds) |
Sleep function for staying idle. More... | |
bool | isThreadRunning () const |
Flag to check if the thread is running. More... | |
Signal analyzer: Fourier transformation of the recorded audio signal.
The signal analyzer receives the audio signal from the audio device. It contains another cyclic buffer which can hold audio data of about a minute. After detecting a keystroke, the AudioRecorderAdapter sends a message to start the SignalAnalyzer. During recording the SignalAnalyzer continuously performs Fourer transformations on the current signal. When the recording is finished, a final Fourier transformation is carried out. Various steps for signal preprocessing are included as well.
Definition at line 54 of file signalanalyzer.h.
|
private |
Enumerator | |
---|---|
ROLE_IDLE |
Idle. |
ROLE_RECORD_KEYSTROKE |
Recording a key stroke in recording operation mode. |
ROLE_ROLLING_FFT |
Performing rolling ffts in tuning mode. |
Definition at line 65 of file signalanalyzer.h.
SignalAnalyzer::SignalAnalyzer | ( | AudioRecorderAdapter * | recorder | ) |
Constructor of the SignalAnalyzer.
recorder | : pointer to the implementation of the AudioRecorder |
Definition at line 56 of file signalanalyzer.cpp.
|
inline |
Definition at line 74 of file signalanalyzer.h.
|
private |
Analyze the final signal (in recording mode only)
Definition at line 341 of file signalanalyzer.cpp.
|
private |
Changes the current role of the signal analyzer.
This will stop all current actions first, and then adjust all parameters.
Definition at line 164 of file signalanalyzer.cpp.
|
private |
Create a polygon for drawing.
powerspec | : reference to the power spectrum rendered by the FFT |
poly | : reference to a map relating frequency and power (f->I). |
Definition at line 685 of file signalanalyzer.cpp.
|
private |
Clipping detector.
The signature of an overdriven signal is the clipping at certain maximal amplitudes. This is recognized here by counting the PCM values which are close to the maximal amplitude.
Similarly, some audio devices transmit intermittent data with random strings of zeros in between. This is detected by counting the number of vanishing PCM amplitudes.
signal | : vector holding the incoming audio signal |
Definition at line 647 of file signalanalyzer.cpp.
|
inline |
Definition at line 77 of file signalanalyzer.h.
|
finaloverrideprivatevirtual |
Message receiver and dispatcer.
m | : pointer to the received message |
Implements MessageListener.
Definition at line 101 of file signalanalyzer.cpp.
|
private |
identify final key
Identify the selected key.
The KeyRecognizer has continuously sent messages containing the number of the recognized key. These results have beeen registered by the in the histogram mKeyCount. This function declares a key as selected if more it was recognized for more than 50%.
Definition at line 733 of file signalanalyzer.cpp.
void SignalAnalyzer::init | ( | ) |
Initializes the SignalAnalyzer and its components.
This function will initialize the KeyRecognizer that could take a while if the fft transform shall be optimized.
Definition at line 75 of file signalanalyzer.cpp.
|
finaloverrideprivatevirtual |
Implements KeyRecognizerCallback.
Definition at line 750 of file signalanalyzer.cpp.
|
private |
Perform fast Fourier transform.
This function transforms the real-valued signal of length N to a complex-valued Fourier transform of length N/2+1. Then it computes the power spectrum by computing the intensities (squares).
signal | : reference to the vector of the incoming audio signal. |
powerspectrum | : reference to the resulting powerspectrum. |
Definition at line 619 of file signalanalyzer.cpp.
|
private |
Process the singal after recording has finsihed.
In ROLE_RECORD_KEYSTROKE this will perform the actual analysis of the complete recorded signal. in ROLE_ROLLING_FFT this wont do anything.
Definition at line 416 of file signalanalyzer.cpp.
|
private |
Record signal and perform FFT's in regular intervals.
Definition at line 249 of file signalanalyzer.cpp.
|
private |
Function for signal preprocessing.
This function performs several steps to make the input signal ready for the Fourier transformation:
signal | : real-valued vector with PCM data |
Definition at line 486 of file signalanalyzer.cpp.
|
private |
Function for signal processing.
Create an FFT vector and perform the FFT.
On ROLE_ROLLING_FFT this will permanentely send FinalFFT On ROLE_RECORD_KEYSTRO this will peramaentely send NewFFT
Definition at line 569 of file signalanalyzer.cpp.
|
overridevirtual |
Stop the thread.
Mark the thread for cancellation. This function waits for termination of the thread, i.e. it blocks until the thread has been shut down. This waiting time will depend on the implementation of the workerFunction.
Reimplemented from SimpleThreadHandler.
Definition at line 87 of file signalanalyzer.cpp.
|
private |
|
private |
|
finaloverrideprivatevirtual |
Thread function of the SignalAnalyzer.
This is the main function of the SignalAnalyzer which is running in an independent thread. It is started by a message from the AudioRecorder when the recording begins. By calling the AudioRecorder via the readAll function the actual data is read and processed.
Implements SimpleThreadHandler.
Definition at line 218 of file signalanalyzer.cpp.
|
private |
|
private |
|
static |
Maximal size of the audio buffer.
Definition at line 60 of file signalanalyzer.h.
|
private |
Definition at line 128 of file signalanalyzer.h.
|
private |
Pointer to the audio recorder.
Definition at line 113 of file signalanalyzer.h.
|
private |
Local audio buffer.
Definition at line 111 of file signalanalyzer.h.
|
private |
The data buffer might change its size during recording and key selection, lock it.
Definition at line 112 of file signalanalyzer.h.
|
private |
Instance of the Fourier transformer.
Definition at line 118 of file signalanalyzer.h.
|
private |
Instance of the FFT analyzer.
Definition at line 120 of file signalanalyzer.h.
|
static |
Time interval for at most one FFT.
Definition at line 61 of file signalanalyzer.h.
|
private |
Count which key is selected how often.
Definition at line 123 of file signalanalyzer.h.
|
private |
Corresponding mutex.
Definition at line 124 of file signalanalyzer.h.
|
private |
Is the key selection forced.
Definition at line 126 of file signalanalyzer.h.
|
private |
Instance of the Key recognizer.
Definition at line 121 of file signalanalyzer.h.
|
private |
Instance of the overpull estimator.
Definition at line 122 of file signalanalyzer.h.
|
private |
Pointer to the piano.
Definition at line 110 of file signalanalyzer.h.
|
private |
the last recorded powerspectrum
Definition at line 116 of file signalanalyzer.h.
|
private |
the current signal (after preprocessing)
Definition at line 115 of file signalanalyzer.h.
|
private |
Flag indicating ongoing recording.
Definition at line 114 of file signalanalyzer.h.
|
private |
The selected key by the user.
Definition at line 125 of file signalanalyzer.h.