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
audiorecorderadapter.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 // Adapter for audio input
22 //=============================================================================
23 
24 #ifndef AUDIORECORDERADAPTER_H
25 #define AUDIORECORDERADAPTER_H
26 
27 //#include "../audiobase.h"
28 #include "../circularbuffer.h"
29 #include "stroboscope.h"
30 //#include "../../messages/messagelistener.h"
31 #include "../../piano/piano.h"
32 
33 #include <vector>
34 #include <map>
35 #include <mutex>
36 
50 
52 {
53 public:
54  // Static constants, explained in the source file:
55 
56  static const int BUFFER_SIZE_IN_SECONDS; // size of circular buffer
57  static const int UPDATE_IN_MILLISECONDS; // elementary packet size
58  static const double ATTACKRATE; // for sliding level
59  static const double DECAYRATE; // for sliding level
60  static const double LEVEL_RETRIGGER; // level for retriggering
61  static const double LEVEL_TRIGGER; // level where rec. starts
62  static const double LEVEL_CUTOFF; // highest allowed level
63  static const double DB_OFF; // dB shift for off mark
64 
65 public:
67  virtual ~AudioRecorderAdapter() {}
68 
69  void resetInputLevelControl(); // Reset level control
70  void setMuted(bool muted); // Mute the input device
71 
72  void readAll(PacketType &packet); // Read all buffered data
73  void cutSilence (PacketType &packet); // Cut off trailing silence
74 
75  double getStopLevel() const { return mStopLevel; }
76 
77  void setStandby (bool flag) { mStandby = flag; }
78  void setWaitingFlag (bool flag) { mWaiting = flag; }
79 
81 
82 protected:
83  // The implementation calls the following functions:
84  void pushRawData (const PacketType &data);
85  void setSamplingRate (int rate) override;
86 
87  // This class controls the input gain of the implementation
88  virtual void setDeviceInputGain(double volume) = 0;
89  virtual double getDeviceInputGain() const = 0;
90 
91 
92 
93 private:
94  bool mMuted;
95  double mGain;
96  int mCounter;
98  double mPacketM1;
99  double mPacketM2;
100  double mSlidingLevel;
101  double mStopLevel;
102  bool mRecording;
104  bool mWaiting;
105  bool mStandby;
107 
108  std::map <int,double> mIntensityHistogram;
109 
111  mutable std::mutex mCurrentPacketMutex;
112 
114 
115  double convertIntensityToLevel (double intensity); // map for VU meter
116  double convertLevelToIntensity (double level); // inverse map VU meter
117  void controlRecordingState(double level); // switch recording on/off
118  void automaticControl (double intensity, double level); // automatic input level control
119 };
120 
121 #endif // AUDIORECORDERADAPTER_H
AudioRecorderAdapter()
Constructor.
std::vector< PCMDataType > PacketType
Type definition of a PCM packet (vector of PCM values).
Definition: audiobase.h:51
Stroboscope * getStroboscope()
Abstract adapter class for recording audio signals.
virtual ~AudioRecorderAdapter()
Empty destructor.
void setWaitingFlag(bool flag)
double mSlidingLevel
Sliding VU level of the signal.
static const double ATTACKRATE
Attack rate at which the sliding level goes up (1=instantly).
double convertIntensityToLevel(double intensity)
Convert an intensity (variance) of the signal to a VU level.
void setMuted(bool muted)
Set and reset the muting flag.
CircularBuffer< PCMDataType > mCurrentPacket
Local audio buffer.
bool mMuted
Is the input device muted.
static const double LEVEL_TRIGGER
Level above which the recorder starts to operate.
void pushRawData(const PacketType &data)
The implementation calls this function when new data is available.
void controlRecordingState(double level)
Control the beginning and the end of recording.
static const int BUFFER_SIZE_IN_SECONDS
Capacity of the local circular audio buffer in seconds.
std::mutex mCurrentPacketMutex
Buffer access mutexbo.
Stroboscope mStroboscope
Instance of stroboscope.
bool mStandby
Standby flag.
bool mRecording
Flag true if recording is on.
Class for a stroboscopic tuning indicator.
Definition: stroboscope.h:67
virtual void setDeviceInputGain(double volume)=0
bool mWaiting
Wait for the data analysis to be completed.
int mCounterThreshold
Counter threshold for updating energy.
double getStopLevel() const
double convertLevelToIntensity(double level)
Convert a VU level to the corresponding intensity. This function is the inverse of convertIntensityTo...
std::map< int, double > mIntensityHistogram
Histogram of intensities.
int mPacketCounter
Counter for the number of packages.
static const int UPDATE_IN_MILLISECONDS
Update interval in milliseconds, defining the packet size.
void readAll(PacketType &packet)
Read all data from the internal buffer.
void resetInputLevelControl()
Reset input level control.
double mStopLevel
Level at which recording stops.
double mGain
Recording amplification factor.
static const double LEVEL_CUTOFF
Level above which the input mGain is automatically reduced.
void setSamplingRate(int rate) override
void automaticControl(double intensity, double level)
Automatic noise estimation and threshold adjustment.
static const double DB_OFF
dB shift for off mark (high value = shorter recording)
double mPacketM2
Second intensity moment of a single packet.
static const double LEVEL_RETRIGGER
Level below which retriggering (restart) is allowed.
double mPacketM1
First intensity moment of a single packet.
virtual double getDeviceInputGain() const =0
bool mRestartable
Flag true if start/retriggering possible.
int mCounter
Counts counting incoming PCM values.
void cutSilence(PacketType &packet)
Cut trailing silence.
void setStandby(bool flag)
Abstract base class for audio interfaces.
Definition: audiobase.h:43
static const double DECAYRATE
Decay rate at which the sliding level goes down.