2022-01-05 22:30:15 +00:00
|
|
|
//! Audio support for the game engine Bevy
|
|
|
|
//!
|
2022-07-04 13:04:18 +00:00
|
|
|
//! ```no_run
|
2022-01-05 22:30:15 +00:00
|
|
|
//! # use bevy_ecs::{system::Res, event::EventWriter};
|
|
|
|
//! # use bevy_audio::{Audio, AudioPlugin};
|
|
|
|
//! # use bevy_asset::{AssetPlugin, AssetServer};
|
2023-03-17 18:45:34 -07:00
|
|
|
//! # use bevy_app::{App, AppExit, NoopPluginGroup as MinimalPlugins, Startup};
|
2022-01-05 22:30:15 +00:00
|
|
|
//! fn main() {
|
|
|
|
//! App::new()
|
2023-06-21 22:51:03 +02:00
|
|
|
//! .add_plugins((MinimalPlugins, AssetPlugin::default(), AudioPlugin::default()))
|
2023-03-17 18:45:34 -07:00
|
|
|
//! .add_systems(Startup, play_background_audio)
|
2022-01-05 22:30:15 +00:00
|
|
|
//! .run();
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! fn play_background_audio(asset_server: Res<AssetServer>, audio: Res<Audio>) {
|
|
|
|
//! audio.play(asset_server.load("background_audio.ogg"));
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
|
|
|
|
#![forbid(unsafe_code)]
|
Suppress the `clippy::type_complexity` lint (#8313)
# Objective
The clippy lint `type_complexity` is known not to play well with bevy.
It frequently triggers when writing complex queries, and taking the
lint's advice of using a type alias almost always just obfuscates the
code with no benefit. Because of this, this lint is currently ignored in
CI, but unfortunately it still shows up when viewing bevy code in an
IDE.
As someone who's made a fair amount of pull requests to this repo, I
will say that this issue has been a consistent thorn in my side. Since
bevy code is filled with spurious, ignorable warnings, it can be very
difficult to spot the *real* warnings that must be fixed -- most of the
time I just ignore all warnings, only to later find out that one of them
was real after I'm done when CI runs.
## Solution
Suppress this lint in all bevy crates. This was previously attempted in
#7050, but the review process ended up making it more complicated than
it needs to be and landed on a subpar solution.
The discussion in https://github.com/rust-lang/rust-clippy/pull/10571
explores some better long-term solutions to this problem. Since there is
no timeline on when these solutions may land, we should resolve this
issue in the meantime by locally suppressing these lints.
### Unresolved issues
Currently, these lints are not suppressed in our examples, since that
would require suppressing the lint in every single source file. They are
still ignored in CI.
2023-04-06 17:27:36 -04:00
|
|
|
#![allow(clippy::type_complexity)]
|
2022-01-05 22:30:15 +00:00
|
|
|
#![warn(missing_docs)]
|
|
|
|
|
2020-10-20 14:44:50 -04:00
|
|
|
mod audio;
|
2020-07-16 13:46:51 -07:00
|
|
|
mod audio_output;
|
|
|
|
mod audio_source;
|
2023-02-20 15:31:07 +00:00
|
|
|
mod sinks;
|
2020-07-16 13:46:51 -07:00
|
|
|
|
2022-01-05 22:30:15 +00:00
|
|
|
#[allow(missing_docs)]
|
2020-07-16 19:20:51 -07:00
|
|
|
pub mod prelude {
|
2022-11-02 20:40:45 +00:00
|
|
|
#[doc(hidden)]
|
2023-02-20 15:31:07 +00:00
|
|
|
pub use crate::{
|
2023-04-11 02:08:43 +12:00
|
|
|
Audio, AudioOutput, AudioSink, AudioSinkPlayback, AudioSource, Decodable, GlobalVolume,
|
|
|
|
PlaybackSettings, SpatialAudioSink,
|
2023-02-20 15:31:07 +00:00
|
|
|
};
|
2020-07-16 19:20:51 -07:00
|
|
|
}
|
|
|
|
|
2021-02-19 00:20:37 +03:00
|
|
|
pub use audio::*;
|
|
|
|
pub use audio_output::*;
|
|
|
|
pub use audio_source::*;
|
2023-01-17 22:42:00 +00:00
|
|
|
|
2022-10-27 12:34:38 +00:00
|
|
|
pub use rodio::cpal::Sample as CpalSample;
|
|
|
|
pub use rodio::source::Source;
|
|
|
|
pub use rodio::Sample;
|
2023-02-20 15:31:07 +00:00
|
|
|
pub use sinks::*;
|
2021-02-19 00:20:37 +03:00
|
|
|
|
2020-07-16 18:47:51 -07:00
|
|
|
use bevy_app::prelude::*;
|
2023-01-17 22:42:00 +00:00
|
|
|
use bevy_asset::{AddAsset, Asset};
|
2020-07-16 13:46:51 -07:00
|
|
|
|
2022-01-05 22:30:15 +00:00
|
|
|
/// Adds support for audio playback to a Bevy Application
|
|
|
|
///
|
|
|
|
/// Use the [`Audio`] resource to play audio.
|
2020-07-16 13:46:51 -07:00
|
|
|
#[derive(Default)]
|
2023-04-11 02:08:43 +12:00
|
|
|
pub struct AudioPlugin {
|
|
|
|
/// The global volume for all audio sources with a [`Volume::Relative`] volume.
|
|
|
|
pub global_volume: GlobalVolume,
|
|
|
|
}
|
2020-07-16 13:46:51 -07:00
|
|
|
|
2020-08-07 20:22:17 -07:00
|
|
|
impl Plugin for AudioPlugin {
|
2021-07-27 20:21:06 +00:00
|
|
|
fn build(&self, app: &mut App) {
|
2022-12-11 18:10:00 +00:00
|
|
|
app.init_resource::<AudioOutput<AudioSource>>()
|
2020-07-16 13:46:51 -07:00
|
|
|
.add_asset::<AudioSource>()
|
2022-03-01 01:12:11 +00:00
|
|
|
.add_asset::<AudioSink>()
|
2023-02-20 15:31:07 +00:00
|
|
|
.add_asset::<SpatialAudioSink>()
|
2020-10-20 14:44:50 -04:00
|
|
|
.init_resource::<Audio<AudioSource>>()
|
2023-04-11 02:08:43 +12:00
|
|
|
.insert_resource(self.global_volume)
|
2023-03-17 18:45:34 -07:00
|
|
|
.add_systems(PostUpdate, play_queued_audio_system::<AudioSource>);
|
2021-06-03 19:58:08 +00:00
|
|
|
|
|
|
|
#[cfg(any(feature = "mp3", feature = "flac", feature = "wav", feature = "vorbis"))]
|
2022-01-05 22:30:15 +00:00
|
|
|
app.init_asset_loader::<AudioLoader>();
|
2020-07-16 13:46:51 -07:00
|
|
|
}
|
|
|
|
}
|
2023-01-17 22:42:00 +00:00
|
|
|
|
|
|
|
impl AddAudioSource for App {
|
|
|
|
fn add_audio_source<T>(&mut self) -> &mut Self
|
|
|
|
where
|
|
|
|
T: Decodable + Asset,
|
2023-02-20 15:31:07 +00:00
|
|
|
f32: rodio::cpal::FromSample<T::DecoderItem>,
|
2023-01-17 22:42:00 +00:00
|
|
|
{
|
|
|
|
self.add_asset::<T>()
|
|
|
|
.init_resource::<Audio<T>>()
|
2023-01-18 17:20:26 +00:00
|
|
|
.init_resource::<AudioOutput<T>>()
|
2023-03-17 18:45:34 -07:00
|
|
|
.add_systems(PostUpdate, play_queued_audio_system::<T>)
|
2023-01-17 22:42:00 +00:00
|
|
|
}
|
|
|
|
}
|