Skip to main content

StaticSoundHandle

Struct StaticSoundHandle 

Source
pub struct StaticSoundHandle { /* private fields */ }
Expand description

Controls a static sound.

Implementations§

Source§

impl StaticSoundHandle

Source

pub fn state(&self) -> PlaybackState

Returns the current playback state of the sound.

Source

pub fn position(&self) -> f64

Returns the current playback position of the sound (in seconds).

Source

pub fn set_volume(&mut self, volume: impl Into<Value<Decibels>>, tween: Tween)

Sets the volume of the sound.

§Examples

Set the volume of the sound immediately:

use kira::Tween;

sound.set_volume(-6.0, Tween::default());

Smoothly transition the volume to a target volume:

use kira::Tween;
use std::time::Duration;

sound.set_volume(-6.0, Tween {
	duration: Duration::from_secs(3),
	..Default::default()
});

Link the volume to a modulator, smoothly transitioning from the current value:

use kira::{
	AudioManager, AudioManagerSettings, DefaultBackend,
	sound::static_sound::{StaticSoundData, StaticSoundSettings},
	modulator::tweener::TweenerBuilder,
	Value, Tween, Mapping, Easing,
	Decibels,
};
use std::time::Duration;

let mut manager = AudioManager::<DefaultBackend>::new(AudioManagerSettings::default())?;
let tweener = manager.add_modulator(TweenerBuilder {
	initial_value: 0.5,
})?;
let mut sound = manager.play(StaticSoundData::from_file("sound.ogg")?)?;
sound.set_volume(Value::FromModulator {
	id: tweener.id(),
	mapping: Mapping {
		input_range: (0.0, 1.0),
		output_range: (Decibels::SILENCE, Decibels::IDENTITY),
		easing: Easing::Linear,
	}
}, Tween {
	duration: Duration::from_secs(3),
	..Default::default()
});
Source

pub fn set_playback_rate( &mut self, playback_rate: impl Into<Value<PlaybackRate>>, tween: Tween, )

Sets the playback rate of the sound.

Changing the playback rate will change both the speed and pitch of the sound.

§Examples

Set the playback rate of the sound immediately:

use kira::Tween;

sound.set_playback_rate(0.5, Tween::default());

Smoothly transition the playback rate to a target value in semitones:

use kira::{Tween, Semitones};
use std::time::Duration;

sound.set_playback_rate(Semitones(-2.0), Tween {
	duration: Duration::from_secs(3),
	..Default::default()
});

Link the playback rate to a modulator, smoothly transitioning from the current value:

use kira::{
	AudioManager, AudioManagerSettings, DefaultBackend,
	sound::static_sound::{StaticSoundData, StaticSoundSettings},
	modulator::tweener::TweenerBuilder,
	Value, Easing, Mapping, Tween,
	PlaybackRate,
};
use std::time::Duration;

let mut manager = AudioManager::<DefaultBackend>::new(AudioManagerSettings::default())?;
let tweener = manager.add_modulator(TweenerBuilder {
	initial_value: 0.5,
})?;
let mut sound = manager.play(StaticSoundData::from_file("sound.ogg")?)?;
sound.set_playback_rate(Value::FromModulator {
	id: tweener.id(),
	mapping: Mapping {
		input_range: (0.0, 1.0),
		output_range: (PlaybackRate(0.0), PlaybackRate(1.0)),
		easing: Easing::Linear,
	},
}, Tween {
	duration: Duration::from_secs(3),
	..Default::default()
});
Source

pub fn set_panning(&mut self, panning: impl Into<Value<Panning>>, tween: Tween)

Sets the panning of the sound, where -1.0 is hard left, 0.0 is center, and 1.0 is hard right.

§Examples

Smoothly transition the panning to a target value:

use kira::Tween;
use std::time::Duration;

sound.set_panning(-0.5, Tween {
	duration: Duration::from_secs(3),
	..Default::default()
});

Link the panning to a modulator, smoothly transitioning from the current value:

use kira::{
	AudioManager, AudioManagerSettings, DefaultBackend,
	sound::static_sound::{StaticSoundData, StaticSoundSettings},
	modulator::tweener::TweenerBuilder,
	Value, Easing, Mapping, Tween,
	Panning,
};
use std::time::Duration;

let mut manager = AudioManager::<DefaultBackend>::new(AudioManagerSettings::default())?;
let tweener = manager.add_modulator(TweenerBuilder {
	initial_value: -0.5,
})?;
let mut sound = manager.play(StaticSoundData::from_file("sound.ogg")?)?;
sound.set_panning(Value::FromModulator {
	id: tweener.id(),
	mapping: Mapping {
		input_range: (-1.0, 1.0),
		output_range: (Panning::LEFT, Panning::RIGHT),
		easing: Easing::Linear,
	},
}, Tween {
	duration: Duration::from_secs(3),
	..Default::default()
});
Source

pub fn set_loop_region(&mut self, loop_region: impl IntoOptionalRegion)

Sets the portion of the sound that will play in a loop.

§Examples

Set the sound to loop the portion from 3 seconds in to the end:

sound.set_loop_region(3.0..);

Set the sound to loop the portion from 2 to 4 seconds:

sound.set_loop_region(2.0..4.0);

Set a sound that was previously looping to stop looping:

sound.set_loop_region(None);
Source

pub fn pause(&mut self, tween: Tween)

Fades out the sound to silence with the given tween and then pauses playback.

Source

pub fn resume(&mut self, tween: Tween)

Resumes playback and fades in the sound from silence with the given tween.

Source

pub fn resume_at(&mut self, start_time: StartTime, tween: Tween)

Resumes playback at the given start time and fades in the sound from silence with the given tween.

Source

pub fn stop(&mut self, tween: Tween)

Fades out the sound to silence with the given tween and then stops playback.

Once the sound is stopped, it cannot be restarted.

Source

pub fn seek_to(&mut self, position: f64)

Sets the playback position to the specified time in seconds.

Source

pub fn seek_by(&mut self, amount: f64)

Moves the playback position by the specified amount of time in seconds.

Trait Implementations§

Source§

impl Debug for StaticSoundHandle

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,