1use rg3d_core::algebra::Point3;
2use rg3d_sound::{
3 algebra::{UnitQuaternion, Vector3},
4 buffer::{DataSource, SoundBufferResource},
5 context::{self, SoundContext},
6 engine::SoundEngine,
7 futures::executor::block_on,
8 hrtf::HrirSphere,
9 renderer::{hrtf::HrtfRenderer, Renderer},
10 source::{generic::GenericSourceBuilder, spatial::SpatialSourceBuilder, SoundSource, Status},
11};
12use std::{
13 thread,
14 time::{self, Duration},
15};
16
17fn main() {
18 let engine = SoundEngine::new();
20
21 let hrir_sphere =
22 HrirSphere::from_file("examples/data/IRC_1002_C.bin", context::SAMPLE_RATE).unwrap();
23
24 let context = SoundContext::new();
26
27 engine.lock().unwrap().add_context(context.clone());
28
29 context
31 .state()
32 .set_renderer(Renderer::HrtfRenderer(HrtfRenderer::new(hrir_sphere)));
33
34 let sound_buffer = SoundBufferResource::new_generic(
36 block_on(DataSource::from_file("examples/data/door_open.wav")).unwrap(),
37 )
38 .unwrap();
39 let source = SpatialSourceBuilder::new(
40 GenericSourceBuilder::new()
41 .with_buffer(sound_buffer)
42 .with_status(Status::Playing)
43 .build()
44 .unwrap(),
45 )
46 .build_source();
47 context.state().add_source(source);
48
49 let sound_buffer = SoundBufferResource::new_generic(
50 block_on(DataSource::from_file("examples/data/helicopter.wav")).unwrap(),
51 )
52 .unwrap();
53 let source = SpatialSourceBuilder::new(
54 GenericSourceBuilder::new()
55 .with_buffer(sound_buffer)
56 .with_status(Status::Playing)
57 .with_looping(true)
58 .build()
59 .unwrap(),
60 )
61 .build_source();
62 let source_handle = context.state().add_source(source);
63
64 let start_time = time::Instant::now();
66 let mut angle = 0.0f32;
67 while (time::Instant::now() - start_time).as_secs() < 360 {
68 {
71 if let SoundSource::Spatial(spatial) = context.state().source_mut(source_handle) {
72 let axis = Vector3::y_axis();
73 let rotation_matrix =
74 UnitQuaternion::from_axis_angle(&axis, angle.to_radians()).to_homogeneous();
75 spatial.set_position(
76 rotation_matrix
77 .transform_point(&Point3::new(0.0, 0.0, 3.0))
78 .coords,
79 );
80 }
81
82 angle += 1.6;
83
84 println!(
85 "Sound render time {:?}",
86 context.state().full_render_duration()
87 );
88 }
89
90 thread::sleep(Duration::from_millis(100));
92 }
93}