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
entropyminimizer::EntropyMinimizer Class Reference

Iterative entropy minimization algorithm. More...

#include <entropyminimizer.h>

+ Inheritance diagram for entropyminimizer::EntropyMinimizer:
+ Collaboration diagram for entropyminimizer::EntropyMinimizer:

Public Member Functions

 EntropyMinimizer (const Piano &piano, const AlgorithmFactoryDescription &desciption)
 Constructor. More...
 
 ~EntropyMinimizer ()
 
- Public Member Functions inherited from Algorithm
 Algorithm (const Piano &piano, const AlgorithmFactoryDescription &desciption)
 
virtual ~Algorithm ()
 
virtual void workerFunction () overridefinal
 Virtual worker function, executed within the new 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...
 
virtual void stop ()
 Stop the thread. More...
 
- Public Member Functions inherited from MessageListener
 MessageListener (bool defaultActivation=true)
 
virtual ~MessageListener ()
 
bool isMessageListenerActive () const
 
void activateMessageListener ()
 
void deactivateMessageListener ()
 

Protected Member Functions

virtual void handleMessage (MessagePtr m) overridefinal
 Message listener and dispatcher for mouse clicks in the tuning curve. More...
 
void algorithmWorkerFunction () overridefinal
 Main calculation, running in an independent thread. More...
 
void writeAccumulator (std::string filename)
 
void writeSpectrum (int k, std::string filename, int pitch=0)
 
- Protected Member Functions inherited from Algorithm
void updateTuningCurve (int keynumber, double frequency)
 
bool terminateThread ()
 
void showCalculationProgress (double fraction)
 Transmit the current percentage of progress to the messaging system. More...
 
void showCalculationProgress (const Key &key, double start=0, double range=1)
 Transmit the current percentage of progress to the messaging system. 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...
 

Private Types

using SpectrumType = Key::SpectrumType
 
using Keys = Keyboard::Keys
 

Private Member Functions

bool performAuditoryPreprocessing ()
 Auditory preprocessing. More...
 
void ComputeInitialTuningCurve ()
 Compute initial condition of the tuning curve. More...
 
void minimizeEntropy ()
 Entropy minimizer. More...
 
void updateTuningcurve (int keynumber)
 Update the tuning curve for a given key number. More...
 
void updateTuningcurve ()
 Update the entire tuning curve. More...
 
void clear ()
 
double getElement (SpectrumType &spectrum, int m)
 Truncate logspectrum at the cutoffs and return an element. More...
 
void addToAccumulator (SpectrumType &spectrum, int shift, double intensity)
 Add or subtract a spectrum to the accumulator. More...
 
void modifySpectralComponent (int key, int pitch)
 
void setAllSpectralComponents ()
 
void addReferenceSpectrum (double intensity)
 
int getTolerance (int keynumber)
 Allowed tolerance of the tuning curve during the Monte Carlo process. More...
 
double computeEntropy ()
 Compute the entropy of the current normalized accumulator content. More...
 
double getRecordedPitchET440 (int keynumber)
 Get recorded pitch. More...
 
int getRecordedPitchET440AsInt (int keynumber)
 Get recorded pitch. More...
 
int getPitchET440 (int keynumber, double f)
 Get pitch from frequ. More...
 

Static Private Member Functions

static double ftom (double f)
 Convert frequency to array index in cent spacing. More...
 
static double mtof (int m)
 Convert array index in cent spacing to frequency in Hz. More...
 

Private Attributes

const int NumberOfBins = Key::NumberOfBins
 
SpectrumType mAccumulator
 Accumulator holding the sum of all spectra. More...
 
std::vector< int > mPitch
 Vector of pitches (in cents) More...
 
std::vector< double > mInitialPitch
 Vector of initial pitches. More...
 
int mLowerCutoff
 Lower cutoff for fluctuations. More...
 
int mUpperCutoff
 Upper cutoff for fluctuations. More...
 
bool mRecalculateEntropy
 Flag for entropy recalculation (after manual intervention by the user) More...
 
int mRecalculateKey
 Number of manually changed key. More...
 
double mRecalculateFrequency
 Frequency of manually changed key. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from SimpleThreadHandler
static void setThreadName (std::string s)
 Specify the name of the thread. More...
 
- Protected Types inherited from Algorithm
using Keys = Keyboard::Keys
 
using SpectrumType = Key::SpectrumType
 
- Protected Attributes inherited from Algorithm
const size_t NumberOfBins = Key::NumberOfBins
 
Piano mPiano
 Copy of the piano. More...
 
const AlgorithmFactoryDescriptionmFactoryDescription
 
KeyboardmKeyboard
 Reference to the keyboard. More...
 
KeysmKeys
 Reference to the keys. More...
 
const int mNumberOfKeys
 The number of keys. More...
 
const int mKeyNumberOfA4
 Number of A4. More...
 

Detailed Description

Iterative entropy minimization algorithm.

This class is at the center of the entropy piano tuner, i.e. it carries out the actual tuning procedure.

