Decimal places in xml->setAttribute

Hi all,
I have an issue with losing decimal places when saving a preset with
xml->setAttribute

The parameters are all floats and have 7 decimal places in the editor but only 2 decimal places seem to be retrieved when the plugin is restored from being saved in the DAW.
Am I only ever hearing 2 decimal places of precision or are they not being stored and recalled correctly.
I think I want at least 3 decimal places!

The relevant parts of the code are below I think !

Sean

juce::AudioParameterFloat* mDelayOneTimeParameter;
    addParameter(mDelayOneTimeParameter = new juce::AudioParameterFloat("delay one delaytime", "Delay One Delay Time", 0.0001, MAX_DELAY_TIME, 0.001));
    addParameter(mDryGainParameter = new juce::AudioParameterFloat("drygain", "Dry Gain", 0.0, 1.0 , 0.5));
    addParameter(mDelayOneGainParameter = new juce::AudioParameterFloat("delayonegain", "Delay One Gain", 0.0, 1.0 , 0.5));
    addParameter(mDelayOneModDepthParameter = new juce::AudioParameterFloat("delayonemodDepth", "Delay One Mod Depth", 0, 1, 0.2));
    addParameter(mDelayOneModRateParameter = new juce::AudioParameterFloat("delayonemodRate", "Delay One Mod Rate", 0, 1, 0.15));
    addParameter(mDelayOneFeedbackParameter = new juce::AudioParameterFloat("feedbackone", "Feedback One", 0.0, 0.98, 0.0));

void Waylomod2020v4AudioProcessor::setStateInformation (const void* data, int sizeInBytes)
{
    // You should use this method to restore your parameters from this memory block,
    // whose contents will have been created by the getStateInformation() call.
    
    
    std::unique_ptr<juce::XmlElement> xml(getXmlFromBinary(data, sizeInBytes));
    
    if (xml.get() != nullptr && xml->hasTagName("waylomod2020"))
    {
        
        
        
        *mDryGainParameter = xml->getDoubleAttribute("drygain");
        //this->setGainSlider(xml->getDoubleAttribute("drygain"));
        
       
        *mDelayOneTimeParameter = xml->getDoubleAttribute("delayonetime");
void Waylomod2020v4AudioProcessor::getStateInformation (juce::MemoryBlock& destData)
{
    // You should use this method to store your parameters in the memory block.
    // You could do that either as raw data, or use the XML or ValueTree classes
    // as intermediaries to make it easy to save and load complex data.
    std::unique_ptr<juce::XmlElement> xml(new juce::XmlElement ("waylomod2020"));
    xml->setAttribute("drygain", *mDryGainParameter);
    xml->setAttribute("delayonetime", *mDelayOneTimeParameter);

talking to myself here. First sign of madness I suppose.
I tried this which didnt help

xml->setAttribute("drygain", double(*mDryGainParameter));

I thought casting to double might force some precision into the overridden method called. No dice

this didnt help either adding an f to the variable constants

addParameter(mDryGainParameter = new juce::AudioParameterFloat("drygain", "Dry Gain", 0.000f, 1.0f , 0.501f));

A little bit of an hack, but perhaps you can resort to using the String overload of setAttribute, and explicitly serialize your decimal number to string with the arbitrary number of digits you want, e.g.:

xml->setAttribute ("drygain", String (*mDryGainParameter, 4));

thanks so much yfede I thought it was a plausible plan.

I tried this

xml->setAttribute ("drygain", juce::String(*mDryGainParameter, 6));

I tried a couple of different DAWs cubase and reaper same problem
I also thought of doing divisions in the code by 100 so a value of .15 becomes .0015
Also hacky and weird.

Still battling with this one.

I tried this just calling the setAttribute and passing in a float but it was set to zero in cubase.

void Waylomod2020v4AudioProcessor::getStateInformation (juce::MemoryBlock& destData)
{

    std::unique_ptr<juce::XmlElement> xml(new juce::XmlElement ("waylomod2020"));
    //xml->setAttribute ("drygain", juce::String(*mDryGainParameter, 6));
    //xml->setAttribute("drygain", (*mDryGainParameter));
    xml->setAttribute("drydain", 0.12f);

Really tough one to solve!!

Tried this too

xml->setAttribute("drygain", (mDryGainParameter->get()));

Made some progress !

void Waylomod2020v4AudioProcessorEditor::setSliders(){
    
    auto& params = processor.getParameters();
    juce::AudioParameterFloat* dryGainParameter2 = (juce::AudioParameterFloat*)params.getUnchecked(1);
    mDryGainSlider.setValue(*dryGainParameter2);

this is where the issue is.
the xml is saving with the correct precision.

I tried saving a dry gain of 0.009999.
It’s still letting signal through.

The issue is to get the sliders to move to the correct positions after the plugin reloads.
I think that params.getUnchecked is what is removing the precision somehow.

Still battling

Not an expert about SliderAttachments, but maybe in this case the interval of the Slider that is connected to the parameter has a role in all of this. IIRC it is set as the last parameter of Slider::setRange(), which has a default value if left unspecified

1 Like