Spatial audio with Dynamic Acoustics in Godot | Tutorial

Поділитися
Вставка
  • Опубліковано 21 вер 2024

КОМЕНТАРІ • 69

  • @Blekoh
    @Blekoh  6 місяців тому +14

    !🐛BUG ALERT🐛!
    The video version contains a bug where if the audio player is a child of something that rotates then it will not update the RaycastPlayer correctly. To fix the bug remove the RaycastPlayer from the node and ready function and then use the following _on_update_lowpass_filter function:
    func _on_update_lowpass_filter(_player : Node3D):
    if _lowpass_filter != null:
    var query = PhysicsRayQueryParameters3D.create(self.global_position,self.global_position + (_player.global_position - self.global_position).normalized() * max_raycast_distance, $RaycastForward.get_collision_mask())
    var space_state = get_world_3d().direct_space_state
    var result = space_state.intersect_ray(query)
    var lowpass_cutoff = 20000 #init to a value where nothing gets cutoff
    if !result.is_empty():
    var ray_distance = self.global_position.distance_to(result["position"])
    var distance_to_player = self.global_position.distance_to(_player.global_position)
    var wall_to_player_ratio = ray_distance / max(distance_to_player,0.001)
    if ray_distance < distance_to_player:
    lowpass_cutoff = wall_lowpass_cutoff_amount * wall_to_player_ratio
    _target_lowpass_cutoff = lowpass_cutoff
    Alternatively you can view/download the fixed code here: www.patreon.com/posts/98559205 (its free)

    • @crowlsyong
      @crowlsyong 2 місяці тому

      Dude thank you. Much appreciated.

    • @streider1347
      @streider1347 Місяць тому

      In the debugger I get “_save_to_cache: Condition ‘f.is_null()’ is true.
      Is this a problem for everyone or just me?

  • @foxdancemedia
    @foxdancemedia 7 місяців тому +20

    woah thanks blekoh this is actually something that will super improve a game I'm working on 👍
    people really don't realize how much of a difference good sound design makes

    • @Blekoh
      @Blekoh  7 місяців тому +7

      Nice! Yeah I made it for my current project and I felt like it made a big difference so I thought I'd share it hah. Good luck with your game
      👈👈🤖

    • @PabloWunderlichMusic
      @PabloWunderlichMusic 7 місяців тому +2

      couldnt agree more!

    • @clashofclans9783
      @clashofclans9783 4 місяці тому +1

      ​@@Blekoh What is your next project 🤔 ?!?

    • @Blekoh
      @Blekoh  4 місяці тому +1

      @@clashofclans9783 I've mostly been working on my new game Sacred Forest
      ua-cam.com/video/yGXYHpKD2II/v-deo.html

    • @clashofclans9783
      @clashofclans9783 4 місяці тому +2

      @@Blekoh good luck

  • @jasoniswrongabouteverythin8230
    @jasoniswrongabouteverythin8230 7 днів тому

    I remember seeing a video about something like this that someone was selling as an add on, and they didnt want to share source code or anything. was pretty disappointed, but now youve given out some good code and made a whole TUTORIAL! bloody brilliant mate

  • @JustVoidzzz
    @JustVoidzzz 7 місяців тому +13

    Great tutorial. The effect is very good. The main thing I did differently when following is using ray cast queries instead of individual ray cast nodes. This allows the node to be added just like any other regular node. The only other thing I had to change when doing this is when I emitted the ray cast, I would have to add the current global position to the direction I wanted to emit.

    • @Blekoh
      @Blekoh  7 місяців тому +2

      Awesome! I like the ray cast query idea! I honestly totally forgot those existed since its been a while since I've been working with Godot 3d stuff haha
      👈👈🤖

  • @phillippi2
    @phillippi2 6 місяців тому +2

    This is kind of inspiring for me. I am working on a 3d game in Godot, where the player plays as a dolphin. It focuses on how they use sound for nearly everything. I wanted to implement a system similar to the GSound demo posted here on UA-cam. I thought that I was going to have to write a custom audio server. But this gives me hope that I won't have to.

  • @possiblyzslot838
    @possiblyzslot838 2 місяці тому

    Not the most elegant solution probably, however I'm glad there are any tutorials on this at all and once new developers get more experienced they can create their own improved solutions

  • @PureAsbestos
    @PureAsbestos 7 місяців тому +1

    This is something I have thought about a lot. Thanks for making a video on it!

  • @BetaTester704
    @BetaTester704 7 місяців тому +4

    Really high quality video

    • @Blekoh
      @Blekoh  7 місяців тому +1

      Thanks! 👈👈🤖

  • @Bombastic_Heister
    @Bombastic_Heister 5 днів тому

    Your the best, thank you ❤, this is the sauce which horror games need.

  • @vegameta
    @vegameta 4 місяці тому +1

    Как же мощно! Спасибо 🧶

  • @jamie_dev
    @jamie_dev 3 місяці тому

    thanks for this! I just discovered your channel and love your other videos. looking forward for more!

  • @HakanBacon
    @HakanBacon 2 місяці тому

    Dang this is such a lovely tutorial. Thank you!

  • @Mr-Highball
    @Mr-Highball 7 місяців тому

    Great tutorial! Im glad i saw this on the internet today

    • @Blekoh
      @Blekoh  7 місяців тому

      Thanks!

  • @Marandal
    @Marandal 5 місяців тому

    Super helpful video. More people should know about this!!!

  • @morgan0
    @morgan0 6 місяців тому

    fyi the lerps here are actually creating a first order lowpass filter (or smoothing filter), or an exponential moving average. it's similar to the filter you're using on the audio, but at a much lower cutoff frequency, and smoothing whatever values instead of audio

  • @ghostcreeper3679
    @ghostcreeper3679 6 місяців тому +1

    Damn this is SICK, i found this tuto so useful as a starting point but other than adding more rays (like each diagonal for xy xz and zy planes and evey diagonal inbetween) i have no clue on how ot improve it further. Doy ou know other effects other than the two you showed so that i might get my hands on them? or do you plan on expanding this example?

    • @Blekoh
      @Blekoh  6 місяців тому +1

      Thanks! Well there are a few ways it could be improved on but for my game I felt like it is good enough as is🤖
      Some systems treat different surfaces different like carpet would be different than tile floor, or wood, etc. Sometimes other spatial audio systems will also add a compressor (which is just another effect that balances the audio out so things don't get blown out). An EQ might also be good to avoid off frequencies idk.
      I think spatial audio can be a rabbit hole for people to go down where a simple system like this will get you 90% of the way and you could spend months trying to perfect the system to get the extra 10% haha

    • @ghostcreeper3679
      @ghostcreeper3679 6 місяців тому +1

      Thanks @@Blekoh I'll look into it :))

    • @morgan0
      @morgan0 6 місяців тому +1

      if you want to add more rays for better resolution of what's around it, you could use a stochastic method where you shoot out rays in a random direction (or random forwards and backwards, or rotating a bunch of rays, whatever) and having some representation of distance in a sphere around it that each ray modifies based on where it is pointing, with some smoothing temporally and spatially. then unless the sound source is moving fast enough that it can't update frequently enough (also stationary objects could update less often), it will trend towards accurate distances.
      after watching some more, if you just want averages, you could use an exponential moving average with random ray directions. for each ray, calculate what its contribution would be, and then blend 0.9 of the old value with 0.1 of the new value (or maybe even smaller). that's different from typical linear interpolation, but is using the lerp function to create a first order smoothing filter, or an exponential moving average.

  • @clashofclans9783
    @clashofclans9783 4 місяці тому +2

    Why can't I hear anything?:(

  • @rigodolfo
    @rigodolfo 5 місяців тому

    tank you

  • @davitdon2963
    @davitdon2963 3 місяці тому

    I'm trying to get how many decibels certain sounds make, So that I can wake up characters if the environment is too loud. How would I get the decibels while the audio is playing?

    • @Blekoh
      @Blekoh  3 місяці тому +1

      Well you need the bus_idx for whichever bus you’re trying to check, so if you have a bus named “Master”
      var bus_idx = AudioServer.get_bus_index(“Master”)
      Then to get the current decibel level you can do
      var db_level = AudioServer.get_bus_volume_db(bus_idx)

  • @mojosulo
    @mojosulo 7 місяців тому

    Amazing tutorial, thanks! Any chance you'll be uploading this code anywhere, or making it into a Godot plugin?

    • @Blekoh
      @Blekoh  7 місяців тому +2

      Thanks! Well this is meant to be sort of a starting point for people to just get an idea for how to make this sort of system and customize it to their needs. I don't think its really plugin quality as-is but anybody is welcome to add features to it and make their own plugin (also I don't want to maintain it long term haha)

    • @mojosulo
      @mojosulo 7 місяців тому +1

      @@Blekoh Totally makes sense. Thanks for the reply!

  • @oshihete0
    @oshihete0 5 місяців тому

    thx

  • @WeirdBrainGoo
    @WeirdBrainGoo 2 місяці тому

    I have a question. How would you treat sound sources that aren't point-like? For example, along a beach there are waves crashing all across the shoreline.
    Of course you could place several AudioStreamPlayer3Ds along the shore, but then I think there will be positions where you can hear two sources at the same time. So as you walk along the beach the sound would be louder in some places even if you're the same distance away from the water.
    Or should there be a trail in the water and you just make the sound source move alongside the player when they're near the shore?
    EDIT - I decided to just place a bunch of AudioStreamPlayer3D nodes along the shore. It works well as long as you set the attenuation mode to inverse.

    • @Blekoh
      @Blekoh  2 місяці тому +1

      @@WeirdBrainGoo Nice, yeah I think placing them along the shore was a good choice for that, or just using a regular AudioStreamPlayer and fade it in when you enter the shore Area, and fade it out when you exit. Good luck with your project!
      👈🏻👈🏻🤖

    • @WeirdBrainGoo
      @WeirdBrainGoo 2 місяці тому +1

      Okay, thank you! 😁

    • @empMerp
      @empMerp Місяць тому

      @@WeirdBrainGoo A more technical approach could also have the sound source follow the player along whatever axis the beach is on, Although if you plan to have it like an island, I cant think of a way to follow a circular path.

    • @WeirdBrainGoo
      @WeirdBrainGoo Місяць тому

      @@empMerp Yea, that's actually what I said in my original comment. 🙂
      I remember seeing a 2D node that was a path of some sort that you could define and set other nodes to move along. It was used in the 2D tutorial in the Godot documentation. There might be a 3D equivalent.
      If the island is circular you could just do sound_source.position = center + r * dir, where r is the radius of the island, dir is the direction the player is from the center of the island and center is the center of the island. So dir = (sound_source.position - player.position).normalized()
      I actually did try this, but it has the problem that when you go slightly to the left from the middle of the island, the sound source will move all the way to the left shore. So you'll only hear the water on the left. And whichever way you move slightly away from the middle, the source will be all the way there. My island is pretty small so it's very noticeable. Placing several sources along the shore fixed that problem. With that solution either the attenuation should be set to inverse, or if you're using inverse square root you should place a lot of them close together.

    • @empMerp
      @empMerp Місяць тому

      @@WeirdBrainGoo Oh sorry I never saw the "show more" option

  • @lmullins573
    @lmullins573 Місяць тому

    Dumb question from me but do I add this to the player scene or a global scene. It doesn’t seem to be affecting the sounds and I know it’s something I’m doing wrong.

    • @Blekoh
      @Blekoh  Місяць тому

      You would add it to whatever you want to make a sound. For example, an enemy might have a spatial audio player that plays monster sounds. Also, I did release the code to where you can just download and use it if you have trouble finding out what is crashing :)
      www.patreon.com/posts/spatialaudioplay-98559205

    • @lmullins573
      @lmullins573 Місяць тому

      @@Blekoh oooooooh that makes sense. I get it now. It’s literally replacing all my audio player 3D nodes. Thank you so much. This game I’m making relies on hearing and this is going to be so solid.

    • @Blekoh
      @Blekoh  Місяць тому

      @@lmullins573 nice, good luck! :)

  • @jeffg4686
    @jeffg4686 3 місяці тому

    Nice, do you happen to know if you can add effects to someone's voice in the room?
    Like multiplayer, but you want to allow the mic on the headset and add effects to external sounds in the room.
    For instance, let's say you're in a game and you enter some weird room where everything is echoing even the people next to you.
    You could sample the audio from the mic and every "pause" you can reverb the sounds before it.
    I mean, I guess, I already know it can be done, but more wondering if you've tried.

    • @Blekoh
      @Blekoh  3 місяці тому +1

      You can definitely apply effects to someone's voice. Godot has tons of built in audio effects that can be applied to an audio bus.
      You can use the AudioStreamMicrophone along with the AudioEffectCapture to get a players voice audio data and then you can send the captured audio data over the network to nearby players.
      Then on every other players pc you can take that audio data that was received from the network and feed it into an AudioStreamPlayer that plays audio on an audio bus with your desired echo effect (either with reverb effect or delay effect)
      You can add/remove effects to audio buses whenever you want, so you can just add it when you're in your special room.
      Here is Godot's official demo project for recording audio which would be a good starting point:
      github.com/godotengine/godot-demo-projects/tree/master/audio/mic_record

    • @jeffg4686
      @jeffg4686 3 місяці тому

      @@Blekoh - Nice, thanks for the details. I think my first adventure into XR might be a haunted house or haunted mansion (side loaded for client) and something like that definitely add a nice effect to everything. Since everyone's in the room - no network needed in my scenario. Just process the audio coming in with effects. But you can't replay the audio portion itself - only the effects portion so I guess you'd maybe skip ahead of the initial sound itself (which the player has already heard)

  • @escapZe
    @escapZe 7 місяців тому

    Help, why does this happen):. Out of bounds get index '10' (on base: 'Array')

    • @Blekoh
      @Blekoh  7 місяців тому

      Count how many 0's you added in the _distance_array at the top of the script, there should be ten 0's, it looks like you might have 11

    • @escapZe
      @escapZe 7 місяців тому +1

      Thank you! @@Blekoh

    • @Blekoh
      @Blekoh  7 місяців тому

      ​@@escapZe I would put a break point in the script to check first of all that _on_update_lowpass_filter is actually being called and also add a break point to make sure that in the _lerp_parameters function, it is actually setting the cutoff_hz value
      If you stand behind a wall and add the breakpoint to the _lerp_parameters function you should be able to see that your _target_lowpass_cutoff value should be set to some value less than 1000

    • @Blekoh
      @Blekoh  7 місяців тому

      ​@@escapZe If you still get stuck I uploaded my copy here for reference:
      www.patreon.com/posts/spatialaudioplay-98559205

    • @escapZe
      @escapZe 7 місяців тому

      Anything past (self.bus = _audio_bus_name) doesnt work in ready function.@@Blekoh

  • @sunnivaflusund2987
    @sunnivaflusund2987 5 місяців тому

    I cant find the "spatialAudioPlayer3D" is my Nodes, is it like an addon?

    • @Blekoh
      @Blekoh  5 місяців тому +1

      SpatialAudioPlayer3D is the node we make in the video 👈🏻👈🏻🤖

    • @sunnivaflusund2987
      @sunnivaflusund2987 5 місяців тому +2

      Oh sorry, i see now, i am a total newbie xD I just wanna say really appreciate your skills with coding, i am more an artist than a programmer and find it a bit hard to learn sometimes, but folk like you makes me believe that it is possible, so thanks