The most important member variable is the mAccumulator, where the spectra are added up. The purpose of the accumulator is to save time. Instead of computing the sum of all spectra after each Monte Carlo step again, we simply subtract the previous and add the new spectrum of the modified key alone.

Definition at line 48 of file entropyminimizer.h.

Member Typedef Documentation

Constructor & Destructor Documentation

entropyminimizer::EntropyMinimizer::EntropyMinimizer ( const Piano piano,
const AlgorithmFactoryDescription description 
)

Constructor.

Parameters
piano: The complete piano structure will be copied from here

Definition at line 65 of file entropyminimizer.cpp.

+ Here is the call graph for this function:

entropyminimizer::EntropyMinimizer::~EntropyMinimizer ( )
inline

Definition at line 52 of file entropyminimizer.h.

Member Function Documentation

void entropyminimizer::EntropyMinimizer::addReferenceSpectrum ( double  intensity)
private
void entropyminimizer::EntropyMinimizer::addToAccumulator ( SpectrumType spectrum,
int  shift,
double  intensity 
)
private

Add or subtract a spectrum to the accumulator.

Since the entropy is computed from the accumulator, the accmulator values have a probability interpretation. Therfore, spectra should be added

Parameters
spectrum: Logarithmic spectrum
shift: number of bins by which the spectrum is shifted
intensity: weight at wich the spectrum is added (+) or subtracted (-).

Definition at line 344 of file entropyminimizer.cpp.

+ Here is the call graph for this function:

void entropyminimizer::EntropyMinimizer::algorithmWorkerFunction ( )
finaloverrideprotectedvirtual

Main calculation, running in an independent thread.

This is the main function which carries out the calculation. First it calls the function for auditory preprocessing, then it manages the Monte Carlo iteration.

Implements Algorithm.

Definition at line 97 of file entropyminimizer.cpp.

+ Here is the call graph for this function:

void entropyminimizer::EntropyMinimizer::clear ( )
private

Definition at line 306 of file entropyminimizer.cpp.

double entropyminimizer::EntropyMinimizer::computeEntropy ( )
private

Compute the entropy of the current normalized accumulator content.

Returns
Numerical value of the entropy

Definition at line 517 of file entropyminimizer.cpp.

+ Here is the call graph for this function:

void entropyminimizer::EntropyMinimizer::ComputeInitialTuningCurve ( )
private

Compute initial condition of the tuning curve.

As described in the original publication, the Monte Carlo minimization algorithm could in principle start with a flat (equal temperament) tuning curve. However, starting with a flat curve involves the risk that the algorithm finds false minima (for example, a deviation of -50 cent instead of +50 cent which is of course also a valid solution). To prevent the algorithm from running into false minima, we implemented several additional features. In particular, we start out from an approximate tuning curve which already shows the expected stretch. This curve is computed by first defining a linear section between A3 and A5 and then extending this curve to both sides by matching selected higher partials. The selection of the partials and their weight differs on both sides. Note that possible errors do not play a role at this point because they are elimiated by the subsequent Monte Carlo procedure.

Definition at line 420 of file entropyminimizer.cpp.

+ Here is the call graph for this function:

static double entropyminimizer::EntropyMinimizer::ftom ( double  f)
inlinestaticprivate

Convert frequency to array index in cent spacing.

Definition at line 72 of file entropyminimizer.h.

+ Here is the call graph for this function:

double entropyminimizer::EntropyMinimizer::getElement ( SpectrumType spectrum,
int  m 
)
private

Truncate logspectrum at the cutoffs and return an element.

Parameters
spectrum: Logarithmically binned spectrum.
m: Index of the element to be returned.

Definition at line 324 of file entropyminimizer.cpp.

int entropyminimizer::EntropyMinimizer::getPitchET440 ( int  keynumber,
double  f 
)
private

Get pitch from frequ.

Get the computed pitch versus equal temperament 440 in cents.

Parameters
f: frequency
Returns
Pitch in cents

Definition at line 747 of file entropyminimizer.cpp.

+ Here is the call graph for this function:

double entropyminimizer::EntropyMinimizer::getRecordedPitchET440 ( int  keynumber)
private

Get recorded pitch.

Compute the pitch versus equal temperament 440 in cents.

Returns
Pitch in cents

Definition at line 724 of file entropyminimizer.cpp.

int entropyminimizer::EntropyMinimizer::getRecordedPitchET440AsInt ( int  keynumber)
private

Get recorded pitch.

Definition at line 731 of file entropyminimizer.cpp.

+ Here is the call graph for this function:

int entropyminimizer::EntropyMinimizer::getTolerance ( int  keynumber)
private

Allowed tolerance of the tuning curve during the Monte Carlo process.

In order to avoid false minima, we further restrict the allowed range of the tuning curve. This function returns the allowed tolerance in cents around the initial condition defined above. It is defined heuristically.

Parameters
keynumber: Number of the key

Definition at line 488 of file entropyminimizer.cpp.

+ Here is the call graph for this function:

