Forum

    • Register
    • Login
    • Search
    • Categories

    Math.ratio

    Feature Requests
    3
    5
    171
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • LightandSound
      LightandSound last edited by

      Simple one here, adding a math function that I end up using in most projects.

      Converting one variable in a given range, to the equivalent value in another range via:

      NewValue = (((OldValue - OldMin) * (NewMax - NewMin)) / (OldMax - OldMin)) + NewMin

      Easy to use my own function, but I'm sure this is one many would benefit from and likely already use.

      Final output would look something like:

      Math.rangeRatio(input, oldmin, oldmax, newmin, newmax)

      1 Reply Last reply Reply Quote 1
      • d.healey
        d.healey last edited by d.healey

        I think the math functions in HISE are just wrapper for the JUCE math library, so it would require a little more work to add a custom function (I might be wrong though).

        I also seem to recall with this function that if oldMax - oldMin == 0 the function returns the wrong result, so you have to handle that separately.

        This is from my KSP library

        //Maps a given value in a given range of values to that of another range of values
        taskfunc units.rescale(value, oldmin, oldmax, newmin, newmax) -> return
            
            declare local old_range
            declare local new_range
        
            old_range := oldmax - oldmin
        
            if (old_range = 0)
                return := newmin
            else
                new_range := newmax - newmin
        
                return := (((value - oldmin) * new_range) / old_range) + newmin        
            end if
        
        end taskfunc
        

        Libre Wave - Freedom respecting instruments and effects
        My Patreon - HISE tutorials
        YouTube Channel - Public HISE tutorials

        LightandSound 1 Reply Last reply Reply Quote 2
        • LightandSound
          LightandSound @d.healey last edited by

          @d-healey

          Yeah you do need to handle it if the old range is 0, which is a very, very fringe case though since you'd need to call the function intentionally, without having a previous range at all!

          ustk 1 Reply Last reply Reply Quote 1
          • ustk
            ustk @LightandSound last edited by

            @LightandSound @d-healey In what kind of situation would you have the oldRange equal to 0?
            Variable range going from negative to positive value?

            Tired to press F5 in the forum...
            Studio427 Audio - Audio Instruments & FX Plugins for music production. Website - Facebook

            LightandSound 1 Reply Last reply Reply Quote 0
            • LightandSound
              LightandSound @ustk last edited by

              @ustk that's why I say it's a fringe case, essentially if you set them as variables or for example use getvalue() without setting the min/max value of the panel then you could run in to problems. But there shouldn't be a 'real world use' for a range of 0 to a larger range, since all that does is spit out the same value. It's just to protect for the inevitable case where human error gets in the way.

              1 Reply Last reply Reply Quote 1
              • First post
                Last post

              12
              Online

              855
              Users

              5.7k
              Topics

              53.0k
              Posts