1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*!
Global values that parameters (like volume and playback rate) can be linked to.
Any type that implements [`ModulatorBuilder`] can be added to an audio manager by
using [`AudioManager::add_modulator`](crate::AudioManager::add_modulator).
If needed, you can create custom modulators by implementing the [`ModulatorBuilder`]
and [`Modulator`] traits.
# Why modulators?
Many properties of things in Kira, like the volumes of sounds, can be smoothly
transitioned from one value to another without the use of modulators. Modulators
become handy when:
- You want to control multiple properties of objects in lockstep
- You need to change a property in a way that's more complicated than a simple
transition
# Tweener example
Let's say we have a music track with two layers that play simultaneously:
drums and piano. When the player character enters water, we want the music
to sound "underwater", so we'll fade out the drums and make the piano sound
more muffled using a low-pass filter.
For this situation, a [`tweener`] is appropriate. Tweeners hold a value
that doesn't change until we tell it to, and the value can be smoothly
transitioned.
The tweener is an input value that will generate multiple output values:
the drums volume and piano filter frequency. When the tweener is set to
`0.0`, that represents that the player is not underwater, and when it's
`1.0`, the player is submerged. (These are arbitrary values.)
| Tweener value | Drums volume | Piano filter frequency |
|---------------|--------------|------------------------|
| 0.0 | 1.0 | 20,000 Hz |
| 1.0 | 0.0 | 500 Hz |
First, let's create the tweener:
```no_run
use kira::{
AudioManager, AudioManagerSettings, DefaultBackend,
modulator::tweener::TweenerBuilder,
};
let mut manager = AudioManager::<DefaultBackend>::new(AudioManagerSettings::default())?;
let mut tweener = manager.add_modulator(TweenerBuilder { initial_value: 0.0 })?;
# Result::<(), Box<dyn std::error::Error>>::Ok(())
```
Next, we'll create a mixer track with a low-pass filter effect. The piano will play
on this track so we can make it sound more or less muffled.
```no_run
# use kira::{
# AudioManager, AudioManagerSettings, DefaultBackend,
# modulator::tweener::TweenerBuilder,
# };
use kira::{
effect::filter::FilterBuilder,
Mapping, Value, Easing,
track::TrackBuilder,
};
# let mut manager = AudioManager::<DefaultBackend>::new(AudioManagerSettings::default())?;
# let mut tweener = manager.add_modulator(TweenerBuilder { initial_value: 0.0 })?;
let filter_builder = FilterBuilder::new()
.cutoff(Value::from_modulator(&tweener, Mapping {
input_range: (0.0, 1.0),
output_range: (20_000.0, 500.0),
easing: Easing::Linear,
}));
let mut piano_track = manager.add_sub_track(TrackBuilder::new().with_effect(filter_builder))?;
# Result::<(), Box<dyn std::error::Error>>::Ok(())
```
We use a `Mapping` to map the input values of the tweener to the output values
of the filter cutoff frequency.
Finally, we'll play the sounds:
```no_run
# use kira::{
# AudioManager, AudioManagerSettings, DefaultBackend,
# modulator::tweener::TweenerBuilder,
# effect::filter::FilterBuilder,
# track::TrackBuilder,
# Mapping, Value, Easing,
# Decibels,
# };
use kira::sound::static_sound::StaticSoundData;
# let mut manager = AudioManager::<DefaultBackend>::new(AudioManagerSettings::default())?;
# let mut tweener = manager.add_modulator(TweenerBuilder { initial_value: 0.0 })?;
# let filter_builder = FilterBuilder::new()
# .cutoff(Value::from_modulator(&tweener, Mapping {
# input_range: (0.0, 1.0),
# output_range: (20_000.0, 500.0),
# easing: Easing::Linear,
# }));
# let mut piano_track = manager.add_sub_track(TrackBuilder::new().with_effect(filter_builder))?;
piano_track.play(StaticSoundData::from_file("piano.ogg")?)?;
manager.play(
StaticSoundData::from_file("drums.ogg")?
.volume(Value::from_modulator(&tweener, Mapping {
input_range: (0.0, 1.0),
output_range: (Decibels::IDENTITY, Decibels::SILENCE),
easing: Easing::Linear,
}))
)?;
# Result::<(), Box<dyn std::error::Error>>::Ok(())
```
Notice how we also use a `Mapping` to map the input range of the tweener to the
output values of the sound volume.
Once the player goes underwater, we can smoothly transition the tweener's value from
`0.0` to `1.0`, which will automatically change the drum volume and piano filter frequency.
```no_run
# use kira::{
# AudioManager, AudioManagerSettings, DefaultBackend,
# modulator::tweener::TweenerBuilder,
# track::TrackBuilder,
# effect::filter::FilterBuilder,
# sound::static_sound::StaticSoundData,
# Mapping, Value, Easing,
# Decibels,
# };
use kira::Tween;
use std::time::Duration;
# let mut manager = AudioManager::<DefaultBackend>::new(AudioManagerSettings::default())?;
# let mut tweener = manager.add_modulator(TweenerBuilder { initial_value: 0.0 })?;
# let filter_builder = FilterBuilder::new()
# .cutoff(Value::from_modulator(&tweener, Mapping {
# input_range: (0.0, 1.0),
# output_range: (20_000.0, 500.0),
# easing: Easing::Linear,
# }));
# let mut piano_track = manager.add_sub_track(TrackBuilder::new().with_effect(filter_builder))?;
# piano_track.play(StaticSoundData::from_file("piano.ogg")?)?;
# manager.play(
# StaticSoundData::from_file("drums.ogg")?
# .volume(Value::from_modulator(&tweener, Mapping {
# input_range: (0.0, 1.0),
# output_range: (Decibels::IDENTITY, Decibels::SILENCE),
# easing: Easing::Linear,
# }))
# )?;
tweener.set(1.0, Tween {
duration: Duration::from_secs(3),
..Default::default()
});
# Result::<(), Box<dyn std::error::Error>>::Ok(())
```
*/
use Key;
use crateInfo;
/// Configures a modulator.
/// Produces a stream of values that a parameter can be linked to.
/// A unique identifier for a modulator.
Key);