#kira #component #audio-playback #bevy #integration #spatial

bevy-kira-components

Alternative crate for Bevy integration of Kira

3 releases

0.1.1 Apr 28, 2024
0.1.0 Apr 28, 2024

#309 in Audio

Download history 166/week @ 2024-04-22 117/week @ 2024-04-29

283 downloads per month

MIT license

39KB
658 lines

Bevy <-> Kira as Components

GitHub License GitHub Actions Workflow Status Link to docs

This repository is an experiment in coming up with an alternative API for the integration of the Kira crate into Bevy, as a replacement for the current Bevy audio engine.

Features

  • Audio as components
  • Static and streaming audio sources
  • Support for spatial scenes

lib.rs:

Add audio support to Bevy through the kira crate.

This crate aims at creating a replacement for bevy_audio by instead integrating Kira, a crate for audio playback aimed at games, and used in several other Rust projects.

This particular crate is an experiment in making a component-based ECS API, instead of a resource-based approach, currently taken by bevy_kira_audio.

To get started playing sounds, insert an AudioBundle on an entity. This is a generic bundle which supports any compatible sound source. An implementation over audio files is provided, with streaming support, using the AudioFileBundle type alias.

When an AudioFile is inserted, its playback will begin right away. To prevent that, use the start_paused field from AudioFileBundle::settings and set it to false.

The audio system creates an AudioHandle component when registering an added sound for playback. This handle allows you to control the sound. For AudioFiles, this means pausing/resuming, setting the volume, panning, and playback rate of the sound.

Spatial audio is supported built-in with the SpatialEmitter component, which tells the plugin to add the entity as an emitter, provided it also has a GlobalTransform component attached. Its settings control the behavior of the spatial effect.

Example

use bevy::prelude::*;
use bevy_kira_components::prelude::*;

fn main() {
    App::new()
        .insert_non_send_resource(AudioSettings {
            // Only needed for tests
            backend_settings: AudioBackendSelector::Mock { sample_rate: 48000, },
            ..default()
        })
        .add_plugins((DefaultPlugins, AudioPlugin))
        .add_systems(Startup, add_sound)
        .run();
}

fn add_sound(mut commands: Commands, asset_server: Res<AssetServer>) {
    commands.spawn(AudioFileBundle {
        source: asset_server.load("my_sound.ogg"),
        ..default()
    });
}

Dependencies

~21–58MB
~1M SLoC