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 Class Reference

Synthesizer class. More...

#include <synthesizer.h>

+ Inheritance diagram for Synthesizer:
+ Collaboration diagram for Synthesizer:

Public Types

using Spectrum = std::map< double, double >
 

Public Member Functions

 Synthesizer ()
 Constructor, intitializes the member variables. More...
 
virtual void init (int sampleRate, int channels) overridefinal
 Initialize and start the synthesizer. More...
 
virtual void exit () overridefinal
 Exit, shut down writer interface. More...
 
void setNumberOfKeys (int numberOfKeys)
 Tell the synthesizer to change the total number of keys. More...
 
void preCalculateWaveform (const int id, const Spectrum &spectrum)
 Pre-calculate the PCM waveform of a sound. More...
 
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) More...
 
void ModifySustainLevel (const int id, const double level)
 Change the level (sustain level) of a constantly playing sound. More...
 
void releaseSound (const int id)
 Terminate a sound. More...
 
bool isPlaying (const int id) const
 Check whether a sound with given id is still playing. More...
 
virtual bool generateAudioSignal (AudioBase::PacketType &outputPacket) overridefinal
 Generate waveform. More...
 

Private Types

using Waveform = WaveformGenerator::Waveform
 

Private Member Functions

const TonegetSoundPointer (const int id) const
 Get a pointer to the sound according to the given ID. More...
 
TonegetSoundPointer (const int id)
 
void updateIntensity ()
 Update function to update intensity. More...
 

Private Attributes

WaveformGenerator mWaveformGenerator
 
int mNumberOfKeys
 Number of keys, passed in init() More...
 
std::vector< TonemPlayingTones
 Chord defined as a collection of tones. More...
 
std::mutex mPlayingMutex
 Mutex to protect access to the chord. More...
 
const int_fast64_t SineLength = 16384
 sine value buffer length. More...
 
const double CutoffVolume = 0.00001
 Fade-out volume cutoff. More...
 
Waveform mSineWave
 Sine wave vector, computed in init(). More...
 
FFTRealVector mHammerWaveLeft
 Hammer noise, computed in init(). More...
 
FFTRealVector mHammerWaveRight
 
int mReverbSize
 
int mReverbCounter
 
int mDelay1
 
int mDelay2
 
int mDelay3
 
std::vector< double > mReverbL
 
std::vector< double > mReverbR
 Reverb. More...
 
double mIntensity
 

Static Private Attributes

static const std::vector
< std::vector< int > > 
mHammerKnockFFT
 Hammerknock Fourier data. More...
 

Additional Inherited Members

- Protected Attributes inherited from PCMWriterInterface
int mSampleRate = 0
 Sample rate. More...
 
int mChannels = 0
 Number of channels. More...
 

Detailed Description

Synthesizer class.

This is the synthesizer of the EPT. It runs in an independent thread. The acoustic system of the EPT is driven by messages. The SoundGenerator listens to these messages and tells the Synthesizer which notes have to be played. The Synthesizer in turn calls the WaveformGenerator to pre-calculate PCM data for sound generation. The main task of the synthesizer class is to create a real-time superposition of these pre-calculated PCM waveforms. Moreover, it creates appropriate volume differences and phase shifts between the two stereo channels.

Definition at line 109 of file synthesizer.h.

Member Typedef Documentation

using Synthesizer::Spectrum = std::map<double,double>

Definition at line 113 of file synthesizer.h.

Definition at line 141 of file synthesizer.h.

Constructor & Destructor Documentation

Synthesizer::Synthesizer ( )

Constructor, intitializes the member variables.

Parameters
audioadapter: Pointer to the implementation of the AudioPlayer

Definition at line 73 of file synthesizer.cpp.

Member Function Documentation

virtual void Synthesizer::exit ( )
inlinefinaloverridevirtual

Exit, shut down writer interface.

Implements PCMWriterInterface.

Definition at line 118 of file synthesizer.h.

+ Here is the call graph for this function:

bool Synthesizer::generateAudioSignal ( AudioBase::PacketType outputBuffer)
finaloverridevirtual

Generate waveform.

Parameters
outputBufferThe buffer where to write
Returns
True on success, false if the player should pause (no notes)

This is the heart of the synthesizer which composes the pre-calculated waveforms.

Implements PCMWriterInterface.

Definition at line 322 of file synthesizer.cpp.

+ Here is the call graph for this function:

const Tone * Synthesizer::getSoundPointer ( const int  id) const
private

Get a pointer to the sound according to the given ID.

Parameters
id: Identifier of the sound, nullptr if not found

Definition at line 447 of file synthesizer.cpp.

Tone * Synthesizer::getSoundPointer ( const int  id)
private

Definition at line 456 of file synthesizer.cpp.

void Synthesizer::init ( int  sampleRate,
int  channels 
)
finaloverridevirtual

Initialize and start the synthesizer.

This function initializes the synthesizer in that it pre-calculates a sine function as well as the hammer noise and then starts the main loop of the synthesizer in an indpendent thread.

Reimplemented from PCMWriterInterface.

Definition at line 100 of file synthesizer.cpp.

+ Here is the call graph for this function:

bool Synthesizer::isPlaying ( const int  id) const

Check whether a sound with given id is still playing.

Parameters
id: Identifier of the sound
Returns
Boolean telling whether the sound is still playing.

Definition at line 498 of file synthesizer.cpp.

+ Here is the call graph for this function:

void Synthesizer::ModifySustainLevel ( const int  id,
const double  level 
)

Change the level (sustain level) of a constantly playing sound.

