Home AMX User Forum AMXForums Archive Threads Tips and Tricks
Options

Volume Conversion

I am trying to convert a volume range from 0 to 255 to 0 to100. My goal is to display a volume percent in a text box next to a bargraph. The 0 to 255 works fine for the bargraph, but for the text box I am using ((LEVEL.VALUE * 100)/255). It works, but the problem is the decimal value resulted from the division. It throws off the volume feedback smooth ramping. Sometimes you have to press the button 3 times to move to the next percent value. For example 128*100/255 = 50.19. Any suggestions? Thanks.

Ricardo

Comments

  • Options
    NMarkRobertsNMarkRoberts Posts: 455
    I think you need to reconsider the range you wish to display and send. Every amp I ever owned acted like the heater in your father's first car. Everything happens just to the left of centre and it was impossible to adjust. You never want the extremes. So consider offering 25% to 50% or something like that - experiment to find out.

    You also need to consider how many volume steps you need. 25 or 30 seems to be plenty for a typical system, although you may be limited by the range mentioned above. And as you mentioned, you need 2 or 3 bumps on 0..255 to get 1 bump on 0..100.

    So 0..255 and 0..100 may not be what you want; maybe you need 25 levels which are hard-coded to send 25..49%, or 20 levels coded to send 30..69%

    Here are my general-purpose conversion functions:
    (******************************************************************************)
    define_function integer AdjustTo255    ( (* Output - Result in range 0 - 255  *)                                                             
       integer nArgValue                   , (* Input  - Value to convert         *)
       integer nArgMinimum                 , (* Input  - Minimum input value      *)
       integer nArgMaximum                 ) (* Input  - Maximum input value      *)
    (******************************************************************************)                 
    {
    return (((nArgValue - nArgMinimum) * 255) / (nArgMaximum - nArgMinimum));
    } (* AdjustTo255 *) 
    
    (******************************************************************************)
    define_function integer AdjustFrom255  ( (* Output - Result in range as below *)
       integer nArgValue                   , (* Input  - Value in range 0 - 255   *)
       integer nArgMinimum                 , (* Input  - Minimum input value      *)
       integer nArgMaximum                 ) (* Input  - Maximum input value      *)
    (******************************************************************************)                 
    {
    return ((nArgValue * (nArgMaximum - nArgMinimum)) / 255) + nArgMinimum;
    } (* AdjustFrom255 *)  
    

    You would call this function like so for the conversion you requested:
    nValue0to100 = AdjustFrom255(nValue0to255,0,100)
    

    To use my suggestions above, you would code like this:
    nValue25to49 = AdjustFrom255(nValue255,25,49)
    
  • Options
    Joe HebertJoe Hebert Posts: 2,159
    Sometimes you have to press the button 3 times to move to the next percent value.
    If you are converting 0-255 to 0-100 then that is correct. That?s how the math works out. To be exact, you have to press the button 2.55 times (on the average) to move to the next percent. The only way for the bargraph and percent text to move in unison is for both ranges to be same.
    My goal is to display a volume percent in a text box next to a bargraph.
    Just want to mention that you also have the option to you enter $P% as the text for the bargraph at design time and the percent will be written in the bargraph in real-time automatically. No math or unnecessary SEND_COMMAND traffic needed.
  • Options
    Thanks all. The $P% is a very nice trick.
  • Options
    pdabrowskipdabrowski Posts: 184
    Something I found myself, if the variable for the bargraph has a decimal value, ie 0-100 multimpled by 2.55 for conversion, and if the TP is disconnected from the AXLINK, after bootup. The bargraph never returned to display no matter what was done, the only way was to reboot the master... I found to get around that I declared the vaiable "vvol_level_result" as "float"
    DEFINE_VARIABLE
    vvol_level  (*0-100 level for volume control device, in this instance it was an Extron MLS406*)
    float vvol_level_result (*0-255 volume level mimic for the TP bargraph*)
    
    DEFINE_PROGRAM
    
    vvol_level_result = ((vvol_level)*2.55) 
      
    send_level vtp,1,(vvol_level_result)
    
  • Options
    DHawthorneDHawthorne Posts: 4,584
    If you want to do true rounding on such conversions, don't use integer math on your conversion function: create a temporary DOUBLE, assign the value to that, do your conversion, add .5 to the result, then TYPE_CAST it back to an integer. Especially with things like volume bars, it will make a smoother transition.
  • Options
    pdabrowskipdabrowski Posts: 184
    that's quite an interesting idea... i wasn't worried because the bargraph was actually on a CP4 so any stuttering while the bargraph changed wasn't a real issue.

    I'll keep that in mind though if ever I need to do similar on a larger display.
Sign In or Register to comment.