• Stars
    star
    336
  • Rank 125,564 (Top 3 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created almost 4 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Bevy plugin to use Kira for game audio

Bevy Kira audio

Crates.io docs license Crates.io

This bevy plugin is intended to test an integration of Kira into Bevy. The goal is to replace or update bevy_audio, if Kira turns out to be a good approach. Currently, this plugin can play ogg, mp3, flac, and wav formats and supports web builds.

Sound can be played in channels. Each channel has controls to pause or stop playback and can change the volume, playback speed, and panning of all sounds playing in it. You can easily add new channels and access them through Bevy's ECS (see the custom_channel example).

Usage

Note: the Bevy feature bevy_audio is enabled by default and not compatible with this plugin. Make sure to not have the bevy_audio feature enabled if you want to use bevy_kira_audio. The same goes for Bevy's vorbis feature. See Bevys' Cargo file for a list of all default features of version 0.9 and list them manually in your Cargo file excluding the ones you do not want. Make sure to set default-features to false for the Bevy dependency. You can take a look at bevy_game_template's cargo file as an example.

To play audio, you usually want to load audio files as assets. This requires AssetLoaders. bevy_kira_audio comes with loaders for most common audio formats. You can enable them with the features ogg (enabled by default), mp3, wav, or flac. The following example assumes that the feature ogg is enabled.

use bevy_kira_audio::prelude::*;
use bevy::prelude::*;

fn main() {
   App::new()
        .add_plugins((DefaultPlugins, AudioPlugin))
        .add_systems(Startup, start_background_audio)
        .run();
}

fn start_background_audio(asset_server: Res<AssetServer>, audio: Res<Audio>) {
    audio.play(asset_server.load("background_audio.ogg")).looped();
}

You can change settings like volume, panning, or playback rate for running sounds, or when starting to play a sound. All changes can be done as smooth transitions. By default, they will be almost instantaneous.

Sound settings

You can configure a sound when playing it:

use bevy_kira_audio::prelude::*;
use bevy::prelude::*;
use std::time::Duration;

fn play_audio(asset_server: Res<AssetServer>, audio: Res<Audio>) {
    audio.play(asset_server.load("background_audio.ogg"))
        // The first 0.5 seconds will not be looped and are the "intro"
        .loop_from(0.5)
        // Fade-in with a dynamic easing
        .fade_in(AudioTween::new(Duration::from_secs(2), AudioEasing::OutPowi(2)))
        // Only play on our right ear
        .with_panning(1.0)
        // Increase playback rate by 50% (this also increases the pitch)
        .with_playback_rate(1.5)
        // Play at half volume
        .with_volume(0.5)
        // play the track reversed
        .reverse();
}

Optionally, you can also load a sound with already applied settings. This requires the feature settings_loader.

Sounds are configured in ron files. The following file loads as a AudioSource which is looped and has a 3 seconds intro before the loop:

(
    // The actual sound file in your assets directory
    file: "sounds/loop.ogg",

    loop_behavior: Some(3.0),
)

would make the loaded sound loop by default and start each repeated playback three seconds into the sound (the three seconds are the intro).

More settings are available. See the settings_loader example for all options.

Controlling sounds

You can either control a whole audio channel and all instances playing in it (channel_control example), or a single audio instance (instance_control example). Both ways offer audio transitions with Tweens supporting multiple easings.

Spacial audio

There is limited spacial audio support. Currently, only the volume of audio and it's panning can be automatically changed based on emitter and receiver positions. Take a look at the spacial example for some code.

Compatible Bevy versions

The main branch is compatible with the latest Bevy release.

Compatibility of bevy_kira_audio versions:

bevy_kira_audio bevy
0.16 0.11
0.15 0.10
0.13 - 0.14 0.9
0.11 - 0.12 0.8
0.9 - 0.10 0.7
0.8 0.6
0.4 - 0.7 0.5
0.3 0.4
main 0.10
bevy_main main

License

Dual-licensed under either of

at your option.

Assets in the examples might be distributed under different terms. See the readme in the examples directory.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

More Repositories

1

bevy_game_template

Template for a Bevy game including CI/CD for web, Windows, Linux, macOS, iOS and Android
Rust
795
star
2

bevy_asset_loader

Bevy plugin helping with asset loading and organization
Rust
467
star
3

bevy_common_assets

Collection of generic asset loaders for common file formats
Rust
180
star
4

oicana

Tower defence game with some puzzling
Rust
56
star
5

cinnog

Experimental static site generator using Leptos with Bevy ECS as a data layer
Rust
34
star
6

gamebox

Minecraft plugin with a vast selection of inventory games
Java
13
star
7

wasm-opt-action

Rust
9
star
8

calendarevents

Minecraft plugin for customized events called at specific dates/times
Java
8
star
9

jekyll_custom_permalink

Jekyll plugin adding support for any Front Matter in permalinks
Ruby
8
star
10

wicked_potions

Rust
7
star
11

jekyll-exif-data

Jekyll plugin to supply websites with exif data from images
Ruby
6
star
12

clempire

Idle game
JavaScript
5
star
13

cinnog_example

Rust
4
star
14

leptos-ssg-with-islands

Rust
3
star
15

the_chase

Rust
2
star
16

nmsutilities

Java
2
star
17

LMGTFY

Bukkit plugin that adds commands for lmgtfy and search engines
Java
2
star
18

friends_vs_zombies

Rust
2
star
19

bevy_jam2

Rust
2
star
20

games-nikl-me

Website with html5 games for my telegram bot
JavaScript
2
star
21

start-axum-workspace-islands-ssg

Rust
2
star
22

gemcrush-for-gamebox

Game for GameBox inspired by Candycrush
Java
1
star
23

cookieclicker-for-gamebox

Cookie Clicker for GameBox
Java
1
star
24

node-chess

A node chess server for playing matches in telegram
JavaScript
1
star
25

NiklsAwesomeBot

Telegram bot serving a few puzzle games
JavaScript
1
star
26

calendarevents-example

Small example usage of the API provided by CalendarEvents
Java
1
star
27

html5-sudoku

JavaScript
1
star
28

test-expo-router

TypeScript
1
star
29

connectfour-for-gamebox

Inventory game ConnectFour for GameBox
Java
1
star
30

dots-and-boxes-python

Dots and Boxes written in Python
Python
1
star
31

me_and_my_unicycle

Rust
1
star
32

gfi_bevy_talk

CSS
1
star