AudioDataLib
A C++ library of classes supporting a variety of audio-data tasks.
Loading...
Searching...
No Matches
AudioDataLib::MidiSynth Class Referenceabstract

Derivatives of this class know how to synthesize MIDI messages into real-time audio. More...

#include <MidiSynth.h>

Inheritance diagram for AudioDataLib::MidiSynth:
AudioDataLib::MidiMsgDestination AudioDataLib::SampleBasedSynth AudioDataLib::SimpleSynth AudioDataLib::SubtractiveSynth

Public Member Functions

 MidiSynth ()
 
virtual ~MidiSynth ()
 
virtual bool Process (Error &error) override
 
virtual SynthModuleGetRootModule (uint16_t channel)=0
 
void SetAudioStream (std::shared_ptr< AudioStream > audioStream)
 
std::shared_ptr< AudioStreamGetAudioStream ()
 
void SetMinMaxLatency (double minLatencySeconds, double maxLatencySeconds)
 
void GetMinMaxLatency (double &minLatencySeconds, double &maxLatencySeconds) const
 
- Public Member Functions inherited from AudioDataLib::MidiMsgDestination
 MidiMsgDestination ()
 
virtual ~MidiMsgDestination ()
 
virtual bool ReceiveMessage (double deltaTimeSeconds, const uint8_t *message, uint64_t messageSize, Error &error)
 
virtual bool Initialize (Error &error)
 
virtual bool Finalize (Error &error)
 

Static Public Member Functions

static double MidiPitchToFrequency (uint8_t pitchValue)
 
static double MidiVelocityToAmplitude (uint8_t velocityValue)
 

Protected Attributes

std::shared_ptr< AudioStream > * audioStream
 
double minLatencySeconds
 This is the minimum amount of audio (measured in seconds) that should always be buffered at any given time.
 
double maxLatencySeconds
 This is the maximum amount of audio (measured in seconds) that should always be buffered at any given time.
 

Detailed Description

Derivatives of this class know how to synthesize MIDI messages into real-time audio.

Constructor & Destructor Documentation

◆ MidiSynth()

MidiSynth::MidiSynth ( )

◆ ~MidiSynth()

MidiSynth::~MidiSynth ( )
virtual

Member Function Documentation

◆ GetAudioStream()

std::shared_ptr< AudioStream > AudioDataLib::MidiSynth::GetAudioStream ( )
inline

Return a shared pointer to the AudioStream we're trying to continuously feed.

◆ GetMinMaxLatency()

void MidiSynth::GetMinMaxLatency ( double & minLatencySeconds,
double & maxLatencySeconds ) const

Get the latency range.

◆ GetRootModule()

virtual SynthModule * AudioDataLib::MidiSynth::GetRootModule ( uint16_t channel)
pure virtual

A derived class must impliment this method to provide a SynthModule that can feed the given channel. Note that the term "channel" is overloaded. It can refer to a channel in a stream of audio (e.g., left, right, mono, etc.), as is the case here, or it can mean a MIDI channel, of which there are 16, so don't get them confused. Context usually makes it clear.

Implemented in AudioDataLib::SampleBasedSynth, AudioDataLib::SimpleSynth, and AudioDataLib::SubtractiveSynth.

◆ MidiPitchToFrequency()

double MidiSynth::MidiPitchToFrequency ( uint8_t pitchValue)
static

◆ MidiVelocityToAmplitude()

double MidiSynth::MidiVelocityToAmplitude ( uint8_t velocityValue)
static

◆ Process()

bool MidiSynth::Process ( Error & error)
overridevirtual

This is where we strike a balance between keeping enough audio data buffered in our audio stream to prevent an audio drop-out while also not buffering too much so as to maintain the lowest possible latency. If we don't feed the audio buffer quick enough, we periodically have to feed silence to the audio stream. If we feed the audio buffer too much, then changes to the audio (such as a note turnning on or off) don't happen soon enough, or as close to when they actually happend as we would like. As far as drop-outs are concerned, this class is really at the mercy of the SynthModule implimentations, which needs to be as quick as they possibly can be.

A derived class might override this to do its own processing, but should call this base-class method as well.

Reimplemented from AudioDataLib::MidiMsgDestination.

Reimplemented in AudioDataLib::SampleBasedSynth.

◆ SetAudioStream()

void MidiSynth::SetAudioStream ( std::shared_ptr< AudioStream > audioStream)

The goal of this class is to feed the given AudioStream class. Typically this is chosen as a ThreadSafeAudioStream, because, while it is fed on the main thread, it is usually consumed in a time-sensative audio callback running on a dedicated audio thread.

◆ SetMinMaxLatency()

void MidiSynth::SetMinMaxLatency ( double minLatencySeconds,
double maxLatencySeconds )

Set the latency range. The synthesizer tries to keep the amount of audio (measured in seconds) to always be somewhere in this range. You typically want the range to be to be as small as is reasonably possible, both in size and in distance to zero.

Member Data Documentation

◆ audioStream

std::shared_ptr<AudioStream>* AudioDataLib::MidiSynth::audioStream
protected

◆ maxLatencySeconds

double AudioDataLib::MidiSynth::maxLatencySeconds
protected

This is the maximum amount of audio (measured in seconds) that should always be buffered at any given time.

◆ minLatencySeconds

double AudioDataLib::MidiSynth::minLatencySeconds
protected

This is the minimum amount of audio (measured in seconds) that should always be buffered at any given time.


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