English Amiga Board


Go Back   English Amiga Board > Coders > Coders. General

 
 
Thread Tools
Old 04 August 2023, 10:19   #1
meynaf
son of 68k
 
meynaf's Avatar
 
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,348
sound spatialization in games

Hi,

I'm wondering if there are standard formulas to compute left and right volumes from sound position.
So we have x,y coordinates of the sound, relative to the player : what to do now ?
For example, simply setting volume=0 for "the other side" doesn't work. If distance=1 left and 10 in front of the player, both ears should hear it.
In addition, while simple stereo can't tell if a sound happens in front of the player or behind, perhaps the former should be a little louder than the latter.

I have routines to do this in my dmcsb project but it finally appears they don't fare very well.
I need a good, yet simple enough system to perform that computation.
Any ideas ?
meynaf is offline  
Old 04 August 2023, 10:35   #2
Karlos
Alien Bleed
 
Karlos's Avatar
 
Join Date: Aug 2022
Location: UK
Posts: 4,317
Attenuation and panning alone isn't enough for more than an approximation. Sound arrives at our ears delayed by varying amounts depending on the direction. For example, suppose you imagine your ears are 20cm apart and your sample is 22050Hz. Travelling at 330m/s in air, a sound that's directly to the right will arrive 0.2/330 = 0.000606 seconds later on the left. That doesn't sound like a lot but at 22050Hz that's a 13 sample phase difference.
The final thing is filtering, but that starts to get computationally expensive. As well arriving a bit later, sound which originates on one side of the head will be slightly filtered on the other.

I'd say basic panning and distance attenuation is going to be enough dor most use cases though.
Karlos is online now  
Old 04 August 2023, 10:45   #3
meynaf
son of 68k
 
meynaf's Avatar
 
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,348
Quote:
Originally Posted by Karlos View Post
I'd say basic panning and distance attenuation is going to be enough dor most use cases though.
Formulas for this ?
meynaf is offline  
Old 04 August 2023, 10:49   #4
Karlos
Alien Bleed
 
Karlos's Avatar
 
Join Date: Aug 2022
Location: UK
Posts: 4,317
Quote:
Originally Posted by meynaf View Post
Formulas for this ?
I had something. Let me find it.

Attention should be a basic inverse square relationship by distance, so that bit is simple. You can ignore the difference in the distance to your ears for that because it's not a strong enough effect.

Last edited by Karlos; 04 August 2023 at 10:55.
Karlos is online now  
Old 04 August 2023, 10:57   #5
Karlos
Alien Bleed
 
Karlos's Avatar
 
Join Date: Aug 2022
Location: UK
Posts: 4,317
Pan laws

https://github.com/0xABADCAFE/random...nal/PanLaw.php

The CentreMax one is probably what you need. It maintains a constant signal power contribution across the stereo field as you change the direction. Since it's got a normalised input for direction (-1/+1 left to right, 0 is centre) you could use something like a sine input based on direction angle.

Don't ask why it's in php...

Last edited by Karlos; 04 August 2023 at 11:11.
Karlos is online now  
Old 04 August 2023, 11:02   #6
meynaf
son of 68k
 
meynaf's Avatar
 
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,348
Quote:
Originally Posted by Karlos View Post
Attention should be a basic inverse square relationship by distance, so that bit is simple. You can ignore the difference in the distance to your ears for that because it's not a strong enough effect.
Overall volume is very easy. As the strength of a signal decreases with the square of the distance, just keep the distance squared and you're done.
The tough part is really to balance between left and right.



Quote:
Originally Posted by Karlos View Post
Pan laws

https://github.com/0xABADCAFE/random...nal/PanLaw.php

Don't ask why it's in php...
Alas this takes pan value as input, which is precisely the value i don't have.
meynaf is offline  
Old 04 August 2023, 11:11   #7
Karlos
Alien Bleed
 
Karlos's Avatar
 
Join Date: Aug 2022
Location: UK
Posts: 4,317
Quote:
Originally Posted by meynaf View Post
Alas this takes pan value as input, which is precisely the value i don't have.
See the edit. Compute the angle of the sound source relative to the observer forwards vector*. Use the sine of that angle. It will be 0 when facing directly towards or away from the source, 1 and -1 when perpendicular to it on one side or the other.

*see: https://www.cuemath.com/geometry/angle-between-vectors/

Last edited by Karlos; 04 August 2023 at 11:45.
Karlos is online now  
Old 04 August 2023, 11:18   #8
Karlos
Alien Bleed
 
Karlos's Avatar
 
