Repository navigation
Expand file tree
/
Copy pathbleed.rs
More file actions
194 lines (182 loc) · 5.69 KB
/
Copy pathbleed.rs
File metadata and controls
194 lines (182 loc) · 5.69 KB
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
use bevy_app::{App, Startup};
use bevy_ecs::prelude::*;
use colorgrad::{Color, GradientBuilder};
use fanim::prelude::*;
fn main() {
_ = std::fs::remove_dir_all("data");
_ = std::fs::create_dir_all("data");
let scale = 32;
App::default()
.add_plugins((
AudioPlugin,
ParamPlugin,
EncoderPlugin,
AnimationPlugin,
RenderPlugin {
width: 16 * scale,
height: 9 * scale,
super_samples: 1,
},
))
.add_systems(Startup, spawn_animation(30))
.set_runner(fanim::runner)
.run();
}
#[derive(Default, Component)]
struct Rms {
smoothing: f32,
state: f32,
}
impl Rms {
pub fn new(sample_rate: SampleRate, timescale: f32) -> Self {
Self {
smoothing: 0.5f32.powf(1.0 / (timescale * *sample_rate as f32)),
state: 0.0,
}
}
pub fn process(&mut self, sample: f32) {
self.state = self.state * self.smoothing + sample * sample * (1.0 - self.smoothing);
}
pub fn sample(&self) -> f32 {
self.state.sqrt()
}
}
#[derive(Default, Component)]
struct Peak {
max: f32,
}
impl Peak {
pub fn process(&mut self, sample: f32) {
self.max = self.max.max(sample.abs());
}
}
pub fn blood_red() -> Palette {
let blood = GradientBuilder::new()
.colors(&[
Color::from_rgba8(0, 0, 0, 255),
Color::from_rgba8(20, 0, 0, 255),
Color::from_rgba8(60, 0, 0, 255),
Color::from_rgba8(120, 0, 0, 255),
Color::from_rgba8(180, 10, 10, 255),
Color::from_rgba8(220, 40, 40, 255),
Color::from_rgba8(255, 80, 80, 255),
])
.build::<colorgrad::LinearGradient>()
.unwrap();
gradient_palette(&blood)
}
fn color_scale_rms(
mut color_scale: Single<&mut ColorScale>,
mut rms: Single<&mut Rms>,
samples: Single<&Samples>,
) {
for (l, r) in samples.iter() {
rms.process((*l + *r) / 2.0);
}
***color_scale = rms.sample() * 50.0;
}
fn palette_peak(
mut rotation: Single<&mut Rotation>,
peak: Single<(&mut Peak, &mut LowPass)>,
samples: Single<&Samples>,
delta: Single<&DeltaTime>,
) {
let (mut peak, mut peak_lp) = peak.into_inner();
for (l, r) in samples.iter() {
peak.process((*l + *r) / 2.0);
}
let peak = peak_lp.process(peak.max);
***rotation += peak * ***delta;
}
fn spawn_animation(fps: usize) -> impl FnMut(Commands) -> bevy_ecs::error::Result {
move |mut commands: Commands| {
commands.spawn(AudioPlayer::new("assets/bleed.mp3"));
commands.spawn(Rms::new(SampleRate(44_100), 0.01));
commands.spawn((Peak::default(), LowPass::new(100.0, SampleRate(44_100))));
let target = commands
.spawn((
VideoEncoder::new("out.mp4", "data", 44_100, fps)?,
Params::default(),
Mandelbrot::default(),
blood_red(),
BurningShip(1.0),
Exponent(3.0),
Iterations(10_000),
View {
x: 0.0,
y: 0.0,
z: 16.0,
},
CPlane { x: -0.4, y: 0.6 },
))
.id();
commands
.spawn(AnimationTarget(target))
.insert(animations![parallel![
(system(color_scale_rms), Duration(10.0)),
(system(palette_peak), Duration(40.0)),
animations![
(
Keyframe(View {
x: 0.0,
y: 0.0,
z: 2.0,
}),
EaseFunction::SineIn,
Duration(9.0)
),
(
Keyframe(View {
x: 0.0,
y: 0.0,
z: 1.25,
}),
EaseFunction::SineOut,
Duration(2.0)
),
],
animations![
(
Keyframe(Exponent(5.0)),
EaseFunction::SineInOut,
Duration(9.0)
),
(
Keyframe(Julia(1.0)),
Keyframe(ColorRotation(0.25)),
Keyframe(Pickover(1.0)),
// Keyframe(BurningShip(0.0)),
Keyframe(Exponent(4.0)),
Keyframe(ColorScale(1.0)),
EaseFunction::ExponentialInOut,
Duration(2.0)
),
(
Keyframe(Exponent(2.0)),
EaseFunction::SineInOut,
Duration(6.0)
),
(
Keyframe(CPlane { x: -1.729, y: 0.0 }),
EaseFunction::SineInOut,
Duration(6.0)
),
(
Keyframe(CPlane {
x: -0.752,
y: -1.131
}),
EaseFunction::SineInOut,
Duration(6.0)
),
(
Keyframe(Exponent(4.0)),
Keyframe(ColorScale(2.0)),
EaseFunction::SineInOut,
Duration(6.0)
)
]
]]);
Ok(())
}
}