Sound with different gain depending on distance/Doppler Effect [sound_distance]

Post Reply
User avatar
Cosmos
New member
Posts: 9
Joined: Fri May 03, 2019 18:57
GitHub: CosmosScripter
In-game: Cosmos

Sound with different gain depending on distance/Doppler Effect [sound_distance]

by Cosmos » Post

Doppler Effect mod

This is a pretty simple mod, all it does is add a function to play a sound, the sound played has a different gain depending on the distance between the sound origin and the player. The sound is lower for players who are further, and higher for players who are closer.

Moving sound sources also have a pitch change while moving towards the player. If that moving sound emitter is above sound speed, the sound will be delayed. If at exact sound speed, you may see a sonic boom.

In real life, people are able to tell if a sound is getting closer or further, this is due to the Doppler Effect. The goal of this mod is to mimic this and to be used by developers.

Function syntax: sound_distance_play(pos, sound, sound_gain, distance, moving, yaw, vel, force_sound_speed)
"pos" is the sound origin position
"sound" is the sound file
"sound_gain" is the gain
"distance" is the max hear distance
"moving" is either true or false, true if the sound source is moving
"yaw" is used for moving sound sources, it can be something like "self.object:get_yaw()"
"vel" is used for moving sound sources, "self.object:get_velocity()" is recommended as a value
"force_sound_speed" is either true or false, if true, object velocity will be ignored and sound barrier breaking effects will be applied even to slow objects

Special thanks to TenPlus1, as the code to check the distance between the sound origin and the player was taken from mobs redo!

The code made by me is licensed under LGPL-2.1
The function which checks player distance (from mobs redo) is licensed under The MIT License

The sonic boom texture is licensed under CC-BY-SA-4.0

Only depends on default and tnt, both included in Minetest Game

For Minetest 5.1.1+
Download and browse code: https://github.com/CosmosScripter/sound_distance

Changelog:

Code: Select all

1-Added pitch changing (for now its just some kind of simple code for moving sound sources)
2-Added sound barrier breaking
Last edited by Cosmos on Wed Jul 28, 2021 23:21, edited 3 times in total.

Astrobe
Member
Posts: 570
Joined: Sun Apr 01, 2018 10:46

Re: Sound with different gain depending on distance/Doppler Effect [sound_distance]

by Astrobe » Post

Just for the sake of correctness, the Doppler effect affects the frequency of the sound, due to the movement of the emitter (actually the relative speed of the source and the receiver), so one should affect the pitch, not the gain, and compute the relative speed of the source and the player.

However, this means one would have to perform s x p computations (with s the number of sources and p the number of players). An acceptable approximation, though, is to consider that the sources don't move or are so slow that it doesn't matters, so using the velocity of players relative to the source should give good results (I think one could use one of those "weird" vector operations for that, the dot product or the scalar product).

User avatar
Cosmos
New member
Posts: 9
Joined: Fri May 03, 2019 18:57
GitHub: CosmosScripter
In-game: Cosmos

Re: Sound with different gain depending on distance/Doppler Effect [sound_distance]

by Cosmos » Post

Astrobe wrote:
Sat Jul 24, 2021 06:55
Just for the sake of correctness, the Doppler effect affects the frequency of the sound, due to the movement of the emitter (actually the relative speed of the source and the receiver), so one should affect the pitch, not the gain, and compute the relative speed of the source and the player.

However, this means one would have to perform s x p computations (with s the number of sources and p the number of players). An acceptable approximation, though, is to consider that the sources don't move or are so slow that it doesn't matters, so using the velocity of players relative to the source should give good results (I think one could use one of those "weird" vector operations for that, the dot product or the scalar product).
Thank you for your correction!

I'm gonna try to study a little more on this so I can improve this simulation and make it more realistic.

User avatar
Cosmos
New member
Posts: 9
Joined: Fri May 03, 2019 18:57
GitHub: CosmosScripter
In-game: Cosmos

Re: Sound with different gain depending on distance/Doppler Effect [sound_distance]

by Cosmos » Post

Astrobe wrote:
Sat Jul 24, 2021 06:55
Just for the sake of correctness, the Doppler effect affects the frequency of the sound, due to the movement of the emitter (actually the relative speed of the source and the receiver), so one should affect the pitch, not the gain, and compute the relative speed of the source and the player.

However, this means one would have to perform s x p computations (with s the number of sources and p the number of players). An acceptable approximation, though, is to consider that the sources don't move or are so slow that it doesn't matters, so using the velocity of players relative to the source should give good results (I think one could use one of those "weird" vector operations for that, the dot product or the scalar product).
Pitch changing for moving sound sources were added (I've tested it and found no errors, however, it still needs more testing). It's a kind of a really simple code for now, still no relative velocity computing.
Also the mod seems to work better with short repeating sounds played over, it has a better effect.

If this feature works I'll probably start working on objects moving above sound speed, which will have a delayed sound.

This wouldn't be possible without your feedback, thank you!

Astrobe
Member
Posts: 570
Joined: Sun Apr 01, 2018 10:46

Re: Sound with different gain depending on distance/Doppler Effect [sound_distance]

by Astrobe » Post

Cosmos wrote:
Tue Jul 27, 2021 21:39
Also the mod seems to work better with short repeating sounds played over, it has a better effect.
Yes, I think you'll hit some limitations of MT with regard to sound handling ; pitch manipulation itself is a nice recent addition. If we could get the duration of a sound instead of hard-coding it (like for instance in the Mpd mod ; even if it's in a nice config file it is still manually defined), that would be quite useful.

I've heard (pun intended) that audio programming is difficult due to broken/incomplete APIs (I have seen sound bugs in AAA games), so it might be the reason for those limitations.

User avatar
Cosmos
New member
Posts: 9
Joined: Fri May 03, 2019 18:57
GitHub: CosmosScripter
In-game: Cosmos

Re: Sound with different gain depending on distance/Doppler Effect [sound_distance]

by Cosmos » Post

Added sonic booms and sound barrier breaking.

Object at exact sound speed: only people behind can hear + sonic boom
Object above sound speed: only people behind can hear + sound delay

In Minetest, an object above sound speed moves faster than map generation (I've attached the player to an entity to test this), so I also added a way to apply a sonic boom even if the object is slow.

Post Reply

Who is online

Users browsing this forum: No registered users and 19 guests