M11.2: audio playback + waveforms + audio effects
- Audio effects (Backwards, Bass & Treble, Delay, Flange & Chorus, High-Low Pass, Modulator, Parametric EQ, Reverb, Stereo Mixer, Tone) applied per layer in the comp mixdown, so export and preview share them. RBJ-cookbook biquads, Schroeder/Moorer reverb, modulated delay lines; stateless per block via pre-roll. Tone makes any layer audible. - Real-time audio preview: ui-egui AudioFeed/AudioPlayback (feeder thread mixing ahead) behind an AudioDevice hook; the desktop app implements it with cpal (L6). Audio clock drives playback (consumed frames minus device latency, looped through the work area). Preview panel Include Audio / playback.muteAudio. - Audio panel VU meters with peak hold and clip indicators; selected layer's Audio Levels. - Timeline Audio > Waveform for footage with audio (L reveals Audio Levels, LL the Waveform), drawn from a cached per-item peak summary. - ui.playback reports audio state and meter levels. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
1c0da40173
commit
ddb33cdfd0
@@ -0,0 +1,95 @@
|
||||
//! Audio waveforms for the Timeline's Audio > Waveform property (revealed with `LL`).
|
||||
//!
|
||||
//! Each footage item's audio is summarised once into min/max peaks
|
||||
//! (`effectcraft_render::audio::footage_peaks`, [`BINS_PER_SEC`] bins per second) on a
|
||||
//! background thread; the timeline draws from that summary at any zoom.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use effectcraft_engine::project::{ItemId, ItemKind, Layer, LayerSource, Project};
|
||||
use effectcraft_engine::time::Tick;
|
||||
use egui::{Color32, Painter, Rect, Stroke, pos2};
|
||||
|
||||
use super::timeline::TMap;
|
||||
use crate::EffectcraftApp;
|
||||
|
||||
/// Peak-summary resolution.
|
||||
pub const BINS_PER_SEC: u32 = 200;
|
||||
|
||||
/// A footage item's peak summary: `[min L, max L, min R, max R]` per bin.
|
||||
pub struct Summary {
|
||||
pub bins_per_sec: u32,
|
||||
pub peaks: Vec<[f32; 4]>,
|
||||
}
|
||||
|
||||
/// Summaries by footage item (`None` while computing).
|
||||
#[derive(Default)]
|
||||
pub struct Cache {
|
||||
map: Arc<Mutex<HashMap<u64, Option<Arc<Summary>>>>>,
|
||||
}
|
||||
|
||||
/// The footage item whose audio a layer plays, when it has audio.
|
||||
pub fn audio_item(project: &Project, l: &Layer) -> Option<ItemId> {
|
||||
let LayerSource::Footage { item } = &l.source else { return None };
|
||||
match &project.item(*item)?.kind {
|
||||
ItemKind::Footage(f) if f.has_audio && !f.missing => Some(*item),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The summary for `item`, starting its computation on first request.
|
||||
pub fn summary(app: &EffectcraftApp, ctx: &egui::Context, item: ItemId) -> Option<Arc<Summary>> {
|
||||
let mut m = app.waveforms.map.lock().ok()?;
|
||||
if let Some(s) = m.get(&item.0) {
|
||||
return s.clone();
|
||||
}
|
||||
let ItemKind::Footage(f) = &app.session.project.item(item)?.kind else { return None };
|
||||
m.insert(item.0, None);
|
||||
let (f, footage, map, ctx) = (f.clone(), app.session.footage.clone(), app.waveforms.map.clone(), ctx.clone());
|
||||
std::thread::Builder::new()
|
||||
.name("ec-waveform".into())
|
||||
.spawn(move || {
|
||||
let peaks = effectcraft_engine::render::audio::footage_peaks(footage.as_ref(), item, &f, f.duration, BINS_PER_SEC);
|
||||
if let Ok(mut m) = map.lock() {
|
||||
m.insert(item.0, Some(Arc::new(Summary { bins_per_sec: BINS_PER_SEC, peaks })));
|
||||
}
|
||||
ctx.request_repaint();
|
||||
})
|
||||
.ok()?;
|
||||
None
|
||||
}
|
||||
|
||||
/// Draw `layer`'s waveform in `rect` (left channel above right), columns mapped through the
|
||||
/// timeline's time map and the layer's time mapping, limited to its [in, out) span.
|
||||
pub(crate) fn draw(p: &Painter, rect: Rect, tm: &TMap, layer: &Layer, s: &Summary, color: Color32) {
|
||||
let mid = [rect.min.y + rect.height() * 0.25, rect.min.y + rect.height() * 0.75];
|
||||
let half = rect.height() * 0.25 - 1.0;
|
||||
let x0 = tm.x(layer.in_point.seconds()).max(rect.min.x).floor() as i32;
|
||||
let x1 = tm.x(layer.out_point.seconds()).min(rect.max.x).ceil() as i32;
|
||||
let n = s.peaks.len() as i64;
|
||||
for c in 0..2 {
|
||||
p.line_segment([pos2(x0 as f32, mid[c]), pos2(x1 as f32, mid[c])], Stroke::new(1.0, color.gamma_multiply(0.5)));
|
||||
}
|
||||
for x in x0..x1 {
|
||||
let (ta, tb) = (tm.t(x as f32), tm.t(x as f32 + 1.0));
|
||||
let src = |t: f64| layer.layer_time(Tick::from_seconds_f64(t)).seconds() * s.bins_per_sec as f64;
|
||||
let (ba, bb) = (src(ta).floor() as i64, src(tb).ceil() as i64);
|
||||
let (ba, bb) = (ba.min(bb).clamp(0, n), ba.max(bb).max(ba + 1).clamp(0, n));
|
||||
if ba >= bb {
|
||||
continue;
|
||||
}
|
||||
let mut agg = [0.0f32; 4];
|
||||
for b in &s.peaks[ba as usize..bb as usize] {
|
||||
agg[0] = agg[0].min(b[0]);
|
||||
agg[1] = agg[1].max(b[1]);
|
||||
agg[2] = agg[2].min(b[2]);
|
||||
agg[3] = agg[3].max(b[3]);
|
||||
}
|
||||
for c in 0..2 {
|
||||
let (lo, hi) = (agg[c * 2].clamp(-1.0, 1.0), agg[c * 2 + 1].clamp(-1.0, 1.0));
|
||||
let (ya, yb) = (mid[c] - hi * half, mid[c] - lo * half);
|
||||
p.line_segment([pos2(x as f32 + 0.5, ya), pos2(x as f32 + 0.5, yb.max(ya + 1.0))], Stroke::new(1.0, color));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user