How do I override a public member function of a juce voice?

118 Views Asked by At

Hi I am coding a synth in the Juce framework.

When my adsr is running I need to override isVoiceActive() and set it to true from inside the voice. This is a public member function of the SynthesiserVoice class.

virtual bool SynthesiserVoice::isVoiceActive  (       )   const   

Returns true if this voice is currently busy playing a sound.

By default this just checks the getCurrentlyPlayingNote() value, but can be overridden for more advanced checking.

So in the voice I have another member function virtual void renderNextBlock() and from inside it I want to override isVoiceActive

class SynthVoice : public SynthesiserVoice
{
public:
  void renderNextBlock (AudioBuffer <float> &outputBuffer, int startSample, int numSamples) override
    {
  for (int sample = startSample; sample < (startSample + numSamples); ++sample)
        {
            float env_value = adsr.getNextSample();
            if(env_value > 0)
               isVoiceActive = true; //???????
               ...
1

There are 1 best solutions below

0
On

you cannot do this because isVoiceActive is not an attribute it's a function, you can use your isVoiceActive as a data member and not as a function member so you can assign it true or false. or if you want to work with function then you have to add '&' so you can assign to that function a value (if you don't use '&' then you cannot do what you want to do. you have 2 choices:

1/ to use isVoiceActivat as a data member and you can do like this:

class SynthesiserVoice
{
public:
    bool isVoiceActive;
};
class SynthVoice :public SynthesiserVoice
{
public:
    oid renderNextBlock (AudioBuffer <float> &outputBuffer, int startSample, int numSamples) override
    {
  for (int sample = startSample; sample < (startSample + numSamples); ++sample)
        {
            float env_value = adsr.getNextSample();
            if(env_value > 0)
               isVoiceActive = true;
            }
     }
};

2/ to do the following:

class SynthesiserVoice
{
public:
    bool i = false;
    virtual bool& isVoiceActive() { return i; } // or virtual bool& isVoiceActive()=0;
};
class SynthVoice :public SynthesiserVoice
{
public:
    bool& isVoiceActive()override { return i; }
    void renderNextBlock (AudioBuffer <float> &outputBuffer, int startSample, int numSamples) override
    {
  for (int sample = startSample; sample < (startSample + numSamples); ++sample)
        {
            float env_value = adsr.getNextSample();
            if(env_value > 0)
               isVoiceActive ()= true;
             }
     }  
};

Now you have to implement this to suit your functions and data.

Hope it's clear and helpful