# findFreeVoice Hack

**URL:** <https://forum.juce.com/t/findfreevoice-hack/7579>\
**Category:** Audio Plugins\
**Created:** [October 17, 2011, 5:54pm UTC](https://forum.juce.com/t/findfreevoice-hack/7579 "2011-10-17T17:54:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hanley](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/hanley/32/3911_2.png) [@hanley](https://forum.juce.com/u/hanley)\
**Post date:** [October 17, 2011, 5:54pm UTC](https://forum.juce.com/t/findfreevoice-hack/7579/1 "2011-10-17T17:54:17Z")

</div>

I saw an older post briefly touch upon this, but it didn’t address my specific issue, so…

In my synth based off the Synth Demo code, I’m trying to customize the findFreeVoice function for purposes of note stealing, voice modes and unison. I tried bringing the findFreeVoice function into my Synth class, but it can’t access the private variables from SynthesiserVoice in this part of the code:

`if (voice->canPlaySound (soundToPlay)
				&& (oldest == nullptr || oldest->noteOnTime > voice->noteOnTime))
				oldest = voice;`

I tried adding:

```auto

```

as well as:

```auto

```

to my SynthVoice class but that didn’t fix it.

How can I get my synth subclass to be friends with SynthesiserVoice class?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [October 18, 2011, 8:16am UTC](https://forum.juce.com/t/findfreevoice-hack/7579/2 "2011-10-18T08:16:16Z")

</div>

Rather than trying to hack yourself access to those variables, you should just implement your own algorithm independently of the way the base class does it.

It’s always a bad idea to try to use internal variables like that anyway, because if in the future I change the way the base class is implemented, it could silently break your code. That’s why they’re kept private.

---

<div class="post-metadata">

**Author:** ![hanley](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/hanley/32/3911_2.png) [@hanley](https://forum.juce.com/u/hanley)\
**Post date:** [October 18, 2011, 2:33pm UTC](https://forum.juce.com/t/findfreevoice-hack/7579/3 "2011-10-18T14:33:52Z")

</div>

Makes sense.

I get what you’re saying, I’m just pretty hazy on how to do that. The relationship between my Synth class, it’s juce base, my SynthVoice class and it’s juce base, is a big mess in my head. Whenever I try to override functions from the Synthesiser and SynthesiserVoice class, I run into these private access problems. So then I add my own variables to my derived classes to use instead of the base class’s, but the overridden functions are still calling on the base class private variables, because they’re still using the “voice” object, which sometimes calls on SynthesiserVoice, and at other times SynthVoice. When I try to go even deeper and get “voice” to point to my SynthVoice functions/variables instead of SynthesiserVoice, I can get it to compile, but there’s no sound, because (as far as I can tell) somewhere there’s a disconnect between SynthesierVoice and SynthVoice.

When you say “implement your own algorithm” do you mean create my own functions instead of overriding the base class functions? (Sorry, my C++ understanding is growing, but not quite advanced to enough to fully understand this).

---

<div class="post-metadata">

**Author:** ![hanley](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/hanley/32/3911_2.png) [@hanley](https://forum.juce.com/u/hanley)\
**Post date:** [October 18, 2011, 3:15pm UTC](https://forum.juce.com/t/findfreevoice-hack/7579/4 "2011-10-18T15:15:20Z")

</div>

OK, I’ve boiled down my problem to one issue and one question.

When

`synth.addVoice` is called, it adds a SynthVoice object to

`OwnedArray <SynthesiserVoice> voices`.

That’s why, whenver a function or variable is called via

```auto

```

it calls on the SynthesiserVoice base class, instead of my derived SynthVoice class. The only time it jumps ship to the SynthVoice class is when the SynthVoice function is an overridden version of the base class’s virtual function. Sooooo…

How do I add my own function to SynthVoice and be able to call on it via the “voices” object without also adding it as a virtual function in the Synthesiser base class?

---

<div class="post-metadata">

**Author:** ![otristan](https://avatars.discourse-cdn.com/v4/letter/o/f0a364/32.png) [@otristan](https://forum.juce.com/u/otristan)\
**Post date:** [October 18, 2011, 3:20pm UTC](https://forum.juce.com/t/findfreevoice-hack/7579/5 "2011-10-18T15:20:15Z")

</div>

Look on google for dynamic\_cast

This is a C++ issue not a Juce one.

---

<div class="post-metadata">

**Author:** ![hanley](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/hanley/32/3911_2.png) [@hanley](https://forum.juce.com/u/hanley)\
**Post date:** [October 18, 2011, 3:59pm UTC](https://forum.juce.com/t/findfreevoice-hack/7579/6 "2011-10-18T15:59:38Z")

</div>

Ah. Sorry. Thanks.

---

<div class="post-metadata">

**Author:** ![jucemarler](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@jucemarler](https://forum.juce.com/u/jucemarler)\
**Post date:** [April 1, 2015, 4:08pm UTC](https://forum.juce.com/t/findfreevoice-hack/7579/7 "2015-04-01T16:08:00Z")

</div>

I too am running into a few design headache's / queries regarding custom voice classes derived from JUCE's synth voice class.&nbsp;

Most of my issues have been solved by creating the various components of my Synth as AudioProcessors and hooking them up into an AudioProcessorGraph.&nbsp;

So dealing with things like filter cutoff parameters and delay parameters is becoming much more flexible.&nbsp;

&nbsp;

However, I am still having the issue that my WaveTableOscillator class is being implemented&nbsp;as a member of my derived synth voice class. The various functions within the voice class for rendering the voice and noteOn&nbsp;call the oscillators output to fill the buffer and also in turn set it's frequency.&nbsp;

I need access to a few paramters of my oscillator within my derived synth voice (fine tune etc. for modulation and GUI param changes). As previouslty stated in this thread the getVoices() function returns&nbsp;a SynthVoice and the only way i can call my derived voices functions/access its member variables is to perform a dynamic cast.&nbsp;

I'm wondering whether dynamic casting in this instance really is the best / only option or not ? It's working but is it an ugly work around ? I'm working through the various Meyers texts and think im getting a reasonable understanding of the consequences of dynamic casts.&nbsp;

&nbsp;

Does anyone have any advice in regards to this ?&nbsp;

&nbsp;

Any help for a still "in learning" C++ programmer would be most appreciated.&nbsp;
