edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching0 forksRust18.3 MiBef5832fTahinli3 months ago597 commits
| 1 | //! Where a clip goes quiet: RMS levels off the same read-only decode path the |
| 2 | //! waveform takes, and the stretches of them nobody wants to watch. |
| 3 | //! |
| 4 | //! Three steps, kept apart so only the first one costs a decode: [`levels`] |
| 5 | //! turns a *stretch* of a file into one loudness number per 20 ms window, |
| 6 | //! [`regions`] turns those numbers plus a [`Settings`] into silent stretches of |
| 7 | //! seconds from where that read began, and [`timeline_regions`] puts those on |
| 8 | //! the timeline through one clip's own range and rate. A card tweaking a |
| 9 | //! threshold re-runs the last two and decodes nothing. |
| 10 | //! |
| 11 | //! The stretch is the *clip's*, not the file's: a take cut in half is half a |
| 12 | //! decode, and the half nobody is looking at is never read (which is what a |
| 13 | //! person means by "scan this clip"). |
| 14 | //! |
| 15 | //! Loudness in **dBFS**, which is the unit every tool that does this speaks |
| 16 | //! (ffmpeg's `silencedetect` noise floor, auto-editor's threshold) -- and RMS |
| 17 | //! rather than the peak [`crate::waveform::peaks`] draws, because a single |
| 18 | //! sample spike in a breath is not speech and a peak meter cannot tell them |
| 19 | //! apart. |
| 20 | |
| 21 | use Path; |
| 22 | use ; |
| 23 | |
| 24 | use crateAudioSession; |
| 25 | use crate; |
| 26 | |
| 27 | /// Loudness windows per second: 50, i.e. 20 ms each -- short enough to place a |
| 28 | /// cut inside one frame at any sane rate, long enough that one glottal pulse is |
| 29 | /// not a silence of its own. |
| 30 | pub const WINDOWS_PER_SEC: u32 = 50; |
| 31 | |
| 32 | /// What a window with no samples in it, or with digital nothing in it, reads |
| 33 | /// as: a floor rather than `-inf`, so a level is always a number a comparison |
| 34 | /// and a `Display` can take. |
| 35 | pub const SILENT_DB: f32 = -120.; |
| 36 | |
| 37 | /// What counts as a silence worth cutting. Seconds, not frames: detection runs |
| 38 | /// on the *sound*, whose sample rate is its own business, and the frame grid |
| 39 | /// is applied once at the end ([`timeline_regions`]). |
| 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 | /// One RMS level in dBFS per [`WINDOWS_PER_SEC`]th of a second of `stream` of |
| 71 | /// `path`, over the half-open `range` of **source seconds** -- the same decode |
| 72 | /// [`crate::waveform::peaks`] runs and with the same contract: channels folded |
| 73 | /// together, `Ok(None)` for a file with **no audio track at all**, which is a |
| 74 | /// source to refuse by name rather than one long silence to cut. |
| 75 | /// |
| 76 | /// `levels[0]` is the window at `range.0`, never the window at media time 0: |
| 77 | /// what is read is what is asked for, so a clip that plays a minute of a film |
| 78 | /// costs a minute of decode and the rest of the file is never touched. Whole |
| 79 | /// file is `(0., f64::INFINITY)`. |
| 80 | /// |
| 81 | /// Linear in the range's length at ~1700x realtime, so a ten-minute take scans |
| 82 | /// in well under a second; a caller caches it per source, stream and range, |
| 83 | /// because the settings above are all applied to the result and none of them |
| 84 | /// needs a second decode. |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | /// What a running [`levels_with_progress`] says about itself while it runs, and |
| 94 | /// the one word that stops it. Tenths of a second rather than a fraction: how |
| 95 | /// far into the sound the decode has come is a fact the moment it starts, where |
| 96 | /// a percentage needs a duration the header does not always carry. |
| 97 | /// |
| 98 | /// Shared by an [`Arc`](std::sync::Arc) between the worker and whatever is |
| 99 | /// drawing it, the way an export's progress is. |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | /// [`levels`] reporting where it has come to and stopping when it is told to. |
| 115 | /// |
| 116 | /// A **cancelled scan returns what it had got to**, which is a prefix of the |
| 117 | /// track and not an answer: the caller sets [`Progress::cancel`], so the caller |
| 118 | /// is the one that must check it before believing the result. |
| 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 | /// Mean square -> dBFS, floored at [`SILENT_DB`]: full scale is 1.0, so a |
| 178 | /// window of digital zero is `-inf` and is reported as the floor instead. |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | /// The silent stretches of a `levels` track, as half-open `(from, to)` seconds |
| 191 | /// **from where the read began**, in order and never overlapping. |
| 192 | /// |
| 193 | /// Pure arithmetic: this is what a card re-runs on every keystroke. The three |
| 194 | /// settings are applied in the order a person means them -- quiet runs first, |
| 195 | /// then the short ones forgiven ([`Settings::min_silence`]), then a |
| 196 | /// [`Settings::padding`] left at both ends so the cut never touches speech, and |
| 197 | /// last the slivers too short to keep ([`Settings::min_keep`]) swallowed by the |
| 198 | /// silences around them. Padding before the merge on purpose: what the merge |
| 199 | /// measures is what would actually survive the cut. |
| 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 | /// Where those source stretches sit on the timeline, as `(start, len)` frame |
| 233 | /// pairs of the lane `clip` is on -- what a preview marks and what |
| 234 | /// [`crate::Project::cut_regions`] cuts. |
| 235 | /// |
| 236 | /// `regions` are seconds from the clip's **in point**, which is where |
| 237 | /// [`levels`] was asked to start reading; they land at `in_frame` plus that. |
| 238 | /// Only the part of each region the clip actually plays survives -- a region |
| 239 | /// running past `out_frame` is trimmed to it and one starting beyond it is |
| 240 | /// dropped, so a short read or a rounding at the tail cannot mark a frame the |
| 241 | /// clip does not have. Positions go through the clip's **own rate** |
| 242 | /// ([`Speed::timeline_at`]), so a silence in a 2x clip marks the frames it is |
| 243 | /// really on. |
| 244 | /// |
| 245 | /// Both edges are rounded **inward** -- start up, end down -- so a frame that |
| 246 | /// is partly speech stays outside the region at every rate. An empty remainder |
| 247 | /// is dropped rather than returned as a zero-length cut. |
| 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 | /// [`Speed::timeline_at`] floored instead of ceiled: which timeline frame of a |
| 275 | /// clip a source frame lands on, rounded the way an *end* has to be so a |
| 276 | /// region never eats the frame speech starts on. The ceil is the right rounding |
| 277 | /// for a start and for playback's stamps, and this is its partner. |
| 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 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 |