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