#30981 - ProblemBaby - Mon Dec 06, 2004 9:01 pm
Hi
How is the volume in left and right calculated?
if ive have a pan value between -64 and 64
if it for example is far left than left should be played at full volume and right not at all, right?
But if it is in middle what volume should left/right have??
#30988 - tepples - Mon Dec 06, 2004 10:12 pm
These formulas should work well enough on a handheld game:
Left: 100% left, 0% right
Middle: 70% left, 70% right
Right: 0% left, 100% right
The reason why middle isn't 50% involves an inverse square law, the fact that power is the square of amplitude.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#30992 - ProblemBaby - Mon Dec 06, 2004 10:30 pm
thanks for the reply
but ive to ask one more question:
if it is between middle and left should right still be 70% then?
so it just to do something like:
rightvol = 70;
leftvol = 70;
if (pan > 0)
finalrightvol = rightvol + pan;
else if (pan < 0)
finalleftvol = leftvol - pan;
#31007 - pyros - Tue Dec 07, 2004 12:21 am
I suspect you will need a less 'discreet' function to give smooth fading.
I think the equations you are looking for are something like:
pan=-64 is left
pan=0 is middle
pan=64 is right
then:
Code: |
leftvol = 100 * sqrt(128-(2*pan)) / 16
rightvol = 100 * sqrt(128+(2*pan)) / 16
|
I think this would ensure that the power is always a constant 100%.
Obviously you will need to change the '100' value to something more appropriate.
I've not coded any sound before, so someone correct me if i'm wrong.
#31069 - bertsnks - Tue Dec 07, 2004 4:23 pm
squareroot operation is very pricy on GBA I suspect - and calculating new volumes with XM is done almost on every 'tick' .
#31075 - tepples - Tue Dec 07, 2004 5:13 pm
bertsnks wrote: |
squareroot operation is very pricy on GBA I suspect |
Not with only 255 or fewer levels. In those cases, a square root can be done with a quick table lookup.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#31088 - pyros - Tue Dec 07, 2004 7:00 pm
uh, i was assuming that pan was an integer.
if that was the case then for pan=-64 to 64, there would be only 128 leftvol values and 128 rightvol values. so quite a small lookup table. for anyone concerned about space, then you could actually use the same table for both, as rightvol[pan+64] = leftvol[64-pan] :)
#31201 - ProblemBaby - Wed Dec 08, 2004 8:59 pm
It was a nice formula thanks!
I thought totally wrong when I wrote the other thing.
But i changed it a bit cause it will only create 32 + 1 levels
and i want 128 + 1.
so I did this
leftvol = 100 * sqrt(2048 - (32 * pan)) >> 6;
rightvol = 100 * sqrt(2048 + (32 * pan)) >> 6;
that must be correct, huh?
#31211 - pyros - Wed Dec 08, 2004 10:07 pm
I'm not too sure what you mean. However, if pan is between -64 and 64, then the right-hand-side of the equation should be correct.
However, what you will need to change is the value '100'. Because what you need to produce is actually:
Code: |
leftvol = sample * sqrt(128-(2*pan)) / 16
rightvol = sample * sqrt(128+(2*pan)) / 16
Which will give:
if pan = -64, then: leftvol = sample*1, rightvol = sample*0
if pan = 0, then: leftvol = sample*0.7, rightvol = sample*0.7
if pan = +64, then: leftvol = sample*0, rightvol = sample*1
obviously:
sample*1 = full volume
sample*0.7 = 'half power'
sample*0 = silent
|
For ANY value of (-64>pan>64):
leftvol^2 + rightvol^2 = sample^2
Thus the total sound from both speakers combined is always at 'full power'.
When you put this in a lookup-table, you will have to use a set value instead of sample, and then multiply(and shift-right) for each actual sample.