reverb/
reverb.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_resource::io::FsResourceIo;
22use fyrox_sound::buffer::SoundBufferResourceExtension;
23use fyrox_sound::renderer::hrtf::{HrirSphereResource, HrirSphereResourceExt};
24use fyrox_sound::{
25    algebra::{Point3, UnitQuaternion, Vector3},
26    buffer::{DataSource, SoundBufferResource},
27    context::{self, SoundContext},
28    effects::{reverb::Reverb, Effect},
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    let hrir_path = PathBuf::from("examples/data/IRC_1002_C.bin");
43    let hrir_sphere = HrirSphere::from_file(&hrir_path, context::SAMPLE_RATE).unwrap();
44
45    // Initialize sound engine with default output device.
46    let engine = SoundEngine::new().unwrap();
47
48    // Initialize new sound context.
49    let context = SoundContext::new();
50
51    engine.state().add_context(context.clone());
52
53    // Set HRTF renderer instead of default for binaural sound.
54    context
55        .state()
56        .set_renderer(Renderer::HrtfRenderer(HrtfRenderer::new(
57            HrirSphereResource::from_hrir_sphere(hrir_sphere, hrir_path.into()),
58        )));
59
60    {
61        // Create reverb effect and set its decay time.
62        let mut reverb = Reverb::new();
63        reverb.set_decay_time(10.0);
64
65        // Add the reverb to the primary bus.
66        let mut state = context.state();
67        state
68            .bus_graph_mut()
69            .primary_bus_mut()
70            .add_effect(Effect::Reverb(reverb));
71    }
72
73    // Create some sounds.
74    let sound_buffer = SoundBufferResource::new_generic(
75        block_on(DataSource::from_file(
76            "examples/data/door_open.wav", // Load from the default resource io (File system)
77            &FsResourceIo,
78        ))
79        .unwrap(),
80    )
81    .unwrap();
82    let source = SoundSourceBuilder::new()
83        // Each sound must specify the bus to which it will output the samples. By default it is "Primary" bus.
84        .with_bus("Primary")
85        .with_buffer(sound_buffer)
86        .with_status(Status::Playing)
87        .build()
88        .unwrap();
89    context.state().add_source(source);
90
91    let sound_buffer = SoundBufferResource::new_generic(
92        block_on(DataSource::from_file(
93            "examples/data/drop.wav",
94            // Load from the default resource io (File system)
95            &FsResourceIo,
96        ))
97        .unwrap(),
98    )
99    .unwrap();
100    let source = SoundSourceBuilder::new()
101        .with_buffer(sound_buffer)
102        .with_status(Status::Playing)
103        .with_looping(true)
104        .build()
105        .unwrap();
106    let drop_sound_handle = context.state().add_source(source);
107
108    // Move sound around listener for some time.
109    let start_time = time::Instant::now();
110    let mut angle = 0.0f32;
111    while (time::Instant::now() - start_time).as_secs() < 360 {
112        let axis = Vector3::y_axis();
113        let rotation_matrix =
114            UnitQuaternion::from_axis_angle(&axis, angle.to_radians()).to_homogeneous();
115        context.state().source_mut(drop_sound_handle).set_position(
116            rotation_matrix
117                .transform_point(&Point3::new(0.0, 0.0, 1.0))
118                .coords,
119        );
120
121        angle += 1.6;
122
123        println!(
124            "Sound render time {:?}",
125            context.state().full_render_duration()
126        );
127
128        // Limit rate of context updates.
129        thread::sleep(Duration::from_millis(100));
130    }
131}