play_spatial_sound/
play_spatial_sound.rs

1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21use fyrox_core::futures::executor::block_on;
22use fyrox_resource::io::FsResourceIo;
23use fyrox_sound::buffer::SoundBufferResourceExtension;
24use fyrox_sound::{
25    algebra::{Point3, UnitQuaternion, Vector3},
26    buffer::{DataSource, SoundBufferResource},
27    context::SoundContext,
28    engine::SoundEngine,
29    pool::Handle,
30    source::{SoundSource, SoundSourceBuilder, Status},
31};
32use std::{
33    thread,
34    time::{self, Duration},
35};
36
37fn main() {
38    // Initialize sound engine with default output device.
39    let engine = SoundEngine::new().unwrap();
40
41    // Initialize new sound context.
42    let context = SoundContext::new();
43
44    engine.state().add_context(context.clone());
45
46    // Load sound buffer.
47    let drop_buffer = SoundBufferResource::new_generic(
48        block_on(DataSource::from_file(
49            "examples/data/drop.wav",
50            // Load from the default resource io (File system)
51            &FsResourceIo,
52        ))
53        .unwrap(),
54    )
55    .unwrap();
56
57    // Create spatial source - spatial sources can be positioned in space.
58    let source = SoundSourceBuilder::new()
59        .with_buffer(drop_buffer)
60        .with_looping(true)
61        .with_status(Status::Playing)
62        .build()
63        .unwrap();
64
65    // Each sound sound must be added to context, context takes ownership on source
66    // and returns pool handle to it by which it can be accessed later on if needed.
67    let source_handle: Handle<SoundSource> = context.state().add_source(source);
68
69    // Move sound around listener for some time.
70    let start_time = time::Instant::now();
71    let mut angle = 0.0f32;
72    while (time::Instant::now() - start_time).as_secs() < 11 {
73        let axis = Vector3::y_axis();
74        let rotation_matrix =
75            UnitQuaternion::from_axis_angle(&axis, angle.to_radians()).to_homogeneous();
76        context.state().source_mut(source_handle).set_position(
77            rotation_matrix
78                .transform_point(&Point3::new(0.0, 0.0, 3.0))
79                .coords,
80        );
81
82        angle += 3.6;
83
84        // Limit rate of updates.
85        thread::sleep(Duration::from_millis(100));
86    }
87}