-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.rs
267 lines (240 loc) · 7.82 KB
/
main.rs
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
mod gui;
use bot::*;
use clap::{Parser, ValueEnum};
use std::{
io::BufReader,
path::{Path, PathBuf},
};
pub mod built_info {
// the file has been placed there by the build script.
include!(concat!(env!("OUT_DIR"), "/built.rs"));
}
#[derive(ValueEnum, Debug, Clone)]
enum ArgExprVariable {
None,
Variation,
Value,
TimeOffset,
}
impl std::fmt::Display for ArgExprVariable {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
#[derive(Parser, Debug)]
#[command(author, version, about = "Run without any arguments to launch GUI.", long_about = None)]
struct Args {
#[arg(long, help = "Path to replay file")]
replay: String,
#[arg(long, help = "Path to clickpack folder")]
clicks: String,
#[arg(
long,
help = "Whether to overlay the noise.* file in the clickpack directory",
default_value_t = false
)]
noise: bool,
#[arg(long, help = "Noise volume multiplier", default_value_t = 1.0)]
noise_volume: f32,
#[arg(long, short, help = "Path to output file", default_value_t = String::from("output.wav"))]
output: String,
#[arg(
long,
help = "Whether to normalize the output audio (make all samples to be in range of 0-1)",
default_value_t = false
)]
normalize: bool,
#[arg(
long,
help = "Whether pitch variation is enabled",
default_value_t = true
)]
pitch_enabled: bool,
#[arg(long, help = "Minimum pitch value", default_value_t = 0.98)]
pitch_from: f32,
#[arg(long, help = "Maximum pitch value", default_value_t = 1.02)]
pitch_to: f32,
#[arg(long, help = "Pitch table step", default_value_t = 0.0005)]
pitch_step: f32,
#[arg(long, help = "Hard click timing", default_value_t = 2.0)]
hard_timing: f32,
#[arg(long, help = "Regular click timing", default_value_t = 0.15)]
regular_timing: f32,
#[arg(
long,
help = "Soft click timing (anything below is microclicks)",
default_value_t = 0.025
)]
soft_timing: f32,
#[arg(long, help = "Enable spam volume changes", default_value_t = true)]
vol_enabled: bool,
#[arg(
long,
help = "Time between actions where clicks are considered spam clicks",
default_value_t = 0.3
)]
spam_time: f32,
#[arg(
long,
help = "The spam volume offset is multiplied by this value",
default_value_t = 0.9
)]
spam_vol_offset_factor: f32,
#[arg(
long,
help = "The spam volume offset is clamped by this value",
default_value_t = 0.3
)]
max_spam_vol_offset: f32,
#[arg(
long,
help = "Enable changing volume of release sounds",
default_value_t = false
)]
change_releases_volume: bool,
#[arg(long, help = "Global clickbot volume factor", default_value_t = 1.0)]
global_volume: f32,
#[arg(
long,
help = "Random variation in volume (+/-) for each click",
default_value_t = 0.2
)]
volume_var: f32,
#[arg(long, help = "Audio framerate", default_value_t = 44100)]
sample_rate: u32,
#[arg(long, help = "Sort actions by time / frame", default_value_t = true)]
sort_actions: bool,
#[arg(long, help = "Volume expression", default_value_t = String::new())]
volume_expr: String,
#[arg(long, help = "The variable that the expression should affect", default_value_t = ArgExprVariable::None)]
expr_variable: ArgExprVariable,
#[arg(
long,
help = "Extend the variation range to negative numbers. Only works for variation",
default_value_t = true
)]
expr_negative: bool,
#[arg(
long,
help = "Cut overlapping sounds. Changes the sound significantly in spams",
default_value_t = false
)]
cut_sounds: bool,
}
#[cfg(windows)]
fn hide_console_window() {
// note that this does not hide the console window when running from a batch file
let debug_exists = std::path::Path::new("zcb3.debug")
.try_exists()
.unwrap_or(false);
if !debug_exists {
unsafe { winapi::um::wincon::FreeConsole() };
}
}
fn main() {
env_logger::init(); // set envvar RUST_LOG=debug to see logs
if std::env::args().len() > 1 {
// we have arguments, probably need to run in cli mode
let args = Args::parse();
log::info!("passed args: {args:?} (running in cli mode)");
run_cli(args);
} else {
log::info!("no args, running gui. pass -h or --help to see help");
// hide console window if running gui
#[cfg(windows)]
{
hide_console_window();
}
gui::run_gui().unwrap();
egui_clickpack_db::cleanup();
}
}
/// Run command line interface
fn run_cli(mut args: Args) {
// open replay
let f = std::fs::File::open(args.replay.clone()).expect("failed to open replay file");
let replay_filename = Path::new(&args.replay)
.file_name()
.unwrap()
.to_str()
.unwrap();
let pitch = if args.pitch_enabled {
Pitch {
from: args.pitch_from,
to: args.pitch_to,
step: args.pitch_step,
}
} else {
Pitch::NO_PITCH
};
let timings = Timings {
hard: args.hard_timing,
regular: args.regular_timing,
soft: args.soft_timing,
};
let vol_settings = VolumeSettings {
enabled: args.vol_enabled,
spam_time: args.spam_time,
spam_vol_offset_factor: args.spam_vol_offset_factor,
max_spam_vol_offset: args.max_spam_vol_offset,
change_releases_volume: args.change_releases_volume,
global_volume: args.global_volume,
volume_var: args.volume_var,
};
// create bot and load clickpack
let mut bot = Bot::new(args.sample_rate);
bot.load_clickpack(&PathBuf::from(args.clicks), pitch)
.expect("failed to load clickpack");
// parse replay
let format = ReplayType::guess_format(replay_filename).expect("failed to guess format");
let replay = Replay::build()
.with_timings(timings)
.with_vol_settings(vol_settings)
.with_extended(true)
.with_sort_actions(args.sort_actions)
.parse(format, BufReader::new(f))
.unwrap();
// try to compile volume expression to check for errors
if !args.volume_expr.is_empty() {
bot.compile_expression(&args.volume_expr)
.expect("failed to compile volume expression");
// check for undefined vars
bot.update_namespace(
&ExtendedAction::default(),
0,
replay.last_frame(),
replay.fps as _,
);
bot.eval_expr().expect("failed to evaluate expression");
}
// render output file
let segment = bot.render_replay(
&replay,
args.noise,
args.noise_volume,
args.normalize,
if !args.volume_expr.is_empty() {
match args.expr_variable {
ArgExprVariable::None => ExprVariable::None,
ArgExprVariable::Value => ExprVariable::Value,
ArgExprVariable::TimeOffset => ExprVariable::TimeOffset,
ArgExprVariable::Variation => ExprVariable::Variation {
negative: args.expr_negative,
},
}
} else {
ExprVariable::None
},
args.pitch_enabled,
args.cut_sounds,
);
// save
if args.output.is_empty() {
log::warn!("output path is empty, defaulting to 'output.wav'");
args.output = String::from("output.wav"); // can't save to empty path
} else if !args.output.ends_with(".wav") {
log::warn!("output path is not a .wav, however the output format is always .wav");
}
let f = std::fs::File::create(args.output).unwrap();
segment.export_wav(f).unwrap();
}