ValveSoftware / steam-audio

Steam Audio
https://valvesoftware.github.io/steam-audio/
Apache License 2.0
2.26k stars 156 forks source link

How do I play sound at a new position immediately? #366

Open DrSanjeep opened 1 month ago

DrSanjeep commented 1 month ago

steamaudio_unity_4.5.3 Unity version: 2022.3.38f1

I don't know ahead of time where the next sound will play I hoped that i could move an audio source and then immediately play a sound but it sounds like its interpolating

Steps To Reproduce create a blank unity 3d project set up steamaudio unity 4.5.3 create an empty game object add audio source with short audio clip e.g. footstep set the 3D slider to 3D add the steam sudio source Enable Distance Attenuation add the below script play the scene check Spatialize to hear distorted sounds

we expect to hear only one sound because the second sound its too far away but we hear two distorted sounds

the script below attempts to play the sound in one position, waits 0.5 seconds then plays the sound in the second position and repeats. And it sounds terrible

using UnityEngine;

public class SteamBug : MonoBehaviour
{
    bool stage;

    float timer;
    void Update()
    {
        timer += Time.deltaTime;
        if (timer < 0.5f)
            return;

        timer = 0;

        var cam = Camera.main.transform;
        Vector3 left = Vector3.Cross(cam.forward, Vector3.up);
        stage = !stage;
        if (stage == false)
            transform.position = cam.position;
        else
            transform.position = cam.position + left * 100;

        GetComponent<AudioSource>().Play();
    }
}
lakulish commented 1 month ago

@DrSanjeep Steam Audio is designed to interpolate between successive frames for smooth variation of distance attenuation, direction, etc, so rapid teleportation of the AudioSource may not produce the results you might be expecting.

Also, what is the value of Doppler level in the Audio Source settings? If this is non-zero, Unity might be applying very aggressive Doppler effect to the audio clip, resulting in distortion.

DrSanjeep commented 1 month ago

@lakulish thank you. i have tried doppler 0 and 1 and it sounds the same. do you happen to know the recommended method to overcome or clear this interpolation? can this system make a bullet ricochet sound from a raycast for instance? do i have to recreate the unity audio source?

lakulish commented 1 month ago

@DrSanjeep You can certainly use Steam Audio to make one-off sounds like a bullet ricochet. You will, however, have to use a separate Audio Source for each ricochet (you can use a pool of a finite number of Audio Sources for this).

DrSanjeep commented 1 month ago

@lakulish so i can only use an audio source once. i think i can do that but its going to be annoying. Wish i could just reset it

DrSanjeep commented 1 month ago

Using a pool of audio sources was not a good solution because 1. The pool runs out 2. It means continuously filling the pool

DrSanjeep commented 1 month ago

turns out the performance suffers having to create a new source for every sound played. will anything be done?

lakulish commented 1 month ago

@DrSanjeep I'd like to understand more about the performance impact you're seeing. Is there a minimal Unity project you can share that exhibits this issue?

DrSanjeep commented 1 month ago

iplSourceCreate @lakulish I don't have a minimal Unity project to hand but the profiler image shows it costs around 1ms to create the sound

DrSanjeep commented 1 month ago

@lakulish or this iplSourceCreate2

MKokeshi commented 1 month ago

Hey, sounds like you’ve got a tricky issue there! It seems like the rapid position changes are messing with the audio playback. Here are a few more things you might try:

  1. Check for Overlapping Audio Sources: Since you're changing the position so quickly, the AudioSource might be getting confused or creating overlaps. Creating a new AudioSource object each time you change positions, as you’re doing, is a smart move. Just make sure you’re cleaning up old ones properly to avoid clutter.

  2. Debug with Simple Sounds: Sometimes, testing with a simple sound or a different clip can help rule out if the issue is with the clip itself or how it's being played.

  3. Experiment with Audio Settings: Double-check all the audio settings in Unity and Steam Audio. Sometimes, small tweaks in distance attenuation or spatial settings can make a big difference.

  4. Check Steam Audio Docs: There might be some specific settings or recommendations in the Steam Audio documentation that could help with this kind of setup.

  5. Test in a New Scene: Sometimes, starting fresh in a new scene can help isolate the problem. Try setting up a new scene with just the basics to see if the issue persists.

Hopefully, this helps smooth things out a bit!

DrSanjeep commented 1 month ago

also i would like to avoid allocating 1KB every sound played

lakulish commented 1 month ago

@DrSanjeep That does seem like a significant amount of time for instantiating the prefab. Can you share more details about how the Steam Audio Source is configured? Again, a minimal Unity project will help us diagnose the issue. Thanks!