Loading...
Searching...
No Matches
juce::Synthesiser Class Reference

`#include <juce_Synthesiser.h>`

## Public Member Functions

| Synthesiser ()
virtual | ~Synthesiser ()
void | clearVoices ()
int | getNumVoices () const noexcept
SynthesiserVoice * | getVoice (int index) const
SynthesiserVoice * | addVoice (SynthesiserVoice *newVoice)
void | removeVoice (int index)
void | clearSounds ()
int | getNumSounds () const noexcept
SynthesiserSound::Ptr | getSound (int index) const noexcept
SynthesiserSound * | addSound (const SynthesiserSound::Ptr &newSound)
void | removeSound (int index)
void | setNoteStealingEnabled (bool shouldStealNotes)
bool | isNoteStealingEnabled () const noexcept
virtual void | noteOn (int midiChannel, int midiNoteNumber, float velocity)
virtual void | noteOff (int midiChannel, int midiNoteNumber, float velocity, bool allowTailOff)
virtual void | allNotesOff (int midiChannel, bool allowTailOff)
virtual void | handlePitchWheel (int midiChannel, int wheelValue)
virtual void | handleController (int midiChannel, int controllerNumber, int controllerValue)
virtual void | handleAftertouch (int midiChannel, int midiNoteNumber, int aftertouchValue)
virtual void | handleChannelPressure (int midiChannel, int channelPressureValue)
virtual void | handleSustainPedal (int midiChannel, bool isDown)
virtual void | handleSostenutoPedal (int midiChannel, bool isDown)
virtual void | handleSoftPedal (int midiChannel, bool isDown)
virtual void | handleProgramChange (int midiChannel, int programNumber)
virtual void | setCurrentPlaybackSampleRate (double sampleRate)
void | renderNextBlock (AudioBuffer< float > &outputAudio, const MidiBuffer &inputMidi, int startSample, int numSamples)
void | renderNextBlock (AudioBuffer< double > &outputAudio, const MidiBuffer &inputMidi, int startSample, int numSamples)
double | getSampleRate () const noexcept
void | setMinimumRenderingSubdivisionSize (int numSamples, bool shouldBeStrict=false) noexcept

## Protected Member Functions

virtual void | renderVoices (AudioBuffer< float > &outputAudio, int startSample, int numSamples)
virtual void | renderVoices (AudioBuffer< double > &outputAudio, int startSample, int numSamples)
virtual SynthesiserVoice * | findFreeVoice (SynthesiserSound *soundToPlay, int midiChannel, int midiNoteNumber, bool stealIfNoneAvailable) const
virtual SynthesiserVoice * | findVoiceToSteal (SynthesiserSound *soundToPlay, int midiChannel, int midiNoteNumber) const
void | startVoice (SynthesiserVoice *voice, SynthesiserSound *sound, int midiChannel, int midiNoteNumber, float velocity)
void | stopVoice (SynthesiserVoice *, float velocity, bool allowTailOff)
virtual void | handleMidiEvent (const MidiMessage &)

## Protected Attributes

CriticalSection | lock
OwnedArray< SynthesiserVoice > | voices
ReferenceCountedArray< SynthesiserSound > | sounds
int | lastPitchWheelValues [16]

## Detailed Description

Base class for a musical device that can play sounds.

To create a synthesiser, you'll need to create a subclass of SynthesiserSound to describe each sound available to your synth, and a subclass of SynthesiserVoice which can play back one of these sounds.

Then you can use the addVoice() and addSound() methods to give the synthesiser a set of sounds, and a set of voices it can use to play them. If you only give it one voice it will be monophonic - the more voices it has, the more polyphony it'll have available.

Then repeatedly call the renderNextBlock() method to produce the audio. Any midi events that go in will be scanned for note on/off messages, and these are used to start and stop the voices playing the appropriate sounds.

While it's playing, you can also cause notes to be triggered by calling the noteOn(), noteOff() and other controller methods.

Before rendering, be sure to call the setCurrentPlaybackSampleRate() to tell it what the target playback rate is. This value is passed on to the voices so that they can pitch their output correctly.

Definition at line 307 of file juce_Synthesiser.h.

## Constructor & Destructor Documentation