This function changes the sustain volume of a constantly playing sound The synthesizer will adjust to the new volume adiabatically with the respective decay rate.

Parameters
id: Identity tag of the sound (number of key).
level: New sustain level in (0...1).

Definition at line 517 of file synthesizer.cpp.

+ Here is the call graph for this function:

void Synthesizer::playSound ( const int  keynumber,
const double  frequency,
const double  volume,
const Envelope env,
const bool  waitforcomputation = false,
const bool  stereo = true 
)

Function which plays a single note (sound)

This function generates a sound according to the sound structure passed as a parameter. If the waveform for the sound was already computed the sound is played with the envelope specified by the parameter env. If a pre-calculated waveform does not yet exist, then by default no sound is played. However, if the flag waitforcomputation is set, the function waits until the computation of the sound is completed, forcing the sound to be played. This is particularly important for the echo sound after recording.

Parameters
keynumber: Number of the key
frequency: Frequency of the sound
volume: Volume of the sound
env: Envelope structure describing dynamics (ADSR-curve)
waitforcomputation: Wait until the sound has been computed

Definition at line 230 of file synthesizer.cpp.

+ Here is the call graph for this function:

void Synthesizer::preCalculateWaveform ( const int  id,
const Spectrum spectrum 
)

Pre-calculate the PCM waveform of a sound.

The purpose of this function is mainly to save time and to ensure a resonable performance of the synthesizer even on small mobile devices. It pre-calculates the wave forms to be played and stores them in the map SoundLibrary. The sounds are identified by an integer identifier tag (id). The function generates the texture of the sound for the specified sampletime at constant volume. Pre-calculation does not involve any aspects of envelopes and sound dynamics.

The frequency accuracy is limited by the total sampletime. For example, if we generate a sound with a sampletime of 1 second, then it is expected to differ up to 1 Hz from the true sound, leading to potential beats of 1 second. For a quick survey a sampletime of 1 second would be sufficient, but for longer times 5-10 seconds would be desirable.

Parameters
id: Identification tag (usually keynumber + offset)
sound: The sound to be produced (frequency and spectrum)

Definition at line 200 of file synthesizer.cpp.

+ Here is the call graph for this function:

void Synthesizer::releaseSound ( const int  id)

Terminate a sound.

This function forces the sound to fade out, entering the release phase.

Parameters
id: identity tag of the sound (number of key).

Definition at line 477 of file synthesizer.cpp.

void Synthesizer::setNumberOfKeys ( int  numberOfKeys)

Tell the synthesizer to change the total number of keys.

Parameters
numberOfKeys: Number of keys

Definition at line 160 of file synthesizer.cpp.

+ Here is the call graph for this function:

void Synthesizer::updateIntensity ( )
private

Update function to update intensity.

This function looks whether there are sounds in the mPlayingTones-queue. If there are notes to play, it checks their volume. The tones below a certain cutoff volume are removed from the queue. All other notes will be played.

This function will be called in generateAudioSignal at the beginning to update the current states of the played notes.

Definition at line 285 of file synthesizer.cpp.

Member Data Documentation

const double Synthesizer::CutoffVolume = 0.00001
private

Fade-out volume cutoff.

Definition at line 151 of file synthesizer.h.

int Synthesizer::mDelay1
private

Definition at line 160 of file synthesizer.h.

int Synthesizer::mDelay2
private

Definition at line 160 of file synthesizer.h.

int Synthesizer::mDelay3
private

Definition at line 160 of file synthesizer.h.

const std::vector< std::vector< int > > Synthesizer::mHammerKnockFFT
staticprivate

Hammerknock Fourier data.

This file contains the Fourier transformation of a hammer knock over a length of one second. The list contains four sublists:

  1. real part left channel
  2. imag part left channel
  3. real part right channel
  4. imag part right channel Apply an FFT to this data to get the waveform according to the chosen sample rate.

This variable is part of Synthesizer but defined externally in hammerknock.h

Definition at line 155 of file synthesizer.h.

FFTRealVector Synthesizer::mHammerWaveLeft
private

Hammer noise, computed in init().

Definition at line 156 of file synthesizer.h.

FFTRealVector Synthesizer::mHammerWaveRight
private

Definition at line 157 of file synthesizer.h.

double Synthesizer::mIntensity
private

Definition at line 162 of file synthesizer.h.

int Synthesizer::mNumberOfKeys
private

Number of keys, passed in init()

Definition at line 145 of file synthesizer.h.

std::mutex Synthesizer::mPlayingMutex
mutableprivate

Mutex to protect access to the chord.

Definition at line 148 of file synthesizer.h.

std::vector<Tone> Synthesizer::mPlayingTones
private

Chord defined as a collection of tones.

Definition at line 147 of file synthesizer.h.

int Synthesizer::mReverbCounter
private

Definition at line 159 of file synthesizer.h.

std::vector<double> Synthesizer::mReverbL
private

Definition at line 161 of file synthesizer.h.

std::vector<double> Synthesizer::mReverbR
private

Reverb.

Definition at line 161 of file synthesizer.h.

int Synthesizer::mReverbSize
private

Definition at line 159 of file synthesizer.h.

Waveform Synthesizer::mSineWave
private

Sine wave vector, computed in init().

Definition at line 153 of file synthesizer.h.

WaveformGenerator Synthesizer::mWaveformGenerator
private

Definition at line 143 of file synthesizer.h.

const int_fast64_t Synthesizer::SineLength = 16384
private

sine value buffer length.

Definition at line 150 of file synthesizer.h.


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