Skip to main content

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