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
SignalAnalyzer Class Reference

Signal analyzer: Fourier transformation of the recorded audio signal. More...

#include <signalanalyzer.h>

+ Inheritance diagram for SignalAnalyzer:
+ Collaboration diagram for SignalAnalyzer:

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...
 
- Public Member Functions inherited from MessageListener
 MessageListener (bool defaultActivation=true)
 
virtual ~MessageListener ()
 
bool isMessageListenerActive () const
 
void activateMessageListener ()
 
void deactivateMessageListener ()
 

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 PianomPiano
 Pointer to the piano. More...
 
CircularBuffer< FFTWTypemDataBuffer
 Local audio buffer. More...
 
std::mutex mDataBufferMutex
 The data buffer might change its size during recording and key selection, lock it. More...
 
AudioRecorderAdaptermAudioRecorder
 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< AnalyzerRolemAnalyzerRole
 

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

Detailed Description

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.

Member Enumeration Documentation

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.

Constructor & Destructor Documentation

SignalAnalyzer::SignalAnalyzer ( AudioRecorderAdapter recorder)

Constructor of the SignalAnalyzer.

Parameters
recorder: pointer to the implementation of the AudioRecorder

Definition at line 56 of file signalanalyzer.cpp.

SignalAnalyzer::~SignalAnalyzer ( )
inline

Definition at line 74 of file signalanalyzer.h.

Member Function Documentation

void SignalAnalyzer::analyzeSignal ( )
private

Analyze the final signal (in recording mode only)

Definition at line 341 of file signalanalyzer.cpp.

+ Here is the call graph for this function:

void SignalAnalyzer::changeRole ( AnalyzerRole  role)
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.

+ Here is the call graph for this function:

void SignalAnalyzer::createPolygon ( const FFTWVector powerspec,
FFTPolygon poly 
) const
private

Create a polygon for drawing.

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

+ Here is the call graph for this function:

bool SignalAnalyzer::detectClipping ( FFTWVector  signal)
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.

Parameters
signal: vector holding the incoming audio signal
Returns
true if a problem has been detected, false if not

Definition at line 647 of file signalanalyzer.cpp.

void SignalAnalyzer::exit ( )
inline

Definition at line 77 of file signalanalyzer.h.

void SignalAnalyzer::handleMessage ( MessagePtr  m)
finaloverrideprivatevirtual

Message receiver and dispatcer.

Parameters
m: pointer to the received message

Implements MessageListener.

Definition at line 101 of file signalanalyzer.cpp.

+ Here is the call graph for this function:

int SignalAnalyzer::identifySelectedKey ( )
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%.

Returns
Number of the key, -1 if none.

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.

+ Here is the call graph for this function:

void SignalAnalyzer::keyRecognized ( int  keyIndex,
double  frequency 
)
finaloverrideprivatevirtual

Implements KeyRecognizerCallback.

Definition at line 750 of file signalanalyzer.cpp.

+ Here is the call graph for this function:

void SignalAnalyzer::PerformFFT ( FFTWVector signal,
FFTWVector powerspectrum 
)
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).

Parameters
signal: reference to the vector of the incoming audio signal.
powerspectrum: reference to the resulting powerspectrum.

Definition at line 619 of file signalanalyzer.cpp.

+ Here is the call graph for this function:

void SignalAnalyzer::recordPostprocessing ( )
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.

+ Here is the call graph for this function:

void SignalAnalyzer::recordSignal ( )
private

Record signal and perform FFT's in regular intervals.

Definition at line 249 of file signalanalyzer.cpp.

+ Here is the call graph for this function:

double SignalAnalyzer::signalPreprocessing ( FFTWVector signal)
private

Function for signal preprocessing.

This function performs several steps to make the input signal ready for the Fourier transformation:

  1. Cut subsonic waves
  2. Modify the input signal in such a way that the volume is constant.
  3. Fade in and out at the end of the buffer
    Parameters
    signal: real-valued vector with PCM data
    Returns
    Average decay time of the envelope, serving as a rough estimate whether a very low or a very high key has been hit.

