hrtf/
hrtf.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::algebra::Point3;
22use fyrox_resource::io::FsResourceIo;
23use fyrox_sound::buffer::SoundBufferResourceExtension;
24use fyrox_sound::renderer::hrtf::{HrirSphereResource, HrirSphereResourceExt};
25use fyrox_sound::{
26    algebra::{UnitQuaternion, Vector3},
27    buffer::{DataSource, SoundBufferResource},
28    context::{self, SoundContext},
29    engine::SoundEngine,
30    futures::executor::block_on,
31    hrtf::HrirSphere,
32    renderer::{hrtf::HrtfRenderer, Renderer},
33    source::{SoundSourceBuilder, Status},
34};
35use std::path::PathBuf;
36use std::{
37    thread,
38    time::{self, Duration},
39};
40
41fn main() {
42    // Initialize sound engine with default output device.
43    let engine = SoundEngine::new().unwrap();
44
45    let hrir_path = PathBuf::from("examples/data/IRC_1002_C.bin");
46    let hrir_sphere = HrirSphere::from_file(&hrir_path, context::SAMPLE_RATE).unwrap();
47
48    // Initialize new sound context with default output device.
49    let context = SoundContext::new();
50
51    engine.state().add_context(context.clone());
52
53    // Set HRTF renderer instead of default.
54    context
55        .state()
56        .set_renderer(Renderer::HrtfRenderer(HrtfRenderer::new(
57            HrirSphereResource::from_hrir_sphere(hrir_sphere, hrir_path.into()),
58        )));
59
60    // Create some sounds.
61    let sound_buffer = SoundBufferResource::new_generic(
62        block_on(DataSource::from_file(
63            "examples/data/door_open.wav", // Load from the default resource io (File system)
64            &FsResourceIo,
65        ))
66        .unwrap(),
67    )
68    .unwrap();
69    let source = SoundSourceBuilder::new()
70        .with_buffer(sound_buffer)
71        .with_status(Status::Playing)
72        .build()
73        .unwrap();
74    context.state().add_source(source);
75
76    let sound_buffer = SoundBufferResource::new_generic(
77        block_on(DataSource::from_file(
78            "examples/data/helicopter.wav", // Load from the default resource io (File system)
79            &FsResourceIo,
80        ))
81        .unwrap(),
82    )
83    .unwrap();
84    let source = SoundSourceBuilder::new()
85        .with_buffer(sound_buffer)
86        .with_status(Status::Playing)
87        .with_looping(true)
88        .build()
89        .unwrap();
90    let source_handle = context.state().add_source(source);
91
92    // Move source sound around listener for some time.
93    let start_time = time::Instant::now();
94    let mut angle = 0.0f32;
95    while (time::Instant::now() - start_time).as_secs() < 360 {
96        // Separate scope for update to make sure that mutex lock will be released before
97        // thread::sleep will be called so context can actually work in background thread.
98        {
99            let axis = Vector3::y_axis();
100            let rotation_matrix =
101                UnitQuaternion::from_axis_angle(&axis, angle.to_radians()).to_homogeneous();
102            context.state().source_mut(source_handle).set_position(
103                rotation_matrix
104                    .transform_point(&Point3::new(0.0, 0.0, 3.0))
105                    .coords,
106            );
107
108            angle += 1.6;
109
110            println!(
111                "Sound render time {:?}",
112                context.state().full_render_duration()
113            );
114        }
115
116        // Limit rate of updates.
117        thread::sleep(Duration::from_millis(100));
118    }
119}