edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits
| 1 | //! The master limiter: one ceiling the whole mix is held under, applied where |
| 2 | //! the lanes are summed and nowhere else ([`crate::audio::mix`]). |
| 3 | //! |
| 4 | //! A mix is an add, and two lanes at full scale add past full scale: what used |
| 5 | //! to happen there was a hard `clamp`, which is a squared-off waveform and the |
| 6 | //! worst-sounding thing an editor can do to a hot timeline. This holds the sum |
| 7 | //! down *before* it reaches the clamp instead, and the clamp stays behind it as |
| 8 | //! the backstop it always was. |
| 9 | //! |
| 10 | //! Per sample, no FFT, no allocation while it runs: |
| 11 | //! |
| 12 | //! * **True peak.** The ceiling is a true-peak ceiling (dBTP), so the peak |
| 13 | //! detector is the one the meter measures with ([`crate::loudness::TruePeak`], |
| 14 | //! the same filter), 8x oversampled rather than the standard's 4x: a 4x grid |
| 15 | //! read up to 1.6 dB under the waveform on dense high-frequency material, and |
| 16 | //! the export then overshot the ceiling by that much. A sample peak limiter |
| 17 | //! lets a waveform overshoot between its samples; this one holds the |
| 18 | //! reconstructed waveform instead. |
| 19 | //! * **Lookahead.** The mix is written into a delay line [`LOOKAHEAD_MS`] long |
| 20 | //! and read out of it that much later, so the gain a peak needs is already in |
| 21 | //! force by the time the peak itself is emitted. No attack overshoot -- the |
| 22 | //! first sample over the ceiling is the first sample held under it, which is |
| 23 | //! what makes the peak of an exported file a *measurement* and not a hope. |
| 24 | //! The detector sees its peak `TP_TAPS / 2` frames before the samples it |
| 25 | //! describes, so the lookahead is what lets it act in time. |
| 26 | //! * **Linked channels.** One gain for the frame, off the loudest channel in |
| 27 | //! it: a per-channel gain would move the stereo image every time one side |
| 28 | //! peaked. |
| 29 | //! * **Soft knee.** [`KNEE_DB`] wide and centred on the ceiling, so the gain |
| 30 | //! comes in gradually instead of switching on at one sample and off at the |
| 31 | //! next. The knee's own curve never lets the output past the ceiling (at the |
| 32 | //! centre it is already `KNEE_DB/8` under it), which is what keeps the |
| 33 | //! guarantee a hard knee would give. |
| 34 | //! * **Release**, not a per-sample snap back: `RELEASE_MS` of exponential |
| 35 | //! recovery, so one loud transient does not pump the whole bar around it. |
| 36 | //! |
| 37 | //! The latency is *taken out again*, not paid: the first `lookahead` frames of |
| 38 | //! output -- the empty delay line -- are dropped, so what comes out is sample |
| 39 | //! aligned with what went in and the sound stays locked to the picture. The |
| 40 | //! last `lookahead` frames are still in the line when the mix runs out; [`drain`] |
| 41 | //! plays them out, so the tail is kept and the cut at the end of the stream is |
| 42 | //! limited like everything before it. |
| 43 | |
| 44 | use crate; |
| 45 | |
| 46 | /// How far ahead the gain looks. 2 ms is ~96 frames at 48 kHz: long enough to |
| 47 | /// ride a transient in smoothly, short enough that the tail it eats is nothing. |
| 48 | /// Never less than the detector's window ([`TP_TAPS`] frames), whatever the rate. |
| 49 | const LOOKAHEAD_MS: f32 = 2.0; |
| 50 | /// How wide the knee is, centred on the ceiling. |
| 51 | const KNEE_DB: f32 = 6.0; |
| 52 | /// How fast the gain comes back once the loud part is over. |
| 53 | const RELEASE_MS: f32 = 120.0; |
| 54 | /// How far under its ceiling the limiter holds the sound, in dB. The 8x detector |
| 55 | /// agrees with ffmpeg's `ebur128=peak=true` to a few hundredths of a dB on the |
| 56 | /// test material (white noise, pink noise, speech and music, a test tone), but |
| 57 | /// ffmpeg's own resampler reads a little higher on some of it: a speech-and-music |
| 58 | /// file held exactly at -1 dBTP read -0.974 there. The margin is that difference, |
| 59 | /// taken from the ceiling the limiter holds to and not from the setting a user |
| 60 | /// names, so the reply reads the measured level and the setting stays what was asked. |
| 61 | const SAFETY_DB: f32 = 0.1; |
| 62 | |
| 63 | /// What a project's master limiter is set to: a ceiling in dBFS and whether it |
| 64 | /// is in circuit at all. Off by default -- a timeline nobody has asked to limit |
| 65 | /// mixes exactly as it always did, down to the bit. |
| 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 | /// dBFS as an amplitude. `db_to_linear(0.0)` is exactly 1.0, which is what |
| 109 | /// keeps a flat setting a bit-exact passthrough. |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | /// One running limiter: the delay line, the peak window and the gain. |
| 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 | |
| 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 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 |