Definition at line 486 of file signalanalyzer.cpp.

+ Here is the call graph for this function:

void SignalAnalyzer::signalProcessing ( FFTWVector signal,
int  samplingrate 
)
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.

+ Here is the call graph for this function:

void SignalAnalyzer::stop ( )
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.

+ Here is the call graph for this function:

void SignalAnalyzer::updateDataBufferSize ( )
private

Definition at line 175 of file signalanalyzer.cpp.

+ Here is the call graph for this function:

void SignalAnalyzer::updateOverpull ( )
private

Definition at line 447 of file signalanalyzer.cpp.

+ Here is the call graph for this function:

void SignalAnalyzer::workerFunction ( )
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.

+ Here is the call graph for this function:

void SignalAnalyzer::WriteFFT ( std::string  filename,
const FFTWVector fft 
)
private

Definition at line 777 of file signalanalyzer.cpp.

+ Here is the call graph for this function:

void SignalAnalyzer::WriteSignal ( std::string  filename,
const FFTWVector signal 
)
private

Definition at line 791 of file signalanalyzer.cpp.

+ Here is the call graph for this function:

Member Data Documentation

const int SignalAnalyzer::AUDIO_BUFFER_SIZE_IN_SECONDS = 60
static

Maximal size of the audio buffer.

Definition at line 60 of file signalanalyzer.h.

std::atomic<AnalyzerRole> SignalAnalyzer::mAnalyzerRole
private

Definition at line 128 of file signalanalyzer.h.

AudioRecorderAdapter* SignalAnalyzer::mAudioRecorder
private

Pointer to the audio recorder.

Definition at line 113 of file signalanalyzer.h.

CircularBuffer<FFTWType> SignalAnalyzer::mDataBuffer
private

Local audio buffer.

Definition at line 111 of file signalanalyzer.h.

std::mutex SignalAnalyzer::mDataBufferMutex
private

The data buffer might change its size during recording and key selection, lock it.

Definition at line 112 of file signalanalyzer.h.

FFT_Implementation SignalAnalyzer::mFFT
private

Instance of the Fourier transformer.

Definition at line 118 of file signalanalyzer.h.

FFTAnalyzer SignalAnalyzer::mFFTAnalyser
private

Instance of the FFT analyzer.

Definition at line 120 of file signalanalyzer.h.

const int SignalAnalyzer::MINIMAL_FFT_INTERVAL_IN_MILLISECONDS = 150
static

Time interval for at most one FFT.

Definition at line 61 of file signalanalyzer.h.

std::map<int,int> SignalAnalyzer::mKeyCountStatistics
private

Count which key is selected how often.

Definition at line 123 of file signalanalyzer.h.

std::mutex SignalAnalyzer::mKeyCountStatisticsMutex
private

Corresponding mutex.

Definition at line 124 of file signalanalyzer.h.

bool SignalAnalyzer::mKeyForced
private

Is the key selection forced.

Definition at line 126 of file signalanalyzer.h.

KeyRecognizer SignalAnalyzer::mKeyRecognizer
private

Instance of the Key recognizer.

Definition at line 121 of file signalanalyzer.h.

OverpullEstimator SignalAnalyzer::mOverpull
private

Instance of the overpull estimator.

Definition at line 122 of file signalanalyzer.h.

const Piano* SignalAnalyzer::mPiano
private

Pointer to the piano.

Definition at line 110 of file signalanalyzer.h.

FFTDataPointer SignalAnalyzer::mPowerspectrum
private

the last recorded powerspectrum

Definition at line 116 of file signalanalyzer.h.

FFTWVector SignalAnalyzer::mProprocessedSignal
private

the current signal (after preprocessing)

Definition at line 115 of file signalanalyzer.h.

std::atomic<bool> SignalAnalyzer::mRecording
private

Flag indicating ongoing recording.

Definition at line 114 of file signalanalyzer.h.

int SignalAnalyzer::mSelectedKey
private

The selected key by the user.

Definition at line 125 of file signalanalyzer.h.


The documentation for this class was generated from the following files: