Obtaining total number of active voices

I see, but how to get it “into” SynthVoice, same as into the editor?

The same way, make both the Synth and the SynthVoice take a State& as a constructor arg

Ok thank you very much!

1 Like

Ok I am sorry to have to bother you again, but as well as I consider myself pretty good at coming up with exciting DSP code, I am equally bad at C++ which I will address after this synth is done.

How exactly do I put my structure as a constructor argument?

In PluginProcessor.h public area I got;

// Note I tried moving this structure definition outside (above) the PluginProcessor Class definition.
struct TGParameters {   
        float test;
    };

TGParameters tgParamStruct;

And when voices in PluginProcessor.cpp are created;

mutineer.addVoice (new SynthVoice (tgParamStruct)); // FAIL

In SynthVoice.h I got;

#include "../JuceLibraryCode/JuceHeader.h"
#include "SynthSound.h"

struct TGParameters;

class SynthVoice : public SynthesiserVoice {
public:
	SynthVoice (TGParameters& tgVS) // Not sure if this is correct
	{
      ...

In PlugInEditor.cpp I got;

MutineerAudioProcessorEditor::MutineerAudioProcessorEditor (MutineerAudioProcessor& p)
	: AudioProcessorEditor (&p), processor (p)
{
    ....

Hi there, I had a similar problem and Ben’s answer was very useful. Some additional steps are necessary though, which I guess he assumed were obvious. The following pattern is what worked out for me:

struct State { };

struct Child 
{
    State& state;
    Child (State& s) : state (s) { }
};

struct Parent 
{
    State state;
    Child child;
    Parent() : child (state) { }
};

Thanks for trying to help, but unfortunately it only makes me more confused. Your “Child” and “Parent” names are they like in programming child/parent relations, or could they be anything, like “Apples” and “Oranges”?

Sorry, surely my terminology is off. I meant “parent” as the object which instantiates another, which would then be its “child” (that is, in terms of hierarchy and not inheritance). So I guess that your PluginProcessor would be the parent, your PluginEditor and your SynthVoice the children. But I am sure somebody else can explain this much better than I can.

Here is a more complete example. Both “parent” and “child” have access to the elements of “state”, but these elements are not global variables.

struct State
{
    int a = 0;
};

struct Child 
{
    State& state;
    
    Child (State& s) : state (s)
    { 
        state.a += 1;
    }
};

struct Parent 
{
    State state;
    Child child;

    Parent() : child (state)
    {
        state.a += 1;
    }
};

int main() 
{
    Parent parent;
    return parent.state.a;  // returns 2
}

Thanks for that, but my lack of understanding for my concrete issue, is due in big part to the in how exactly to pass the structure pointer via the constructor in Class in SynthVoice.h, and even more so where to properly and exactly put/define that “incoming” constructor parameter in my Class SynthVoice.h

So with my little understanding of this, I tried the following in SynthVoice.h private area;

TGParameters& state;

Then in top of SynthVoice.h;

#include "../JuceLibraryCode/JuceHeader.h"
#include "SynthSound.h"

struct TGParameters;

class SynthVoice : public SynthesiserVoice {
public: 
	SynthVoice (TGParameters& s) : state (s)
	{
    ...

The above shows no errors, but in PluginProcessor.cpp I can’t seem to figure out the correct way to pass the the structure;

mutineer.addVoice (new SynthVoice (tgParamStruct)); // Wrong!

What error message do you get?

“No instance of constructor “SynthVoice::SynthVoice” matches the argument list…”

Not quite sure if this will work in your case but try putting your entire struct

struct TGParameters { float test; };

dicrectly before (i.e. above) your SynthVoice and remove any other definiton or declaration of the struct.

Then instantiate the struct only once, within the AudioProcessor. For example:

TGParameters state;

Anywhere else, instantiate it as a reference, as you have done in SynthVoice already:

TGParameters& state;

Pass it from AudioProcessor into SynthVoice like so:

mutineer.addVoice (new SynthVoice (state));

SynthVoice takes it in as a reference in its constructor (and makes its own state object refer to the same state object that the AudioProcessor passed it):

SynthVoice (TGParameters& s) : state (s) { ...

You should then be able to access the variable test within both your SynthVoice and AudioProcessor using state.test

You should also be able to access it within your AudioProcessorEditor using processor.state.test

The reason is that you have already done something similar to the above in your AudioProcessorEditor that grants you access to the public properties of the AudioProcessor:

MutineerAudioProcessorEditor::MutineerAudioProcessorEditor (MutineerAudioProcessor& p) : AudioProcessorEditor (&p), processor (p)

Hope this helps!

(note: if you are a masochist, you can even assign the same reference different names in the different classes)

Thank you very much for your effort in trying to help me, however I still get the same error message at “mutineer.addVoice”. :frowning:

That might mean that the compiler doesn’t consider tgParamStruct to be of type TGParameters&.

Can you double check that it is not by accident TGParameters* instead or something?

Long shot: the SynthVoice constructor should be called from a non-const function, otherwise the type of tgParamStruct would be const TGParameters&.

1 Like

And just to be 100% sure: you are not declaring or defining the struct within your PluginProcessor but are doing so only before your SynthVoice, right?

It is of type `TGParameters&, thanks though.

This is what I got now;

PluginProcessor.h in public area;

struct TGParameters {
        float test;
    };

    TGParameters state;

In PluginProcessor.cpp’s constructor;

mutineer.addVoice (new SynthVoice (state));

In SynthVoice.h;

#include "../JuceLibraryCode/JuceHeader.h"
#include "SynthSound.h"

struct TGParameters { float test; };

class SynthVoice : public SynthesiserVoice {
public: 
	SynthVoice (TGParameters& s) : state (s)
	{
    ....

And in SynthVoice.h private area;

TGParameters& state;

You are defining the struct within your PluginProcessor. Don’t!

That confuse me, as it seems to mean I am unable to set or even access the struct variables within the PluginProcessor.

Perhaps point out in the above code, what is wrong to remove my confusion, thanks.