edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching0 forksRust18.3 MiBcc667e6Tahinli3 months ago285 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 file into one loudness number per 20 ms window, [`regions`] turns |
| 6 | //! those numbers plus a [`Settings`] into silent stretches of *source* seconds, |
| 7 | //! and [`timeline_regions`] puts those on the timeline through one clip's own |
| 8 | //! range and rate. A card tweaking a threshold re-runs the last two and decodes |
| 9 | //! nothing. |
| 10 | //! |
| 11 | //! Loudness in **dBFS**, which is the unit every tool that does this speaks |
| 12 | //! (ffmpeg's `silencedetect` noise floor, auto-editor's threshold) -- and RMS |
| 13 | //! rather than the peak [`crate::waveform::peaks`] draws, because a single |
| 14 | //! sample spike in a breath is not speech and a peak meter cannot tell them |
| 15 | //! apart. |
| 16 | |
| 17 | use Path; |
| 18 | |
| 19 | use crateAudioSession; |
| 20 | use crate; |
| 21 | |
| 22 | /// Loudness windows per second: 50, i.e. 20 ms each -- short enough to place a |
| 23 | /// cut inside one frame at any sane rate, long enough that one glottal pulse is |
| 24 | /// not a silence of its own. |
| 25 | pub const WINDOWS_PER_SEC: u32 = 50; |
| 26 | |
| 27 | /// What a window with no samples in it, or with digital nothing in it, reads |
| 28 | /// as: a floor rather than `-inf`, so a level is always a number a comparison |
| 29 | /// and a `Display` can take. |
| 30 | pub const SILENT_DB: f32 = -120.; |
| 31 | |
| 32 | /// What counts as a silence worth cutting. Seconds, not frames: detection runs |
| 33 | /// on the *source*, whose sample rate is its own business, and the frame grid |
| 34 | /// is applied once at the end ([`timeline_regions`]). |
| 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 | /// One RMS level in dBFS per [`WINDOWS_PER_SEC`]th of a second of `stream` of |
| 66 | /// `path`, from media time 0 -- the same decode [`crate::waveform::peaks`] runs |
| 67 | /// and with the same contract: channels folded together, `Ok(None)` for a file |
| 68 | /// with **no audio track at all**, which is a source to refuse by name rather |
| 69 | /// than one long silence to cut. |
| 70 | /// |
| 71 | /// Linear in source length at ~1700x realtime, so a ten-minute take scans in |
| 72 | /// well under a second; a caller caches it per source and stream, because the |
| 73 | /// settings above are all applied to the result and none of them needs a second |
| 74 | /// decode. |
| 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 | /// Mean square -> dBFS, floored at [`SILENT_DB`]: full scale is 1.0, so a |
| 106 | /// window of digital zero is `-inf` and is reported as the floor instead. |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | /// The silent stretches of a `levels` track, as half-open `(from, to)` *source* |
| 119 | /// seconds, in order and never overlapping. |
| 120 | /// |
| 121 | /// Pure arithmetic: this is what a card re-runs on every keystroke. The three |
| 122 | /// settings are applied in the order a person means them -- quiet runs first, |
| 123 | /// then the short ones forgiven ([`Settings::min_silence`]), then a |
| 124 | /// [`Settings::padding`] left at both ends so the cut never touches speech, and |
| 125 | /// last the slivers too short to keep ([`Settings::min_keep`]) swallowed by the |
| 126 | /// silences around them. Padding before the merge on purpose: what the merge |
| 127 | /// measures is what would actually survive the cut. |
| 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 | /// Where those source stretches sit on the timeline, as `(start, len)` frame |
| 161 | /// pairs of the lane `clip` is on -- what a preview marks and what |
| 162 | /// [`crate::Project::cut_regions`] cuts. |
| 163 | /// |
| 164 | /// Only the part of each region the clip actually plays: the scan reads the |
| 165 | /// whole file and the clip is a range of it, so a silence outside `[in, out)` |
| 166 | /// is not on the timeline at all. Positions go through the clip's **own rate** |
| 167 | /// ([`Speed::timeline_at`]), so a silence in a 2x clip marks the frames it is |
| 168 | /// really on. |
| 169 | /// |
| 170 | /// Both edges are rounded **inward** -- start up, end down -- so a frame that |
| 171 | /// is partly speech stays outside the region at every rate. An empty remainder |
| 172 | /// is dropped rather than returned as a zero-length cut. |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | /// [`Speed::timeline_at`] floored instead of ceiled: which timeline frame of a |
| 197 | /// clip a source frame lands on, rounded the way an *end* has to be so a |
| 198 | /// region never eats the frame speech starts on. The ceil is the right rounding |
| 199 | /// for a start and for playback's stamps, and this is its partner. |
| 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 |