#[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());
}
);

Fields§
§_id: u16
§_slot: i16
Implementations§
Source§impl SoundInst
impl SoundInst
Sourcepub fn stop(self)
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());
}
);
Sourcepub fn position(&mut self, at: impl Into<Vec3>) -> &mut Self
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);
);
Sourcepub fn volume(&mut self, volume: f32) -> &mut Self
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);
);
Sourcepub fn get_position(&self) -> Vec3
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
Sourcepub fn get_volume(&self) -> f32
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
Sourcepub fn get_intensity(&self) -> f32
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();
);
Sourcepub fn is_playing(&self) -> bool
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§
impl Copy for SoundInst
impl StructuralPartialEq for SoundInst
Auto Trait Implementations§
impl Freeze for SoundInst
impl RefUnwindSafe for SoundInst
impl Send for SoundInst
impl Sync for SoundInst
impl Unpin for SoundInst
impl UnwindSafe for SoundInst
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.