Join Date: Aug 2022
Location: UK
Posts: 4,317
Finally, turn the whole angle to LR attenuation coefficients into a table. You only need about 32 entries for a full circle because human sound direction perception sucks.
Karlos is online now  
Old 04 August 2023, 11:48   #9
meynaf
son of 68k
 
meynaf's Avatar
 
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,348
I am afraid this won't work.

From this method you're suggesting, compute volume at some distance right in the front of the player : no attenuation - at a level of 32 (half max), it will give 32:32. Next do the same, with equal distance, but at the left of the player : other side channel ends up muted - result is 32:0.
Means overall perceived volume is much quieter (= halved) while the distance is exactly the same !

In addition, a new, interesting problem comes when the sound happens at the very same cell the player is located. Usually you will want to play the sound at maximum volume on both sides. But now, move just one cell to the side : bam! volume halved due the other side is now mute.
64:64 same cell, 64:0 one cell left ? Not good.
32:32 same cell, 64:0 one cell left ? Not good.
IOW, transition from same cell to nearby side cells is too abrupt.
meynaf is offline  
Old 04 August 2023, 12:12   #10
Karlos
Alien Bleed
 
Karlos's Avatar
 
Join Date: Aug 2022
Location: UK
Posts: 4,317
There are other PanLaws you can use. The simplest is just a linear, where at an input of -1 you have full intensity on the left, zero on the right, for an input of 0 you have half intensity left and right, and at an input of 1 you have full intensity on the right (note my original use case is for sound synthesis which is a different problem domain slightly).

See also: https://film-mixing.com/2015/08/22/u...d-dolby-atmos/

The constant power panning is probably the best for your use case.
Karlos is online now  
Old 04 August 2023, 12:28   #11
meynaf
son of 68k
 
meynaf's Avatar
 
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,348
Quote:
Originally Posted by Karlos View Post
There are other PanLaws you can use. The simplest is just a linear, where at an input of -1 you have full intensity on the left, zero on the right, for an input of 0 you have half intensity left and right, and at an input of 1 you have full intensity on the right (note my original use case is for sound synthesis which is a different problem domain slightly).

See also: https://film-mixing.com/2015/08/22/u...d-dolby-atmos/

The constant power panning is probably the best for your use case.
So, what should the center, +1 left, +1 right, +1 face volume pairs be then ?
meynaf is offline  
Old 04 August 2023, 12:44   #12
chb
Registered User
 
Join Date: Dec 2014
Location: germany
Posts: 439
Quote:
Originally Posted by meynaf View Post
So, what should the center, +1 left, +1 right, +1 face volume pairs be then ?
Constant power paning:

center: 45:45 (64/sqrt(2))
+1 left: 64:0
+1 right: 0:64
chb is offline  
Old 04 August 2023, 12:56   #13
Karlos
Alien Bleed
 
Karlos's Avatar
 
Join Date: Aug 2022
Location: UK
Posts: 4,317
Quote:
Originally Posted by meynaf View Post
So, what should the center, +1 left, +1 right, +1 face volume pairs be then ?
Constant Power is based on RMS intensity. The L/R attenuation pairs are thus
For extreme left: L = 1.0, R = 0.0
For centre: L = R = 0.707 (e.g. 1/sqrt(2))
For extreme right: L = 0.0, R = 1.0

https://mixingsound.files.wordpress....ethods-002.png

The constant power curve for the left channel follows the rising edge of sin(), the right channel follows cos().

You just need to convert your notion of angle into an appropriate input. My code used -1 to +1 because the input was a "voltage signal", but you can use whatever makes the most sense based on how you determine the angle. The key point is that when the angle is "maximally to the left" that your input into sin and cosine is basically 0, and when the angle is "maximally to the right" that your input into sine and cosine is pi/2
Karlos is online now  
Old 04 August 2023, 13:13   #14
meynaf
son of 68k
 
meynaf's Avatar
 
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,348
And therefore the volume in one of your ears will be lower in the case of same cell than with one-cell at the side.
Not to mention the sudden drop in the level.
meynaf is offline  
Old 04 August 2023, 13:52   #15
Karlos
Alien Bleed
 
Karlos's Avatar
 
Join Date: Aug 2022
Location: UK
Posts: 4,317
Quote:
Originally Posted by meynaf View Post
And therefore the volume in one of your ears will be lower in the case of same cell than with one-cell at the side.
Not to mention the sudden drop in the level.
In the real world, sounds are never fully panned. To account for this, you can scale and bias the output of your pan law so that it never reaches zero. I'd probably do this empirically with headphones on.

If you can simulate the phase shift (sample delay) component, that will definitely improve the directionality as it's an important perceptual cue.
Karlos is online now  
Old 04 August 2023, 14:03   #16
meynaf
son of 68k
 