## Synthesiser()

juce::Synthesiser::Synthesiser | ( |  | ) |

Creates a new synthesiser. You'll need to add some sounds and voices before it'll make any sound.

Definition at line 76 of file juce_Synthesiser.cpp.

## ~Synthesiser()

juce::Synthesiser::~Synthesiser | ( |  | ) |

virtual

Destructor.

Definition at line 82 of file juce_Synthesiser.cpp.

## Member Function Documentation

## addSound()

SynthesiserSound * juce::Synthesiser::addSound | ( | const SynthesiserSound::Ptr & | newSound | ) |

Adds a new sound to the synthesiser.

The object passed in is reference counted, so will be deleted when the synthesiser and all voices are no longer using it.

Definition at line 129 of file juce_Synthesiser.cpp.

## addVoice()

SynthesiserVoice * juce::Synthesiser::addVoice | ( | SynthesiserVoice * | newVoice | ) |

Adds a new voice to the synth.

All the voices should be the same class of object and are treated equally.

The object passed in will be managed by the synthesiser, which will delete it later on when no longer needed. The caller should not retain a pointer to the voice.

Definition at line 99 of file juce_Synthesiser.cpp.

## allNotesOff()

void juce::Synthesiser::allNotesOff | ( | int | midiChannel,
|  | bool | allowTailOff )

virtual

Turns off all notes.

This will turn off any voices that are playing a sound on the given midi channel.

If midiChannel is 0 or less, then all voices will be turned off, regardless of which channel they're playing. Otherwise it represents a valid midi channel, from 1 to 16 inclusive.

If allowTailOff is true, the voices will be allowed to fade out the notes gracefully (if they can do). If this is false, the notes will all be cut off immediately.

This method will be called automatically according to the midi data passed into renderNextBlock(), but may be called explicitly too.

Definition at line 380 of file juce_Synthesiser.cpp.

Referenced by handleMidiEvent(), and setCurrentPlaybackSampleRate().

## clearSounds()

void juce::Synthesiser::clearSounds | ( |  | ) |

Deletes all sounds.

Definition at line 123 of file juce_Synthesiser.cpp.

## clearVoices()

void juce::Synthesiser::clearVoices | ( |  | ) |

Deletes all voices.

Definition at line 93 of file juce_Synthesiser.cpp.

## findFreeVoice()

SynthesiserVoice * juce::Synthesiser::findFreeVoice | ( | SynthesiserSound * | soundToPlay,
|  | int | midiChannel,
|  | int | midiNoteNumber,
|  | bool | stealIfNoneAvailable ) const

protected virtual

Searches through the voices to find one that's not currently playing, and which can play the given sound.

Returns nullptr if all voices are busy and stealing isn't enabled.

To implement a custom note-stealing algorithm, you can either override this method, or (preferably) override findVoiceToSteal().

Definition at line 497 of file juce_Synthesiser.cpp.

Referenced by noteOn().

## findVoiceToSteal()

SynthesiserVoice * juce::Synthesiser::findVoiceToSteal | ( | SynthesiserSound * | soundToPlay,
|  | int | midiChannel,
|  | int | midiNoteNumber ) const

protected virtual

Chooses a voice that is most suitable for being re-used. The default method will attempt to find the oldest voice that isn't the bottom or top note being played. If that's not suitable for your synth, you can override this method and do something more cunning instead.

Definition at line 513 of file juce_Synthesiser.cpp.

Referenced by findFreeVoice().

## getNumSounds()

int juce::Synthesiser::getNumSounds | ( |  | ) | const

inline noexcept

Returns the number of sounds that have been added to the synth.

Definition at line 347 of file juce_Synthesiser.h.

## getNumVoices()

int juce::Synthesiser::getNumVoices | ( |  | ) | const

inline noexcept

Returns the number of voices that have been added.

Definition at line 324 of file juce_Synthesiser.h.

## getSampleRate()

double juce::Synthesiser::getSampleRate | ( |  | ) | const

inline noexcept

Returns the current target sample rate at which rendering is being done. Subclasses may need to know this so that they can pitch things correctly.

Definition at line 536 of file juce_Synthesiser.h.

## getSound()