void entropyminimizer::EntropyMinimizer::handleMessage ( MessagePtr  m)
finaloverrideprotectedvirtual

Message listener and dispatcher for mouse clicks in the tuning curve.

The tuninng curve can be manipulated manually, i.e., individual markers can be moved with the mouse. If the calculation is running this requires to reset the entropy calculation (which is usually higher after the manipulation). This function sets the corresponding flags.

Parameters
m: Pointer to the message.

Implements MessageListener.

Definition at line 142 of file entropyminimizer.cpp.

+ Here is the call graph for this function:

void entropyminimizer::EntropyMinimizer::minimizeEntropy ( )
private

Entropy minimizer.

This function is probably the most important one in the EPT. Here the Monte Carlo process is carried out in order to minize the entropy of the superposed spectra.

We have here a so-called zero-temperature Monte Carlo algorithm. This means that a move is accepted if the entropy goes down and rejected otherwise. The fluctuations are in principle arbitrary. Here we consider two types of fluctuations. (a) select a key and vary its pitch randomly, and (b) select a key and vary all pitches either to the left or to the right randomly by the same amount. These two 'methods' are stochastically mixed with a 'methodRatio' which varies slowly as time proceeds. It turns out that this greatly reduces the computation time.

Definition at line 545 of file entropyminimizer.cpp.

+ Here is the call graph for this function:

void entropyminimizer::EntropyMinimizer::modifySpectralComponent ( int  key,
int  pitch 
)
private

Definition at line 362 of file entropyminimizer.cpp.

+ Here is the call graph for this function:

static double entropyminimizer::EntropyMinimizer::mtof ( int  m)
inlinestaticprivate

Convert array index in cent spacing to frequency in Hz.

Definition at line 74 of file entropyminimizer.h.

+ Here is the call graph for this function:

bool entropyminimizer::EntropyMinimizer::performAuditoryPreprocessing ( )
private

Auditory preprocessing.

A large part of the computation is a sensible preprocessing of the logarithmically binned spectra.

Definition at line 183 of file entropyminimizer.cpp.

+ Here is the call graph for this function:

void entropyminimizer::EntropyMinimizer::setAllSpectralComponents ( )
private

Definition at line 384 of file entropyminimizer.cpp.

+ Here is the call graph for this function:

void entropyminimizer::EntropyMinimizer::updateTuningcurve ( int  keynumber)
private

Update the tuning curve for a given key number.

This function translates the actual mPitch value into the corresponding frequency, stores the value in the local piano copy mPiano and sends a message that the tuning curve has to be redrawn.

Parameters
keynumber: Number of the key to be updated

Definition at line 279 of file entropyminimizer.cpp.

+ Here is the call graph for this function:

void entropyminimizer::EntropyMinimizer::updateTuningcurve ( )
private

Update the entire tuning curve.

This function translates the all mPitch values into the corresponding frequencies, stores the values in the local piano copy mPiano and sends a sequence of messages that the tuning curve has to be redrawn.

Definition at line 295 of file entropyminimizer.cpp.

void entropyminimizer::EntropyMinimizer::writeAccumulator ( std::string  filename)
protected

Definition at line 759 of file entropyminimizer.cpp.

+ Here is the call graph for this function:

void entropyminimizer::EntropyMinimizer::writeSpectrum ( int  k,
std::string  filename,
int  pitch = 0 
)
protected

Definition at line 777 of file entropyminimizer.cpp.

+ Here is the call graph for this function:

Member Data Documentation

SpectrumType entropyminimizer::EntropyMinimizer::mAccumulator
private

Accumulator holding the sum of all spectra.

Definition at line 89 of file entropyminimizer.h.

std::vector<double> entropyminimizer::EntropyMinimizer::mInitialPitch
private

Vector of initial pitches.

Definition at line 91 of file entropyminimizer.h.

int entropyminimizer::EntropyMinimizer::mLowerCutoff
private

Lower cutoff for fluctuations.

Definition at line 92 of file entropyminimizer.h.

std::vector<int> entropyminimizer::EntropyMinimizer::mPitch
private

Vector of pitches (in cents)

Definition at line 90 of file entropyminimizer.h.

bool entropyminimizer::EntropyMinimizer::mRecalculateEntropy
private

Flag for entropy recalculation (after manual intervention by the user)

Definition at line 94 of file entropyminimizer.h.

double entropyminimizer::EntropyMinimizer::mRecalculateFrequency
private

Frequency of manually changed key.

Definition at line 96 of file entropyminimizer.h.

int entropyminimizer::EntropyMinimizer::mRecalculateKey
private

Number of manually changed key.

Definition at line 95 of file entropyminimizer.h.

int entropyminimizer::EntropyMinimizer::mUpperCutoff
private

Upper cutoff for fluctuations.

Definition at line 93 of file entropyminimizer.h.

const int entropyminimizer::EntropyMinimizer::NumberOfBins = Key::NumberOfBins
private

Definition at line 66 of file entropyminimizer.h.


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