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.h
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  *****************************************************************************/
19 
20 //=============================================================================
21 // Signal analyzer
22 //=============================================================================
23 
24 #ifndef SIGNALANALYZER_H
25 #define SIGNALANALYZER_H
26 
27 #include <fftw3.h>
28 #include <atomic>
29 #include <map>
30 
31 #include "../system/simplethreadhandler.h"
32 #include "../messages/messagelistener.h"
33 #include "../audio/circularbuffer.h"
34 #include "../math/fftimplementation.h"
35 #include "fftanalyzer.h"
36 #include "keyrecognizer.h"
37 #include "overpull.h"
38 
40 
53 
55  public SimpleThreadHandler,
56  public MessageListener,
57  private KeyRecognizerCallback
58 {
59 public:
60  static const int AUDIO_BUFFER_SIZE_IN_SECONDS = 60;
61  static const int MINIMAL_FFT_INTERVAL_IN_MILLISECONDS = 150;
62 
63 private:
64 
66  {
70  };
71 
72 public:
75 
76  void init();
77  void exit() {}
78 
79  virtual void stop() override;
80 
81 private:
82  void handleMessage (MessagePtr m) override final; // Message receiver
83 
84  void changeRole(AnalyzerRole role);
85  void updateDataBufferSize();
86 
87  void workerFunction() override final; // Thread execution function
88 
89  void recordSignal();
90  void analyzeSignal();
91  void recordPostprocessing(); // processing after recording finished
92  void updateOverpull();
93 
94  double signalPreprocessing(FFTWVector &signal); // Preprocessing of incoming signal
95  void signalProcessing(FFTWVector &signal, int samplingrate); // processing of the current data
96  bool detectClipping(FFTWVector signal); // Clipping detector, not yet implemented
97  void PerformFFT (FFTWVector &signal, FFTWVector &powerspec); // Perform fast Fourier transformation
98  void createPolygon (const FFTWVector &powerspec, FFTPolygon &poly) const; // Create polygon for drawing
99 
100  int identifySelectedKey();
101 
102  // callbacks
103  virtual void keyRecognized(int keyIndex, double frequency) override final;
104 
105 
106  void WriteSignal (std::string filename, const FFTWVector &signal); // Development, will be removed
107  void WriteFFT (std::string filename, const FFTWVector &fft); // Development, will be removed
108 
109 private:
110  const Piano *mPiano;
112  std::mutex mDataBufferMutex;
114  std::atomic<bool> mRecording;
117 
119 
123  std::map<int,int> mKeyCountStatistics;
126  bool mKeyForced;
127 
129 };
130 
131 #endif // SIGNALANALYZER_H
bool detectClipping(FFTWVector signal)
Clipping detector.
void changeRole(AnalyzerRole role)
Changes the current role of the signal analyzer.
std::shared_ptr< Message > MessagePtr
Global type of a shared message pointer.
Definition: message.h:98
std::mutex mKeyCountStatisticsMutex
Corresponding mutex.
int identifySelectedKey()
identify final key
Abstract adapter class for recording audio signals.
virtual void keyRecognized(int keyIndex, double frequency) overridefinal
static const int AUDIO_BUFFER_SIZE_IN_SECONDS
Maximal size of the audio buffer.
std::atomic< AnalyzerRole > mAnalyzerRole
void signalProcessing(FFTWVector &signal, int samplingrate)
Function for signal processing.
Performing rolling ffts in tuning mode.
AudioRecorderAdapter * mAudioRecorder
Pointer to the audio recorder.
void recordPostprocessing()
Process the singal after recording has finsihed.
Callback class for KeyRecognizer.
Definition: keyrecognizer.h:40
FFTDataPointer mPowerspectrum
the last recorded powerspectrum
std::map< double, double > FFTPolygon
Type for a frequency-to-intensity map for graphics.
Definition: fftadapter.h:45
void WriteFFT(std::string filename, const FFTWVector &fft)
static const int MINIMAL_FFT_INTERVAL_IN_MILLISECONDS
Time interval for at most one FFT.
std::mutex mDataBufferMutex
The data buffer might change its size during recording and key selection, lock it.
std::atomic< bool > mRecording
Flag indicating ongoing recording.
Definition: piano.h:40
void handleMessage(MessagePtr m) overridefinal
Message receiver and dispatcer.
FFTAnalyzer mFFTAnalyser
Instance of the FFT analyzer.
bool mKeyForced
Is the key selection forced.
CircularBuffer< FFTWType > mDataBuffer
Local audio buffer.
std::map< int, int > mKeyCountStatistics
Count which key is selected how often.
void createPolygon(const FFTWVector &powerspec, FFTPolygon &poly) const
Create a polygon for drawing.
Template class for a circular buffer.
virtual void stop() override
Stop the thread.
KeyRecognizer mKeyRecognizer
Instance of the Key recognizer.
void updateDataBufferSize()
void analyzeSignal()
Analyze the final signal (in recording mode only)
FFT_Implementation mFFT
Instance of the Fourier transformer.
SignalAnalyzer(AudioRecorderAdapter *recorder)
Constructor of the SignalAnalyzer.
FFTRealType FFTWType
data type
Definition: fftadapter.h:41
void recordSignal()
Record signal and perform FFT's in regular intervals.
OverpullEstimator mOverpull
Instance of the overpull estimator.
void WriteSignal(std::string filename, const FFTWVector &signal)
FFTWVector mProprocessedSignal
the current signal (after preprocessing)
Module performing the final analysis of the Fourier transform.
Definition: fftanalyzer.h:43
Module for fast recognition of the pressed key.
Definition: keyrecognizer.h:57
void PerformFFT(FFTWVector &signal, FFTWVector &powerspec)
Perform fast Fourier transform.
Class for estimating the overpull needed in a pitch raise.
Definition: overpull.h:52
std::shared_ptr< FFTData > FFTDataPointer
Shared pointer of FFTData.
Definition: fftadapter.h:86
std::vector< FFTWType > FFTWVector
fftw array
Definition: fftadapter.h:43
Signal analyzer: Fourier transformation of the recorded audio signal.
double signalPreprocessing(FFTWVector &signal)
Function for signal preprocessing.
Simple thread handler.
Recording a key stroke in recording operation mode.
int mSelectedKey
The selected key by the user.
const Piano * mPiano
Pointer to the piano.
void init()
Initializes the SignalAnalyzer and its components.
Thread-safe implementation of fftw3.
void workerFunction() overridefinal
Thread function of the SignalAnalyzer.