SynthesiserSound::Ptr juce::Synthesiser::getSound | ( | int | index | ) | const

inline noexcept

Returns one of the sounds.

Definition at line 350 of file juce_Synthesiser.h.

## getVoice()

SynthesiserVoice * juce::Synthesiser::getVoice | ( | int | index | ) | const

Returns one of the voices that have been added.

Definition at line 87 of file juce_Synthesiser.cpp.

## handleAftertouch()

void juce::Synthesiser::handleAftertouch | ( | int | midiChannel,
|  | int | midiNoteNumber,
|  | int | aftertouchValue )

virtual

Sends an aftertouch message.

This will send an aftertouch message to any voices that are playing sounds on the given midi channel and note number.

This method will be called automatically according to the midi data passed into renderNextBlock(), but may be called explicitly too.

Parameters

  midiChannel | the midi channel, from 1 to 16 inclusive
  midiNoteNumber | the midi note number, 0 to 127
  aftertouchValue | the aftertouch value, between 0 and 127, as returned by MidiMessage::getAftertouchValue()

Definition at line 419 of file juce_Synthesiser.cpp.

Referenced by handleMidiEvent().

## handleChannelPressure()

void juce::Synthesiser::handleChannelPressure | ( | int | midiChannel,
|  | int | channelPressureValue )

virtual

Sends a channel pressure message.

This will send a channel pressure message to any voices that are playing sounds on the given midi channel.

This method will be called automatically according to the midi data passed into renderNextBlock(), but may be called explicitly too.

Parameters

  midiChannel | the midi channel, from 1 to 16 inclusive
  channelPressureValue | the pressure value, between 0 and 127, as returned by MidiMessage::getChannelPressureValue()

Definition at line 429 of file juce_Synthesiser.cpp.

Referenced by handleMidiEvent().

## handleController()

void juce::Synthesiser::handleController | ( | int | midiChannel,
|  | int | controllerNumber,
|  | int | controllerValue )

virtual

Sends a midi controller message to any active voices.

This will send a midi controller message to any voices that are playing sounds on the given midi channel.

This method will be called automatically according to the midi data passed into renderNextBlock(), but may be called explicitly too.

Parameters

  midiChannel | the midi channel, from 1 to 16 inclusive
  controllerNumber | the midi controller type, as returned by MidiMessage::getControllerNumber()
  controllerValue | the midi controller value, between 0 and 127, as returned by MidiMessage::getControllerValue()

Definition at line 400 of file juce_Synthesiser.cpp.

Referenced by handleMidiEvent().

## handleMidiEvent()

void juce::Synthesiser::handleMidiEvent | ( | const MidiMessage & | m | ) |

protected virtual

Can be overridden to do custom handling of incoming midi events.

Definition at line 254 of file juce_Synthesiser.cpp.

## handlePitchWheel()

void juce::Synthesiser::handlePitchWheel | ( | int | midiChannel,
|  | int | wheelValue )

virtual

Sends a pitch-wheel message to any active voices.

This will send a pitch-wheel message to any voices that are playing sounds on the given midi channel.

This method will be called automatically according to the midi data passed into renderNextBlock(), but may be called explicitly too.

Parameters

  midiChannel | the midi channel, from 1 to 16 inclusive
  wheelValue | the wheel position, from 0 to 0x3fff, as returned by MidiMessage::getPitchWheelValue()

Definition at line 391 of file juce_Synthesiser.cpp.

Referenced by handleMidiEvent().

## handleProgramChange()

void juce::Synthesiser::handleProgramChange | ( | int | midiChannel,
|  | int | programNumber )

virtual

Can be overridden to handle an incoming program change message. The base class implementation of this has no effect, but you may want to make your own synth react to program changes.

Definition at line 490 of file juce_Synthesiser.cpp.

Referenced by handleMidiEvent().

## handleSoftPedal()

void juce::Synthesiser::handleSoftPedal | ( | int | midiChannel,
|  | bool | isDown )

virtual

Can be overridden to handle soft pedal events.

Definition at line 485 of file juce_Synthesiser.cpp.

Referenced by handleController().

## handleSostenutoPedal()

void juce::Synthesiser::handleSostenutoPedal | ( | int | midiChannel,
|  | bool | isDown )

