Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
DrSanjeep opened this issue Aug 2, 2024 · 13 comments
Open

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

DrSanjeep opened this issue Aug 2, 2024 · 13 comments

Comments

@DrSanjeep
Copy link

DrSanjeep commented Aug 2, 2024

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
Copy link
Collaborator

lakulish commented Aug 2, 2024

@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
Copy link
Author

@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
Copy link
Collaborator

lakulish commented Aug 2, 2024

@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
Copy link
Author

@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
Copy link
Author

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
Copy link
Author

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

@lakulish
Copy link
Collaborator

lakulish commented Aug 6, 2024

@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
Copy link
Author

DrSanjeep commented Aug 7, 2024

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
Copy link
Author

@lakulish or this
iplSourceCreate2

@MKokeshi

This comment was marked as spam.

@DrSanjeep
Copy link
Author

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

@lakulish
Copy link
Collaborator

@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!

@DrSanjeep
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants