Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose several types #72

Merged
merged 4 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ impl<'a> From<&mut PlayAudioCommand<'a>> for PlayAudioSettings {
}
}

/// A command for interacting with a playing sound.
NiklasEi marked this conversation as resolved.
Show resolved Hide resolved
pub struct PlayAudioCommand<'a> {
pub(crate) instance_handle: Handle<AudioInstance>,
pub(crate) source: Handle<AudioSource>,
Expand All @@ -173,61 +174,76 @@ impl<'a> PlayAudioCommand<'a> {
}
}

/// Loop the playing sound.
pub fn looped(&mut self) -> &mut Self {
self.settings.loop_behavior = Some(Some(0.0));

self
}

/// Loop the playing sound, starting from the given position.
pub fn loop_from(&mut self, loop_start_position: f64) -> &mut Self {
self.settings.loop_behavior = Some(Some(loop_start_position));

self
}

/// Set the volume of the sound.
pub fn with_volume(&mut self, volume: f64) -> &mut Self {
self.settings.volume = Some(volume);

self
}

/// Set the playback rate of the sound.
pub fn with_playback_rate(&mut self, playback_rate: f64) -> &mut Self {
self.settings.playback_rate = Some(playback_rate);

self
}

/// Start the sound from the given position in seconds.
pub fn start_from(&mut self, start_position: f64) -> &mut Self {
self.settings.start_position = Some(start_position);

self
}

/// Set the panning of the sound.
///
/// The default value is 0.5.
/// Values up to 1.0 pan to the right,
/// while values down to 0.0 pan to the left.
pub fn with_panning(&mut self, panning: f64) -> &mut Self {
self.settings.panning = Some(panning);

self
}

/// Reverse the playing sound.
pub fn reverse(&mut self) -> &mut Self {
let current = self.settings.reverse.unwrap_or(false);
self.settings.reverse = Some(!current);

self
}

/// Set how long will the sound fade in linearly.
pub fn linear_fade_in(&mut self, duration: Duration) -> &mut Self {
self.settings.fade_in = Some(AudioTween::linear(duration));

self
}

/// Set how will the sound fade in,
/// given its duration and easing.
pub fn fade_in(&mut self, tween: AudioTween) -> &mut Self {
self.settings.fade_in = Some(tween);

self
}

/// Get the handle of the audio instance.
pub fn handle(&mut self) -> Handle<AudioInstance> {
self.instance_handle.clone()
}
Expand Down Expand Up @@ -257,9 +273,12 @@ impl TweenCommandKind {
}
}

/// Cross-fade that happens at the start of the audio.
NiklasEi marked this conversation as resolved.
Show resolved Hide resolved
pub struct FadeIn;
/// Cross-fade that happens at the end of the audio.
pub struct FadeOut;

/// A command for interacting with the tweening of the playing sound.
pub struct TweenCommand<'a, Fade> {
pub(crate) kind: TweenCommandKind,
pub(crate) tween: Option<AudioTween>,
Expand All @@ -285,12 +304,15 @@ impl<'a, Fade> TweenCommand<'a, Fade> {
}

impl<'a> TweenCommand<'a, FadeIn> {
/// Set how long will the sound fade in linearly.
pub fn linear_fade_in(&mut self, duration: Duration) -> &mut Self {
self.tween = Some(AudioTween::linear(duration));

self
}

/// Set how will the sound fade in,
/// given its duration and easing.
pub fn fade_in(&mut self, tween: AudioTween) -> &mut Self {
self.tween = Some(tween);

Expand All @@ -299,12 +321,15 @@ impl<'a> TweenCommand<'a, FadeIn> {
}

impl<'a> TweenCommand<'a, FadeOut> {
/// Set how long will the sound fade out linearly.
pub fn linear_fade_out(&mut self, duration: Duration) -> &mut Self {
self.tween = Some(AudioTween::linear(duration));

self
}

/// Set how will the sound fade out,
/// given its duration and easing.
pub fn fade_out(&mut self, tween: AudioTween) -> &mut Self {
self.tween = Some(tween);

Expand Down
12 changes: 9 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,21 @@ mod channel;
mod instance;
mod source;

pub use audio::{AudioApp, AudioEasing, AudioTween, PlaybackState};
pub use audio::{
AudioApp, AudioEasing, AudioTween, FadeIn, FadeOut, PlayAudioCommand, PlaybackState,
TweenCommand,
};
pub use backend_settings::AudioSettings;
pub use channel::AudioControl;
pub use source::AudioSource;

/// Most commonly used types
pub mod prelude {
#[doc(hidden)]
pub use crate::audio::{AudioApp, AudioEasing, AudioTween, PlaybackState};
pub use crate::audio::{
AudioApp, AudioEasing, AudioTween, FadeIn, FadeOut, PlayAudioCommand, PlaybackState,
TweenCommand,
};
#[doc(hidden)]
pub use crate::backend_settings::AudioSettings;
#[doc(hidden)]
Expand All @@ -59,7 +65,7 @@ pub mod prelude {
#[doc(hidden)]
pub use crate::channel::AudioControl;
#[doc(hidden)]
pub use crate::instance::{AudioInstance, AudioInstanceAssetsExt};
pub use crate::instance::{AudioCommandError, AudioInstance, AudioInstanceAssetsExt};
#[doc(hidden)]
pub use crate::source::AudioSource;
#[doc(hidden)]
Expand Down