virtual

Handles a sostenuto pedal event.

Definition at line 468 of file juce_Synthesiser.cpp.

Referenced by handleController().

## handleSustainPedal()

void juce::Synthesiser::handleSustainPedal | ( | int | midiChannel,
|  | bool | isDown )

virtual

Handles a sustain pedal event.

Definition at line 438 of file juce_Synthesiser.cpp.

Referenced by handleController().

## isNoteStealingEnabled()

bool juce::Synthesiser::isNoteStealingEnabled | ( |  | ) | const

inline noexcept

Returns true if note-stealing is enabled.

See also
  setNoteStealingEnabled

Definition at line 374 of file juce_Synthesiser.h.

## noteOff()

void juce::Synthesiser::noteOff | ( | int | midiChannel,
|  | int | midiNoteNumber,
|  | float | velocity,
|  | bool | allowTailOff )

virtual

Triggers a note-off event.

This will turn off any voices that are playing a sound for the given note/channel.

If allowTailOff is true, the voices will be allowed to fade out the notes gracefully (if they can do). If this is false, the notes will all be cut off immediately.

This method will be called automatically according to the midi data passed into renderNextBlock(), but may be called explicitly too.

The midiChannel parameter is the channel, between 1 and 16 inclusive.

Definition at line 351 of file juce_Synthesiser.cpp.

Referenced by handleMidiEvent().

## noteOn()

void juce::Synthesiser::noteOn | ( | int | midiChannel,
|  | int | midiNoteNumber,
|  | float | velocity )

virtual

Triggers a note-on event.

The default method here will find all the sounds that want to be triggered by this note/channel. For each sound, it'll try to find a free voice, and use the voice to start playing the sound.

Subclasses might want to override this if they need a more complex algorithm.

This method will be called automatically according to the midi data passed into renderNextBlock(), but may be called explicitly too.

The midiChannel parameter is the channel, between 1 and 16 inclusive.

Definition at line 295 of file juce_Synthesiser.cpp.

Referenced by handleMidiEvent().

## removeSound()

void juce::Synthesiser::removeSound | ( | int | index | ) |

Removes and deletes one of the sounds.

Definition at line 135 of file juce_Synthesiser.cpp.

## removeVoice()

void juce::Synthesiser::removeVoice | ( | int | index | ) |

Deletes one of the voices.

Definition at line 117 of file juce_Synthesiser.cpp.

## renderNextBlock() [1/2]

void juce::Synthesiser::renderNextBlock | ( | AudioBuffer< double > & | outputAudio,
|  | const MidiBuffer & | inputMidi,
|  | int | startSample,
|  | int | numSamples )

Definition at line 236 of file juce_Synthesiser.cpp.

## renderNextBlock() [2/2]

void juce::Synthesiser::renderNextBlock | ( | AudioBuffer< float > & | outputAudio,
|  | const MidiBuffer & | inputMidi,
|  | int | startSample,
|  | int | numSamples )

Creates the next block of audio output.

This will process the next numSamples of data from all the voices, and add that output to the audio block supplied, starting from the offset specified. Note that the data will be added to the current contents of the buffer, so you should clear it before calling this method if necessary.

The midi events in the inputMidi buffer are parsed for note and controller events, and these are used to trigger the voices. Note that the startSample offset applies both to the audio output buffer and the midi input buffer, so any midi events with timestamps outside the specified region will be ignored.

Definition at line 230 of file juce_Synthesiser.cpp.

## renderVoices() [1/2]

void juce::Synthesiser::renderVoices | ( | AudioBuffer< double > & | outputAudio,
|  | int | startSample,
|  | int | numSamples )

protected virtual

Definition at line 248 of file juce_Synthesiser.cpp.

## renderVoices() [2/2]

void juce::Synthesiser::renderVoices | ( | AudioBuffer< float > & | outputAudio,
|  | int | startSample,
|  | int | numSamples )

protected virtual

Renders the voices for the given range. By default this just calls renderNextBlock() on each voice, but you may need to override it to handle custom cases.

Definition at line 242 of file juce_Synthesiser.cpp.

## setCurrentPlaybackSampleRate()

