SoundInst

Struct SoundInst 

Source
#[repr(C)]
pub struct SoundInst { pub _id: u16, pub _slot: i16, }
Expand description

This represents a play instance of a Sound! You can get one when you call Sound::play(). This allows you to do things like cancel a piece of audio early, or change the volume and position of it as it’s playing. https://stereokit.net/Pages/StereoKit/SoundInst.html

see also: Sound /// ### Examples

use stereokit_rust::{maths::{Vec3, Matrix}, mesh::Mesh, material::Material,
                     sound::Sound, util::named_colors};

let sphere = Mesh::generate_sphere(0.5, None);
let material = Material::pbr().tex_file_copy("textures/sound.jpeg", true, None)
                   .expect("sound.jpeg should be there");
let mut position1 = Vec3::new(-0.5, 0.0, 0.5);
let mut position2 = Vec3::new( 0.5, 0.0, 0.5);

let mut plane_sound1 = Sound::from_file("sounds/no.wav")
                          .expect("no.wav should be there");
plane_sound1.id("sound_plane1").decibels(70.0);
let mut plane_sound_inst1 = plane_sound1.play(position1, Some(1.0));

let mut plane_sound2 = Sound::from_file("sounds/no.wav")
                          .expect("no.wav should be there");
plane_sound2.id("sound_plane2").decibels(70.0);
let mut plane_sound_inst2 = plane_sound2.play(position2, Some(1.0));
plane_sound_inst2.stop();

number_of_steps = 150;
filename_scr = "screenshots/sound_inst.jpeg";
test_screenshot!( // !!!! Get a proper main loop !!!!
    let transform1 = Matrix::t(position1);
    let transform2 = Matrix::t(position2);
    sphere.draw(token, &material, transform1, Some(named_colors::PINK.into()), None  );
    sphere.draw(token, &material, transform2, Some(named_colors::LIGHT_GREEN.into()), None  );

    if iter == 0 {
        //TODO: assert!(plane_sound_inst1.is_playing());
        assert!(!plane_sound_inst2.is_playing());
        position1 = Vec3::new(-0.3, 0.0, 0.3);
        plane_sound_inst1
            .position(position1)
            .volume(0.5);
    } else if iter == 150 - 2 {
        //TODO: assert!(plane_sound_inst1.is_playing());
        position2 = Vec3::new(0.3, 0.0, 0.3);
        plane_sound_inst2 = plane_sound2.play(position2, Some(1.0));
        assert!(plane_sound_inst2.is_playing());
   }
);
screenshot

Fields§

§_id: u16§_slot: i16

Implementations§

Source§

impl SoundInst

Source

pub fn stop(self)

This stops the sound early if it’s still playing. consume the SoundInst as it will not be playable again. https://stereokit.net/Pages/StereoKit/SoundInst/Stop.html

see also sound_inst_stop

§Examples
use stereokit_rust::{maths::Vec3, sound::Sound};

let mut plane_sound = Sound::from_file("sounds/plane_engine.mp3").
                          expect("A sound should be created");
let mut plane_sound_inst = plane_sound.play([0.0, 0.0, 0.0], Some(1.0));

test_steps!( // !!!! Get a proper main loop !!!!
    if iter == 1 {
        plane_sound_inst.stop();
        assert!(!plane_sound_inst.is_playing());
    }
);
Source

pub fn position(&mut self, at: impl Into<Vec3>) -> &mut Self

The 3D position in world space this sound instance is currently playing at. If this instance is no longer valid, the position will be at zero. https://stereokit.net/Pages/StereoKit/SoundInst/Position.html

see also sound_inst_set_pos

§Examples
use stereokit_rust::{maths::Vec3, sound::Sound};

let mut position = Vec3::new(-2.5, 0.0, 0.5);

let mut plane_sound = Sound::from_file("sounds/plane_engine.mp3").
                          expect("A sound should be created");
plane_sound.id("sound_plane").decibels(70.0);

