Home AMX User Forum NetLinx Studio

How do I round a float into an integer?

Hi. I'm going to control a Marantz receiver through RS232. It's volume varies from -80.0 dB to +18.0 dB. I have read in it's command specification that when I query it for the value of it's volume, it answers like: "ab.c dB" = abc or -abc. So -30.5dB would come out -305 ; 5.5 dB would come out 055 and so on.
I have decided to sum 800 to whichever values come in, and then divide the result for 9.8 to obtain the percentage of the volume, so that I can send it to a bargraph's level. But how do I round the value I get after the division into an integer value?

Thanks in advance

Comments

  • mpullinmpullin Posts: 949
    jp1016 wrote: »
    Hi. I'm going to control a Marantz receiver through RS232. It's volume varies from -80.0 dB to +18.0 dB. I have read in it's command specification that when I query it for the value of it's volume, it answers like: "ab.c dB" = abc or -abc. So -30.5dB would come out -305 ; 5.5 dB would come out 055 and so on.
    I have decided to sum 800 to whichever values come in, and then divide the result for 9.8 to obtain the percentage of the volume, so that I can send it to a bargraph's level. But how do I round the value I get after the division into an integer value?

    Thanks in advance
    So you have a number from -800 to 180 and you want to find a percentage. You are right to add 800, we now know the number will be from 0 to 980. Dividing by 9.8 will get you a number from 0 to 100. So far so good. You need to round the number to an even percentage if it's not?

    Look at (N % 1) with N being your number. This is the remainder when N is divided by 1.

    If (N % 1) >= 0.5, then subtract (N % 1) from N, and add 1, because we rounded up.
    If (N % 1) < 0.5, then just subtract (N % 1) from N and that's the answer.

    There you have it. I don't think there are any rounding functions in NetLinx but that procedure seems easy enough.
  • jjamesjjames Posts: 2,908
    Does this help?

    Part of an AXI floating around - I don't have the original axi - just copied the function I needed out of it - so I would give proper credit - but . . . . dunno who to give it to.
    (***********************************************************)
    (* FUNCTION:     ROUNDER                                   *)
    (* RETURN:       DOUBLE                                    *)
    (* PARAMETERS:   dVALUE - DOUBLE INPUT VALUE               *)
    (*               nDECIMALS - INTEGER AMOUNT OF DIGITS 0.X  *)
    (***********************************************************)   
    DEFINE_FUNCTION DOUBLE ROUNDER(DOUBLE dVALUE, INTEGER nDECIMALS)
    STACK_VAR
        INTEGER nJ
        DOUBLE nA
        CHAR strFORMAT[10]
    {
        nA = 1
        SWITCH (nDECIMALS)
        {
            CASE 0: nA = 1
            CASE 1: nA = 10
            DEFAULT:
            {
                FOR (nJ = 1; nJ <= nDECIMALS; nJ++)
                    nA = nA * 10
            }
        }
        RETURN ATOI(FORMAT('%-9.0f', (dVALUE * nA) + 0.5)) / nA
    }
    
  • jweatherjweather Posts: 320
    TYPE_CAST(float + 0.5)

    Casting a float to an integer truncates it (8.7 -> 8). Adding 0.5 beforehand means the truncation will round it correctly (8.7 -> 9.2 -> 9, 8.3 -> 8.8 -> 8)

    Jeremy
  • mpullinmpullin Posts: 949
    jweather wrote: »
    TYPE_CAST(float + 0.5)

    Casting a float to an integer truncates it (8.7 -> 8). Adding 0.5 beforehand means the truncation will round it correctly (8.7 -> 9.2 -> 9, 8.3 -> 8.8 -> 8)

    Jeremy

    Nice trick!
  • a_riot42a_riot42 Posts: 1,624
    jp1016 wrote: »
    I have decided to sum 800 to whichever values come in, and then divide the result for 9.8 to obtain the percentage of the volume, so that I can send it to a bargraph's level.

    The Denon preamps use a similar range with a similar format. I did this slightly differently where I just send the 2 digit value to the bargraph level, ignoring the third digit when there was one. So if the volume is 415 I just send 41. With a bargraph with 256 values 200 pixels across its almost impossible to move your finger with enough precision to be able to change from 41 to 415 for example. So I made the volume up/down buttons increment in half db steps when pressed (41 to 415 to 42 to 425 etc) and gave the bargraph a slightly coarser resolution. The maximum gain for the Denon changes depending on the source though, so its +18dB for an audio CD but may only be +2.5dB for a Blu-ray disc.
    Paul
  • GSLogicGSLogic Posts: 562
    I would like to know who decided that the user needs .05db control. Most people can't even hear 1db increments let alone .05db. Just another case of not asking people in the field.
    One other thing, I understand -80db to +10db are the real values and 0 is unity gain, but cut all the wanna be an engineer junk and just give me 0-100.
    I've owned recording studios for 20 years and produced/engineer many records (dual 2" analog tape machines, for those who know) and I've always said... you don't hear with your eyes.
  • ColzieColzie Posts: 470
    Here is a post with a sweet trick that somewhat applies to this thread

    http://amxforums.com/showthread.php?t=301

    You can have your TP level automatically display the percent of the bargraph - no calculations required! (assuming the bargraph min/max/current are correct....)
  • ericmedleyericmedley Posts: 4,177
    GSLogic wrote: »
    I would like to know who decided that the user needs .05db control. Most people can't even hear 1db increments let alone .05db. Just another case of not asking people in the field.
    One other thing, I understand -80db to +10db are the real values and 0 is unity gain, but cut all the wanna be an engineer junk and just give me 0-100.
    I've owned recording studios for 20 years and produced/engineer many records (dual 2" analog tape machines, for those who know) and I've always said... you don't hear with your eyes.
    Word! Me too.

    Plus, the volume knob on a receiver is not a true representation of the gain anyway.

    What 2" deck(s) did you have? I had a Studer A80. My very first tape machine was a 1" Scully 8 track. I wish I still had that. It sounded so good. I'm all Pro Tools with a mix buss nowadays. I still mix to analog however.

    Oops. sorry about the threadjack
  • GSLogicGSLogic Posts: 562
    ericmedley wrote: »
    What 2" deck(s) did you have? I had a Studer A80. My very first tape machine was a 1" Scully 8 track. I wish I still had that. It sounded so good. I'm all Pro Tools with a mix buss nowadays. I still mix to analog however.

    Oops. sorry about the threadjack

    I originally had a Stevens 40 track machine (Roy Thomas Baker ala:Queen/The Cars), sounded better than anything but kept needing repair, than I had a Studer A80, than 2 Otari MTR-90s. I was one of the original beta testers of Pro Tools back when it was Sound Designer. I ended up using a Pro Tools 48 track digital system with tons of vintage gear. I have four tube Telefunken V76 pre-amps from Abbey Road Studios, that is the stuff.
    Sorry to everyone for the threadjack!
Sign In or Register to comment.