Skip to main content

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