1use fyrox_core::algebra::Point3;
22use fyrox_resource::io::FsResourceIo;
23use fyrox_sound::buffer::SoundBufferResourceExtension;
24use fyrox_sound::renderer::hrtf::{HrirSphereResource, HrirSphereResourceExt};
25use fyrox_sound::{
26 algebra::{UnitQuaternion, Vector3},
27 buffer::{DataSource, SoundBufferResource},
28 context::{self, SoundContext},
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 engine = SoundEngine::new().unwrap();
44
45 let hrir_path = PathBuf::from("examples/data/IRC_1002_C.bin");
46 let hrir_sphere = HrirSphere::from_file(&hrir_path, context::SAMPLE_RATE).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 let sound_buffer = SoundBufferResource::new_generic(
62 block_on(DataSource::from_file(
63 "examples/data/door_open.wav", &FsResourceIo,
65 ))
66 .unwrap(),
67 )
68 .unwrap();
69 let source = SoundSourceBuilder::new()
70 .with_buffer(sound_buffer)
71 .with_status(Status::Playing)
72 .build()
73 .unwrap();
74 context.state().add_source(source);
75
76 let sound_buffer = SoundBufferResource::new_generic(
77 block_on(DataSource::from_file(
78 "examples/data/helicopter.wav", &FsResourceIo,
80 ))
81 .unwrap(),
82 )
83 .unwrap();
84 let source = SoundSourceBuilder::new()
85 .with_buffer(sound_buffer)
86 .with_status(Status::Playing)
87 .with_looping(true)
88 .build()
89 .unwrap();
90 let source_handle = context.state().add_source(source);
91
92 let start_time = time::Instant::now();
94 let mut angle = 0.0f32;
95 while (time::Instant::now() - start_time).as_secs() < 360 {
96 {
99 let axis = Vector3::y_axis();
100 let rotation_matrix =
101 UnitQuaternion::from_axis_angle(&axis, angle.to_radians()).to_homogeneous();
102 context.state().source_mut(source_handle).set_position(
103 rotation_matrix
104 .transform_point(&Point3::new(0.0, 0.0, 3.0))
105 .coords,
106 );
107
108 angle += 1.6;
109
110 println!(
111 "Sound render time {:?}",
112 context.state().full_render_duration()
113 );
114 }
115
116 thread::sleep(Duration::from_millis(100));
118 }
119}