04 August 2023, 10:19 | #1 |
son of 68k
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 ? |
04 August 2023, 10:35 | #2 |
Alien Bleed
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. |
04 August 2023, 10:45 | #3 |
son of 68k
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,348
|
|
04 August 2023, 10:49 | #4 |
Alien Bleed
Join Date: Aug 2022
Location: UK
Posts: 4,317
|
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. |
04 August 2023, 10:57 | #5 |
Alien Bleed
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. |
04 August 2023, 11:02 | #6 | ||
son of 68k
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,348
|
Quote:
The tough part is really to balance between left and right. Quote:
|
||
04 August 2023, 11:11 | #7 | |
Alien Bleed
Join Date: Aug 2022
Location: UK
Posts: 4,317
|
Quote:
*see: https://www.cuemath.com/geometry/angle-between-vectors/ Last edited by Karlos; 04 August 2023 at 11:45. |
|
04 August 2023, 11:18 | #8 |
Alien Bleed
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.
|
04 August 2023, 11:48 | #9 |
son of 68k
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. |
04 August 2023, 12:12 | #10 |
Alien Bleed
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. |
04 August 2023, 12:28 | #11 | |
son of 68k
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,348
|
Quote:
|
|
04 August 2023, 12:44 | #12 |
Registered User
Join Date: Dec 2014
Location: germany
Posts: 439
|
|
04 August 2023, 12:56 | #13 | |
Alien Bleed
Join Date: Aug 2022
Location: UK
Posts: 4,317
|
Quote:
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 |
|
04 August 2023, 13:13 | #14 |
son of 68k
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. |
04 August 2023, 13:52 | #15 | |
Alien Bleed
Join Date: Aug 2022
Location: UK
Posts: 4,317
|
Quote:
If you can simulate the phase shift (sample delay) component, that will definitely improve the directionality as it's an important perceptual cue. |
|
04 August 2023, 14:03 | #16 | ||
son of 68k
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,348
|
Quote:
Quote:
Only issue is to know how many of them should be played. |
||
04 August 2023, 14:34 | #17 | ||
Registered User
Join Date: Mar 2012
Location: Norfolk, UK
Posts: 1,156
|
Quote:
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:
|
||
04 August 2023, 15:24 | #18 | |
Alien Bleed
Join Date: Aug 2022
Location: UK
Posts: 4,317
|
Quote:
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. |
|
04 August 2023, 15:44 | #19 |
son of 68k
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,348
|
|
04 August 2023, 16:20 | #20 |
Alien Bleed
Join Date: Aug 2022
Location: UK
Posts: 4,317
|
|
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 |
|
|