flutter3d
Showcase Changelog 38 packages API reference

Mixer buses: the source

since 0.2.0 Simulation, audio and XR

Regions the guide quotes: mixer (line 24), set (line 58), read (line 76).

1/// A group of sounds a player can turn down as one, and the mixer that reads
2/// every bus's own volume against a shared master.
3///
4/// Quoted by `audio_buses.md` and shown whole in the Source tab.
5library;
6
7import 'package:flutter3d/flutter3d.dart';
8import 'package:flutter3d_audio/flutter3d_audio.dart';
9import 'package:flutter3d_showcase/src/demo/demo.dart';
10import 'package:flutter3d_showcase/src/demo/scene_kit.dart';
11import 'package:vector_math/vector_math.dart';
13final class AudioBusesDemo extends ShowcaseDemo {
14 double musicVolume = 0.6;
15 double masterVolume = 1.0;
17 late final Mixer _mixer;
18 late final BarGauge _music;
19 late final BarGauge _sfx;
20 late final BarGauge _out;
22 @override
23 Scene build(DemoContext context) {
24 _mixer = Mixer();
26 _music = BarGauge(
27 context,
28 'music',
29 Vector4(0.45, 0.65, 0.95, 1.0),
30 Vector3(-2.5, 0.0, 0.0),
31 width: 1.0,
32 vertical: true,
33 );
34 _sfx = BarGauge(
35 context,
36 'sfx',
37 Vector4(0.95, 0.75, 0.3, 1.0),
38 Vector3(0.0, 0.0, 0.0),
39 width: 1.0,
40 vertical: true,
41 );
42 _out = BarGauge(
43 context,
44 'master',
45 Vector4(0.7, 0.4, 0.8, 1.0),
46 Vector3(2.5, 0.0, 0.0),
47 width: 1.0,
48 vertical: true,
49 );
50 return sceneOf(<SceneNode>[
51 floorNode(context, width: 10.0, depth: 5.0),
52 ..._music.nodes,
53 ..._sfx.nodes,
54 ..._out.nodes,
55 ]);
56 }
58 void _apply() {
59 _mixer
60 ..setVolume(AudioBus.music, musicVolume)
61 ..setVolume(AudioBus.master, masterVolume);
62 }
64 @override
65 void configureView(DemoContext context) {
66 context.orbit
67 ..distance = 9.0
68 ..pitch = 0.3
69 ..yaw = 0.0;
70 context.orbit.target.setValues(0.0, 1.5, 0.0);
71 }
73 @override
74 void update(DemoContext context, double dt) {
75 _apply();
76 final musicGain = _mixer.gainFor(AudioBus.music);
77 final sfxGain = _mixer.gainFor(AudioBus.sfx);
78 _music.set(musicGain);
79 _sfx.set(sfxGain);
80 _out.set(masterVolume);
81 }
83 @override
84 List<DemoControl> controls(DemoContext context) => <DemoControl>[
85 SliderControl(
86 'Music volume',
87 min: 0,
88 max: 1,
89 value: () => musicVolume,
90 onChanged: (double v) => musicVolume = v,
91 ),
92 SliderControl(
93 'Master volume',
94 min: 0,
95 max: 1,
96 value: () => masterVolume,
97 onChanged: (double v) => masterVolume = v,
98 ),
99 ];
101 @override
102 void verify(Scene scene, FrameResult frame) {
103 if (frame.drawCalls < 1) {
104 throw StateError('the mixer marker was not drawn');
105 }
106 _apply();
107 if (_mixer.gainFor(AudioBus.sfx) != 1.0) {
108 throw StateError('a bus nobody configured should stay at full volume');
109 }
110 masterVolume = 0.5;
111 _apply();
112 if ((_mixer.gainFor(AudioBus.music) - 0.3).abs() > 1e-9) {
113 throw StateError(
114 'music at 0.6 through a master turned to 0.5 should play at 0.3',
115 );
116 }
117 }