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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//! Types and traits for managing per-sample effects.
use crate;
use ;
/// An effect applied to a sample player.
///
/// This targets the [`SampleEffects`] component.
;
/// A serial chain of effects applied on a per-sampler basis.
///
/// These effects -- audio nodes with at least two inputs and outputs
/// -- are applied in the order they're spawned. There are two main
/// ways to use [`SampleEffects`].
///
/// ## Dynamic pools
///
/// When applied to a [`SamplePlayer`][crate::prelude::SamplePlayer] without an explicit pool assignment,
/// a pool is dynamically created according to the shape of the effects.
/// ```
/// # use bevy::prelude::*;
/// # use bevy_seedling::prelude::*;
/// # fn dynamic(mut commands: Commands, server: Res<AssetServer>) {
/// // Creates a pool on-demand with per-sample volume control.
/// commands.spawn((
/// SamplePlayer::new(server.load("my_sample.wav")),
/// sample_effects![VolumeNode::default()],
/// ));
///
/// // Since this shape already exists, this sample will be queued in the
/// // same dynamic pool we just created.
/// commands.spawn((
/// SamplePlayer::new(server.load("my_other_sample.wav")),
/// // You can always provide arbitrary initial values.
/// sample_effects![VolumeNode {
/// volume: Volume::Decibels(-6.0),
/// ..Default::default()
/// }],
/// ));
/// # }
/// ```
/// By default, these pools are spawned with relatively few samplers,
/// spawning more as demand grows up to some maximum bound.
///
/// Dynamic pools are convenient, especially for prototyping, but
/// may become cumbersome as projects grow. If you'd prefer to disable
/// dynamic pools entirely, insert a [`DefaultPoolSize`] resource of `0..=0`.
///
/// [`DefaultPoolSize`]: crate::prelude::DefaultPoolSize
///
/// ## Static pools
///
/// When applied to a [`SamplerPool`][crate::prelude::SamplerPool], [`SampleEffects`]
/// serves as a template for all samples played in the pool.
/// ```
/// # use bevy::prelude::*;
/// # use bevy_seedling::prelude::*;
/// # fn pools(mut commands: Commands, server: Res<AssetServer>) {
/// #[derive(PoolLabel, Clone, PartialEq, Eq, Debug, Hash)]
/// struct MusicPool;
///
/// // Creates a pool where all samplers have volume and spatial processors.
/// commands.spawn((
/// SamplerPool(MusicPool),
/// sample_effects![
/// // The defaults established here will be applied to each
/// // sample player unless explicitly overwritten.
/// VolumeNode {
/// volume: Volume::Decibels(-3.0),
/// ..Default::default()
/// },
/// SpatialBasicNode::default(),
/// ],
/// ));
///
/// // Samples don't need to mention effects.
/// commands.spawn((MusicPool, SamplePlayer::new(server.load("track_one.wav"))));
///
/// // Overwriting just a subset works, too.
/// commands.spawn((
/// SamplePlayer::new(server.load("my_other_sample.wav")),
/// sample_effects![VolumeNode {
/// volume: Volume::Decibels(-6.0),
/// ..Default::default()
/// }],
/// ));
/// # }
/// ```
///
/// Samples played in a pool don't need to respect the ordering
/// or presence of effects; when a sample is queued, missing effects
/// are inserted and the order of effects is corrected. Consequently,
/// the exact index of a particular effect within [`SampleEffects`]
/// may change, so the [`EffectsQuery`] trait is the best way to reliably access
/// them.
///
/// ## Notes
///
/// Rather than existing in the audio graph directly, nodes with [`EffectOf`]
/// components serve as plain-old-data baselines.
/// When a sample is queued in a particular pool, the fully-connected
/// nodes on the selected sampler start tracking the entities in the
/// sample's [`SampleEffects`].
;
pub use recursive_spawn;
/// Returns a spawnable list of [`SampleEffects`].
///
/// This is equivalent to `related!(SampleEffects[/* ... */])`.
///
/// [`SampleEffects`] represents a collection of audio nodes
/// connected in serial, in the order they're spawned, to the
/// underlying sampler node. As effects, `bevy_seedling` expects
/// each node to have at least two input and output channels.
;
}
/// Errors for effects queries.
///
/// Since these queries require direct fetching with `get` and
/// related methods, [`EffectsQuery`] are not infallible.
/// An extension trait for simplifying [`SampleEffects`] queries.
///
/// Since Bevy does not yet support sophisticated relationship queries,
/// it can be cumbersome to manually join relationship-dependent queries.
/// This trait eases the burden with a few convenience methods.
///
/// For example, if you want to query over all [`LowPassNode`]s whose
/// sample entity contains some marker:
///
/// [`LowPassNode`]: crate::prelude::LowPassNode
/// ```
/// # use bevy::prelude::*;
/// # use bevy_seedling::prelude::*;
/// # fn example(mut commands: Commands, server: Res<AssetServer>) {
/// #[derive(Component)]
/// struct Marker;
///
/// commands.spawn((
/// Marker,
/// SamplePlayer::new(server.load("my_sample.wav")),
/// sample_effects![LowPassNode::default()],
/// ));
///
/// fn effects_of_marker(
/// samples: Query<&SampleEffects, With<Marker>>,
/// low_pass: Query<&LowPassNode>,
/// ) -> Result {
/// for effects in samples {
/// let low_pass = low_pass.get_effect(effects)?;
/// // ...
/// }
///
/// Ok(())
/// }
/// # }
/// ```
///
/// When Bevy's related queries story matures, this trait will likely be deprecated.