Voice stealing when the same note is pressed


Hi, I’m trying to emulate a hardware synth I own, and I noticed that it uses a different voice every time I press the same key.
Juce on the other hand wants to use same voice if I repeatedly press a key.
I’ve got around this a little hackerly, being new to the voice stuff:-

	bool canPlaySound(SynthesiserSound *sou)  override
		sound = dynamic_cast<SynthSound*>(sou);
		if (adsr1.getState() != ADSR::idle || sound == nullptr)
			return false;
		return true;

…Which basically says I can’t use this voice because the envelope is still being used.
But it throws an assert when it can’t play anything.
Is there a more ready to use method of selecting a different voice every key press?


How about overriding the Synthesiser::noteOn method so it does roughly the same thing except removes the following lines of code:

            // If hitting a note that's still ringing, stop it first (it could be
        // still playing because of the sustain or sostenuto pedal).
        for (auto* voice : voices)
            if (voice->getCurrentlyPlayingNote() == midiNoteNumber && voice->isPlayingChannel (midiChannel))
                stopVoice (voice, 1.0f, true);


Oh thanks, I guess I do need to look into the Juce synth code at some point anyhow.


Rather in your noteOn use findFreeVoice() to send it a voice as the first parameter to startVoice()