void juce::Synthesiser::setCurrentPlaybackSampleRate | ( | double | sampleRate | ) |

virtual

Tells the synthesiser what the sample rate is for the audio it's being used to render.

This value is propagated to the voices so that they can use it to render the correct pitches.

Definition at line 154 of file juce_Synthesiser.cpp.

## setMinimumRenderingSubdivisionSize()

void juce::Synthesiser::setMinimumRenderingSubdivisionSize | ( | int | numSamples,
|  | bool | shouldBeStrict = false )

noexcept

Sets a minimum limit on the size to which audio sub-blocks will be divided when rendering.

When rendering, the audio blocks that are passed into renderNextBlock() will be split up into smaller blocks that lie between all the incoming midi messages, and it is these smaller sub-blocks that are rendered with multiple calls to renderVoices().

Obviously in a pathological case where there are midi messages on every sample, then renderVoices() could be called once per sample and lead to poor performance, so this setting allows you to set a lower limit on the block size.

The default setting is 32, which means that midi messages are accurate to about < 1ms accuracy, which is probably fine for most purposes, but you may want to increase or decrease this value for your synth.

If shouldBeStrict is true, the audio sub-blocks will strictly never be smaller than numSamples.

If shouldBeStrict is false (default), the first audio sub-block in the buffer is allowed to be smaller, to make sure that the first MIDI event in a buffer will always be sample-accurate (this can sometimes help to avoid quantisation or phasing issues).

Definition at line 146 of file juce_Synthesiser.cpp.

## setNoteStealingEnabled()

void juce::Synthesiser::setNoteStealingEnabled | ( | bool | shouldStealNotes | ) |

If set to true, then the synth will try to take over an existing voice if it runs out and needs to play another note.

The value of this boolean is passed into findFreeVoice(), so the result will depend on the implementation of this method.

Definition at line 141 of file juce_Synthesiser.cpp.

## startVoice()

void juce::Synthesiser::startVoice | ( | SynthesiserVoice * | voice,
|  | SynthesiserSound * | sound,
|  | int | midiChannel,
|  | int | midiNoteNumber,
|  | float | velocity )

protected

Starts a specified voice playing a particular sound. You'll probably never need to call this, it's used internally by noteOn(), but may be needed by subclasses for custom behaviours.

Definition at line 317 of file juce_Synthesiser.cpp.

Referenced by noteOn().

## stopVoice()

void juce::Synthesiser::stopVoice | ( | SynthesiserVoice * | voice,
|  | float | velocity,
|  | bool | allowTailOff )

protected

Stops a given voice. You should never need to call this, it's used internally by noteOff, but is protected in case it's useful for some custom subclasses. It basically just calls through to SynthesiserVoice::stopNote(), and has some assertions to sanity-check a few things.

Definition at line 341 of file juce_Synthesiser.cpp.

Referenced by handleSostenutoPedal(), handleSustainPedal(), noteOff(), and noteOn().

## Member Data Documentation

## lastPitchWheelValues

int juce::Synthesiser::lastPitchWheelValues[16]

protected

The last pitch-wheel values for each midi channel.

Definition at line 569 of file juce_Synthesiser.h.

Referenced by handleMidiEvent(), startVoice(), and Synthesiser().

## lock

CriticalSection juce::Synthesiser::lock

protected

This is used to control access to the rendering callback and the note trigger methods.

Definition at line 563 of file juce_Synthesiser.h.

Referenced by addSound(), addVoice(), allNotesOff(), clearSounds(), clearVoices(), findFreeVoice(), getVoice(), handleAftertouch(), handleChannelPressure(), handleController(), handlePitchWheel(), handleSostenutoPedal(), handleSustainPedal(), noteOff(), noteOn(), removeSound(), removeVoice(), and setCurrentPlaybackSampleRate().

## sounds

ReferenceCountedArray<SynthesiserSound> juce::Synthesiser::sounds

protected

Definition at line 566 of file juce_Synthesiser.h.

## voices

OwnedArray<SynthesiserVoice> juce::Synthesiser::voices

protected

Definition at line 565 of file juce_Synthesiser.h.

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

- juce_Synthesiser.h
- juce_Synthesiser.cpp