I am trying to figure out how to correctly bind a ComboBox component to an AudioProcessorValueTreeState property.
From my understanding, ComboBox items’ indexes cannot start at 0, and it seems to be an issue when connecting it to an APVTS instance, using a ComboboxAttachement.
I put together a very small project demonstrating the issue, which can be seen here:
It is a very simple plugin with a Combobox containing only three options. it is bound to the APVTS with a Combobox attachment. The issues are two fold:
Option 2 can never be selected (it will automatically switch to option 3)
More concerning, it seems that something goes wrong internally with the plugin, because after closing and reopening any DAW (I am able to reproduce this with both Ableton Live 10 and Logic Pro X), trying to open a project containing the plugin, or adding a new instance of the plugin to a new project will cause the DAW to crash
Any help would be greatly appreciated I am developing on macOs 10.15, using Juce version 6.0.7.
Now, working backwards, you can see that you need a parameter in your APVT. That may look like what is below. Note that you can use the same function to “fill the range” (not sure how else to say this) this.
auto type = std::make_uniquejuce::AudioParameterChoice(
AudioPluginAudioProcessor::prmParameter1,
TRANS(“Filter Types”),
AudioPluginAudioProcessor::getFilterTypes(),
0,
TRANS(“”),
nullptr,
nullptr);
Now you can add your editor listener and attach the control to the parameter just like you would any other.
Thanks for the quick reply ! it sounds like what you suggested could work I can definitely give it a try!
However, i would like to understand why the code example i uploaded to github does not work as expected… this is the same kind of implementation I use for slider attachments, and have no issues with those
Please let me know if you have any thoughts on this I am very new to Juce, so I still have a lot to learn
You can use the values from the AudioParameterChoice using the getAllValueStrings() method
That method is available with all parameters btw.
You don’t need to set the selectedID if you added the options before the ComboBoxAttachment
The attachment will set that (it can’t, if the options are not yet present)
The reason why the zero cannot be used as ID is, because the ComboBox uses the PopupMenu, and there ID==0 is reserved as “cancelled”.
The crash might result from incompatibilities in getStateInformation / setStateInformation. Don’t you get a Stack trace when the crash happens?
And maybe check the result from juce::ValueTree::fromXml(*xmlState) if it is valid.
To add to something @daniel brings up here - IDs and indexes are two different numbering systems for ComboBox items, which can be confusing at first. When you call addItem on a ComboBox, you can assign the item any int ID you want (except zero, and it has to be unique), but the index is an automatically generated, zero-base count of the items.