| 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 |
use quasi_router::layout::{FieldKind, Notice, Tone}; |
| 100 |
use quasi_router::{ |
| 101 |
Act, Action, Choice, Field, Figure, Node, RegionKind, Request, Response, RouteError, Router, |
| 102 |
Screen, Slot, |
| 103 |
}; |
| 104 |
|
| 105 |
use audiofiles_core::edit::FadeCurve; |
| 106 |
|
| 107 |
use super::{Editing, Panels}; |
| 108 |
use crate::state::EditResultMode; |
| 109 |
|
| 110 |
|
| 111 |
const BODY: &str = "edit-body"; |
| 112 |
|
| 113 |
|
| 114 |
const CLIPPING: &str = "edit-clipping"; |
| 115 |
|
| 116 |
|
| 117 |
const START: &str = "start"; |
| 118 |
|
| 119 |
const END: &str = "end"; |
| 120 |
|
| 121 |
const GAIN: &str = "gain"; |
| 122 |
|
| 123 |
const MODE: &str = "mode"; |
| 124 |
|
| 125 |
const TARGET: &str = "target"; |
| 126 |
|
| 127 |
const CURVE: &str = "curve"; |
| 128 |
|
| 129 |
const LENGTH: &str = "length"; |
| 130 |
|
| 131 |
const AT: &str = "at"; |
| 132 |
|
| 133 |
const FROM: &str = "from"; |
| 134 |
|
| 135 |
const TO: &str = "to"; |
| 136 |
|
| 137 |
const RESULT: &str = "result"; |
| 138 |
|
| 139 |
const REMEMBER: &str = "remember"; |
| 140 |
|
| 141 |
|
| 142 |
const PEAK: &str = "peak"; |
| 143 |
|
| 144 |
|
| 145 |
const LUFS: &str = "lufs"; |
| 146 |
|
| 147 |
|
| 148 |
const FADE_IN: &str = "in"; |
| 149 |
|
| 150 |
|
| 151 |
pub fn routes(router: Router<Panels<'_>>) -> Router<Panels<'_>> { |
| 152 |
router |
| 153 |
.get("/edit", screen) |
| 154 |
.post("/edit/trim", trim) |
| 155 |
.post("/edit/gain", gain) |
| 156 |
.post("/edit/gain/preview", clipping) |
| 157 |
.post("/edit/normalize", normalize) |
| 158 |
.post("/edit/reverse", reverse) |
| 159 |
.post("/edit/fade", fade) |
| 160 |
.post("/edit/silence/insert", insert_silence) |
| 161 |
.post("/edit/silence/remove", remove_range) |
| 162 |
.post("/edit/play", play) |
| 163 |
.post("/edit/stop", stop) |
| 164 |
.post("/edit/cancel", cancel) |
| 165 |
.post("/edit/undo", undo) |
| 166 |
.post("/edit/result", remember) |
| 167 |
.post("/edit/result/choose", choose) |
| 168 |
.post("/edit/result/discard", discard) |
| 169 |
.post("/edit/batch/normalize", batch_normalize) |
| 170 |
.post("/edit/batch/gain", batch_gain) |
| 171 |
.post("/edit/batch/reverse", batch_reverse) |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
fn screen(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 176 |
Ok(editor(state)?.into()) |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
fn editor(state: &Panels<'_>) -> Result<Screen, RouteError> { |
| 181 |
let sample = subject(state)?; |
| 182 |
let body = if sample.asking { |
| 183 |
asking(&sample) |
| 184 |
} else { |
| 185 |
editing(&sample) |
| 186 |
}; |
| 187 |
Ok(Screen::sidebar_content("Sample Editor").with(body)) |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
fn subject(state: &Panels<'_>) -> Result<Editing, RouteError> { |
| 196 |
state |
| 197 |
.editor |
| 198 |
.subject() |
| 199 |
.ok_or_else(|| RouteError::not_found("nothing is being edited")) |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
fn asking(sample: &Editing) -> Slot { |
| 211 |
Slot::new(BODY, RegionKind::Pane) |
| 212 |
.with(Node::page("Edit Result")) |
| 213 |
.with(Node::text("How should the edited sample be handled?")) |
| 214 |
.with(Node::Form { |
| 215 |
fields: vec![ |
| 216 |
Field::radio(RESULT, "Result", result_modes()).value( |
| 217 |
sample |
| 218 |
.result |
| 219 |
.clone() |
| 220 |
.unwrap_or_else(|| EditResultMode::Sibling.as_value().to_owned()), |
| 221 |
), |
| 222 |
|
| 223 |
|
| 224 |
Field::new(FieldKind::Checkbox, REMEMBER, "Remember my choice") |
| 225 |
.value(if sample.result.is_some() { "on" } else { "" }), |
| 226 |
], |
| 227 |
submit: "Use this".to_owned(), |
| 228 |
action: Action::post("/edit/result/choose"), |
| 229 |
}) |
| 230 |
.with(Node::Act( |
| 231 |
Act::new("Discard edit", Action::post("/edit/result/discard")) |
| 232 |
.tone(Tone::Danger) |
| 233 |
.confirm("Throw this edit away?"), |
| 234 |
)) |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
fn editing(sample: &Editing) -> Slot { |
| 239 |
let mut body = Slot::new(BODY, RegionKind::Pane).with(Node::page(sample.name.clone())); |
| 240 |
|
| 241 |
|
| 242 |
body = body.with(Node::Figure(Figure::new( |
| 243 |
sample.sample_rate.to_string(), |
| 244 |
"Hz", |
| 245 |
))); |
| 246 |
if let Some(duration) = sample.duration { |
| 247 |
body = body.with(Node::Figure(Figure::new( |
| 248 |
format!("{duration:.3}"), |
| 249 |
"seconds", |
| 250 |
))); |
| 251 |
} |
| 252 |
if let Some(peak) = sample.peak_db { |
| 253 |
body = body.with(Node::Figure(Figure::new(format!("{peak:.1}"), "dBFS"))); |
| 254 |
} |
| 255 |
|
| 256 |
if sample.working { |
| 257 |
body = body |
| 258 |
.with(Node::Notice { |
| 259 |
kind: Notice::Banner, |
| 260 |
tone: Tone::Info, |
| 261 |
text: "Applying edit...".to_owned(), |
| 262 |
}) |
| 263 |
.with(Node::Act(Act::new("Cancel", Action::post("/edit/cancel")))); |
| 264 |
} |
| 265 |
|
| 266 |
body = transport(body, sample); |
| 267 |
body = trim_section(body); |
| 268 |
body = levels(body, sample); |
| 269 |
body = transform(body, sample); |
| 270 |
body = silence(body, sample); |
| 271 |
body = result(body, sample); |
| 272 |
batch(body, sample) |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
fn transport(body: Slot, sample: &Editing) -> Slot { |
| 277 |
body.with(Node::Act( |
| 278 |
Act::new( |
| 279 |
if sample.playing { "Pause" } else { "Play" }, |
| 280 |
Action::post("/edit/play"), |
| 281 |
) |
| 282 |
.key("space"), |
| 283 |
)) |
| 284 |
.with(Node::Act(Act::new("Stop", Action::post("/edit/stop")))) |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
fn trim_section(body: Slot) -> Slot { |
| 289 |
body.with(Node::section("Trim")).with(Node::Form { |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
fields: vec![span(START, "Start"), span(END, "End").value("1")], |
| 296 |
submit: "Trim".to_owned(), |
| 297 |
action: Action::post("/edit/trim"), |
| 298 |
}) |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
fn span(name: &str, label: &str) -> Field { |
| 303 |
Field::range(name, label, "0", "1").step("0.001").value("0") |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
fn levels(body: Slot, sample: &Editing) -> Slot { |
| 308 |
let mut body = body.with(Node::section("Levels")); |
| 309 |
|
| 310 |
body = body |
| 311 |
.with(Node::Form { |
| 312 |
fields: vec![ |
| 313 |
Field::range(GAIN, "Gain", "-24", "24") |
| 314 |
.step("0.1") |
| 315 |
.value("0") |
| 316 |
|
| 317 |
|
| 318 |
.changes(Action::post("/edit/gain/preview")), |
| 319 |
], |
| 320 |
submit: "Apply gain".to_owned(), |
| 321 |
action: Action::post("/edit/gain"), |
| 322 |
}) |
| 323 |
.with(Node::Region( |
| 324 |
Slot::new(CLIPPING, RegionKind::Group).with(clips(sample.peak_db, 0.0)), |
| 325 |
)); |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
body.with(Node::Form { |
| 334 |
fields: vec![ |
| 335 |
Field::radio( |
| 336 |
MODE, |
| 337 |
"Normalize by", |
| 338 |
vec![ |
| 339 |
Choice::new(PEAK, "Peak"), |
| 340 |
Choice::new(LUFS, "Loudness (LUFS)"), |
| 341 |
], |
| 342 |
) |
| 343 |
.value(PEAK), |
| 344 |
Field::range(TARGET, "Target", "-24", "0") |
| 345 |
.step("0.1") |
| 346 |
.value("-1"), |
| 347 |
], |
| 348 |
submit: "Normalize".to_owned(), |
| 349 |
action: Action::post("/edit/normalize"), |
| 350 |
}) |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
fn clips(peak: Option<f64>, gain: f64) -> Node { |
| 358 |
let Some(peak) = peak else { |
| 359 |
return Node::empty("Peak unknown until this sample is analysed."); |
| 360 |
}; |
| 361 |
let predicted = peak + gain; |
| 362 |
if predicted <= 0.0 { |
| 363 |
return Node::text(format!("Peak: {peak:.1} dB -> {predicted:.1} dB")); |
| 364 |
} |
| 365 |
Node::Notice { |
| 366 |
kind: Notice::Banner, |
| 367 |
tone: Tone::Danger, |
| 368 |
text: format!("Peak: {peak:.1} dB -> {predicted:.1} dB (clips!)"), |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
fn transform(body: Slot, sample: &Editing) -> Slot { |
| 374 |
body.with(Node::section("Transform")) |
| 375 |
.with(Node::Act(disable_while( |
| 376 |
Act::new("Reverse", Action::post("/edit/reverse")), |
| 377 |
sample, |
| 378 |
))) |
| 379 |
.with(Node::Form { |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
fields: vec![ |
| 385 |
Field::radio( |
| 386 |
FADE_IN, |
| 387 |
"Fade", |
| 388 |
vec![Choice::new("in", "In"), Choice::new("out", "Out")], |
| 389 |
) |
| 390 |
.value("in"), |
| 391 |
Field::range(LENGTH, "Length", "10", "10000") |
| 392 |
.step("10") |
| 393 |
.value("100"), |
| 394 |
Field::select( |
| 395 |
CURVE, |
| 396 |
"Curve", |
| 397 |
FadeCurve::all() |
| 398 |
.into_iter() |
| 399 |
.map(|curve| Choice::new(curve.as_value(), curve.label())) |
| 400 |
.collect(), |
| 401 |
) |
| 402 |
.value(FadeCurve::Linear.as_value()), |
| 403 |
], |
| 404 |
submit: "Apply fade".to_owned(), |
| 405 |
action: Action::post("/edit/fade"), |
| 406 |
}) |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
fn silence(body: Slot, sample: &Editing) -> Slot { |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
let cap = sample.duration.map(|seconds| seconds * 1000.0); |
| 415 |
|
| 416 |
body.with(Node::section("Silence")) |
| 417 |
.with(Node::Form { |
| 418 |
fields: vec![ |
| 419 |
milliseconds(AT, "Insert at", cap).value("0"), |
| 420 |
milliseconds(LENGTH, "Duration", Some(60_000.0)).value("100"), |
| 421 |
], |
| 422 |
submit: "Insert".to_owned(), |
| 423 |
action: Action::post("/edit/silence/insert"), |
| 424 |
}) |
| 425 |
.with(Node::Form { |
| 426 |
fields: vec![ |
| 427 |
milliseconds(FROM, "Remove from", cap).value("0"), |
| 428 |
milliseconds(TO, "to", cap).value("0"), |
| 429 |
], |
| 430 |
submit: "Remove".to_owned(), |
| 431 |
action: Action::post("/edit/silence/remove"), |
| 432 |
}) |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
fn milliseconds(name: &str, label: &str, cap: Option<f64>) -> Field { |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
let mut field = Field::new(FieldKind::Number, name, label).step("10"); |
| 441 |
field.min = Some("0".to_owned()); |
| 442 |
field.max = cap.map(|cap| format!("{cap:.0}")); |
| 443 |
field |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
fn result(body: Slot, sample: &Editing) -> Slot { |
| 448 |
let mut field = |
| 449 |
Field::radio(RESULT, "Result", result_modes()).changes(Action::post("/edit/result")); |
| 450 |
if let Some(chosen) = &sample.result { |
| 451 |
field = field.value(chosen.clone()); |
| 452 |
} |
| 453 |
|
| 454 |
let mut body = body |
| 455 |
.with(Node::section("Result")) |
| 456 |
.with(Node::Field(Box::new(field))); |
| 457 |
|
| 458 |
if sample.result.as_deref() == Some(EditResultMode::Replace.as_value()) { |
| 459 |
body = body.with(Node::Notice { |
| 460 |
kind: Notice::Banner, |
| 461 |
tone: Tone::Warning, |
| 462 |
text: "Replace mode: the original is removed from this vault. Use Create sibling to keep both, or Undo below to revert.".to_owned(), |
| 463 |
}); |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
if let Some(last) = &sample.undoing { |
| 471 |
body = body |
| 472 |
.with(Node::text(format!("Last edit: {last}"))) |
| 473 |
.with(Node::Act(Act::new("Undo", Action::post("/edit/undo")))); |
| 474 |
} |
| 475 |
body |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
fn result_modes() -> Vec<Choice> { |
| 480 |
vec![ |
| 481 |
Choice::new(EditResultMode::Replace.as_value(), "Replace original"), |
| 482 |
Choice::new(EditResultMode::Sibling.as_value(), "Create sibling"), |
| 483 |
] |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
fn batch(body: Slot, sample: &Editing) -> Slot { |
| 488 |
if sample.chosen < 2 { |
| 489 |
return body; |
| 490 |
} |
| 491 |
let chosen = sample.chosen; |
| 492 |
|
| 493 |
body.with(Node::section(format!("Batch: {chosen} samples"))) |
| 494 |
.with(Node::text("Applies to every chosen sample at once.")) |
| 495 |
.with(Node::Form { |
| 496 |
|
| 497 |
|
| 498 |
fields: vec![ |
| 499 |
Field::radio( |
| 500 |
MODE, |
| 501 |
"Normalize by", |
| 502 |
vec![ |
| 503 |
Choice::new(PEAK, "Peak"), |
| 504 |
Choice::new(LUFS, "Loudness (LUFS)"), |
| 505 |
], |
| 506 |
) |
| 507 |
.value(PEAK), |
| 508 |
Field::range(TARGET, "Target", "-24", "0") |
| 509 |
.step("0.1") |
| 510 |
.value("-1"), |
| 511 |
], |
| 512 |
submit: format!("Normalize {chosen} samples"), |
| 513 |
action: Action::post("/edit/batch/normalize"), |
| 514 |
}) |
| 515 |
.with(Node::Form { |
| 516 |
fields: vec![ |
| 517 |
Field::range(GAIN, "Gain", "-24", "24") |
| 518 |
.step("0.1") |
| 519 |
.value("0"), |
| 520 |
], |
| 521 |
submit: format!("Apply gain to {chosen} samples"), |
| 522 |
action: Action::post("/edit/batch/gain"), |
| 523 |
}) |
| 524 |
.with(Node::Act( |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
reverse_batch(chosen), |
| 530 |
)) |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
fn reverse_batch(chosen: usize) -> Act { |
| 535 |
let act = Act::new( |
| 536 |
format!("Reverse {chosen} samples"), |
| 537 |
Action::post("/edit/batch/reverse"), |
| 538 |
); |
| 539 |
if chosen > REGRET { |
| 540 |
act.confirm(format!("Reverse {chosen} samples?")) |
| 541 |
} else { |
| 542 |
act |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
const REGRET: usize = 10; |
| 552 |
|
| 553 |
|
| 554 |
fn disable_while(act: Act, sample: &Editing) -> Act { |
| 555 |
if sample.working { act.disabled() } else { act } |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
fn trim(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 560 |
let sample = subject(state)?; |
| 561 |
let start = fraction(&request, START)?; |
| 562 |
let end = fraction(&request, END)?; |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
if start >= end { |
| 567 |
return Err(RouteError::not_found("start must be before end")); |
| 568 |
} |
| 569 |
state.editor.trim(start, end); |
| 570 |
answered(state, format!("Trimming {}.", sample.name)) |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
fn gain(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 575 |
subject(state)?; |
| 576 |
let db = decimal(&request, GAIN)?; |
| 577 |
state.editor.gain(db); |
| 578 |
answered(state, format!("Applying {db:.1} dB.")) |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
fn clipping(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 586 |
let sample = subject(state)?; |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
let gain = request |
| 591 |
.payload |
| 592 |
.get(GAIN) |
| 593 |
.and_then(|value| value.parse().ok()) |
| 594 |
.unwrap_or(0.0); |
| 595 |
Ok(Response::fragment(CLIPPING, clips(sample.peak_db, gain))) |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
fn normalize(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 600 |
subject(state)?; |
| 601 |
let (peak, target) = normalizing(&request)?; |
| 602 |
state.editor.normalize(peak, target); |
| 603 |
answered(state, "Normalizing.".to_owned()) |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
fn reverse(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 608 |
subject(state)?; |
| 609 |
state.editor.reverse(); |
| 610 |
answered(state, "Reversing.".to_owned()) |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
fn fade(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 615 |
subject(state)?; |
| 616 |
let fading_in = request.payload.get(FADE_IN).unwrap_or("in") != "out"; |
| 617 |
let ms = decimal(&request, LENGTH)?; |
| 618 |
let curve = request.payload.get(CURVE).unwrap_or_default(); |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
let curve = |
| 623 |
FadeCurve::from_value(curve).ok_or_else(|| RouteError::not_found("no such fade curve"))?; |
| 624 |
state.editor.fade(fading_in, ms, curve.as_value()); |
| 625 |
answered( |
| 626 |
state, |
| 627 |
format!( |
| 628 |
"Fading {} over {ms:.0} ms.", |
| 629 |
if fading_in { "in" } else { "out" } |
| 630 |
), |
| 631 |
) |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
fn insert_silence(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 636 |
subject(state)?; |
| 637 |
let at = decimal(&request, AT)?; |
| 638 |
let ms = decimal(&request, LENGTH)?; |
| 639 |
if ms <= 0.0 { |
| 640 |
return Err(RouteError::not_found("silence has to be longer than that")); |
| 641 |
} |
| 642 |
state.editor.insert_silence(at, ms); |
| 643 |
answered(state, format!("Inserting {ms:.0} ms.")) |
| 644 |
} |
| 645 |
|
| 646 |
|
| 647 |
fn remove_range(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 648 |
subject(state)?; |
| 649 |
let from = decimal(&request, FROM)?; |
| 650 |
let to = decimal(&request, TO)?; |
| 651 |
if from >= to { |
| 652 |
return Err(RouteError::not_found( |
| 653 |
"the span has to start before it ends", |
| 654 |
)); |
| 655 |
} |
| 656 |
state.editor.remove_range(from, to); |
| 657 |
answered(state, format!("Removing {:.0} ms.", to - from)) |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
fn play(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 662 |
subject(state)?; |
| 663 |
state.editor.play(); |
| 664 |
Ok(editor(state)?.into()) |
| 665 |
} |
| 666 |
|
| 667 |
|
| 668 |
fn stop(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 669 |
subject(state)?; |
| 670 |
state.editor.stop(); |
| 671 |
Ok(editor(state)?.into()) |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
fn cancel(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 676 |
subject(state)?; |
| 677 |
state.editor.cancel(); |
| 678 |
answered(state, "Edit cancelled.".to_owned()) |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
fn undo(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 683 |
let sample = subject(state)?; |
| 684 |
|
| 685 |
|
| 686 |
let last = sample |
| 687 |
.undoing |
| 688 |
.ok_or_else(|| RouteError::not_found("there is nothing to undo"))?; |
| 689 |
state.editor.undo(); |
| 690 |
answered(state, format!("Undoing {last}.")) |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
fn remember(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 695 |
subject(state)?; |
| 696 |
let mode = mode_named(&request)?; |
| 697 |
state.editor.remember(mode.as_value()); |
| 698 |
Ok(editor(state)?.into()) |
| 699 |
} |
| 700 |
|
| 701 |
|
| 702 |
fn choose(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 703 |
subject(state)?; |
| 704 |
let mode = mode_named(&request)?; |
| 705 |
let remember = request.payload.get(REMEMBER).unwrap_or_default() == "on"; |
| 706 |
state.editor.choose(mode.as_value(), remember); |
| 707 |
answered(state, "Edit applied.".to_owned()) |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
fn discard(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 712 |
subject(state)?; |
| 713 |
state.editor.discard(); |
| 714 |
answered(state, "Edit result discarded.".to_owned()) |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
fn batch_normalize(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 719 |
let chosen = batched(state)?; |
| 720 |
let (peak, target) = normalizing(&request)?; |
| 721 |
state.editor.batch_normalize(peak, target); |
| 722 |
answered(state, format!("Normalizing {chosen} samples.")) |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
fn batch_gain(state: &Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 727 |
let chosen = batched(state)?; |
| 728 |
let db = decimal(&request, GAIN)?; |
| 729 |
state.editor.batch_gain(db); |
| 730 |
answered(state, format!("Applying {db:.1} dB to {chosen} samples.")) |
| 731 |
} |
| 732 |
|
| 733 |
|
| 734 |
fn batch_reverse(state: &Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 735 |
let chosen = batched(state)?; |
| 736 |
state.editor.batch_reverse(); |
| 737 |
answered(state, format!("Reversing {chosen} samples.")) |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
fn batched(state: &Panels<'_>) -> Result<usize, RouteError> { |
| 745 |
let chosen = subject(state)?.chosen; |
| 746 |
if chosen < 2 { |
| 747 |
return Err(RouteError::not_found("choose more than one sample first")); |
| 748 |
} |
| 749 |
Ok(chosen) |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
fn normalizing(request: &Request) -> Result<(bool, f64), RouteError> { |
| 758 |
let peak = request.payload.get(MODE).unwrap_or(PEAK) != LUFS; |
| 759 |
let target = decimal(request, TARGET)?; |
| 760 |
let allowed = if peak { -24.0..=0.0 } else { -24.0..=-6.0 }; |
| 761 |
if !allowed.contains(&target) { |
| 762 |
return Err(RouteError::not_found(if peak { |
| 763 |
"a peak target runs from -24 to 0 dBFS" |
| 764 |
} else { |
| 765 |
"a loudness target runs from -24 to -6 LUFS" |
| 766 |
})); |
| 767 |
} |
| 768 |
Ok((peak, target)) |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
fn mode_named(request: &Request) -> Result<EditResultMode, RouteError> { |
| 773 |
EditResultMode::from_value(request.payload.get(RESULT).unwrap_or_default()) |
| 774 |
.ok_or_else(|| RouteError::not_found("no such result mode")) |
| 775 |
} |
| 776 |
|
| 777 |
|
| 778 |
fn fraction(request: &Request, name: &str) -> Result<f32, RouteError> { |
| 779 |
let value: f32 = request |
| 780 |
.payload |
| 781 |
.get(name) |
| 782 |
.unwrap_or_default() |
| 783 |
.parse() |
| 784 |
.map_err(|_| RouteError::not_found("that is not a position"))?; |
| 785 |
if !(0.0..=1.0).contains(&value) { |
| 786 |
return Err(RouteError::not_found("a position runs from 0 to 1")); |
| 787 |
} |
| 788 |
Ok(value) |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
fn decimal(request: &Request, name: &str) -> Result<f64, RouteError> { |
| 793 |
request |
| 794 |
.payload |
| 795 |
.get(name) |
| 796 |
.unwrap_or_default() |
| 797 |
.parse() |
| 798 |
.map_err(|_| RouteError::not_found("that is not a number")) |
| 799 |
} |
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
fn answered(state: &Panels<'_>, say: String) -> Result<Response, RouteError> { |
| 807 |
Ok(Response::from(editor(state)?).toast(Tone::Success, say)) |
| 808 |
} |
| 809 |
|