1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Refactored some AudioProcessor addParam methods to make sure the parameters always have a valid index set

This commit is contained in:
jules 2019-06-12 14:14:45 +01:00
parent 63e31a9fea
commit b6c111645a
2 changed files with 69 additions and 41 deletions

View file

@ -420,68 +420,92 @@ AudioProcessorListener* AudioProcessor::getListenerLocked (int index) const noex
void AudioProcessor::updateHostDisplay()
{
for (int i = listeners.size(); --i >= 0;)
if (auto* l = getListenerLocked (i))
if (auto l = getListenerLocked (i))
l->audioProcessorChanged (this);
}
const Array<AudioProcessorParameter*>& AudioProcessor::getParameters() const
#if JUCE_DEBUG
void AudioProcessor::checkDuplicateParamIDs()
{
if (flatParamListNeedsRebuilding)
{
flatParamListNeedsRebuilding = false;
flatParameterList = parameterTree.getParameters (true);
duplicateParamIDCheck.reset();
#ifdef JUCE_DEBUG
StringArray usedIDs;
usedIDs.ensureStorageAllocated (flatParameterList.size());
#endif
StringArray usedIDs;
usedIDs.ensureStorageAllocated (flatParameterList.size());
int index = 0;
for (auto& p : flatParameterList)
if (auto* withID = dynamic_cast<AudioProcessorParameterWithID*> (p))
usedIDs.add (withID->paramID);
for (auto& p : flatParameterList)
{
p->processor = const_cast<AudioProcessor*> (this);
p->parameterIndex = index++;
usedIDs.sort (false);
#ifdef JUCE_DEBUG
if (auto* withID = dynamic_cast<AudioProcessorParameterWithID*> (p))
usedIDs.add (withID->paramID);
#endif
}
#ifdef JUCE_DEBUG
usedIDs.sort (false);
// This assertion checks whether you attempted to add two or more parameters with the same ID
for (int i = 1; i < usedIDs.size(); ++i)
jassert (usedIDs[i - 1] != usedIDs[i]);
#endif
}
return flatParameterList;
// This assertion checks whether you attempted to add two or more parameters with the same ID
for (int i = 1; i < usedIDs.size(); ++i)
jassert (usedIDs[i - 1] != usedIDs[i]);
}
struct AudioProcessor::DuplicateParamIDCheck : private AsyncUpdater
{
DuplicateParamIDCheck (AudioProcessor& p) : owner (p) { triggerAsyncUpdate(); }
void handleAsyncUpdate() override { owner.checkDuplicateParamIDs(); }
AudioProcessor& owner;
};
#endif
void AudioProcessor::triggerDuplicateParamIDCheck()
{
#if JUCE_DEBUG
if (MessageManager::getInstanceWithoutCreating() != nullptr)
duplicateParamIDCheck = std::make_unique<DuplicateParamIDCheck> (*this);
#endif
}
const Array<AudioProcessorParameter*>& AudioProcessor::getParameters() const { return flatParameterList; }
const AudioProcessorParameterGroup& AudioProcessor::getParameterTree() const { return parameterTree; }
void AudioProcessor::addParameter (AudioProcessorParameter* param)
{
jassert (param != nullptr);
parameterTree.addChild (std::unique_ptr<AudioProcessorParameter> (param));
flatParamListNeedsRebuilding = true;
param->processor = this;
param->parameterIndex = flatParameterList.size();
flatParameterList.add (param);
triggerDuplicateParamIDCheck();
}
void AudioProcessor::addParameterGroup (std::unique_ptr<AudioProcessorParameterGroup> group)
{
parameterTree.addChild (std::move (group));
flatParamListNeedsRebuilding = true;
}
jassert (group != nullptr);
const AudioProcessorParameterGroup& AudioProcessor::getParameterTree() const
{
return parameterTree;
auto oldSize = flatParameterList.size();
flatParameterList.addArray (group->getParameters (true));
for (int i = oldSize; i < flatParameterList.size(); ++i)
{
auto p = flatParameterList.getUnchecked (i);
p->processor = this;
p->parameterIndex = i;
}
parameterTree.addChild (std::move (group));
triggerDuplicateParamIDCheck();
}
void AudioProcessor::setParameterTree (AudioProcessorParameterGroup&& newTree)
{
parameterTree = std::move (newTree);
flatParamListNeedsRebuilding = true;
flatParameterList = parameterTree.getParameters (true);
for (int i = 0; i < flatParameterList.size(); ++i)
{
auto p = flatParameterList.getUnchecked (i);
p->processor = this;
p->parameterIndex = i;
}
triggerDuplicateParamIDCheck();
}
void AudioProcessor::refreshParameterList() {}

View file

@ -1467,8 +1467,7 @@ private:
int cachedTotalIns = 0, cachedTotalOuts = 0;
AudioProcessorParameterGroup parameterTree;
mutable Array<AudioProcessorParameter*> flatParameterList;
mutable bool flatParamListNeedsRebuilding = true;
Array<AudioProcessorParameter*> flatParameterList;
AudioProcessorParameter* getParamChecked (int) const;
@ -1478,12 +1477,17 @@ private:
#endif
bool textRecursionCheck = false;
struct DuplicateParamIDCheck;
std::unique_ptr<DuplicateParamIDCheck> duplicateParamIDCheck;
void checkDuplicateParamIDs();
#endif
AudioProcessorListener* getListenerLocked (int) const noexcept;
void updateSpeakerFormatStrings();
void audioIOChanged (bool busNumberChanged, bool channelNumChanged);
void getNextBestLayout (const BusesLayout&, BusesLayout&) const;
void triggerDuplicateParamIDCheck();
template <typename floatType>
void processBypassed (AudioBuffer<floatType>&, MidiBuffer&);