I’m starting to use more ValueTree function to update the several components in a better way. As first component I refactored my thumbnail view. Basically it works but I realised a very high cpu load (over 200 % on XCode Debug Navigator) while I move the start / end marker. I’m not sure if I use the ValueTree / CachedValue technic in a prober way.
- Preparations
struct ThumbnailEditor : public Component, public juce::ValueTree::Listener
…
juce::CachedValue<double> startMarkerPosition;
juce::CachedValue<double> endMarkerPosition;
- Grab a piece of ValueTree I want to change / listen
void setSamplerSoundByValueTree(juce::ValueTree valueTree, int soundIndex)
{
if(!valueTree.isValid())
return;
if( valueTree.getProperty(te::IDs::type).toString() == "sampler")
{
int index = 0;
juce::Array<juce::ValueTree> sounds;
for(auto child : valueTree)
{
if( child.getType() == te::IDs::SOUND )
{
sounds.add(child);
}
index++;
}
samplerSoundValueTree = sounds[soundIndex];
}
samplerSoundValueTree.addListener(this);
startMarkerPosition.referTo (samplerSoundValueTree, te::IDs::startTime, nullptr);
endMarkerPosition.referTo (samplerSoundValueTree, te::IDs::length, nullptr);
}
ValueTree as XML
<SOUND source="Instruments/kick.ogg" name="kick" startTime="0.0" length="0.0"
keyNote="60" minNote="60" maxNote="60" gainDb="1.0" pan="0.0"/>
- Change the values through CachedValue
void mouseDrag (const MouseEvent& e) override
{
double positionInSeconds = jmax (0.0, xToTime ((float) e.x));
if (abs(positionInSeconds - startMarkerPosition) < abs(positionInSeconds - endMarkerPosition))
{
startMarkerPosition.setValue(positionInSeconds, nullptr);
}
else
{
if(positionInSeconds > audioFile->getLength())
endMarkerPosition.setValue(audioFile->getLength(), nullptr);
else
endMarkerPosition.setValue(positionInSeconds, nullptr);
}
}
- Listen for changes and update
void updateThumbnail()
{
float markerThickness = 2.0f;
startPositionMarker.setBounds ( timeToX(startMarkerPositionValue), 0, markerThickness, getHeight() );
endPositionMarker.setBounds ( timeToX(endMarkerPositionValue) - markerThickness, 0, markerThickness, getHeight() );
repaint();
}
void valueTreePropertyChanged (ValueTree& v, const Identifier& i) override
{
if (v == samplerSoundValueTree)
if (i == te::IDs::startTime || i == te::IDs::length)
updateThumbnail();
}
Maybe it isn’t a good idea to change the CachedValue
s with every mouse move. What’s the right way to have the benefits of ValueTree and good cpu loads?