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