tellur-core 0.2.0

Core component model for tellur: vector/raster/timeline components, layout, easing, and events
Documentation
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
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
//! The media / subtitle leaves: [`VideoFile`], [`AudioFile`], [`Subtitle`].

use crate::audio::{self, AudioMix};
use crate::geometry::Vec2;
use crate::raster::{RasterImage, Resolution};
use crate::render_context::RenderContext;
use crate::time::Time;
use crate::timeline_component::{
    Arrangement, AudioBuffer, Clock, Cue, NodeKind, TimelineComponent,
};

// ── Leaves ───────────────────────────────────────────────────────────────────

/// A last-resort media duration for the [`probe`](VideoFile::probe) seam when no
/// real length is available (`.sketch/02 §12`): a missing/un-probable file, or a
/// placeholder test path. Real decode reads the header — video via
/// `ffprobe`/`ffmpeg` ([`video_decode`](crate::video_decode)), audio via
/// `symphonia`. A `.trim` or an injected `duration` wins over this.
pub(super) const STUB_PROBE_SECONDS: f32 = 1.0;

/// Decoded video — the visual channel. Built with
/// `VideoFile::builder().path("x.mp4")`.
///
/// Its intrinsic length is the file's, read once by the resolve pass via an
/// `ffprobe` header read (`probe`); an injected `duration` (or, when the
/// file is un-probable, `STUB_PROBE_SECONDS`) is the fallback so a
/// test that names a non-existent path still resolves. A `.trim(a..b)` crops the
/// SOURCE seconds, shortening the reported duration to `b - a`.
///
/// DECODE (step 9): a per-source `ffmpeg` CHILD process emitting raw `rgba`
/// scaled to the target (`.sketch/01` ZONE C). The mutable decoder state (the
/// child + frame cache + decode position) lives OUTSIDE this struct in a
/// process-global pool ([`video_decode`](crate::video_decode)), so `VideoFile`
/// stays `Clone + Keyable` pure data (`path` + `trim` + injected `duration`) and
/// the decoder state never enters any cache key.
#[crate::component(timeline)]
// `Clone` so the leaf can be a field of a `#[component(timeline)]` fn (e.g. the
// `.sketch/01` `Dialogue(voice: AudioFile)`): the macro clones `self` to
// destructure the body's fields, so every component field type must be `Clone`.
#[derive(Clone, crate::Keyable)]
pub struct VideoFile {
    #[builder(into)]
    pub path: String,
    /// Optional override for the probed duration. Test-injectable; `None` reads
    /// the real `ffprobe` length (with the trim length / stub as fallbacks).
    #[builder(into)]
    pub duration: Option<f32>,
    /// SOURCE-clock crop `(start, end)` set by [`Self::trim`]; `None` plays the
    /// whole file. The reported [`duration`](TimelineComponent::duration) is
    /// `end - start` when set, and `frame` offsets the source time by `start`.
    #[builder(skip)]
    pub trim: Option<(f32, f32)>,
}

impl VideoFile {
    /// Crops to SOURCE seconds `a..b` (the in/out crop). Shortens
    /// [`duration`](TimelineComponent::duration) to `b - a`. An inherent method
    /// so it shadows the generic [`Timed::trim`](crate::timeline_component::Timed::trim)
    /// no-op for the concrete leaf (`.trim` actually records the crop here).
    pub fn trim(mut self, r: std::ops::Range<f32>) -> Self {
        self.trim = Some((r.start, r.end));
        self
    }

    /// Duration probe (`.sketch/02 §12`). An injected `duration` wins; otherwise
    /// the SOURCE length read by `ffprobe`, cropped by a `.trim`. If the file is
    /// un-probable (e.g. a placeholder test path), falls back to the trim length
    /// or [`STUB_PROBE_SECONDS`] so resolve still has a determinate length.
    fn probe(&self) -> f32 {
        if let Some(d) = self.duration {
            return d;
        }
        if let Some((a, b)) = self.trim {
            // A trim fixes the length regardless of the source (clamped to ≥ 0).
            return (b - a).max(0.0);
        }
        crate::video_decode::probe_duration(&self.path).unwrap_or(STUB_PROBE_SECONDS)
    }
}

