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
stroboscope.cpp
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 // Stroboscope
22 //=============================================================================
23 
24 #include "audiorecorderadapter.h"
25 #include "stroboscope.h"
26 #include "../../messages/messagehandler.h"
27 #include "../../messages/messagestroboscope.h"
28 #include "../../math/mathtools.h"
29 #include "core/settings.h"
30 
31 //-----------------------------------------------------------------------------
32 // Constructor
33 //-----------------------------------------------------------------------------
34 
39 
41  mRecorder(recorder),
42  mActive(false),
43  mSamplesPerFrame(22050),
44  mSampleCounter(0),
45  mMaxAmplitude(1E-21)
46 {
47 }
48 
49 
50 //-----------------------------------------------------------------------------
51 // push raw PCM data to the stroboscope (called by AudioRecorderAdapter)
52 //-----------------------------------------------------------------------------
53 
58 
60 {
61  // data packet size is typically 1100, can be 0.
62  if (mActive) if (Settings::getSingleton().isStroboscopeActive())
63  {
64  std::lock_guard<std::mutex> lock (mMutex);
65  for (auto &pcm : data)
66  {
67  if (fabs(pcm)>mMaxAmplitude) mMaxAmplitude=fabs(pcm);
68  if (mMaxAmplitude < 1E-20) continue;
69 
70  int N = mComplexPhase.size();
71  for (int i=0; i<N; ++i)
72  {
75  }
76  if (mSampleCounter-- <= 0)
77  {
78  for (auto &c : mComplexPhase) c /= std::abs(c);
79  ComplexVector normalizedPhases (mMeanComplexPhase);
80  for (auto &c : normalizedPhases) c /= 0.5*mSamplesPerFrame/(1-FRAME_DAMPING);
81  MessageHandler::send<MessageStroboscope>(normalizedPhases);
82 
83  for (auto &c : mMeanComplexPhase) c *= FRAME_DAMPING;
86  }
87  }
88  }
89 }
90 
91 
92 //-----------------------------------------------------------------------------
93 // Set frames per second
94 //-----------------------------------------------------------------------------
95 
100 
102 {
104 }
105 
106 
107 //-----------------------------------------------------------------------------
108 // Set frequencies of the partials to be analyzed
109 //-----------------------------------------------------------------------------
110 
115 
116 void Stroboscope::setFrequencies(const std::vector<double> &frequencies)
117 {
118  std::lock_guard<std::mutex> lock (mMutex);
119  mComplexPhase.resize(frequencies.size());
120  mComplexPhase.assign(frequencies.size(),1);
121  mMeanComplexPhase.resize(frequencies.size());
122  mMeanComplexPhase.assign(frequencies.size(),0);
123  mComplexIncrement.clear();
124  for (auto &f : frequencies)
125  mComplexIncrement.push_back(std::exp(Complex(0,MathTools::TWO_PI) *
126  (f/mRecorder->getSamplingRate())));
127 }
128 
ComplexVector mComplexPhase
Rotating complex number.
Definition: stroboscope.h:94
void setFramesPerSecond(double fps)
Stroboscope::setFramesPerSecond.
std::vector< PCMDataType > PacketType
Type definition of a PCM packet (vector of PCM values).
Definition: audiobase.h:51
Abstract adapter class for recording audio signals.
static Settings & getSingleton()
Get a pointer to the singleton instance.
Definition: settings.cpp:46
void pushRawData(const AudioBase::PacketType &data)
Stroboscope::pushRawData.
Definition: stroboscope.cpp:59
bool mActive
Flag indicating activity (start/stop)
Definition: stroboscope.h:90
std::mutex mMutex
Mutex protecting access from different threads.
Definition: stroboscope.h:97
ComplexVector mComplexIncrement
Factor by which the complex number rotates.
Definition: stroboscope.h:95
ComplexVector mMeanComplexPhase
Phase average over the actual frame.
Definition: stroboscope.h:96
const double FRAME_DAMPING
Damping of the complex phases from frame to frame (0...1)
Definition: stroboscope.h:74
int mSampleCounter
Actual number of PCM samples read.
Definition: stroboscope.h:92
Stroboscope(AudioRecorderAdapter *recorder)
Constructor.
Definition: stroboscope.cpp:40
std::vector< Complex > ComplexVector
Type for a vector of complex numbers.
Definition: stroboscope.h:78
void setFrequencies(const std::vector< double > &frequencies)
Stroboscope::setFrequencies.
int getSamplingRate() const
Get the actual sampling rate.
Definition: audiobase.cpp:78
const double AMPLITUDE_DAMPING
Damping factor of the normalizing amplitude level on a single frame (0...1)
Definition: stroboscope.h:71
double mMaxAmplitude
Sliding amplitude to normalize the data.
Definition: stroboscope.h:93
AudioRecorderAdapter * mRecorder
Pointer to the audio recorder.
Definition: stroboscope.h:89
int mSamplesPerFrame
Number of PCM samples per frame.
Definition: stroboscope.h:91
std::complex< double > Complex
Type for a complex number.
Definition: stroboscope.h:77
const double TWO_PI
Definition: mathtools.h:38