Array<float> ::set() method don't work


#1

At the end I choose a personal way of programming in C++ my VST Plugin instead of using ValueTree component class…
I have some arrays of float ( Array ) in a separate object called “Presets” (class “Presets.h”).
I have one constructor and some methods to initialize and retrieve arrays and their values but when I try to change an array parameter via preset.set() method, it doesn’t change…
Here is the code:

Preset Object:

class Presets
{
public:
    
    Presets()
    {
        configurations = { "CHORUS,DLY,REV", "PSHIFT,DELAY", "REVERB", "DELAY,REVERB", "CHORUS,DELAY", "PSHIFT,DELAY" };
  
        presetNames = { "01 crs Pan Verb", "02 liquid Shift", "03 crs Delay Verb" };
    }
    
    Array<float> getPresetsArray(int presetNumber)
    {
        switch (presetNumber)
        {
            case 0:
                return crsPanVerb;
                break;

            case 1:
                return liquidShift;
                break;
            case 2:
                return crsDelayVerb;
                break;
        }
    }
    
    String getPresetConfig(Array<float> arr)
    {
        return configurations[arr[0]];
    }

    void setParamToArray(Array<float> arr, int index, float value)
    {
        arr.set(index, value);
    }

    Array<String> configurations;
    
    Array<String> presetNames;
    
    // Preset Arrays
    
    Array<float> crsPanVerb = { 0.0, // Configuration
        0.0, 0.0, // Pitch LA
        0.0, 0.0, // Pitch RA
        0.0, 0.0, // Pitch LB
        0.0, 0.0, // Pitch RB
        500.0, 0.4, 0.15, 0.5, // Chorus
        0.0, 0.0, 0.0, 0.0, // Delay
        1.0, 0.18, 0.5, 0.5 }; // Reverb

    Array<float> liquidShift = { 1.0, // Configuration
        -0.7, 0.75, // Pitch LA
        0.5, 0.25, // Pitch RA
        -0.3, 0.75, // Pitch LB
        0.5, 0.25, // Pitch RB
        0.0, 0.0, 0.0, 0.0, // Chorus
        0.0, 0.0, 0.0, 0.0, // Delay
        0.0, 0.0, 0.0, 0.0 }; // Reverb
    
    Array<float> crsDelayVerb = { 0.0, // Configuration
        0.0, 0.0, // Pitch LA
        0.0, 0.0, // Pitch RA
        0.0, 0.0, // Pitch LB
        0.0, 0.0, // Pitch RB
        500.0, 0.6, 0.3, 0.5, // Chorus
        9261.0, 1.0, 9261.0, 0.5, // Delay
        1.0, 0.2, 0.8, 0.5 }; // Reverb
};

And here the code in the PluginProcessorEditor.cpp inside a SliderListener callback:

if (slider == &paramSlider && menuPage)
{
    if (selEffect == 0) // Pitch A
    {
        paramSlider.setRange(pitchA.minValues[selParam - 1], pitchA.maxValues[selParam - 1], 0.1);
        Array<float> presetArray = preset.getPresetsArray(selPreset);
        float paramVal = paramSlider.getValue();
        //preset.setParamToArray(presetArray, selParam, paramVal);
        presetArray.set(selParam, paramVal);
        float curParam = presetArray[selParam];
        lcdText.setText(pitchA.params[selParam - 1] + "    " + std::to_string(curParam), dontSendNotification);
    }

    if (selEffect == 1) // Pitch B
    {
        paramSlider.setRange(pitchB.minValues[selParam - 1], pitchB.maxValues[selParam - 1], 0.1);
        Array<float> presetArray = preset.getPresetsArray(selPreset);
        float paramVal = paramSlider.getValue();
        //preset.setParamToArray(presetArray, selParam, paramVal);
        presetArray.set(selParam, paramVal);
        float curParam = presetArray[selParam];
        lcdText.setText(pitchA.params[selParam - 1] + "    " + std::to_string(curParam), dontSendNotification);
    }
}

Am I wrong in declaring the arrays in the Preset object?


#2

I have only briefly looked at it, but

you copy-by-value the “arr” parameter in this case, so the original object won’t change, you may have to use a reference, to make it work

void setParamToArray(Array<float> &arr, int index, float value)


#3

It doesn’t work…
Do it need a listener/callback?


#4

Is It wrong the declaration of the Arrays?


#5

As @chkn said, the type declaration of setParamToArray is wrong.
You can think of what implicitly happens like this:

  • Copy the parameters array to a new temp array
  • Change the temp array
  • Throw the temp array away
  • At this point the original parameters array is unchanged because only a copy of it was modified.

In C, unlike other languages like Python, when one does a = b, then a becomes a copy of b rather than another reference to the same memory. Function arguments also behave like this. One can use pointers to refer to existing structures, or C++'s “references” where a = b behaves differently…


#6

Excuse me if I’m late…
I change the method:

 Array<float> getPresetsArray(int presetNumber)

in:

Array<float> &getPresetsArray(int presetNumber)

This functions but when I modify the array in the Editor, but it don’t changes the preset array…

I use:

preset.getPresetArray(0).set(index, value);

Any more suggestion?