let mut plane_sound_inst = plane_sound.play(position, None);
assert_eq!(plane_sound_inst.get_position(), position);

number_of_steps = 150;
test_steps!( // !!!! Get a proper main loop !!!!
    position += Vec3::new(0.0001, 0.0, 0.0);
    plane_sound_inst.position(position);
);
Source

pub fn volume(&mut self, volume: f32) -> &mut Self

The volume multiplier of this Sound instance! A number between 0 and 1, where 0 is silent, and 1 is full volume. https://stereokit.net/Pages/StereoKit/SoundInst/Volume.html

see also sound_inst_set_volume

§Examples
use stereokit_rust::{maths::Vec3, sound::Sound, system::Assets};

let mut position = Vec3::new(0.0, 0.0, 0.5);
let mut volume = 0.0;

let mut plane_sound = Sound::from_file("sounds/plane_engine.mp3").
                          expect("A sound should be created");
plane_sound.id("sound_plane");
Assets::block_for_priority(i32::MAX);

let mut plane_sound_inst = plane_sound.play(position, None);
plane_sound_inst.volume(0.005);

number_of_steps = 150;
test_steps!( // !!!! Get a proper main loop !!!!
    volume += 0.01;
    plane_sound_inst.volume(volume);
);
Source

pub fn get_position(&self) -> Vec3

The 3D position in world space this sound instance is currently playing at. If this instance is no longer valid, the position will be at zero. https://stereokit.net/Pages/StereoKit/SoundInst/Position.html

see also sound_inst_get_pos see example in SoundInst::position

Source

pub fn get_volume(&self) -> f32

The volume multiplier of this Sound instance! A number between 0 and 1, where 0 is silent, and 1 is full volume. https://stereokit.net/Pages/StereoKit/SoundInst/Volume.html

see also sound_inst_get_volume see example in SoundInst::volume

Source

pub fn get_intensity(&self) -> f32

The maximum intensity of the sound data since the last frame, as a value from 0-1. This is unaffected by its 3d position or volume settings, and is straight from the audio file’s data. https://stereokit.net/Pages/StereoKit/SoundInst/Intensity.html

see also sound_inst_get_intensity

§Examples
use stereokit_rust::{maths::Vec3, sound::Sound};

let mut plane_sound = Sound::from_file("sounds/plane_engine.mp3").
                          expect("A sound should be created");
plane_sound.id("sound_plane").decibels(70.0);

let mut plane_sound_inst = plane_sound.play([0.0, 0.0, 0.0], Some(1.0));
plane_sound_inst.volume(1.0);


test_steps!( // !!!! Get a proper main loop !!!!
    assert_eq!(plane_sound_inst.get_intensity(), 0.0);
    plane_sound_inst.stop();
);
Source

pub fn is_playing(&self) -> bool

Is this Sound instance currently playing? For streaming assets, this will be true even if they don’t have any new data in them, and they’re just idling at the end of their data. https://stereokit.net/Pages/StereoKit/SoundInst/IsPlaying.html

see also sound_inst_is_playing

§Examples
use stereokit_rust::{maths::Vec3, sound::Sound};

let mut plane_sound = Sound::from_file("sounds/plane_engine.mp3").
                          expect("A sound should be created");
let mut plane_sound_inst = plane_sound.play([0.0, 0.0, 0.0], Some(1.0));

test_steps!( // !!!! Get a proper main loop !!!!
    if iter == 1 {
        assert!(plane_sound_inst.is_playing());
        plane_sound_inst.stop();
    } else if iter > 1 {
        assert!(!plane_sound_inst.is_playing());
    }
);

Trait Implementations§

Source§

impl Clone for SoundInst

Source§

fn clone(&self) -> SoundInst

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SoundInst

Source§

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

Formats the value using the given formatter. Read more
Source§

impl PartialEq for SoundInst

Source§

fn eq(&self, other: &SoundInst) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for SoundInst

Source§

impl StructuralPartialEq for SoundInst

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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 + Sync + Send>

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<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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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> 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