Just Intonation  Version 1.3.0 (18)
Explore scale-independent dynamically adapting tuning in just intonation
tuner.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright 2016-2017 Karolin Stange, Christoph Wick, and Haye Hinrichsen
3  *
4  * This file is part of JustIntonation.
5  *
6  * JustIntonation 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  * JustIntonation 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
14  * for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with JustIntonation. If not, see http://www.gnu.org/licenses/.
18  *****************************************************************************/
19 
20 //=============================================================================
21 // Tuner: User interface for instant tuning
22 //=============================================================================
23 
24 #ifndef TUNER_H
25 #define TUNER_H
26 
27 #include <QObject>
28 #include <QVector>
29 #include <QMap>
30 #include <QVariant>
31 #include <QTimer>
32 #include <QElapsedTimer>
33 #include <QMidiMessage>
34 
35 #include "tunerreadme.h"
36 #include "system/threadbase.h"
37 #include "tunerkeydata.h"
38 #include "tuneralgorithm.h"
39 
47 
48 class Tuner : public ThreadBase
49 {
50  Q_OBJECT
51 public:
52  Tuner(); // Constructor
53  bool init() override final; // Initialization after construction
54  bool start() override final; // Start tuner thread
55 
56 public slots:
57 
58  void setFrequencyOfA4 (double f=440);
59  void setTuningMode (int mode, int wolfsShift); // 0:ET 1:UT 2:JI
60  void enableTuning (bool enable);
61  void setStaticTuningMode (bool enable, int reference = 0);
63  double secondsSustainBass,
64  double secondsSustainTreble,
65  double secondsRelease);
66 
67  void setIntervalSize (int halftones, double cents);
68  void setIntervalWeight (int halftones, double weight);
69  void setPitchProgressionCompensationParameter (double lambda);
70  void setDelayParameter (double delay);
71  void setMemoryLength (double seconds);
72 
73  void receiveMidiEvent (QMidiMessage event);
74  void resetPitchProgression();
75 
76 signals:
80 
83  void signalTuningCorrections (QMap<int,double> corrections);
84 
87  void signalAveragePitchProgression (double progression);
88 
90  void signalIntervalString (QVariant str);
91 
93  void signalTension (QVariant mu);
94 
95  //-------------------------------------------------------------------------
96 
97 private:
98  // Constants:
99  const uint updateIntervalMsec = 20;
100  const uint noteTimeoutSeconds = 60;
101  const double cutoffIntensity = 0.001;
102  const double cutoffMemory = 0.0001;
103  const double memoryOnSeconds = 0.3;
104  const double memoryOnFactor = exp(-0.005*updateIntervalMsec/memoryOnSeconds);
105 
106 private:
107  // Member variables
113  QString mLastMessage;
114  QVector<double> mIntervalSizes;
115  QVector<double> mIntervalWeights;
116  QVector<TunerKeyData> mKeyData;
118  QElapsedTimer *pElapsedTimer;
123 
124 private:
125  friend class TunerAlgorithm;
126 
127  qint64 getNow();
128  void handlePitchProgression();
129  void emitPitchCorrections();
130 
131  virtual void initiallyCalledWorker() override final;
132  virtual void periodicallyCalledWorker() override final;
133  virtual void finallyCalledWorker() override final;
134 
135 private slots:
136  void tune();
137 };
138 
139 #endif // TUNER_H
void signalIntervalString(QVariant str)
Signal emitting the tuned interval sizes in a human-readable form.
bool start() override final
Public slot: Start the tuner, starting the tuner thread.
Definition: tuner.cpp:92
Universal base class for threaded modules.
Definition: threadbase.h:60
const double cutoffMemory
Definition: tuner.h:102
double mPitchAutoCorrectionParameter
Pitch correction parameters.
Definition: tuner.h:120
const double memoryOnSeconds
Definition: tuner.h:103
QVector< double > mIntervalWeights
List of 12 interval weights.
Definition: tuner.h:115
TunerAlgorithm mTunerAlgorithm
The tuning algorithm.
Definition: tuner.h:117
void enableTuning(bool enable)
Public Slot: Enable or disable adaptive tuning.
Definition: tuner.cpp:168
double mMemoryOffFactor
Update factor for memory loss.
Definition: tuner.h:122
void setStaticTuningMode(bool enable, int reference=0)
Slot: Enable or disable static tuning mode.
Definition: tuner.cpp:449
bool init() override final
Initialize the Tuner.
Definition: tuner.cpp:66
void setMemoryLength(double seconds)
Public slot: Set memory parameter.
Definition: tuner.cpp:239
void resetPitchProgression()
Reset average pitch progression to a given value.
Definition: tuner.cpp:648
QString mLastMessage
remember last message sent
Definition: tuner.h:113
const uint noteTimeoutSeconds
Definition: tuner.h:100
void setTuningMode(int mode, int wolfsShift)
Public slot: Set tuning mode.
Definition: tuner.cpp:131
Tuner()
Constructor of the Tuner.
Definition: tuner.cpp:35
virtual void periodicallyCalledWorker() override final
Periodically called worker function: Update key data.
Definition: tuner.cpp:538
void signalAveragePitchProgression(double progression)
Signal that periodically transmits the average pitch progression.
void handlePitchProgression()
Handle pitch progression.
Definition: tuner.cpp:613
bool mTuningEnabled
Flag for temporary on/off.
Definition: tuner.h:112
double mDelayParameter
Delay time.
Definition: tuner.h:121
QString f(Eigen::MatrixXd mat)
Definition: tunerdebug.h:29
QVector< TunerKeyData > mKeyData
Vector containing all key data.
Definition: tuner.h:116
void setDelayParameter(double delay)
Public Slot: Set delay parameter.
Definition: tuner.cpp:218
void signalTension(QVariant mu)
Signal emitting the network tension in the tuned chord.
int mStaticReferenceKey
Index of static UT reference key.
Definition: tuner.h:110
void setIntervalSize(int halftones, double cents)
Public Slot: Set interval size This function allows the user to specify the temperament. To this end one specifies the interval sizes relative to the equal temperament in cents. The complete definition of the temperament requires to set all interval sizes, meaning that this slot has to be called 11 times.
Definition: tuner.cpp:331
QVector< double > mIntervalSizes
List of 12 interval sizes.
Definition: tuner.h:114
double mGlobalTargetPitchInCents
Global pitch against 440Hz.
Definition: tuner.h:108
This is the main class of the tuner module to be included by the user.
Definition: tuner.h:48
qint64 getNow()
Private function: Get current elapsed tuner runtime in milliseconds.
Definition: tuner.cpp:433
void signalTuningCorrections(QMap< int, double > corrections)
Signal sending the tuning results to the microtonal sound device in the form of a map keyindex -> cen...
const uint updateIntervalMsec
Definition: tuner.h:99
void receiveMidiEvent(QMidiMessage event)
Public slot: Receive Midi event (Main input of the tuner)
Definition: tuner.cpp:263
Main Tuning Algorithm.
void setFrequencyOfA4(double f=440)
Public slot: Set the desired target frequency of A4 (concert pitch)
Definition: tuner.cpp:108
const double memoryOnFactor
Definition: tuner.h:104
int mWolfsIntervalShift
Placement of the wolfs interval.
Definition: tuner.h:111
void setIntervalWeight(int halftones, double weight)
Public Slot: Set interval weight The human perception of various intervals depends on their size...
Definition: tuner.cpp:361
void setPitchProgressionCompensationParameter(double lambda)
Public slot: Set the parameter for pitch progression compensation.
Definition: tuner.cpp:193
void emitPitchCorrections()
Emit the pitch corrections This functions checks to what extent the calculated pitches differ from th...
Definition: tuner.cpp:381
void signalReadyForReceivingParameters()
Signal telling the GUI that the tuner is ready and that the GUI may send the user-defined parameters ...
QElapsedTimer * pElapsedTimer
Time elapsed since construction.
Definition: tuner.h:118
bool mStaticTuningModeEnabled
Flag: Tune statically.
Definition: tuner.h:119
virtual void finallyCalledWorker() override final
Cleanup of the terminating thread.
Definition: tuner.cpp:523
const double cutoffIntensity
Definition: tuner.h:101
int mTuningMode
Actual tuning mode.
Definition: tuner.h:109
void setEnvelopeParameters(double secondsSustainBass, double secondsSustainTreble, double secondsRelease)
Set the essential parameters of the envelope.
Definition: tuner.cpp:483
virtual void initiallyCalledWorker() override final
Initialization of the starting thread.
Definition: tuner.cpp:509
void tune()
Tune the pitches of the pressed keys.
Definition: tuner.cpp:580