bevy_kira_audio/
channel.rs

1pub mod dynamic;
2pub mod typed;
3
4use crate::audio::{AudioCommand, FadeIn, FadeOut, PlayAudioCommand, TweenCommand};
5use crate::instance::AudioInstance;
6use crate::{AudioSource, PlaybackState};
7use bevy::asset::Handle;
8use kira::sound::static_sound::StaticSoundData;
9use kira::tween::Value;
10use kira::Volume;
11use std::any::TypeId;
12
13#[derive(Clone, PartialEq, Eq, Hash)]
14pub enum Channel {
15    Typed(TypeId),
16    Dynamic(String),
17}
18
19pub(crate) struct ChannelState {
20    pub(crate) paused: bool,
21    pub(crate) volume: Volume,
22    pub(crate) playback_rate: f64,
23    pub(crate) panning: f64,
24}
25
26impl Default for ChannelState {
27    fn default() -> Self {
28        ChannelState {
29            paused: false,
30            volume: 1.0.into(),
31            playback_rate: 1.0,
32            panning: 0.5,
33        }
34    }
35}
36
37impl ChannelState {
38    pub(crate) fn apply(&self, sound: &mut StaticSoundData) {
39        sound.settings.volume = Value::Fixed(self.volume);
40        sound.settings.playback_rate = self.playback_rate.into();
41        sound.settings.panning = Value::Fixed(self.panning);
42    }
43}
44
45/// Play and control audio
46pub trait AudioControl {
47    /// Play audio
48    ///
49    /// ```
50    /// # use bevy::prelude::*;
51    /// # use bevy_kira_audio::prelude::*;
52    ///
53    /// fn my_system(asset_server: Res<AssetServer>, audio: Res<Audio>) {
54    ///     audio.play(asset_server.load("audio.mp3"));
55    /// }
56    /// ```
57    fn play(&self, audio_source: Handle<AudioSource>) -> PlayAudioCommand;
58
59    /// Stop all audio
60    ///
61    /// ```
62    /// # use bevy::prelude::*;
63    /// # use bevy_kira_audio::prelude::*;
64    ///
65    /// fn my_system(audio: Res<Audio>) {
66    ///     audio.stop();
67    /// }
68    /// ```
69    fn stop(&self) -> TweenCommand<FadeOut>;
70
71    /// Pause all audio
72    ///
73    /// ```
74    /// # use bevy::prelude::*;
75    /// # use bevy_kira_audio::prelude::*;
76    ///
77    /// fn my_system(audio: Res<Audio>) {
78    ///     audio.pause();
79    /// }
80    /// ```
81    fn pause(&self) -> TweenCommand<FadeOut>;
82
83    /// Resume all audio
84    ///
85    /// ```
86    /// # use bevy::prelude::*;
87    /// # use bevy_kira_audio::prelude::*;
88    ///
89    /// fn my_system(audio: Res<Audio>) {
90    ///     audio.resume();
91    /// }
92    /// ```
93    fn resume(&self) -> TweenCommand<FadeIn>;
94
95    /// Set the volume
96    ///
97    /// The default value is 1.
98    /// This method supports setting the volume in Decibels or as Amplitude.
99    ///
100    /// ```
101    /// # use bevy::prelude::*;
102    /// # use bevy_kira_audio::prelude::*;
103    ///
104    /// fn my_system(audio: Res<Audio>) {
105    ///     audio.set_volume(0.5);
106    /// }
107    /// ```
108    fn set_volume(&self, volume: impl Into<Volume>) -> TweenCommand<FadeIn>;
109
110    /// Set panning
111    ///
112    /// The default value is 0.5
113    /// Values up to 1 pan to the right
114    /// Values down to 0 pan to the left
115    ///
116    /// ```
117    /// # use bevy::prelude::*;
118    /// # use bevy_kira_audio::prelude::*;
119    ///
120    /// fn my_system(audio: Res<Audio>) {
121    ///     audio.set_panning(0.9);
122    /// }
123    /// ```
124    fn set_panning(&self, panning: f64) -> TweenCommand<FadeIn>;
125
126    /// Set playback rate
127    ///
128    /// The default value is 1
129    ///
130    /// ```
131    /// # use bevy::prelude::*;
132    /// # use bevy_kira_audio::prelude::*;
133    ///
134    /// fn my_system(audio: Res<Audio>) {
135    ///     audio.set_playback_rate(2.0);
136    /// }
137    /// ```
138    fn set_playback_rate(&self, playback_rate: f64) -> TweenCommand<FadeIn>;
139
140    /// Get state for a playback instance.
141    fn state(&self, instance_handle: &Handle<AudioInstance>) -> PlaybackState;
142
143    /// Returns `true` if there is any sound in this channel that is in the state `Playing`, `Pausing`, or `Stopping`
144    ///
145    /// If there are only `Stopped`, `Paused`, or `Queued` sounds, the method will return `false`.
146    /// The same result is returned if there are no sounds in the channel at all.
147    fn is_playing_sound(&self) -> bool;
148}
149
150pub(crate) trait AudioCommandQue {
151    fn que(&self, command: AudioCommand);
152}