impl TimelineComponent for VideoFile {
    fn duration(&self) -> Option<f32> {
        Some(self.probe())
    }

    fn frame(
        &self,
        clock: Clock<'_>,
        canvas: Vec2,
        target: Resolution,
        ctx: &mut dyn RenderContext,
    ) -> Option<RasterImage> {
        // The `Placed` wrapper has already rebased + speed-scaled `local` to this
        // clip's source-local axis; the leaf only adds its `.trim` start to reach
        // the absolute source time, then decodes scaled to `target` via the
        // per-source ffmpeg child (`video_decode`). `ctx` is unused: decode
        // spawns its own child and does not touch the render context. `canvas`
        // is ignored: a video decodes scaled to the pixel `target` directly,
        // independent of the logical layout space.
        let _ = ctx;
        let _ = canvas;
        crate::video_decode::decode_frame(&self.path, clock.local().seconds(), self.trim, target)
    }

    fn arrangement(&self, offset: f32) -> Arrangement {
        Arrangement {
            kind: NodeKind::Video,
            label: self.path.clone(),
            name: None,
            source: None,
            start: offset,
            end: offset + self.probe(),
            trim: self.trim,
            triggers: Vec::new(),
            children: Vec::new(),
        }
    }
}

/// Decoded audio — the audio channel. Built with
/// `AudioFile::builder().path("v.wav").gain(0.25).fade_out(0.4)`.
///
/// Its intrinsic length is the file's, read once by the resolve pass via a
/// `symphonia` decode (`probe`). An injected `duration` (or, if decode
/// fails, `STUB_PROBE_SECONDS`) is the fallback so tests that name a
/// non-existent path still resolve. A `.trim(a..b)` crops the SOURCE seconds,
/// shortening the reported duration to `b - a`.
#[crate::component(timeline)]
// `Clone`: see `VideoFile` — a media leaf may be a `#[component(timeline)]`
// field (the `.sketch/01` `Dialogue(voice: AudioFile)`), which the macro clones.
#[derive(Clone, crate::Keyable)]
pub struct AudioFile {
    #[builder(into)]
    pub path: String,
    /// Linear gain applied to the decoded samples (`1.0` = unity).
    #[builder(default = 1.0)]
    pub gain: f32,
    /// Linear fade-in duration in clip-local output seconds. A non-positive or
    /// non-finite value disables the fade.
    #[builder(default = 0.0)]
    pub fade_in: f32,
    /// Linear fade-out duration in clip-local output seconds, ending at the
    /// clip's resolved audio end. A non-positive or non-finite value disables
    /// the fade.
    #[builder(default = 0.0)]
    pub fade_out: f32,
    /// Optional override for the probed duration. Test-injectable; `None` reads
    /// the real decoded length (with the stub as a last-resort fallback).
    #[builder(into)]
    pub duration: Option<f32>,
    /// SOURCE-clock crop `(start, end)` set by [`Self::trim`]; `None` plays the
    /// whole file. The reported [`duration`](TimelineComponent::duration) is
    /// `end - start` when set.
    #[builder(skip)]
    pub trim: Option<(f32, f32)>,
}

impl AudioFile {
    /// Crops to SOURCE seconds `a..b` (the in/out crop). Shortens
    /// [`duration`](TimelineComponent::duration) to `b - a`. An inherent method
    /// so it shadows the generic [`Timed::trim`](crate::timeline_component::Timed::trim)
    /// no-op for the concrete leaf (`.trim` actually records the crop here).
    pub fn trim(mut self, r: std::ops::Range<f32>) -> Self {
        self.trim = Some((r.start, r.end));
        self
    }

