How to change valueToTextFunction?


#1

Is there any way to change the valueToTextFunction for a parameters added using createAndAddParameter?

That function pointer is a public member of AudioProcessorValueTreeState::Parameter, since it’s a struct, but the Parameter struct itself is a private definition within AudioProcessorValueTreeState, so I can’t cast the result of getParameter() to that type, and thus cannot change the function pointer.

We need to be able to provide a different set of text values depending upon the state of the Processor (specifically, the value of another parameter), but I cannot find any way to accomplish that when using the AudioProcessorValueTreeState. The callback itself does not provide an extra data pointer with which I could pass a pointer to the Processor, so I have no way to query instance data for that Processor instance in order to find out which set of data to display.

Any ideas?
Thanks!


#2

From https://docs.juce.com/master/tutorial_audio_processor_value_tree_state.html :

parameters.createAndAddParameter ("invertPhase", "Invert Phase", String(),
                                  NormalisableRange<float> (0.0f, 1.0f, 1.0f), 0.0f,
                                  [](float value)
                                  {
                                      // value to text function
                                      return value < 0.5 ? "Normal" : "Inverted";
                                  },
                                  [](const String& text)
                                  {
                                      // text to value function
                                      if (text == "Normal")    return 0.0f;
                                      if (text == "Inverted")  return 1.0f;
                                      return 0.0f;
                                  });

#3

Thanks, but I think you misunderstand the problem. I have three complete sets of names for the possible Keys, based on the current Scale selection. The Major and Minor scales each present a completely different set of Key names than the Chromatic (or other) scales. So the translation from the Key parameter’s value to its key name needs to be different, depending upon the Scale. For example, if the Scale is Major, the text (Key name) for the Key index 1 is “Db”, but it is “C#” if the Scale is Minor or Chromatic (or other). So I need a different callback, depending upon the Scale, which is another parameter that the Key parameter has no access to. If the Parameter struct were not a private member of the AudioProcessorValueTreeState class, then I could re-assign the Key parameter’s valueToTextFunction pointer when the Scale parameter changes. But it is, so I can’t. So I’m looking for another solution.


#4

You should be able to use the C++11 lambda using solution proposed by Im_Jimmi. Just capture the state for the lambda (likely the “this” pointer of your AudioProcessor) you need for determining the text you need to output.


#5

I don’t see how I can do that. The function definition only passes a float parameter. And I cannot capture 'this" in that code block (as evidenced by the error message that appears in Xcode if you reference ‘this’ in that code block).


#6

Do you know how the C++11 lambda syntax works? The captures (that are not part of the lambda’s function signature) go inside the [] like [this]. Im_Jimmi’s example was a bit misleading because it didn’t include that.


#7

Ah! I get you now. Thanks, that should work perfectly! And no, I am not very familiar with the C++11 lambda syntax. Dang yung’uns always tryin’ to teach us old dogs new tricks! :slight_smile: