Loading...
Searching...
No Matches
juce_MPESynthesiser.cpp
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
MPESynthesiser::MPESynthesiser()
{
}
MPESynthesiser::MPESynthesiser (MPEInstrument& mpeInstrument)
: MPESynthesiserBase (mpeInstrument)
{
}
MPESynthesiser::~MPESynthesiser()
{
}
//==============================================================================
void MPESynthesiser::startVoice (MPESynthesiserVoice* voice, MPENote noteToStart)
{
jassert (voice != nullptr);
voice->currentlyPlayingNote = noteToStart;
voice->noteOnTime = lastNoteOnCounter++;
voice->noteStarted();
}
void MPESynthesiser::stopVoice (MPESynthesiserVoice* voice, MPENote noteToStop, bool allowTailOff)
{
jassert (voice != nullptr);
voice->currentlyPlayingNote = noteToStop;
voice->noteStopped (allowTailOff);
}
//==============================================================================
void MPESynthesiser::noteAdded (MPENote newNote)
{
const ScopedLock sl (voicesLock);
if (auto* voice = findFreeVoice (newNote, shouldStealVoices))
startVoice (voice, newNote);
}
void MPESynthesiser::notePressureChanged (MPENote changedNote)
{
const ScopedLock sl (voicesLock);
for (auto* voice : voices)
{
if (voice->isCurrentlyPlayingNote (changedNote))
{
voice->currentlyPlayingNote = changedNote;
voice->notePressureChanged();
}
}
}
void MPESynthesiser::notePitchbendChanged (MPENote changedNote)
{
const ScopedLock sl (voicesLock);
for (auto* voice : voices)
{
if (voice->isCurrentlyPlayingNote (changedNote))
{
voice->currentlyPlayingNote = changedNote;
voice->notePitchbendChanged();
}
}
}
void MPESynthesiser::noteTimbreChanged (MPENote changedNote)
{
const ScopedLock sl (voicesLock);
for (auto* voice : voices)
{
if (voice->isCurrentlyPlayingNote (changedNote))
{
voice->currentlyPlayingNote = changedNote;
voice->noteTimbreChanged();
}
}
}
void MPESynthesiser::noteKeyStateChanged (MPENote changedNote)
{
const ScopedLock sl (voicesLock);
for (auto* voice : voices)
{
if (voice->isCurrentlyPlayingNote (changedNote))
{
voice->currentlyPlayingNote = changedNote;
voice->noteKeyStateChanged();
}
}
}
void MPESynthesiser::noteReleased (MPENote finishedNote)
{
const ScopedLock sl (voicesLock);
for (auto i = voices.size(); --i >= 0;)
{
auto* voice = voices.getUnchecked (i);
if (voice->isCurrentlyPlayingNote (finishedNote))
stopVoice (voice, finishedNote, true);
}
}
void MPESynthesiser::setCurrentPlaybackSampleRate (const double newRate)
{
MPESynthesiserBase::setCurrentPlaybackSampleRate (newRate);
const ScopedLock sl (voicesLock);
turnOffAllVoices (false);
for (auto i = voices.size(); --i >= 0;)
voices.getUnchecked (i)->setCurrentSampleRate (newRate);
}
void MPESynthesiser::handleMidiEvent (const MidiMessage& m)
{
if (m.isController())
handleController (m.getChannel(), m.getControllerNumber(), m.getControllerValue());
else if (m.isProgramChange())
handleProgramChange (m.getChannel(), m.getProgramChangeNumber());
MPESynthesiserBase::handleMidiEvent (m);
}
MPESynthesiserVoice* MPESynthesiser::findFreeVoice (MPENote noteToFindVoiceFor, bool stealIfNoneAvailable) const
{
const ScopedLock sl (voicesLock);
for (auto* voice : voices)
{
if (! voice->isActive())
return voice;
}
if (stealIfNoneAvailable)
return findVoiceToSteal (noteToFindVoiceFor);
return nullptr;
}
MPESynthesiserVoice* MPESynthesiser::findVoiceToSteal (MPENote noteToStealVoiceFor) const
{
// This voice-stealing algorithm applies the following heuristics:
// - Re-use the oldest notes first
// - Protect the lowest & topmost notes, even if sustained, but not if they've been released.
// apparently you are trying to render audio without having any voices...
jassert (voices.size() > 0);
// These are the voices we want to protect (ie: only steal if unavoidable)
MPESynthesiserVoice* low = nullptr; // Lowest sounding note, might be sustained, but NOT in release phase
MPESynthesiserVoice* top = nullptr; // Highest sounding note, might be sustained, but NOT in release phase
// All major OSes use double-locking so this will be lock- and wait-free as long as stealLock is not
// contended. This is always the case if you do not call findVoiceToSteal on multiple threads at
// the same time.
const ScopedLock sl (stealLock);
// this is a list of voices we can steal, sorted by how long they've been running
usableVoicesToStealArray.clear();
for (auto* voice : voices)
{
jassert (voice->isActive()); // We wouldn't be here otherwise
usableVoicesToStealArray.add (voice);
// NB: Using a functor rather than a lambda here due to scare-stories about
// compilers generating code containing heap allocations..
struct Sorter
{
bool operator() (const MPESynthesiserVoice* a, const MPESynthesiserVoice* b) const noexcept { return a->noteOnTime < b->noteOnTime; }
};
std::sort (usableVoicesToStealArray.begin(), usableVoicesToStealArray.end(), Sorter());
if (! voice->isPlayingButReleased()) // Don't protect released notes
{
auto noteNumber = voice->getCurrentlyPlayingNote().initialNote;
if (low == nullptr || noteNumber < low->getCurrentlyPlayingNote().initialNote)
low = voice;
if (top == nullptr || noteNumber > top->getCurrentlyPlayingNote().initialNote)
top = voice;
}
}
// Eliminate pathological cases (ie: only 1 note playing): we always give precedence to the lowest note(s)
if (top == low)
top = nullptr;
// If we want to re-use the voice to trigger a new note,
// then The oldest note that's playing the same note number is ideal.
if (noteToStealVoiceFor.isValid())
for (auto* voice : usableVoicesToStealArray)
if (voice->getCurrentlyPlayingNote().initialNote == noteToStealVoiceFor.initialNote)
return voice;
// Oldest voice that has been released (no finger on it and not held by sustain pedal)
for (auto* voice : usableVoicesToStealArray)
if (voice != low && voice != top && voice->isPlayingButReleased())
return voice;
// Oldest voice that doesn't have a finger on it:
for (auto* voice : usableVoicesToStealArray)
if (voice != low && voice != top
&& voice->getCurrentlyPlayingNote().keyState != MPENote::keyDown
&& voice->getCurrentlyPlayingNote().keyState != MPENote::keyDownAndSustained)
return voice;
// Oldest voice that isn't protected
for (auto* voice : usableVoicesToStealArray)
if (voice != low && voice != top)
return voice;
// We've only got "protected" voices now: lowest note takes priority
jassert (low != nullptr);
// Duophonic synth: give priority to the bass note:
if (top != nullptr)
return top;
return low;
}
//==============================================================================
void MPESynthesiser::addVoice (MPESynthesiserVoice* const newVoice)
{
{
const ScopedLock sl (voicesLock);
newVoice->setCurrentSampleRate (getSampleRate());
voices.add (newVoice);
}
{
const ScopedLock sl (stealLock);
usableVoicesToStealArray.ensureStorageAllocated (voices.size() + 1);
}
}
void MPESynthesiser::clearVoices()
{
const ScopedLock sl (voicesLock);
voices.clear();
}
MPESynthesiserVoice* MPESynthesiser::getVoice (const int index) const
{
const ScopedLock sl (voicesLock);
return voices [index];
}
void MPESynthesiser::removeVoice (const int index)
{
const ScopedLock sl (voicesLock);
voices.remove (index);
}
void MPESynthesiser::reduceNumVoices (const int newNumVoices)
{
// we can't possibly get to a negative number of voices...
jassert (newNumVoices >= 0);
const ScopedLock sl (voicesLock);
while (voices.size() > newNumVoices)
{
if (auto* voice = findFreeVoice ({}, true))
voices.removeObject (voice);
else
voices.remove (0); // if there's no voice to steal, kill the oldest voice
}
}
void MPESynthesiser::turnOffAllVoices (bool allowTailOff)
{
{
const ScopedLock sl (voicesLock);
// first turn off all voices (it's more efficient to do this immediately
// rather than to go through the MPEInstrument for this).
for (auto* voice : voices)
{
voice->currentlyPlayingNote.noteOffVelocity = MPEValue::from7BitInt (64); // some reasonable number
voice->currentlyPlayingNote.keyState = MPENote::off;
voice->noteStopped (allowTailOff);
}
}
// finally make sure the MPE Instrument also doesn't have any notes anymore.
instrument.releaseAllNotes();
}
//==============================================================================
void MPESynthesiser::renderNextSubBlock (AudioBuffer<float>& buffer, int startSample, int numSamples)
{
const ScopedLock sl (voicesLock);
for (auto* voice : voices)
{
if (voice->isActive())
voice->renderNextBlock (buffer, startSample, numSamples);
}
}
void MPESynthesiser::renderNextSubBlock (AudioBuffer<double>& buffer, int startSample, int numSamples)
{
const ScopedLock sl (voicesLock);
for (auto* voice : voices)
{
if (voice->isActive())
voice->renderNextBlock (buffer, startSample, numSamples);
}
}
} // namespace juce
juce::AudioBuffer
Definition juce_AudioSampleBuffer.h:34
juce::MPEInstrument
Definition juce_MPEInstrument.h:54
juce::MPESynthesiserVoice
Definition juce_MPESynthesiserVoice.h:38
juce::MPESynthesiserVoice::setCurrentSampleRate
virtual void setCurrentSampleRate(double newRate)
Definition juce_MPESynthesiserVoice.h:152
juce::MPESynthesiserVoice::getCurrentlyPlayingNote
MPENote getCurrentlyPlayingNote() const noexcept
Definition juce_MPESynthesiserVoice.h:51
juce::MPESynthesiserVoice::noteStarted
virtual void noteStarted()=0
juce::MPESynthesiserVoice::noteOnTime
uint32 noteOnTime
Definition juce_MPESynthesiserVoice.h:162
juce::MPESynthesiserVoice::noteStopped
virtual void noteStopped(bool allowTailOff)=0
juce::MPESynthesiserVoice::isPlayingButReleased
bool isPlayingButReleased() const noexcept
Definition juce_MPESynthesiserVoice.cpp:40
juce::MPESynthesiser::removeVoice
void removeVoice(int index)
Definition juce_MPESynthesiser.cpp:287
juce::MPESynthesiser::reduceNumVoices
void reduceNumVoices(int newNumVoices)
Definition juce_MPESynthesiser.cpp:293
juce::MPESynthesiser::findFreeVoice
virtual MPESynthesiserVoice * findFreeVoice(MPENote noteToFindVoiceFor, bool stealIfNoneAvailable) const
Definition juce_MPESynthesiser.cpp:157
juce::MPESynthesiser::MPESynthesiser
MPESynthesiser()
Definition juce_MPESynthesiser.cpp:26
juce::MPESynthesiser::stopVoice
void stopVoice(MPESynthesiserVoice *voice, MPENote noteToStop, bool allowTailOff)
Definition juce_MPESynthesiser.cpp:49
juce::MPESynthesiser::setCurrentPlaybackSampleRate
void setCurrentPlaybackSampleRate(double newRate) override
Definition juce_MPESynthesiser.cpp:135
juce::MPESynthesiser::startVoice
void startVoice(MPESynthesiserVoice *voice, MPENote noteToStart)
Definition juce_MPESynthesiser.cpp:40
juce::MPESynthesiser::notePressureChanged
void notePressureChanged(MPENote changedNote) override
Definition juce_MPESynthesiser.cpp:66
juce::MPESynthesiser::noteReleased
void noteReleased(MPENote finishedNote) override
Definition juce_MPESynthesiser.cpp:122
juce::MPESynthesiser::clearVoices
void clearVoices()
Definition juce_MPESynthesiser.cpp:275
juce::MPESynthesiser::addVoice
void addVoice(MPESynthesiserVoice *newVoice)
Definition juce_MPESynthesiser.cpp:261
juce::MPESynthesiser::~MPESynthesiser
~MPESynthesiser() override
Definition juce_MPESynthesiser.cpp:35
juce::MPESynthesiser::findVoiceToSteal
virtual MPESynthesiserVoice * findVoiceToSteal(MPENote noteToStealVoiceFor=MPENote()) const
Definition juce_MPESynthesiser.cpp:173
juce::MPESynthesiser::noteTimbreChanged
void noteTimbreChanged(MPENote changedNote) override
Definition juce_MPESynthesiser.cpp:94
juce::MPESynthesiser::noteAdded
void noteAdded(MPENote newNote) override
Definition juce_MPESynthesiser.cpp:58
juce::MPESynthesiser::getVoice
MPESynthesiserVoice * getVoice(int index) const
Definition juce_MPESynthesiser.cpp:281
juce::MPESynthesiser::noteKeyStateChanged
void noteKeyStateChanged(MPENote changedNote) override
Definition juce_MPESynthesiser.cpp:108
juce::MPESynthesiser::turnOffAllVoices
virtual void turnOffAllVoices(bool allowTailOff)
Definition juce_MPESynthesiser.cpp:309
juce::MPESynthesiser::handleProgramChange
virtual void handleProgramChange(int, int)
Definition juce_MPESynthesiser.h:173
juce::MPESynthesiser::renderNextSubBlock
void renderNextSubBlock(AudioBuffer< float > &outputAudio, int startSample, int numSamples) override
Definition juce_MPESynthesiser.cpp:330
juce::MPESynthesiser::notePitchbendChanged
void notePitchbendChanged(MPENote changedNote) override
Definition juce_MPESynthesiser.cpp:80
juce::MPESynthesiser::handleMidiEvent
void handleMidiEvent(const MidiMessage &) override
Definition juce_MPESynthesiser.cpp:147
juce::MPESynthesiser::handleController
virtual void handleController(int, int, int)
Definition juce_MPESynthesiser.h:162
juce::MPEValue::from7BitInt
static MPEValue from7BitInt(int value) noexcept
Definition juce_MPEValue.cpp:30
juce::MidiMessage
Definition juce_MidiMessage.h:35
juce::MidiMessage::getChannel
int getChannel() const noexcept
Definition juce_MidiMessage.cpp:379
juce::MidiMessage::isProgramChange
bool isProgramChange() const noexcept
Definition juce_MidiMessage.cpp:524
juce::MidiMessage::isController
bool isController() const noexcept
Definition juce_MidiMessage.cpp:563
juce::MidiMessage::getControllerNumber
int getControllerNumber() const noexcept
Definition juce_MidiMessage.cpp:574
juce::MidiMessage::getProgramChangeNumber
int getProgramChangeNumber() const noexcept
Definition juce_MidiMessage.cpp:529
juce::MidiMessage::getControllerValue
int getControllerValue() const noexcept
Definition juce_MidiMessage.cpp:580
juce::MPENote
Definition juce_MPENote.h:40
juce::MPENote::keyDown
@ keyDown
Definition juce_MPENote.h:46
juce::MPENote::off
@ off
Definition juce_MPENote.h:45
juce::MPENote::keyDownAndSustained
@ keyDownAndSustained
Definition juce_MPENote.h:48
juce::MPENote::initialNote
uint8 initialNote
Definition juce_MPENote.h:108
juce::MPENote::keyState
KeyState keyState
Definition juce_MPENote.h:169
juce::MPENote::isValid
bool isValid() const noexcept
Definition juce_MPENote.cpp:62
juce::MPESynthesiserBase::handleMidiEvent
virtual void handleMidiEvent(const MidiMessage &)
Definition juce_MPESynthesiserBase.cpp:97
juce::MPESynthesiserBase::instrument
MPEInstrument & instrument
Definition juce_MPESynthesiserBase.h:202
juce::MPESynthesiserBase::setCurrentPlaybackSampleRate
virtual void setCurrentPlaybackSampleRate(double sampleRate)
Definition juce_MPESynthesiserBase.cpp:145
juce::MPESynthesiserBase::getSampleRate
double getSampleRate() const noexcept
Definition juce_MPESynthesiserBase.h:83
juce::MPESynthesiserBase::MPESynthesiserBase
MPESynthesiserBase()
Definition juce_MPESynthesiserBase.cpp:26