|
-
Jun 26th, 2012, 07:13 AM
#1
Thread Starter
Lively Member
-
Jun 26th, 2012, 11:34 AM
#2
Re: Logarithmic function for Audio Slider
This is just a remapping problem of a curve. You say you want to use a logarithm, so the first question is, since the base is what shapes the log curve, what base?
Whatever you decide, let's assign the base to the variable B, and call our logarithm to the base b of a number n LogB(n)
Since your slide ranges from 0-84 (let's replace 84 with Slider.MaxValue), and your desired output ranges from 0-1, we need to map create a log function such that f(0) = 0 and f(Slider.MaxValue) = 1.
Since LogB(n) = 0 when n = 1, and LogB(n) = 1 when n = B, this mapping turns out to be very easy: If we let Slider.Value equal the slider value, then n = 1 + (B-1)*Slider.Value/Slider.MaxValue.
Thus, our function is LogB(1 + (B-1)*Slider.Value/Slider.MaxValue).
Now, depending on what tools you are using to code, you may not be able to simply compute a logarithm to any random base. Therefore, you need to convert bases. This is also very easy, using this formula (remember that ln is the natural logarithm, lg2 is log to base 2 and lg10 is log to base 10):
LogB(n) = ln(n)/ln(B) = lg2(n)/lg2(B) = lg10(n)/lg10(B)
Thus, if your language has a natural log function ln(), to compute volume from your slider setting, use this formula:
Volume = ln(1 + (B-1)*Slider.Value/Slider.MaxValue) / ln(B)
So all you need to do now is choose a base. If you need assistance, I suggest you refer to whatever source told you that "The ideal volume slider follows an exponential curve, value between zero (silence) and 1.0 (maximum)." That source may have a good suggestion. Alternatively, you can graph this formula in excel or a matlab or something and change the B values to see how the different curves look and just choose one that looks right to you.
-
Jun 26th, 2012, 12:42 PM
#3
Re: Logarithmic function for Audio Slider
You can also easily plot such functions like this.
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
-
Jun 27th, 2012, 03:09 AM
#4
Thread Starter
Lively Member
Re: Logarithmic function for Audio Slider
Hi
and thanks to all for reply.
Your function is working good and I have solved my problem,
but I would like to know how you have derived the function.
Sorry, but I'm a little hard with algebra.
I'm trying to solve, with new values, using your explanation
Logarithm base=10
slide ranges from 0-84
desired output ranges from [-60 , 24]
f(0) = -60 and f(Slider.MaxValue) = 24
Log10(1E+24) = 24 , and Log10(6E-60) = -60
...but I can't figure out how to continue to find the correct function
Please, can you explain the logical steps for derive the function?
Thanks again for help
-
Jun 27th, 2012, 05:11 AM
#5
Re: Logarithmic function for Audio Slider
Lengries assumed originally that the argument to the logarithm is a linear function, that is, a function of the form
g(t) = a*t + b (for constants a and b; t corresponds to the slider's value)
Which constants a and b you need to use is the question, since the final answer will just be
log10 (a*Slider.Value + b)
The equations you've written give
log10 (a*0 + b) = log10 (b) = -60
=> b = 10^(-60)
log10 (a*Slider.MaxValue + b) = 24
=> a*Silder.MaxValue + b = 10^24
=> a = (10^24 - 10^(-60))/Slider.MaxValue
So, the answer you were looking for is
log10 ((10^24 - 10^(-60)) * Slider.Value/Slider.MaxValue + 10^(-60))
However, this is not as useful as you might expect because of limits on numerical precision. 10^24 - 10^(-60) requires more precision than standard floating point allows. This suggests that you weren't seeking a function like the above after all. An alternative is the following type of function, where the linearity is "outside" rather than "inside" the logarithm:
a * log10 (t+1) + b
Note that I have added one to t, to prevent the logarithm from giving -infinity when t=0. This is the reason Lengries chose the first way originally, to avoid arbitrary linear-ness. Oh well. Your conditions here become
a * log10 (1) + b = -60
=> b = -60
a * log10 (Slider.MaxValue + 1) + b = 24
=> a = 84 / log10 (Slider.MaxValue+1)
The resulting function looks like this. If neither of these alternatives is what you were after, please clarify the question by saying precisely what you mean by a logarithmic fit.
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
-
Jun 27th, 2012, 09:40 AM
#6
Thread Starter
Lively Member
Re: Logarithmic function for Audio Slider
Hi jemidiah
Thanks for your time and kidness.
I'll try to explain better.
The human perception of loudness is not linear, it is logarithmic.
The slider will be used to adjust the sound level intensity (volume).
Decibels(dB) is a measure of sound level intensity and is based
on a logarithmic scale.
output ranges from [-60 , 24] are dB
slide ranges from [0, 84] are pixel
Positive dB values mean amplification, negative values mean attenuation
dB = 20 * log10(ratio)
Minimal value for ratio for 16 bit sound is 1/32768
dB = 20 * log10(1/32768) = -90.3 dB
dB = 20 * Log10(1/1000)=-60dB
In the attached graph, the vertical axis shows the perceived
volume changes. As you can see, the corresponding section marked
by the curve at the ‘silent’ end is much larger than at the ‘loud’ end.
Thanks for help
-
Jun 27th, 2012, 10:11 AM
#7
Re: Logarithmic function for Audio Slider
Let's start at the basics: y = f(x)
In this case, x is the input of a function and y is the output. Whatever function f() may represent doesn't really matter, except to understand that it has a known behavior. For example, sin(), cos(), logB() (where B is the base of the logarithm), etc all have really familiar shapes.
Now what you wanted to do was take a particular segment of the log() function and map it to your ranges of input and output. So instead of y = f(x), you want y' = f'(x'). So what you really want to do is create a function x = g(x') and a function y' = h(y), such that y' = h(f(g(x')))
In your problem, g(x') and h(y) are both linear functions with two known points:
- for g(x'), the endpoints are (x', x): (Slider.Min, 1) and (Slider.Max, B).
- for h(x), the endpoints are (y, y'): (0, Volume.Min) and (1, Volume.Max).
Note that the endpoint values of y are 0 and 1 because the logB(1) = 0 and logB(B) = 1.
So first let's generalize g(x') and h(y). As stated before, these both happen to be linear functions, which makes this easy. Remember, the function of any non-vertical is y = ax + b, where a is the slope and b is the y-intercept. Given two points (x1, y1) and (x2, y2), the slope a = (y2-y1)/(x2-x1) and the y-intercept [b = y1 - x1(y2-y1)/(x2-x1)] OR [b = y2 - x2(y2-y1)/(x2-x1)]
So now let's derive g(x'): The slope a = (B-1)/(Slider.Max-Slider.Min) and the y-intercept b = 1 - Slider.Min*(B-1)/(Slider.Max-Slider.Min). Put them together and we get:
x = g(x') = x'*(B-1)/(Slider.Max-Slider.Min) + 1 - Slider.Min*(B-1)/(Slider.Max-Slider.Min)
= (x'*(B-1) - Slider.Min*(B-1))/(Slider.Max-Slider.Min) + 1
= ((x'-Slider.Min)*(B-1))/(Slider.Max-Slider.Min) + 1
Now h(y): The slope a = (Volume.Max - Volume.Min)/(1-0) = (Volume.Max - Volume.Min), and the y-intercept b = Volume.Min - 0(Volume.Max - Volume.Min)/(1-0) = Volume.Min - 0 = Volume.Min. Put them together and we get:
y' = h(y) = y * (Volume.Max - Volume.Min) + Volume.Min
Now we need to put all three functions together so that we get y' = h(f(g(x'))):
y' = logB(((x'-Slider.Min)*(B-1))/(Slider.Max-Slider.Min) + 1) * (Volume.Max - Volume.Min) + Volume.Min
And that's it. Now let's check our answer using the numbers from your OP, where Slider.Max = 84, Slider.Min = 0, Volume.Min = 0 and Volume.Max = 1, we get
y' = logB(((x'-0)*(B-1))/(84-0) + 1) * (1-0) + 0
= logB(((x')*(B-1))/(84) + 1) * (1)
= logB((x'*(B-1))/84 + 1) * 1
= logB(((B-1)*x')/84 + 1)
= logB(1 + (B-1)*x'/84)
Now let's replace x' with Slider.Value and y' with Volume and we get
Volume = logB(1 + (B-1)*Slider.Value/84)
The formula I posted in post #2 was
Volume = logB(1 + (B-1)*Slider.Value/Slider.MaxValue)
If we replace Slider.MaxValue with 84, we get:
Volume = logB(1 + (B-1)*Slider.Value/84)
And that confirms our answer. So for any Slider min, max, and value, and for any volume min and max value, your formula is:
Volume = logB(((Slider.Value-Slider.Min)*(B-1))/(Slider.Max-Slider.Min) + 1) * (Volume.Max - Volume.Min) + Volume.Min
And if you need to change logarithmic bases, use this:
Volume = log(((Slider.Value-Slider.Min)*(B-1))/(Slider.Max-Slider.Min) + 1)/log(B) * (Volume.Max - Volume.Min) + Volume.Min
-
Jun 28th, 2012, 06:14 AM
#8
Thread Starter
Lively Member
Re: Logarithmic function for Audio Slider
Hi Lenggries
Thank you all for the awesome explanations.
Thanks to all for help and suggestions.
My problem was solved
Thank you all for the time spent
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|