Mixer buses: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: mixer (line 29), set (line 63), read (line 81).
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;
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';
12/// The `audio-buses` page: a mixer of buses under one master. Its check is
13/// that a bus nobody set stays at full volume and that music at 0.6 under a
14/// master at 0.5 plays at 0.3.
15final class AudioBusesDemo extends ShowcaseDemo {
16 /// The music bus's own volume, 0 to 1.
17 double musicVolume = 0.6;
19 /// The master volume every bus is multiplied by, 0 to 1.
20 double masterVolume = 1.0;
22 late final Mixer _mixer;
23 late final BarGauge _music;
24 late final BarGauge _sfx;
25 late final BarGauge _out;
28 Scene build(DemoContext context) {
29 _mixer = Mixer();
31 _music = BarGauge(
32 context,
33 'music',
34 Vector4(0.45, 0.65, 0.95, 1.0),
35 Vector3(-2.5, 0.0, 0.0),
36 width: 1.0,
37 vertical: true,
38 );
39 _sfx = BarGauge(
40 context,
41 'sfx',
42 Vector4(0.95, 0.75, 0.3, 1.0),
43 Vector3(0.0, 0.0, 0.0),
44 width: 1.0,
45 vertical: true,
46 );
47 _out = BarGauge(
48 context,
49 'master',
50 Vector4(0.7, 0.4, 0.8, 1.0),
51 Vector3(2.5, 0.0, 0.0),
52 width: 1.0,
53 vertical: true,
54 );
55 return sceneOf(<SceneNode>[
56 floorNode(context, width: 10.0, depth: 5.0),
57 ..._music.nodes,
58 ..._sfx.nodes,
59 ..._out.nodes,
60 ]);
61 }
63 void _apply() {
64 _mixer
65 ..setVolume(AudioBus.music, musicVolume)
66 ..setVolume(AudioBus.master, masterVolume);
67 }
70 void configureView(DemoContext context) {
71 context.orbit
72 ..distance = 9.0
73 ..pitch = 0.3
74 ..yaw = 0.0;
75 context.orbit.target.setValues(0.0, 1.5, 0.0);
76 }
79 void update(DemoContext context, double dt) {
80 _apply();
81 final musicGain = _mixer.gainFor(AudioBus.music);
82 final sfxGain = _mixer.gainFor(AudioBus.sfx);
83 _music.set(musicGain);
84 _sfx.set(sfxGain);
85 _out.set(masterVolume);
86 }
89 List<DemoControl> controls(DemoContext context) => <DemoControl>[
90 SliderControl(
91 'Music volume',
92 min: 0,
93 max: 1,
94 value: () => musicVolume,
95 onChanged: (double v) => musicVolume = v,
96 ),
97 SliderControl(
98 'Master volume',
99 min: 0,
100 max: 1,
101 value: () => masterVolume,
102 onChanged: (double v) => masterVolume = v,
103 ),
104 ];
107 void verify(Scene scene, FrameResult frame) {
108 if (frame.drawCalls < 1) {
109 throw StateError('the mixer marker was not drawn');
110 }
111 _apply();
112 if (_mixer.gainFor(AudioBus.sfx) != 1.0) {
113 throw StateError('a bus nobody configured should stay at full volume');
114 }
115 masterVolume = 0.5;
116 _apply();
117 if ((_mixer.gainFor(AudioBus.music) - 0.3).abs() > 1e-9) {
118 throw StateError(
119 'music at 0.6 through a master turned to 0.5 should play at 0.3',
120 );
121 }
122 }
123}