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
synthesizer.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 // A simple sine wave synthesizer
22 //=============================================================================
23 
24 #ifndef SYNTHESIZER_H
25 #define SYNTHESIZER_H
26 
27 #include "audioplayeradapter.h"
28 #include "waveformgenerator.h"
29 #include "pcmwriterinterface.h"
30 #include "../../system/simplethreadhandler.h"
31 
32 
33 //=============================================================================
34 // Structure describing an envelope
35 //=============================================================================
36 
45 
46 struct Envelope
47 {
48  double attack;
49  double decay;
50  double sustain;
51  double release;
52  double hammer;
53 
54  Envelope(double attack=0, double decay=0,
55  double sustain=0, double release=0,
56  double hammer=0);
57 };
58 
59 //=============================================================================
60 // Structure of a tone
61 //=============================================================================
62 
74 
75 struct Tone
76 {
77  int keynumber;
78  double frequency;
79  double leftamplitude;
80  double rightamplitude;
81  double phaseshift;
83 
84  int_fast64_t clock;
85  int stage;
86  double amplitude;
87 
89 };
90 
91 
92 //=============================================================================
93 // Synthesizer class
94 //=============================================================================
95 
108 
110 {
111 public:
112 
113  using Spectrum = std::map<double,double>; // type of spectrum
114 
115  Synthesizer ();
116 
117  virtual void init (int sampleRate, int channels) override final;
118  virtual void exit () override final { mWaveformGenerator.exit();}
119 
120  void setNumberOfKeys (int numberOfKeys);
121 
122  void preCalculateWaveform (const int id, const Spectrum &spectrum);
123 
124  void playSound (const int id,
125  const double frequency,
126  const double volume,
127  const Envelope &env,
128  const bool waitforcomputation = false,
129  const bool stereo = true);
130 
131  void ModifySustainLevel (const int id, const double level);
132 
133  void releaseSound (const int id);
134 
135  bool isPlaying (const int id) const;
136 
137  virtual bool generateAudioSignal(AudioBase::PacketType &outputPacket) override final;
138 
139 private:
140 
142 
144 
146 
147  std::vector<Tone> mPlayingTones;
148  mutable std::mutex mPlayingMutex;
149 
150  const int_fast64_t SineLength = 16384;
151  const double CutoffVolume = 0.00001;
152 
154 
155  static const std::vector< std::vector<int> > mHammerKnockFFT;
158 
161  std::vector<double> mReverbL,mReverbR;
162  double mIntensity;
163 
164  const Tone* getSoundPointer (const int id) const;
165  Tone* getSoundPointer (const int id);
166  void updateIntensity();
167 };
168 
169 #endif // SYNTHESIZER_H
int stage
1=attack 2=decay 3=sustain 4=release.
Definition: synthesizer.h:85
double sustain
Sustain level.
Definition: synthesizer.h:50
std::vector< PCMDataType > PacketType
Type definition of a PCM packet (vector of PCM values).
Definition: audiobase.h:51
int_fast64_t clock
Running time in sample cycles.
Definition: synthesizer.h:84
double leftamplitude
Left stereo volume.
Definition: synthesizer.h:79
std::vector< Tone > mPlayingTones
Chord defined as a collection of tones.
Definition: synthesizer.h:147
Abstract base class for sound-producing software components which transmit data to the AudioPlayerAda...
The WaveformGenerator class.
bool isPlaying(const int id) const
Check whether a sound with given id is still playing.
int keynumber
Identification tag (negativ=sine)
Definition: synthesizer.h:77
const Tone * getSoundPointer(const int id) const
Get a pointer to the sound according to the given ID.
Envelope(double attack=0, double decay=0, double sustain=0, double release=0, double hammer=0)
Envelope::Envelope.
Definition: synthesizer.cpp:49
double rightamplitude
Right stereo volume.
Definition: synthesizer.h:80
std::vector< double > mReverbL
Definition: synthesizer.h:161
int mNumberOfKeys
Number of keys, passed in init()
Definition: synthesizer.h:145
WaveformGenerator mWaveformGenerator
Definition: synthesizer.h:143
void playSound(const int id, const double frequency, const double volume, const Envelope &env, const bool waitforcomputation=false, const bool stereo=true)
Function which plays a single note (sound)
FFTRealVector mHammerWaveRight
Definition: synthesizer.h:157
std::vector< double > mReverbR
Reverb.
Definition: synthesizer.h:161
Waveform mSineWave
Sine wave vector, computed in init().
Definition: synthesizer.h:153
static const std::vector< std::vector< int > > mHammerKnockFFT
Hammerknock Fourier data.
Definition: synthesizer.h:155
double decay
Subsequent decay rate.
Definition: synthesizer.h:49
double mIntensity
Definition: synthesizer.h:162
Synthesizer()
Constructor, intitializes the member variables.
Definition: synthesizer.cpp:73
std::mutex mPlayingMutex
Mutex to protect access to the chord.
Definition: synthesizer.h:148
virtual void exit() overridefinal
Exit, shut down writer interface.
Definition: synthesizer.h:118
double phaseshift
Stereo phase shift.
Definition: synthesizer.h:81
double attack
Initial attack rate.
Definition: synthesizer.h:48
WaveformGenerator::Waveform Waveform
Definition: synthesizer.h:141
Synthesizer class.
Definition: synthesizer.h:109
virtual bool generateAudioSignal(AudioBase::PacketType &outputPacket) overridefinal
Generate waveform.
void releaseSound(const int id)
Terminate a sound.
const int_fast64_t SineLength
sine value buffer length.
Definition: synthesizer.h:150
std::vector< float > Waveform
int mReverbCounter
Definition: synthesizer.h:159
void updateIntensity()
Update function to update intensity.
void setNumberOfKeys(int numberOfKeys)
Tell the synthesizer to change the total number of keys.
Envelope envelope
Dynamic properties of the tone.
Definition: synthesizer.h:82
double frequency
Fundamental frequency.
Definition: synthesizer.h:78
std::vector< FFTRealType > FFTRealVector
Definition: fftadapter.h:35
double hammer
Intensity of hammer noise.
Definition: synthesizer.h:52
void preCalculateWaveform(const int id, const Spectrum &spectrum)
Pre-calculate the PCM waveform of a sound.
void ModifySustainLevel(const int id, const double level)
Change the level (sustain level) of a constantly playing sound.
std::map< double, double > Spectrum
Definition: synthesizer.h:113
double release
Release rate.
Definition: synthesizer.h:51
virtual void init(int sampleRate, int channels) overridefinal
Initialize and start the synthesizer.
const double CutoffVolume
Fade-out volume cutoff.
Definition: synthesizer.h:151
Structure describing the envelope (dynamics) of a sound.
Definition: synthesizer.h:46
WaveformGenerator::Waveform waveform
Copy of the waveform.
Definition: synthesizer.h:88
FFTRealVector mHammerWaveLeft
Hammer noise, computed in init().
Definition: synthesizer.h:156
double amplitude
current envelope amplitude
Definition: synthesizer.h:86
Structure of a single tone.
Definition: synthesizer.h:75