    /// Duration probe (`.sketch/02 §12`). An injected `duration` wins; otherwise
    /// the SOURCE length read by decoding the file via `symphonia`, cropped by a
    /// `.trim`. If decode fails (e.g. a placeholder test path), falls back to
    /// the trim length or [`STUB_PROBE_SECONDS`] so resolve still has a
    /// determinate length.
    fn probe(&self) -> f32 {
        if let Some(d) = self.duration {
            return d;
        }
        // A trim fixes the length regardless of the source, as long as the
        // source is at least that long — but we still need the source length to
        // clamp. Decode to read the true length; fall back gracefully.
        match audio::decoded_duration(&self.path, self.trim) {
            Ok(duration) => duration,
            Err(_) => self
                .trim
                .map(|(a, b)| (b - a).max(0.0))
                .unwrap_or(STUB_PROBE_SECONDS),
        }
    }
}

impl TimelineComponent for AudioFile {
    fn duration(&self) -> Option<f32> {
        Some(self.probe())
    }

    fn samples(&self, clock: Clock<'_>, window: f32) -> Option<AudioBuffer> {
        // The eager mix-down uses `mix_into`; this per-window seam is unused.
        let _ = (clock, window);
        None
    }

    fn mix_into(&self, mix: &mut AudioMix, start_secs: f32, speed: f32) {
        // Reuse a full-source conformed audio buffer, slice the part that can
        // land inside this mix window, and sum it at the visible destination
        // start. Decode/conform failure ⇒ silence.
        let speed = if speed.is_finite() && speed > 0.0 {
            speed
        } else {
            1.0
        };
        let mix_duration = mix.duration();
        let visible_start = start_secs.max(0.0);
        if visible_start >= mix_duration {
            return;
        }
        let source_offset = ((visible_start - start_secs) * speed).max(0.0);
        let source_window = (mix_duration - visible_start).max(0.0) * speed;
        let trim_start = self.trim.map(|(start, _)| start).unwrap_or(0.0);
        let trim_end = self.trim.map(|(_, end)| end);
        let decode_start = trim_start + source_offset;
        let decode_end = trim_end
            .map(|end| (decode_start + source_window).min(end))
            .unwrap_or(decode_start + source_window);
        if decode_end <= decode_start {
            return;
        }

        if let Ok(buf) = audio::conform_file_cached(
            &self.path,
            self.trim,
            mix.rate(),
            mix.channels(),
            self.gain,
            speed,
        ) {
            let output_start = source_offset / speed;
            let clip_duration = self
                .duration
                .map(|duration| (duration / speed).max(0.0))
                .unwrap_or_else(|| audio_buffer_duration(&buf));
            let output_duration = (mix_duration - visible_start)
                .max(0.0)
                .min((clip_duration - output_start).max(0.0));
            if output_duration <= 0.0 {
                return;
            }
            let window = audio::slice_audio_buffer(
                &buf,
                Some((output_start, output_start + output_duration)),
            );
            let mut samples = window.samples;
            apply_audio_fade(
                &mut samples,
                mix.rate(),
                mix.channels(),
                output_start,
                clip_duration,
                self.fade_in,
                self.fade_out,
            );
            mix.add(&samples, visible_start);
        }
    }

    fn arrangement(&self, offset: f32) -> Arrangement {
        Arrangement {
            kind: NodeKind::Audio,
            label: self.path.clone(),
            name: None,
            source: None,
            start: offset,
            end: offset + self.probe(),
            trim: self.trim,
            triggers: Vec::new(),
            children: Vec::new(),
        }
    }
}

fn audio_buffer_duration(buffer: &AudioBuffer) -> f32 {
    let channels = buffer.channels.max(1) as usize;
    let frames = buffer.samples.len() / channels;
    frames as f32 / buffer.rate.max(1) as f32
}

fn apply_audio_fade(
    samples: &mut [f32],
    rate: u32,
    channels: u16,
    local_start: f32,
    clip_duration: f32,
    fade_in: f32,
    fade_out: f32,
) {
    let fade_in = fade_seconds(fade_in);
    let fade_out = fade_seconds(fade_out);
    if fade_in == 0.0 && fade_out == 0.0 {
        return;
    }

    let channels = channels.max(1) as usize;
    let frames = samples.len() / channels;
    let rate = rate.max(1) as f32;
    for frame in 0..frames {
        let local_time = local_start + frame as f32 / rate;
        let gain = audio_fade_gain(local_time, clip_duration, fade_in, fade_out);
        for channel in 0..channels {
            samples[frame * channels + channel] *= gain;
        }
    }
}

fn fade_seconds(seconds: f32) -> f32 {
    if seconds.is_finite() {
        seconds.max(0.0)
    } else {
        0.0
    }
}

fn audio_fade_gain(local_time: f32, clip_duration: f32, fade_in: f32, fade_out: f32) -> f32 {
    let rise = if fade_in <= 0.0 {
        1.0
    } else {
        (local_time / fade_in).clamp(0.0, 1.0)
    };
    let fall = if fade_out <= 0.0 {
        1.0
    } else {
        ((clip_duration - local_time) / fade_out).clamp(0.0, 1.0)
    };
    rise.min(fall)
}

/// An invisible, silent placeholder with an explicit `duration` and no
/// visual, audio, or subtitle output of its own — the timeline twin of the
/// layout side's [`SizedBox`](crate::layout::SizedBox). Built with
/// `TimeBox::builder().duration(1.5)`.
///
/// Useful anywhere a `Timeline`/`Sequence` needs an explicit length to hang
/// triggers on or to reserve a beat, without authoring a stub media file
/// just to give a clip a duration (the role
/// `DialogueDuration` played ad hoc before this leaf existed).
#[crate::component(timeline)]
#[derive(Clone, Copy, crate::Keyable)]
pub struct TimeBox {
    pub duration: f32,
}

impl TimelineComponent for TimeBox {
    fn duration(&self) -> Option<f32> {
        Some(self.duration)
    }

    fn arrangement(&self, offset: f32) -> Arrangement {
        Arrangement {
            kind: NodeKind::Timeline,
            label: "time box".to_owned(),
            name: None,
            source: None,
            start: offset,
            end: offset + self.duration,
            trim: None,
            triggers: Vec::new(),
            children: Vec::new(),
        }
    }
}

/// 字幕 — the subtitle channel only (written to .srt/.vtt, NOT a burned-in
/// telop, which is a visual). Built with `Subtitle::builder().text("…")`.
///
/// TIMELESS (`measure()` = `None`): its interval comes from the placement window
/// (`.at(0.0..dur)`) or a `.fill()` taking the container's resolved length. Its
/// [`cues`](TimelineComponent::cues) emit `Cue { start: offset, end: offset +
/// resolved_len, text }`. `frame` / `samples` are `None`.
#[crate::component(timeline)]
// `Clone`: see `VideoFile` — a leaf may be a `#[component(timeline)]` field that
// the macro clones to build the body.
#[derive(Clone, crate::Keyable)]
pub struct Subtitle {
    #[builder(into)]
    pub text: String,
}

impl TimelineComponent for Subtitle {
    // `duration` defaults to `None` (timeless): the placement window supplies the
    // length, so `measure` (which defaults to `duration`) is `None` too.

    fn cues(&self, offset: f32) -> Vec<Cue> {
        // The resolved length comes from the placement window / `.fill()`, which
        // wraps this leaf in a `Placed`. When called directly (no window) the
        // leaf is timeless, so the cue is a zero-length point at `offset`; the
        // wrapping `Placed` (or container, for `.fill()`) re-stamps the real end.
        let end = offset + self.duration().unwrap_or(0.0);
        vec![Cue {
            start: offset,
            end,
            text: self.text.clone(),
        }]
    }

    fn arrangement(&self, offset: f32) -> Arrangement {
        // Timeless: un-windowed it is a 0-length point at `offset`; the wrapping
        // `Placed` / `.fill()` container stamps the real end (mirrors `cues`).
        Arrangement {
            kind: NodeKind::Subtitle,
            label: self.text.clone(),
            name: None,
            source: None,
            start: offset,
            end: offset + self.duration().unwrap_or(0.0),
            trim: None,
            triggers: Vec::new(),
            children: Vec::new(),
        }
    }
}