# Voice stealing when the same note is pressed

**URL:** https://forum.juce.com/t/voice-stealing-when-the-same-note-is-pressed/30344
**Category:** General JUCE discussion
**Created:** [November 13, 2018, 4:59pm UTC](https://forum.juce.com/t/voice-stealing-when-the-same-note-is-pressed/30344 "2018-11-13T16:59:59Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![DaveH](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daveh/32/4924_2.png) [@DaveH](https://forum.juce.com/u/DaveH)
#### Post date: [November 13, 2018, 4:59pm UTC](https://forum.juce.com/t/voice-stealing-when-the-same-note-is-pressed/30344/1 "2018-11-13T16:59:59Z")

</div>

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:-

```auto
	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?  
Thanks.  
Dave

---

<div class="post-metadata">

### Author: ![alibarker](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@alibarker](https://forum.juce.com/u/alibarker)
#### Post date: [November 13, 2018, 5:17pm UTC](https://forum.juce.com/t/voice-stealing-when-the-same-note-is-pressed/30344/2 "2018-11-13T17:17:42Z")

</div>

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);
```

---

<div class="post-metadata">

### Author: ![DaveH](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daveh/32/4924_2.png) [@DaveH](https://forum.juce.com/u/DaveH)
#### Post date: [November 13, 2018, 6:31pm UTC](https://forum.juce.com/t/voice-stealing-when-the-same-note-is-pressed/30344/3 "2018-11-13T18:31:09Z")

</div>

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

---

<div class="post-metadata">

### Author: ![railjonrogut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/railjonrogut/32/505_2.png) [@railjonrogut](https://forum.juce.com/u/railjonrogut)
#### Post date: [November 14, 2018, 12:33am UTC](https://forum.juce.com/t/voice-stealing-when-the-same-note-is-pressed/30344/4 "2018-11-14T00:33:33Z")

</div>

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

Rail

---

<div class="post-metadata">

### Author: ![DaveH](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daveh/32/4924_2.png) [@DaveH](https://forum.juce.com/u/DaveH)
#### Post date: [February 26, 2019, 9:13am UTC](https://forum.juce.com/t/voice-stealing-when-the-same-note-is-pressed/30344/5 "2019-02-26T09:13:45Z")

</div>

Just as a catch-up, all I had to was override ‘isVoiceActive’ in the voice itself, and return true if the ADSR is still running.

---

<div class="post-metadata">

### Author: ![gerdb](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/gerdb/32/22544_2.png) [@gerdb](https://forum.juce.com/u/gerdb)
#### Post date: [September 28, 2026, 2:16pm UTC](https://forum.juce.com/t/voice-stealing-when-the-same-note-is-pressed/30344/6 "2026-09-28T14:16:49Z")

</div>

Thanks, that works really well.
