bevyengine / bevy

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

Add an audio example demonstrating how to play randomized sound effects #12652

Open alice-i-cecile opened 5 months ago

alice-i-cecile commented 5 months ago

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

When working with sound effects in games, audio files should

What solution would you like?

Core requirements:

  1. The player must be able to do something.
  2. When a trigger occurs, the sound effect plays.
  3. The sound effect is chosen from a pool of random sound effects.

Stretch goals:

  1. The player can take multiple actions, and the set of sound effects is structured sensibly to model this.
  2. The sound effects are procedurally varied in some way.

Additional context

You should consider reusing existing sound effects from the repo to save on file size.

gabrielkryss commented 5 months ago

This seems interesting to tackle for me. I'd like to ask some more questions before I start experimenting

1) From my understanding, is the player just a first-person camera controller, or is the vision more like a top-down angle of a camera looking at a character that the player can control and move? Or is it more open ended? 2) Is the trigger like a box collider trigger, or is it any input trigger? Or maybe its more open ended as well based on the stretch goals 3) any references material in how to specifically procedurally vary sounds?

alice-i-cecile commented 5 months ago

Very open-ended on the game-like elements: I want something simple but plausible-looking :) It's not the point of the example, so our main goal is to avoid distracting the reader.

Here's a simple article on procedural sound variations: https://olliebatesonsound.wordpress.com/2019/10/16/procedural-sound-design/

I'm not at all sure we can easily do that in Bevy today: I would personally leave it for follow-up. @harudagondi may have advice on the state of this in Rust.

mgi388 commented 2 months ago

I have some code in my “game” that may achieve the “random” and “procedural” requirements of this example. I’m using bevy-kira-components though. Unclear if it’s worth writing something in bevy_audio and I know bevy-kira-components also has an issue to port the bevy_audio examples over!

Anyway, my code plays SFX text scripts loaded as assets. Each script has a list of sounds it randomly picks from. Each sound has a frequency and frequency deviation value. Using that we can change the pitch every time we play a picked sound. On top of that, the script has “silence” sounds which creates the randomness for delays between sounds. Other knobs available in each sound that I’m currently not changing are: volume, attack and release.

alice-i-cecile commented 2 months ago

Perfect; that's pretty much exactly what I had in mind. Mild preference to add this as an example to bevy_audio today, but adding it to bevy_kira_components would be super useful.

mgi388 commented 2 months ago

https://github.com/bevyengine/bevy/assets/135186256/d69d0278-9eaf-423d-88b1-3dc56b6624cd

@alice-i-cecile how about this (sound on)?

  1. Based off the 2d camera follow example.
  2. Has 2 "NPCs"—when the player collides with them, it starts the "NPC talking SFX 'script'".
  3. Each NPC has their own vocal range so the left one sounds deeper on average.
  4. To avoid adding more sounds, I just used the existing collision sound. I thought it was kind of cute that I could change the pitch and volume of it to make "voice sounds" (it's not the best sound you've ever heard but maybe good enough for an example?). In this way, this example won't demonstrate playing random sounds, but it's possibly obvious how to do that from the code anyway.
  5. It includes a really simple collision system to start/stop the sound. I think it's small enough for an example.
  6. Didn't want to push the code until I got some feedback the example is OK / needs adjusting.
alice-i-cecile commented 2 months ago

That sounds and looks great :)

mgi388 commented 1 month ago

P.S Alice it’s been on my todo list to push this example but I see you’ve picked it up and taken a different approach. Sorry for the partial effort here!

alice-i-cecile commented 1 month ago

Yep, no worries! It came up again with the jam working group and I wanted to get something in place for them :)