bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
36.28k stars 3.58k forks source link

Add Spatial/Positional Audio in Rust #5832

Open harudagondi opened 2 years ago

harudagondi commented 2 years ago

What problem does this solve or what need does it fill?

What solution would you like?

Create two Components:

AudioEmitter's API should be similar to bevy_audio::Audio, while AudioListener works similar to AudioOutput internally, or computes the resulting sound wave and send it to the mixer.

These components should probably have its own thread, as the sample rate (the number of audio samples per second) is usually around 44100 Hz, so systems dealing with audio components should be able to run for at least 44100 times a second to avoid underruns.

What alternative(s) have you considered?

Additional context

See the discussion in the bevy discord in the #audio-dev channel for more info.

bjorn3 commented 2 years ago

These components should probably have its own thread, as the sample rate (the number of audio samples per second) is usually around 44100 Hz, so systems dealing with audio components should be able to run for at least 44100 times a second to avoid underruns.

I believe samples are pretty much always batched, so you don't need to run 44100 times for a 44100 Hz sample rate. As for running in their own thread, rodio (which bevy currently uses for audio) already spawns a background thread for audio handling AFAIK.

harudagondi commented 2 years ago

samples are pretty much always batched

background thread for audio handling

Whoops, i forgot about that. Maybe AudioListener and AudioEmitter could just use mspc channels, as long as it is fast enough.

mockersf commented 2 years ago

I have a small demo showing rodio limited spatial audio capabilities: https://vleue.itch.io/rodio-spatial-audio. It's waiting on a rodio release to be upstreamed in Bevy

basilefff commented 1 year ago

This issue seems to be resolved, should it be closed?

mockersf commented 1 year ago

It currently only does attenuation based on the distance to left/right "ears", it's very far from spatial...