How do I round a float into an integer?
jp1016
Posts: 32
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
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
0
Comments
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.
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.
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!
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
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.
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....)
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
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!