Occlusion: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: live (line 77), wall (line 139), scene (line 148), listen (line 152).
1/// What the world does to a sound between there and here, as a callback the
2/// scene asks and never a wall test of its own.
3///
4/// Quoted by `audio_occlusion.md` and shown whole in the Source tab.
5library;
7import 'dart:math' as math;
9import 'package:flutter3d/flutter3d.dart';
10import 'package:flutter3d_audio/flutter3d_audio.dart';
11import 'package:flutter3d_showcase/src/demo/demo.dart';
12import 'package:flutter3d_showcase/src/demo/scene_kit.dart';
14/// The `audio-occlusion` page: a bell behind a wall and a listener walking
15/// past it. Its check is that the bell heard through the wall is muffled by
16/// half.
17final class AudioOcclusionDemo extends ShowcaseDemo {
18 late final String _report;
20 /// Where the listener stands along x, in metres, -6 to 8; written each frame
21 /// while [walking].
22 double listenerX = 0.0;
24 /// Whether the listener walks back and forth on its own; dragging the slider
25 /// stops it.
26 bool walking = true;
28 late final AudioScene _audio;
29 late final AudioEmitter _bell;
30 late final AudioListener _listener;
31 late final MeshNode _source;
32 late final MeshNode _head;
33 late final BarGauge _gain;
34 late final BarGauge _muffle;
35 double _clock = 0.0;
38 void configureView(DemoContext context) {
39 context.orbit
40 ..distance = 12.0
41 ..pitch = 0.8
42 ..yaw = 0.0;
43 context.orbit.target.setValues(1.0, 0.0, 1.0);
44 }
47 Scene build(DemoContext context) {
48 _report = _run();
49 _source = ballNode(
50 context,
51 'bell',
52 0.5,
53 Vector4(0.9, 0.75, 0.3, 1.0),
54 at: Vector3(4.0, 0.5, 0.0),
55 );
56 _head = blockNode(
57 context,
58 'listener',
59 Vector3(0.7, 0.7, 0.7),
60 Vector4(0.45, 0.65, 0.95, 1.0),
61 );
62 _gain = BarGauge(
63 context,
64 'gain',
65 Vector4(0.9, 0.75, 0.3, 1.0),
66 Vector3(-4.0, 0.0, 4.5),
67 height: 5.0,
68 );
69 _muffle = BarGauge(
70 context,
71 'muffle',
72 Vector4(0.7, 0.4, 0.85, 1.0),
73 Vector3(-4.0, 0.0, 5.5),
74 height: 5.0,
75 );
77 // The same scene and the same question, asked every frame while the
78 // listener walks: the wall is only ever the callback.
79 _audio = AudioScene(backend: SilentBackend(), occlusion: _occlusion);
80 _bell = _audio.play(
81 const SoundDef(name: 'bell', asset: 'bell.wav', loop: true),
82 Vector3(4.0, 0.0, 0.0),
83 );
84 _listener = AudioListener(position: Vector3.zero());
85 return sceneOf(<SceneNode>[
86 floorNode(context, width: 18.0, depth: 14.0),
87 blockNode(
88 context,
89 'wall',
90 Vector3(0.3, 2.0, 6.0),
91 Vector4(0.6, 0.5, 0.4, 1.0),
92 at: Vector3(2.0, 1.0, 0.0),
93 ),
94 _source,
95 _head,
96 ..._gain.nodes,
97 ..._muffle.nodes,
98 ]);
99 }
102 void update(DemoContext context, double dt) {
103 _clock += dt;
104 if (walking) listenerX = 2.0 + 5.5 * math.sin(_clock * 0.45);
105 _listener.position.setValues(listenerX, 0.0, 0.0);
106 _audio.update(_listener);
107 _head.setPosition(listenerX, 0.35, 0.0);
108 _gain.set(_bell.audibleGain);
109 _muffle.set(_bell.muffle);
110 // A muffled bell goes dull.
111 _source.material.baseColor = LinearColor.fromSrgb(
112 0.9 - 0.45 * _bell.muffle,
113 0.75 - 0.3 * _bell.muffle,
114 0.3 + 0.2 * _bell.muffle,
115 1.0,
116 );
117 }
120 List<DemoControl> controls(DemoContext context) => <DemoControl>[
121 SliderControl(
122 'Listener at',
123 min: -6.0,
124 max: 8.0,
125 value: () => listenerX,
126 onChanged: (double v) {
127 walking = false;
128 listenerX = v;
129 },
130 format: (double v) => '${v.toStringAsFixed(1)} m',
131 ),
132 ToggleControl(
133 'Walk about',
134 value: () => walking,
135 onChanged: (bool v) => walking = v,
136 ),
137 ];
139 /// A wall at x = 2: a straight line that crosses it is half heard, and one
140 /// that does not is heard clearly. The scene never learns what a wall is;
141 /// it only asks this question.
142 static double _occlusion(Vector3 from, Vector3 to) {
143 final crossesWall = (from.x - 2.0).sign != (to.x - 2.0).sign;
144 return crossesWall ? 0.5 : 1.0;
145 }
147 static String _run() {
148 final scene = AudioScene(backend: SilentBackend(), occlusion: _occlusion);
149 const bell = SoundDef(name: 'bell', asset: 'bell.wav', loop: true);
150 final behindTheWall = scene.play(bell, Vector3(4.0, 0.0, 0.0));
152 final listener = AudioListener(position: Vector3(0.0, 0.0, 0.0));
153 scene.update(listener);
154 final gainBehindWall = behindTheWall.audibleGain;
155 final muffleBehindWall = behindTheWall.muffle;
157 listener.position.setValues(5.0, 0.0, 0.0);
158 scene.update(listener);
159 final gainSameSide = behindTheWall.audibleGain;
161 return 'listener at 0, sound past the wall at 4: gain '
162 '${gainBehindWall.toStringAsFixed(3)}, muffle '
163 '${muffleBehindWall.toStringAsFixed(2)}\n'
164 'listener moved to 5, same side as the sound now: gain '
165 '${gainSameSide.toStringAsFixed(3)}';
166 }
169 void verify(Scene scene, FrameResult frame) {
170 if (frame.drawCalls < 1) {
171 throw StateError('the wall marker was not drawn');
172 }
173 if (!_report.contains('muffle 0.50')) {
174 throw StateError(
175 'a sound heard through the wall should be muffled '
176 'by half',
177 );
178 }
179 }
180}