26SynthesiserSound::SynthesiserSound() {}
35 return currentPlayingMidiChannel == midiChannel;
50 currentlyPlayingNote = -1;
51 currentlyPlayingSound =
nullptr;
52 currentPlayingMidiChannel = 0;
60 return noteOnTime <
other.noteOnTime;
64 int startSample,
int numSamples)
68 startSample, numSamples);
90 return voices [index];
105 newVoice->setCurrentPlaybackSampleRate (sampleRate);
111 usableVoicesToStealArray.ensureStorageAllocated (voices.size() + 1);
120 voices.remove (index);
138 sounds.remove (index);
149 minimumSubBlockSize = numSamples;
162 for (
auto*
voice : voices)
167template <
typename floatType>
223 [&] (
const MidiMessageMetadata& meta) { handleMidiEvent (meta.getMessage()); });
227template void Synthesiser::processNextBlock<float> (AudioBuffer<float>&,
const MidiBuffer&,
int,
int);
228template void Synthesiser::processNextBlock<double> (AudioBuffer<double>&,
const MidiBuffer&,
int,
int);
231 int startSample,
int numSamples)
237 int startSample,
int numSamples)
244 for (
auto*
voice : voices)
245 voice->renderNextBlock (buffer, startSample, numSamples);
250 for (
auto*
voice : voices)
297 const float velocity)
301 for (
auto*
sound : sounds)
307 for (
auto*
voice : voices)
319 const int midiChannel,
321 const float velocity)
325 if (
voice->currentlyPlayingSound !=
nullptr)
326 voice->stopNote (0.0f,
false);
329 voice->currentPlayingMidiChannel = midiChannel;
330 voice->noteOnTime = ++lastNoteOnCounter;
332 voice->setKeyDown (
true);
333 voice->setSostenutoPedalDown (
false);
334 voice->setSustainPedalDown (sustainPedalsDown[midiChannel]);
353 const float velocity,
358 for (
auto*
voice : voices)
361 &&
voice->isPlayingChannel (midiChannel))
363 if (
auto sound =
voice->getCurrentlyPlayingSound())
366 &&
sound->appliesToChannel (midiChannel))
368 jassert (!
voice->keyIsDown ||
voice->isSustainPedalDown() == sustainPedalsDown [midiChannel]);
370 voice->setKeyDown (
false);
372 if (! (
voice->isSustainPedalDown() ||
voice->isSostenutoPedalDown()))
384 for (
auto*
voice : voices)
388 sustainPedalsDown.
clear();
395 for (
auto*
voice : voices)
414 for (
auto*
voice : voices)
423 for (
auto*
voice : voices)
425 && (midiChannel <= 0 ||
voice->isPlayingChannel (midiChannel)))
433 for (
auto*
voice : voices)
440 jassert (midiChannel > 0 && midiChannel <= 16);
445 sustainPedalsDown.
setBit (midiChannel);
447 for (
auto*
voice : voices)
448 if (
voice->isPlayingChannel (midiChannel) &&
voice->isKeyDown())
449 voice->setSustainPedalDown (
true);
453 for (
auto*
voice : voices)
455 if (
voice->isPlayingChannel (midiChannel))
457 voice->setSustainPedalDown (
false);
459 if (! (
voice->isKeyDown() ||
voice->isSostenutoPedalDown()))
464 sustainPedalsDown.
clearBit (midiChannel);
470 jassert (midiChannel > 0 && midiChannel <= 16);
473 for (
auto*
voice : voices)
475 if (
voice->isPlayingChannel (midiChannel))
478 voice->setSostenutoPedalDown (
true);
479 else if (
voice->isSostenutoPedalDown())
487 jassert (midiChannel > 0 && midiChannel <= 16);
493 jassert (midiChannel > 0 && midiChannel <= 16);
503 for (
auto*
voice : voices)
504 if ((!
voice->isVoiceActive()) &&
voice->canPlaySound (soundToPlay))
533 usableVoicesToStealArray.clear();
535 for (
auto*
voice : voices)
537 if (
voice->canPlaySound (soundToPlay))
541 usableVoicesToStealArray.add (
voice);
550 std::sort (usableVoicesToStealArray.begin(), usableVoicesToStealArray.end(),
Sorter());
552 if (!
voice->isPlayingButReleased())
554 auto note =
voice->getCurrentlyPlayingNote();
556 if (low ==
nullptr ||
note < low->getCurrentlyPlayingNote())
570 for (
auto*
voice : usableVoicesToStealArray)
575 for (
auto*
voice : usableVoicesToStealArray)
580 for (
auto*
voice : usableVoicesToStealArray)
585 for (
auto*
voice : usableVoicesToStealArray)
A multi-channel buffer containing floating point audio samples.
void makeCopyOf(const AudioBuffer< OtherType > &other, bool avoidReallocating=false)
Resizes this buffer to match the given one, and copies all of its content across.
BigInteger & clear() noexcept
Resets the value to 0.
BigInteger & clearBit(int bitNumber) noexcept
Clears a particular bit in the number.
BigInteger & setBit(int bitNumber)
Sets a specified bit to 1.
Automatically locks and unlocks a mutex object.
Holds a sequence of time-stamped midi events.
Encapsulates a MIDI message.
bool isAftertouch() const noexcept
Returns true if the message is an aftertouch event.
bool isNoteOn(bool returnTrueForVelocity0=false) const noexcept
Returns true if this message is a 'key-down' event.
float getFloatVelocity() const noexcept
Returns the velocity of a note-on or note-off message.
int getChannel() const noexcept
Returns the midi channel associated with the message.
bool isProgramChange() const noexcept
Returns true if the message is a program (patch) change message.
bool isController() const noexcept
Returns true if this is a midi controller message.
bool isAllSoundOff() const noexcept
Checks whether this message is an all-sound-off message.
int getControllerNumber() const noexcept
Returns the controller number of a controller message.
int getChannelPressureValue() const noexcept
Returns the pressure from a channel pressure change message.
bool isNoteOff(bool returnTrueForNoteOnVelocity0=true) const noexcept
Returns true if this message is a 'key-up' event.
bool isPitchWheel() const noexcept
Returns true if the message is a pitch-wheel move.
int getNoteNumber() const noexcept
Returns the midi note number for note-on and note-off messages.
int getProgramChangeNumber() const noexcept
Returns the new program number of a program change message.
int getAfterTouchValue() const noexcept
Returns the amount of aftertouch from an aftertouch messages.
int getControllerValue() const noexcept
Returns the controller value from a controller message.
bool isAllNotesOff() const noexcept
Checks whether this message is an all-notes-off message.
bool isChannelPressure() const noexcept
Returns true if the message is a channel-pressure change event.
int getPitchWheelValue() const noexcept
Returns the pitch wheel position from a pitch-wheel move message.
Describes one of the sounds that a Synthesiser can play.
~SynthesiserSound() override
Destructor.
Represents a voice that a Synthesiser can use to play a SynthesiserSound.
SynthesiserVoice()
Creates a voice.
virtual void channelPressureChanged(int newChannelPressureValue)
Called to let the voice know that the channel pressure has changed.
virtual void renderNextBlock(AudioBuffer< float > &outputBuffer, int startSample, int numSamples)=0
Renders the next block of data for this voice.
virtual bool isPlayingChannel(int midiChannel) const
Returns true if the voice is currently playing a sound which is mapped to the given midi channel.
bool isKeyDown() const noexcept
Returns true if the key that triggered this voice is still held down.
virtual void setCurrentPlaybackSampleRate(double newRate)
Changes the voice's reference sample rate.
virtual void aftertouchChanged(int newAftertouchValue)
Called to let the voice know that the aftertouch has changed.
int getCurrentlyPlayingNote() const noexcept
Returns the midi note that this voice is currently playing.
void clearCurrentNote()
Resets the state of this voice after a sound has finished playing.
bool wasStartedBefore(const SynthesiserVoice &other) const noexcept
Returns true if this voice started playing its current note before the other voice did.
virtual ~SynthesiserVoice()
Destructor.
bool isPlayingButReleased() const noexcept
Returns true if a voice is sounding in its release phase.
virtual bool isVoiceActive() const
Returns true if this voice is currently busy playing a sound.
Synthesiser()
Creates a new synthesiser.
virtual ~Synthesiser()
Destructor.
virtual SynthesiserVoice * findFreeVoice(SynthesiserSound *soundToPlay, int midiChannel, int midiNoteNumber, bool stealIfNoneAvailable) const
Searches through the voices to find one that's not currently playing, and which can play the given so...
virtual void handleProgramChange(int midiChannel, int programNumber)
Can be overridden to handle an incoming program change message.
void removeVoice(int index)
Deletes one of the voices.
void startVoice(SynthesiserVoice *voice, SynthesiserSound *sound, int midiChannel, int midiNoteNumber, float velocity)
Starts a specified voice playing a particular sound.
virtual void handleAftertouch(int midiChannel, int midiNoteNumber, int aftertouchValue)
Sends an aftertouch message.
void renderNextBlock(AudioBuffer< float > &outputAudio, const MidiBuffer &inputMidi, int startSample, int numSamples)
Creates the next block of audio output.
virtual void handleController(int midiChannel, int controllerNumber, int controllerValue)
Sends a midi controller message to any active voices.
void clearSounds()
Deletes all sounds.
virtual SynthesiserVoice * findVoiceToSteal(SynthesiserSound *soundToPlay, int midiChannel, int midiNoteNumber) const
Chooses a voice that is most suitable for being re-used.
virtual void handleSoftPedal(int midiChannel, bool isDown)
Can be overridden to handle soft pedal events.
void removeSound(int index)
Removes and deletes one of the sounds.
virtual void allNotesOff(int midiChannel, bool allowTailOff)
Turns off all notes.
SynthesiserVoice * addVoice(SynthesiserVoice *newVoice)
Adds a new voice to the synth.
void stopVoice(SynthesiserVoice *, float velocity, bool allowTailOff)
Stops a given voice.
SynthesiserSound * addSound(const SynthesiserSound::Ptr &newSound)
Adds a new sound to the synthesiser.
virtual void renderVoices(AudioBuffer< float > &outputAudio, int startSample, int numSamples)
Renders the voices for the given range.
virtual void handleMidiEvent(const MidiMessage &)
Can be overridden to do custom handling of incoming midi events.
void 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 pl...
virtual void handlePitchWheel(int midiChannel, int wheelValue)
Sends a pitch-wheel message to any active voices.
CriticalSection lock
This is used to control access to the rendering callback and the note trigger methods.
int lastPitchWheelValues[16]
The last pitch-wheel values for each midi channel.
virtual void noteOn(int midiChannel, int midiNoteNumber, float velocity)
Triggers a note-on event.
SynthesiserVoice * getVoice(int index) const
Returns one of the voices that have been added.
virtual void noteOff(int midiChannel, int midiNoteNumber, float velocity, bool allowTailOff)
Triggers a note-off event.
virtual void handleChannelPressure(int midiChannel, int channelPressureValue)
Sends a channel pressure message.
void clearVoices()
Deletes all voices.
virtual void setCurrentPlaybackSampleRate(double sampleRate)
Tells the synthesiser what the sample rate is for the audio it's being used to render.
void setMinimumRenderingSubdivisionSize(int numSamples, bool shouldBeStrict=false) noexcept
Sets a minimum limit on the size to which audio sub-blocks will be divided when rendering.
virtual void handleSustainPedal(int midiChannel, bool isDown)
Handles a sustain pedal event.
virtual void handleSostenutoPedal(int midiChannel, bool isDown)
Handles a sostenuto pedal event.
constexpr bool approximatelyEqual(Type a, Type b, Tolerance< Type > tolerance=Tolerance< Type >{} .withAbsolute(std::numeric_limits< Type >::min()) .withRelative(std::numeric_limits< Type >::epsilon()))
Returns true if the two floating-point numbers are approximately equal.
constexpr bool exactlyEqual(Type a, Type b)
Equivalent to operator==, but suppresses float-equality warnings.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
constexpr int numElementsInArray(Type(&)[N]) noexcept
Handy function for getting the number of elements in a simple const C array.