1use 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 let engine = SoundEngine::new().unwrap();
47
48 let context = SoundContext::new();
50
51 engine.state().add_context(context.clone());
52
53 context
55 .state()
56 .set_renderer(Renderer::HrtfRenderer(HrtfRenderer::new(
57 HrirSphereResource::from_hrir_sphere(hrir_sphere, hrir_path.into()),
58 )));
59
60 {
61 let mut reverb = Reverb::new();
63 reverb.set_decay_time(10.0);
64
65 let mut state = context.state();
67 state
68 .bus_graph_mut()
69 .primary_bus_mut()
70 .add_effect(Effect::Reverb(reverb));
71 }
72
73 let sound_buffer = SoundBufferResource::new_generic(
75 block_on(DataSource::from_file(
76 "examples/data/door_open.wav", &FsResourceIo,
78 ))
79 .unwrap(),
80 )
81 .unwrap();
82 let source = SoundSourceBuilder::new()
83 .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 &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 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 thread::sleep(Duration::from_millis(100));
130 }
131}