meynaf's Avatar
 
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,348
Quote:
Originally Posted by Karlos View Post
In the real world, sounds are never fully panned. To account for this, you can scale and bias the output of your pan law so that it never reaches zero. I'd probably do this empirically with headphones on.
I did it empirically before, but players said the stereo separation was too low.


Quote:
Originally Posted by Karlos View Post
If you can simulate the phase shift (sample delay) component, that will definitely improve the directionality as it's an important perceptual cue.
Playing empty samples before the normal sound is no big deal.
Only issue is to know how many of them should be played.
meynaf is offline  
Old 04 August 2023, 14:34   #17
robinsonb5
Registered User
 
Join Date: Mar 2012
Location: Norfolk, UK
Posts: 1,156
Quote:
Originally Posted by meynaf View Post
I did it empirically before, but players said the stereo separation was too low.
Perhaps those players were using speakers instead of headphones?

Personally I find 100% separation quite uncomfortable when using headphones, but it doesn't take very much "leakage" to the other channel to make it sound more natural - so assuming you don't want to implement separate "speakers" and "headphones" modes, I'd just tune it with the quiet channel being *just* as loud as it needs to be to avoid it feeling unnatural with headphones.

Note also that if you're hearing the sound mostly in one ear and almost not at all in the other, it implies that what you're hearing is very quiet and very close to you - like an insect buzzing around your head. Louder sounds from further away will have a more similar volume in the two ears, even if the overall volume (after being attenuated by distance) isn't much higher.

Quote:
Playing empty samples before the normal sound is no big deal.
Only issue is to know how many of them should be played.
Karlos posted a formula for this earlier in the thread - obviously it depends on the sample rate, but at 22,050Hz he suggested a maximum of 13 samples for a sound source in line with both ears. Again you could do a simplistic linear interpolation from one side to the other - or you could calculate the physical distance from the position of the sound source in the virtual world to each ear of the player, subtract one from the other and use that difference as a basis.
robinsonb5 is offline  
Old 04 August 2023, 15:24   #18
Karlos
Alien Bleed
 
Karlos's Avatar
 
Join Date: Aug 2022
Location: UK
Posts: 4,317
Quote:
Originally Posted by robinsonb5 View Post
Karlos posted a formula for this earlier in the thread - obviously it depends on the sample rate, but at 22,050Hz he suggested a maximum of 13 samples for a sound source in line with both ears. Again you could do a simplistic linear interpolation from one side to the other - or you could calculate the physical distance from the position of the sound source in the virtual world to each ear of the player, subtract one from the other and use that difference as a basis.
Delay in samples = Sample Frequency (Hz) * Distance between ears (m) / Speed of Sound (m/s).

0.2m is probably a reasonable value for the distance between ears (if you look into some psychoacoustics you'll probably find a better value) and the speed of sound in air is around 330m/s typically.

As for the maximum pan separation, just make that configurable by the user. If you are going to turn it into a lookup table anyway, there's no reason not to allow these parameters to be tuned. Humans are notoriously bad at sound direction perception so the table doesn't need to be that large. A table based on 5 degree increments is probably more than good enough. Real world, it depends on sound frequency but I'm pretty sure a directional resolution of 1 degree is about the upper limit for sounds in front of the observer.

Last edited by Karlos; 04 August 2023 at 15:30.
Karlos is online now  
Old 04 August 2023, 15:44   #19
meynaf
son of 68k
 
meynaf's Avatar
 
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,348
Quote:
Originally Posted by Karlos View Post
Delay in samples = Sample Frequency (Hz) * Distance between ears (m) / Speed of Sound (m/s).
That's for when the sound is at the side. I need it for every possible position.
meynaf is offline  
Old 04 August 2023, 16:20   #20
Karlos
Alien Bleed
 
Karlos's Avatar
 
Join Date: Aug 2022
Location: UK
Posts: 4,317
Quote:
Originally Posted by meynaf View Post
That's for when the sound is at the side. I need it for every possible position.
Linear interpolation based on direction angle is more than sufficient.
Karlos is online now  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Amiga sound cutting out on games pauliekins support.WinUAE 2 28 March 2016 04:24
buggy sound on some DOS games clauddio Retrogaming General Discussion 3 29 September 2012 19:07
Sound lags while recording on certain games bLAZER support.WinUAE 46 05 December 2010 12:51
sound prob on some games using 1.5.3, okay on 1.2 kirk support.WinUAE 5 16 April 2009 07:51
Film Sound FX into Games Dastardly Nostalgia & memories 8 23 June 2004 01:51

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 17:38.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.10354 seconds with 15 queries