Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Algorithms
  4. Convert a value in one number range to a value in another number range?

Convert a value in one number range to a value in another number range?

Scheduled Pinned Locked Moved Algorithms
csharpgraphicsgame-devhelpquestion
9 Posts 6 Posters 0 Views 1 Watching
  • 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.
  • T Offline
    T Offline
    Thrash505
    wrote on last edited by
    #1

    I can't seem to figure this out... I have a value within the range of 0 to 40 and I want to be able to convert that into a value in the range of 0 to 10,000. This is for a simple music player that I have built into one of my programs. I'm using C# and DirectX.AudioVideoPlayback to play mp3 files. I have a working custom slider control that has a range from 0 to 40. This will be used to adjust the volume of an mp3. The problem is that volume in DirectX.AudioVideoPlayback ranges from 0 to 10,000 (technically -10,000 to 0... 0 being max volume). Therefore I need to figure out a formula to convert a value from my custom slider control that is in the range 0 to 40, to a value DirectX.AudioVideoPlayback can use which is in the range of 0 to 10,000. Also these values are integers and the conversion doesn't have to be exact, just close enough. I'm bad at math, so any ideas?

    L N R 3 Replies Last reply
    0
    • T Thrash505

      I can't seem to figure this out... I have a value within the range of 0 to 40 and I want to be able to convert that into a value in the range of 0 to 10,000. This is for a simple music player that I have built into one of my programs. I'm using C# and DirectX.AudioVideoPlayback to play mp3 files. I have a working custom slider control that has a range from 0 to 40. This will be used to adjust the volume of an mp3. The problem is that volume in DirectX.AudioVideoPlayback ranges from 0 to 10,000 (technically -10,000 to 0... 0 being max volume). Therefore I need to figure out a formula to convert a value from my custom slider control that is in the range 0 to 40, to a value DirectX.AudioVideoPlayback can use which is in the range of 0 to 10,000. Also these values are integers and the conversion doesn't have to be exact, just close enough. I'm bad at math, so any ideas?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      did you consider multiplying by 250? :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Voting for dummies? No thanks. X|


      T 1 Reply Last reply
      0
      • L Luc Pattyn

        did you consider multiplying by 250? :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Voting for dummies? No thanks. X|


        T Offline
        T Offline
        Thrash505
        wrote on last edited by
        #3

        Nice, it works. So 10000 / 40 = 250... I knew it was something simple. Thanks a ton :)

        L 1 Reply Last reply
        0
        • T Thrash505

          Nice, it works. So 10000 / 40 = 250... I knew it was something simple. Thanks a ton :)

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          You're welcome. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          Voting for dummies? No thanks. X|


          1 Reply Last reply
          0
          • T Thrash505

            I can't seem to figure this out... I have a value within the range of 0 to 40 and I want to be able to convert that into a value in the range of 0 to 10,000. This is for a simple music player that I have built into one of my programs. I'm using C# and DirectX.AudioVideoPlayback to play mp3 files. I have a working custom slider control that has a range from 0 to 40. This will be used to adjust the volume of an mp3. The problem is that volume in DirectX.AudioVideoPlayback ranges from 0 to 10,000 (technically -10,000 to 0... 0 being max volume). Therefore I need to figure out a formula to convert a value from my custom slider control that is in the range 0 to 40, to a value DirectX.AudioVideoPlayback can use which is in the range of 0 to 10,000. Also these values are integers and the conversion doesn't have to be exact, just close enough. I'm bad at math, so any ideas?

            N Offline
            N Offline
            Nelek
            wrote on last edited by
            #5

            To that things you can always use the Rule of 3 (literal traduction from "Regla de 3" on spanish, don't know the correct name) RangeMax1 ----- RangeMax2 Value1 -------- Value2 To know the equivalent of one value into another different range. Lets say you have the RangeMax1, RangeMax2 and Value1 and you want to know the Value2. It is made with: Value2 = (RangeMax2 * Value1) / RangeMax1 if you want to find Value1 then: Value1 = (RangeMax1 * Value2) / RangeMax2 You multiply the 2 members of the fully known diagonal, and divide with the other number in the diagonal of the desired value. If you use the RangeMaxes and the Unit... you have the generic formule of your transformation or the coeficient you need. 40 ------ 10000 1 ------- x x = 10000 * 1 / 40; x = 250 EDIT: I forgot to say, that the range has to be in absolute width. I mean [-2, 18], translates into 0 ---- 20, you should afterwards adapt the calculated value. Let's say... I have the value -1,5 in the range [-5, 10] and want to know its equivalent in the range [5, 42] 10 - (-5) = 15 42 - 5 = 37 -1,5 - (-5) = 3,5 15 ------ 37 3,5 ----- x x = (37 * 3,5) / 15 = 8,633 8,633 + 5 = 13,633 the equivalent is 13,633

            Regards. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you “The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson Rating helpfull answers is nice, but saying thanks can be even nicer.

            F 1 Reply Last reply
            0
            • T Thrash505

              I can't seem to figure this out... I have a value within the range of 0 to 40 and I want to be able to convert that into a value in the range of 0 to 10,000. This is for a simple music player that I have built into one of my programs. I'm using C# and DirectX.AudioVideoPlayback to play mp3 files. I have a working custom slider control that has a range from 0 to 40. This will be used to adjust the volume of an mp3. The problem is that volume in DirectX.AudioVideoPlayback ranges from 0 to 10,000 (technically -10,000 to 0... 0 being max volume). Therefore I need to figure out a formula to convert a value from my custom slider control that is in the range 0 to 40, to a value DirectX.AudioVideoPlayback can use which is in the range of 0 to 10,000. Also these values are integers and the conversion doesn't have to be exact, just close enough. I'm bad at math, so any ideas?

              R Offline
              R Offline
              Roger Wright
              wrote on last edited by
              #6

              The multiplication by 250 suggestion is the easiest, but the human ear response isn't linear; it's logarithmic. A range of 0 to 40 dB corresponds to a range of 0 to 10,000 mW power level exactly, so I suggest that a conversion implementing the equation: DirectX.AudioVideoPlayback.Volume = 10^(MySlider.Volume/10) might be more useful.

              "A Journey of a Thousand Rest Stops Begins with a Single Movement"

              N CPalliniC 2 Replies Last reply
              0
              • R Roger Wright

                The multiplication by 250 suggestion is the easiest, but the human ear response isn't linear; it's logarithmic. A range of 0 to 40 dB corresponds to a range of 0 to 10,000 mW power level exactly, so I suggest that a conversion implementing the equation: DirectX.AudioVideoPlayback.Volume = 10^(MySlider.Volume/10) might be more useful.

                "A Journey of a Thousand Rest Stops Begins with a Single Movement"

                N Offline
                N Offline
                Nelek
                wrote on last edited by
                #7

                :omg: :doh: :doh: :doh: I totally forgot that it was Audio... you are totally right, the conversion must consider the change between dB and electrical units. I would give you a 5 if I was not in the firm. (I'll do it when I'm home)

                Regards. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you “The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson Rating helpfull answers is nice, but saying thanks can be even nicer.

                1 Reply Last reply
                0
                • R Roger Wright

                  The multiplication by 250 suggestion is the easiest, but the human ear response isn't linear; it's logarithmic. A range of 0 to 40 dB corresponds to a range of 0 to 10,000 mW power level exactly, so I suggest that a conversion implementing the equation: DirectX.AudioVideoPlayback.Volume = 10^(MySlider.Volume/10) might be more useful.

                  "A Journey of a Thousand Rest Stops Begins with a Single Movement"

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #8

                  Since the allowed range for the volume (according to OP) is -10,000 to 0, maybe the effect is already accounted by the DirectX function. I.e. maybe the function accepts sound level instead of volume. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  In testa che avete, signor di Ceprano?

                  1 Reply Last reply
                  0
                  • N Nelek

                    To that things you can always use the Rule of 3 (literal traduction from "Regla de 3" on spanish, don't know the correct name) RangeMax1 ----- RangeMax2 Value1 -------- Value2 To know the equivalent of one value into another different range. Lets say you have the RangeMax1, RangeMax2 and Value1 and you want to know the Value2. It is made with: Value2 = (RangeMax2 * Value1) / RangeMax1 if you want to find Value1 then: Value1 = (RangeMax1 * Value2) / RangeMax2 You multiply the 2 members of the fully known diagonal, and divide with the other number in the diagonal of the desired value. If you use the RangeMaxes and the Unit... you have the generic formule of your transformation or the coeficient you need. 40 ------ 10000 1 ------- x x = 10000 * 1 / 40; x = 250 EDIT: I forgot to say, that the range has to be in absolute width. I mean [-2, 18], translates into 0 ---- 20, you should afterwards adapt the calculated value. Let's say... I have the value -1,5 in the range [-5, 10] and want to know its equivalent in the range [5, 42] 10 - (-5) = 15 42 - 5 = 37 -1,5 - (-5) = 3,5 15 ------ 37 3,5 ----- x x = (37 * 3,5) / 15 = 8,633 8,633 + 5 = 13,633 the equivalent is 13,633

                    Regards. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you “The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson Rating helpfull answers is nice, but saying thanks can be even nicer.

                    F Offline
                    F Offline
                    feekejd
                    wrote on last edited by
                    #9

                    Hi, I've for a similar problem I have a Volume Control that only has values from 0 to 100 and i want to replace a slider control that ranges from -12 to 12??? any help please! J.Feeke:confused:

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups