Using custom methods inside of synth voice


#1

 

So, I've made a subclass of SynthesiserVoice which has some custom functions. I would like to be able to call these functions from outside the voice. 

1) I don't have access to the array within the Synthesiser which holds the voices. If I did, I could just loop through any voices that exist and call these functions. 

2) If i try to use the Synthesiser's getVoice(), I can call, say, stopNote(), but I can't call my own functions. 

Can anyone suggest how I should approach this problem? I am probably teetering on the edge of what I understand about OOP at this point, so "Google <jargon filled phrase>" could be a totally acceptable answer here ;). Or is there a better way to do what I'm trying to do within the framework that's already here?

thanks in advance

n

 

 

 


#2

You can use dynamic_cast:

if ( MyVoice* myVoice = dynamic_cast<MyVoice*> ( synthesizerObject.getVoice () ) )
    myVoice->myCustomFunction();

#3

If you have functionality that works separately to a particular class, then you shouldn't put it inside that class in the first place!


#4

If you need to use dynamic_cast then it probably points to a design flaw. smiley


#5

Well, a good analogy to what I'm trying to do here is the startNote() function. There are aspects of what the voice is doing that need to respond to actions other than MIDI events that happen outside of my voice, and it would be convenient to use a similar function that I can call from outside. So how would you imagine someone might otherwise do that within your framework? 

 

 

 


#6

thanks ckk -- I think I've got it all working now, thanks for the tip.


#7

hmm, well, I'm pretty sure for what I need to do the functionality belongs inside my synthesiser voice. and so as far as I can tell the only way other than using dynamic_cast would be for me to copy/paste/hack the synth code to make my own class. i'm green, but not green enough to think that's an ok way to reuse/extend Juce code :) 

Not to mention there's an example of dynamic_cast being used in the demo code within the synthesiserSound example, so I'll take that as implicit approval from Jules that this is an ok way of dealing with my problem.


#8

Yeah, I often hear people saying "dynamic_cast is always bad", but TBH I'm not sure where that meme originated, and I don't agree.

It's certainly not always bad - there are many situations where it's the best thing for the job. I think it should be treated as just another tool in your bag, and as long as you understand its implications, you should use it when appropriate.