Use audio engine as source of truth for audio tracks
This commit is contained in:
@@ -235,34 +235,30 @@ impl Action for AddClipInstanceAction {
|
||||
}
|
||||
}
|
||||
AudioClipType::Sampled { audio_pool_index } => {
|
||||
// For sampled audio, send AddAudioClipSync query
|
||||
use daw_backend::command::{Query, QueryResponse};
|
||||
|
||||
let internal_start = self.clip_instance.trim_start;
|
||||
let internal_end = self.clip_instance.trim_end.unwrap_or(clip.duration);
|
||||
let effective_duration = self.clip_instance.timeline_duration
|
||||
.unwrap_or(internal_end - internal_start);
|
||||
let start_time = self.clip_instance.timeline_start;
|
||||
|
||||
let query =
|
||||
Query::AddAudioClipSync(*backend_track_id, *audio_pool_index, start_time, effective_duration, internal_start);
|
||||
let instance_id = controller.add_audio_clip(
|
||||
*backend_track_id,
|
||||
*audio_pool_index,
|
||||
start_time,
|
||||
effective_duration,
|
||||
internal_start,
|
||||
);
|
||||
|
||||
match controller.send_query(query)? {
|
||||
QueryResponse::AudioClipInstanceAdded(Ok(instance_id)) => {
|
||||
self.backend_track_id = Some(*backend_track_id);
|
||||
self.backend_audio_instance_id = Some(instance_id);
|
||||
self.backend_track_id = Some(*backend_track_id);
|
||||
self.backend_audio_instance_id = Some(instance_id);
|
||||
|
||||
// Add to global clip instance mapping
|
||||
backend.clip_instance_to_backend_map.insert(
|
||||
self.clip_instance.id,
|
||||
crate::action::BackendClipInstanceId::Audio(instance_id)
|
||||
);
|
||||
// Add to global clip instance mapping
|
||||
backend.clip_instance_to_backend_map.insert(
|
||||
self.clip_instance.id,
|
||||
crate::action::BackendClipInstanceId::Audio(instance_id)
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
QueryResponse::AudioClipInstanceAdded(Err(e)) => Err(e),
|
||||
_ => Err("Unexpected query response".to_string()),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
AudioClipType::Recording => {
|
||||
// Recording clips are not synced to backend until finalized
|
||||
|
||||
@@ -194,29 +194,23 @@ impl Action for RemoveClipInstancesAction {
|
||||
}
|
||||
}
|
||||
AudioClipType::Sampled { audio_pool_index } => {
|
||||
use daw_backend::command::{Query, QueryResponse};
|
||||
|
||||
let internal_start = instance.trim_start;
|
||||
let internal_end = instance.trim_end.unwrap_or(clip.duration);
|
||||
let effective_duration = instance.timeline_duration
|
||||
.unwrap_or(internal_end - internal_start);
|
||||
let start_time = instance.timeline_start;
|
||||
|
||||
let query = Query::AddAudioClipSync(
|
||||
let new_id = controller.add_audio_clip(
|
||||
track_id,
|
||||
*audio_pool_index,
|
||||
start_time,
|
||||
effective_duration,
|
||||
internal_start,
|
||||
);
|
||||
if let Ok(QueryResponse::AudioClipInstanceAdded(Ok(new_id))) =
|
||||
controller.send_query(query)
|
||||
{
|
||||
backend.clip_instance_to_backend_map.insert(
|
||||
instance.id,
|
||||
BackendClipInstanceId::Audio(new_id),
|
||||
);
|
||||
}
|
||||
backend.clip_instance_to_backend_map.insert(
|
||||
instance.id,
|
||||
BackendClipInstanceId::Audio(new_id),
|
||||
);
|
||||
}
|
||||
AudioClipType::Recording => {}
|
||||
}
|
||||
|
||||
@@ -415,8 +415,6 @@ impl Action for SplitClipInstanceAction {
|
||||
}
|
||||
}
|
||||
AudioClipType::Sampled { audio_pool_index } => {
|
||||
use daw_backend::command::{Query, QueryResponse};
|
||||
|
||||
// 1. Trim the original (left) instance
|
||||
let orig_internal_start = original_instance.trim_start;
|
||||
let orig_internal_end = original_instance.trim_end.unwrap_or(clip.duration);
|
||||
@@ -435,7 +433,7 @@ impl Action for SplitClipInstanceAction {
|
||||
.unwrap_or(internal_end - internal_start);
|
||||
let start_time = new_instance.timeline_start;
|
||||
|
||||
let query = Query::AddAudioClipSync(
|
||||
let instance_id = controller.add_audio_clip(
|
||||
*backend_track_id,
|
||||
*audio_pool_index,
|
||||
start_time,
|
||||
@@ -443,21 +441,15 @@ impl Action for SplitClipInstanceAction {
|
||||
internal_start,
|
||||
);
|
||||
|
||||
match controller.send_query(query)? {
|
||||
QueryResponse::AudioClipInstanceAdded(Ok(instance_id)) => {
|
||||
self.backend_track_id = Some(*backend_track_id);
|
||||
self.backend_audio_instance_id = Some(instance_id);
|
||||
self.backend_track_id = Some(*backend_track_id);
|
||||
self.backend_audio_instance_id = Some(instance_id);
|
||||
|
||||
backend.clip_instance_to_backend_map.insert(
|
||||
new_instance_id,
|
||||
crate::action::BackendClipInstanceId::Audio(instance_id),
|
||||
);
|
||||
backend.clip_instance_to_backend_map.insert(
|
||||
new_instance_id,
|
||||
crate::action::BackendClipInstanceId::Audio(instance_id),
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
QueryResponse::AudioClipInstanceAdded(Err(e)) => Err(e),
|
||||
_ => Err("Unexpected query response".to_string()),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
AudioClipType::Recording => {
|
||||
// Recording clips cannot be split
|
||||
|
||||
@@ -646,6 +646,46 @@ pub struct ClipInstance {
|
||||
pub loop_before: Option<f64>,
|
||||
}
|
||||
|
||||
/// High 64-bit sentinel used to identify UUIDs that encode a backend audio clip instance ID.
|
||||
/// Using a sentinel that would never appear in a v4 random UUID (which has specific version bits).
|
||||
const AUDIO_BACKEND_UUID_HIGH: u64 = 0xDEAD_BEEF_CAFE_BABE;
|
||||
|
||||
/// Convert a backend `AudioClipInstanceId` (u32) to a synthetic UUID for use in selection/hit-testing.
|
||||
/// These UUIDs are distinct from real document UUIDs and can be round-tripped via `audio_backend_id_from_uuid`.
|
||||
pub fn audio_backend_uuid(backend_id: u32) -> Uuid {
|
||||
Uuid::from_u64_pair(AUDIO_BACKEND_UUID_HIGH, backend_id as u64)
|
||||
}
|
||||
|
||||
/// Extract a backend `AudioClipInstanceId` from a synthetic UUID created by `audio_backend_uuid`.
|
||||
/// Returns `None` if this is a regular document UUID.
|
||||
pub fn audio_backend_id_from_uuid(uuid: Uuid) -> Option<u32> {
|
||||
let (high, low) = uuid.as_u64_pair();
|
||||
if high == AUDIO_BACKEND_UUID_HIGH {
|
||||
Some(low as u32)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// High 64-bit sentinel used to identify UUIDs that encode a backend MIDI clip instance ID.
|
||||
const MIDI_BACKEND_UUID_HIGH: u64 = 0xDEAD_BEEF_CAFE_BEEF;
|
||||
|
||||
/// Convert a backend `MidiClipInstanceId` (u32) to a synthetic UUID for use in selection/hit-testing.
|
||||
pub fn midi_backend_uuid(backend_id: u32) -> Uuid {
|
||||
Uuid::from_u64_pair(MIDI_BACKEND_UUID_HIGH, backend_id as u64)
|
||||
}
|
||||
|
||||
/// Extract a backend `MidiClipInstanceId` from a synthetic UUID created by `midi_backend_uuid`.
|
||||
/// Returns `None` if this is a regular document UUID.
|
||||
pub fn midi_backend_id_from_uuid(uuid: Uuid) -> Option<u32> {
|
||||
let (high, low) = uuid.as_u64_pair();
|
||||
if high == MIDI_BACKEND_UUID_HIGH {
|
||||
Some(low as u32)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl ClipInstance {
|
||||
/// Create a new clip instance
|
||||
pub fn new(clip_id: Uuid) -> Self {
|
||||
|
||||
@@ -665,6 +665,20 @@ impl Document {
|
||||
self.audio_clips.get(id)
|
||||
}
|
||||
|
||||
/// Find the document audio clip (UUID + ref) that owns the given backend pool index.
|
||||
pub fn audio_clip_by_pool_index(&self, pool_index: usize) -> Option<(Uuid, &AudioClip)> {
|
||||
self.audio_clips.iter()
|
||||
.find(|(_, c)| c.audio_pool_index() == Some(pool_index))
|
||||
.map(|(&id, c)| (id, c))
|
||||
}
|
||||
|
||||
/// Find the document audio clip (UUID + ref) that owns the given backend MIDI clip ID.
|
||||
pub fn audio_clip_by_midi_clip_id(&self, midi_clip_id: u32) -> Option<(Uuid, &AudioClip)> {
|
||||
self.audio_clips.iter()
|
||||
.find(|(_, c)| c.midi_clip_id() == Some(midi_clip_id))
|
||||
.map(|(&id, c)| (id, c))
|
||||
}
|
||||
|
||||
/// Get a mutable vector clip by ID
|
||||
pub fn get_vector_clip_mut(&mut self, id: &Uuid) -> Option<&mut VectorClip> {
|
||||
self.vector_clips.get_mut(id)
|
||||
|
||||
@@ -4998,6 +4998,8 @@ impl eframe::App for EditorApp {
|
||||
let clip_instance = ClipInstance::new(doc_clip_id)
|
||||
.with_timeline_start(self.recording_start_time);
|
||||
|
||||
let clip_instance_id = clip_instance.id;
|
||||
|
||||
// Add instance to layer (works for root and inside movie clips)
|
||||
if let Some(layer) = self.action_executor.document_mut().get_layer_mut(&layer_id) {
|
||||
if let lightningbeam_core::layer::AnyLayer::Audio(audio_layer) = layer {
|
||||
@@ -5006,6 +5008,12 @@ impl eframe::App for EditorApp {
|
||||
}
|
||||
}
|
||||
|
||||
// Insert mapping so the snapshot cache assigns the doc UUID to this recording clip
|
||||
self.clip_instance_to_backend_map.insert(
|
||||
clip_instance_id,
|
||||
lightningbeam_core::action::BackendClipInstanceId::Audio(backend_clip_id),
|
||||
);
|
||||
|
||||
// Store mapping for later updates
|
||||
self.recording_clips.insert(layer_id, backend_clip_id);
|
||||
}
|
||||
@@ -5117,41 +5125,16 @@ impl eframe::App for EditorApp {
|
||||
}
|
||||
}
|
||||
|
||||
// Sync the clip instance to backend for playback
|
||||
if let Some(backend_track_id) = self.layer_to_track_map.get(&layer_id) {
|
||||
if let Some(ref controller_arc) = self.audio_controller {
|
||||
let mut controller = controller_arc.lock().unwrap();
|
||||
use daw_backend::command::{Query, QueryResponse};
|
||||
|
||||
let query = Query::AddAudioClipSync(
|
||||
*backend_track_id,
|
||||
pool_index,
|
||||
timeline_start,
|
||||
duration,
|
||||
trim_start
|
||||
);
|
||||
|
||||
match controller.send_query(query) {
|
||||
Ok(QueryResponse::AudioClipInstanceAdded(Ok(backend_instance_id))) => {
|
||||
// Store the mapping
|
||||
self.clip_instance_to_backend_map.insert(
|
||||
instance_id,
|
||||
lightningbeam_core::action::BackendClipInstanceId::Audio(backend_instance_id)
|
||||
);
|
||||
println!("✅ Synced recording to backend: instance_id={}", backend_instance_id);
|
||||
}
|
||||
Ok(QueryResponse::AudioClipInstanceAdded(Err(e))) => {
|
||||
eprintln!("❌ Failed to sync recording to backend: {}", e);
|
||||
}
|
||||
Ok(_) => {
|
||||
eprintln!("❌ Unexpected query response when syncing recording");
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("❌ Failed to send query to backend: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// The backend already has the clip at _backend_clip_id (created
|
||||
// during handle_start_recording and finalized in handle_stop_recording).
|
||||
// Map the document instance_id → the existing backend clip so that
|
||||
// delete/move/trim actions can reference it correctly.
|
||||
// DO NOT call AddAudioClipSync — that would create a duplicate clip.
|
||||
self.clip_instance_to_backend_map.insert(
|
||||
instance_id,
|
||||
lightningbeam_core::action::BackendClipInstanceId::Audio(_backend_clip_id),
|
||||
);
|
||||
eprintln!("[AUDIO] Mapped doc instance {} → backend clip {}", instance_id, _backend_clip_id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5775,6 +5758,9 @@ impl eframe::App for EditorApp {
|
||||
schneider_max_error: &mut self.schneider_max_error,
|
||||
raster_settings: &mut self.raster_settings,
|
||||
audio_controller: self.audio_controller.as_ref(),
|
||||
clip_snapshot: self.audio_controller.as_ref().map(|arc| {
|
||||
arc.lock().unwrap().clip_snapshot()
|
||||
}),
|
||||
audio_input_opener: &mut self.audio_input,
|
||||
audio_input_stream: &mut self.audio_input_stream,
|
||||
audio_buffer_size: self.audio_buffer_size,
|
||||
@@ -5792,6 +5778,7 @@ impl eframe::App for EditorApp {
|
||||
paint_bucket_gap_tolerance: &mut self.paint_bucket_gap_tolerance,
|
||||
polygon_sides: &mut self.polygon_sides,
|
||||
layer_to_track_map: &self.layer_to_track_map,
|
||||
clip_instance_to_backend_map: &self.clip_instance_to_backend_map,
|
||||
midi_event_cache: &mut self.midi_event_cache,
|
||||
audio_pools_with_new_waveforms: &self.audio_pools_with_new_waveforms,
|
||||
raw_audio_cache: &self.raw_audio_cache,
|
||||
|
||||
@@ -192,6 +192,9 @@ pub struct SharedPaneState<'a> {
|
||||
pub raster_settings: &'a mut crate::tools::RasterToolSettings,
|
||||
/// Audio engine controller for playback control (wrapped in Arc<Mutex<>> for thread safety)
|
||||
pub audio_controller: Option<&'a std::sync::Arc<std::sync::Mutex<daw_backend::EngineController>>>,
|
||||
/// Snapshot of all audio/MIDI clip instances from the backend (for timeline rendering).
|
||||
/// Updated by the audio thread after each mutation; UI reads it each frame.
|
||||
pub clip_snapshot: Option<std::sync::Arc<std::sync::RwLock<daw_backend::AudioClipSnapshot>>>,
|
||||
/// Opener for the microphone/line-in stream — consumed on first use.
|
||||
pub audio_input_opener: &'a mut Option<daw_backend::InputStreamOpener>,
|
||||
/// Live input stream handle; kept alive while recording is active.
|
||||
@@ -202,6 +205,8 @@ pub struct SharedPaneState<'a> {
|
||||
pub video_manager: &'a std::sync::Arc<std::sync::Mutex<lightningbeam_core::video::VideoManager>>,
|
||||
/// Maps all layer/group/clip UUIDs to backend track IDs (audio, MIDI, and metatracks)
|
||||
pub layer_to_track_map: &'a std::collections::HashMap<Uuid, daw_backend::TrackId>,
|
||||
/// Maps document clip instance UUIDs to backend clip instance IDs (for action dispatch)
|
||||
pub clip_instance_to_backend_map: &'a std::collections::HashMap<Uuid, lightningbeam_core::action::BackendClipInstanceId>,
|
||||
/// Global playback state
|
||||
pub playback_time: &'a mut f64, // Current playback position in seconds
|
||||
pub is_playing: &'a mut bool, // Whether playback is currently active
|
||||
|
||||
@@ -7,9 +7,12 @@
|
||||
/// - Basic layer visualization
|
||||
|
||||
use eframe::egui;
|
||||
use lightningbeam_core::clip::ClipInstance;
|
||||
use lightningbeam_core::clip::{
|
||||
ClipInstance, audio_backend_uuid, midi_backend_uuid,
|
||||
};
|
||||
use lightningbeam_core::layer::{AnyLayer, AudioLayerType, GroupLayer, LayerTrait};
|
||||
use super::{DragClipType, NodePath, PaneRenderer, SharedPaneState};
|
||||
use std::collections::HashMap;
|
||||
|
||||
const RULER_HEIGHT: f32 = 30.0;
|
||||
const LAYER_HEIGHT: f32 = 60.0;
|
||||
@@ -402,26 +405,147 @@ fn shift_toggle_layer(
|
||||
*focus = lightningbeam_core::selection::FocusSelection::Layers(vec![layer_id]);
|
||||
}
|
||||
|
||||
/// Build a per-audio-layer clip instance cache from the backend snapshot.
|
||||
///
|
||||
/// Audio layers read clip instances from the backend snapshot (source of truth) rather
|
||||
/// than from `AudioLayer::clip_instances`. The cache maps layer_id → Vec<ClipInstance>.
|
||||
///
|
||||
/// Clip instance UUIDs in the cache are doc UUIDs when available (via reverse lookup of
|
||||
/// `clip_instance_to_backend_map`), falling back to synthetic `audio_backend_uuid` /
|
||||
/// `midi_backend_uuid` values for clips not yet in the map.
|
||||
fn build_audio_clip_cache(
|
||||
snap: &daw_backend::AudioClipSnapshot,
|
||||
layer_to_track_map: &HashMap<uuid::Uuid, daw_backend::TrackId>,
|
||||
document: &lightningbeam_core::document::Document,
|
||||
clip_map: &HashMap<uuid::Uuid, lightningbeam_core::action::BackendClipInstanceId>,
|
||||
) -> HashMap<uuid::Uuid, Vec<ClipInstance>> {
|
||||
use lightningbeam_core::action::BackendClipInstanceId;
|
||||
|
||||
// Build reverse maps: backend_id → doc_instance_uuid
|
||||
let mut audio_id_to_doc: HashMap<u32, uuid::Uuid> = HashMap::new();
|
||||
let mut midi_id_to_doc: HashMap<u32, uuid::Uuid> = HashMap::new();
|
||||
for (&doc_uuid, backend_id) in clip_map {
|
||||
match backend_id {
|
||||
BackendClipInstanceId::Audio(id) => { audio_id_to_doc.insert(*id, doc_uuid); }
|
||||
BackendClipInstanceId::Midi(id) => { midi_id_to_doc.insert(*id, doc_uuid); }
|
||||
}
|
||||
}
|
||||
|
||||
let mut cache: HashMap<uuid::Uuid, Vec<ClipInstance>> = HashMap::new();
|
||||
|
||||
for (&layer_id, &track_id) in layer_to_track_map {
|
||||
// Only process audio layers
|
||||
match document.get_layer(&layer_id) {
|
||||
Some(AnyLayer::Audio(_)) => {}
|
||||
_ => continue,
|
||||
}
|
||||
|
||||
let mut instances = Vec::new();
|
||||
|
||||
// Sampled audio clips
|
||||
if let Some(audio_clips) = snap.audio.get(&track_id) {
|
||||
for ac in audio_clips {
|
||||
if let Some((clip_id, _)) = document.audio_clip_by_pool_index(ac.audio_pool_index) {
|
||||
// Use doc UUID if we have it; otherwise fall back to synthetic UUID
|
||||
let instance_id = audio_id_to_doc.get(&ac.id)
|
||||
.copied()
|
||||
.unwrap_or_else(|| audio_backend_uuid(ac.id));
|
||||
let mut ci = ClipInstance::new(clip_id);
|
||||
ci.id = instance_id;
|
||||
ci.timeline_start = ac.external_start;
|
||||
ci.trim_start = ac.internal_start;
|
||||
ci.trim_end = Some(ac.internal_end);
|
||||
let internal_dur = ac.internal_end - ac.internal_start;
|
||||
if (ac.external_duration - internal_dur).abs() > 1e-9 {
|
||||
ci.timeline_duration = Some(ac.external_duration);
|
||||
}
|
||||
ci.gain = ac.gain;
|
||||
instances.push(ci);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MIDI clips
|
||||
if let Some(midi_clips) = snap.midi.get(&track_id) {
|
||||
for mc in midi_clips {
|
||||
if let Some((clip_id, _)) = document.audio_clip_by_midi_clip_id(mc.clip_id) {
|
||||
let instance_id = midi_id_to_doc.get(&mc.id)
|
||||
.copied()
|
||||
.unwrap_or_else(|| midi_backend_uuid(mc.id));
|
||||
let mut ci = ClipInstance::new(clip_id);
|
||||
ci.id = instance_id;
|
||||
ci.timeline_start = mc.external_start;
|
||||
ci.trim_start = mc.internal_start;
|
||||
ci.trim_end = Some(mc.internal_end);
|
||||
let internal_dur = mc.internal_end - mc.internal_start;
|
||||
if (mc.external_duration - internal_dur).abs() > 1e-9 {
|
||||
ci.timeline_duration = Some(mc.external_duration);
|
||||
}
|
||||
instances.push(ci);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Only insert if we found clips (so layer_clips() can fall back to al.clip_instances
|
||||
// for layers where the snapshot has no clips yet, e.g. during recording setup)
|
||||
if !instances.is_empty() {
|
||||
cache.insert(layer_id, instances);
|
||||
}
|
||||
}
|
||||
|
||||
cache
|
||||
}
|
||||
|
||||
/// Get clip instances for a layer, using the snapshot-based cache for audio layers
|
||||
/// and falling back to the doc's `clip_instances` if the cache has no entry OR is empty
|
||||
/// while the doc has clips (e.g., a recording clip not yet reflected in the snapshot).
|
||||
fn layer_clips<'a>(
|
||||
layer: &'a AnyLayer,
|
||||
audio_cache: &'a HashMap<uuid::Uuid, Vec<ClipInstance>>,
|
||||
) -> &'a [ClipInstance] {
|
||||
match layer {
|
||||
AnyLayer::Audio(al) => {
|
||||
match audio_cache.get(&al.layer.id) {
|
||||
Some(cached) if !cached.is_empty() => cached.as_slice(),
|
||||
// Cache empty or missing: fall back to doc (covers recording-in-progress)
|
||||
_ => &al.clip_instances,
|
||||
}
|
||||
}
|
||||
AnyLayer::Vector(l) => &l.clip_instances,
|
||||
AnyLayer::Video(l) => &l.clip_instances,
|
||||
AnyLayer::Effect(l) => &l.clip_instances,
|
||||
AnyLayer::Group(_) => &[],
|
||||
AnyLayer::Raster(_) => &[],
|
||||
}
|
||||
}
|
||||
|
||||
/// Collect all (layer_ref, clip_instances) tuples from context_layers,
|
||||
/// recursively descending into group children.
|
||||
/// Returns (&AnyLayer, &[ClipInstance]) so callers have access to both layer info and clips.
|
||||
fn all_layer_clip_instances<'a>(context_layers: &[&'a AnyLayer]) -> Vec<(&'a AnyLayer, &'a [ClipInstance])> {
|
||||
fn all_layer_clip_instances<'a>(
|
||||
context_layers: &[&'a AnyLayer],
|
||||
audio_cache: &'a HashMap<uuid::Uuid, Vec<ClipInstance>>,
|
||||
) -> Vec<(&'a AnyLayer, &'a [ClipInstance])> {
|
||||
let mut result = Vec::new();
|
||||
for &layer in context_layers {
|
||||
collect_clip_instances(layer, &mut result);
|
||||
collect_clip_instances(layer, audio_cache, &mut result);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn collect_clip_instances<'a>(layer: &'a AnyLayer, result: &mut Vec<(&'a AnyLayer, &'a [ClipInstance])>) {
|
||||
fn collect_clip_instances<'a>(
|
||||
layer: &'a AnyLayer,
|
||||
audio_cache: &'a HashMap<uuid::Uuid, Vec<ClipInstance>>,
|
||||
result: &mut Vec<(&'a AnyLayer, &'a [ClipInstance])>,
|
||||
) {
|
||||
match layer {
|
||||
AnyLayer::Audio(_) => result.push((layer, layer_clips(layer, audio_cache))),
|
||||
AnyLayer::Vector(l) => result.push((layer, &l.clip_instances)),
|
||||
AnyLayer::Audio(l) => result.push((layer, &l.clip_instances)),
|
||||
AnyLayer::Video(l) => result.push((layer, &l.clip_instances)),
|
||||
AnyLayer::Effect(l) => result.push((layer, &l.clip_instances)),
|
||||
AnyLayer::Group(g) => {
|
||||
for child in &g.children {
|
||||
collect_clip_instances(child, result);
|
||||
collect_clip_instances(child, audio_cache, result);
|
||||
}
|
||||
}
|
||||
AnyLayer::Raster(_) => {}
|
||||
@@ -798,6 +922,7 @@ impl TimelinePane {
|
||||
content_rect: egui::Rect,
|
||||
header_rect: egui::Rect,
|
||||
editing_clip_id: Option<&uuid::Uuid>,
|
||||
audio_cache: &HashMap<uuid::Uuid, Vec<ClipInstance>>,
|
||||
) -> Option<(ClipDragType, uuid::Uuid)> {
|
||||
let context_layers = document.context_layers(editing_clip_id);
|
||||
let rows = build_timeline_rows(&context_layers);
|
||||
@@ -827,14 +952,7 @@ impl TimelinePane {
|
||||
};
|
||||
let _layer_data = layer.layer();
|
||||
|
||||
let clip_instances: &[ClipInstance] = match layer {
|
||||
lightningbeam_core::layer::AnyLayer::Vector(vl) => &vl.clip_instances,
|
||||
lightningbeam_core::layer::AnyLayer::Audio(al) => &al.clip_instances,
|
||||
lightningbeam_core::layer::AnyLayer::Video(vl) => &vl.clip_instances,
|
||||
lightningbeam_core::layer::AnyLayer::Effect(el) => &el.clip_instances,
|
||||
lightningbeam_core::layer::AnyLayer::Group(_) => &[],
|
||||
lightningbeam_core::layer::AnyLayer::Raster(_) => &[],
|
||||
};
|
||||
let clip_instances = layer_clips(layer, audio_cache);
|
||||
|
||||
// Check each clip instance
|
||||
let stacking = compute_clip_stacking(document, layer, clip_instances);
|
||||
@@ -1947,6 +2065,7 @@ impl TimelinePane {
|
||||
waveform_stereo: bool,
|
||||
context_layers: &[&lightningbeam_core::layer::AnyLayer],
|
||||
video_manager: &std::sync::Arc<std::sync::Mutex<lightningbeam_core::video::VideoManager>>,
|
||||
audio_cache: &HashMap<uuid::Uuid, Vec<ClipInstance>>,
|
||||
) -> Vec<(egui::Rect, uuid::Uuid, f64, f64)> {
|
||||
let painter = ui.painter();
|
||||
|
||||
@@ -2330,8 +2449,8 @@ impl TimelinePane {
|
||||
bright_teal.a() as f32 / 255.0,
|
||||
];
|
||||
for child in &g.children {
|
||||
if let AnyLayer::Audio(al) = child {
|
||||
for ci in &al.clip_instances {
|
||||
if let AnyLayer::Audio(_) = child {
|
||||
for ci in layer_clips(child, &audio_cache) {
|
||||
let audio_clip = match document.get_audio_clip(&ci.clip_id) {
|
||||
Some(c) => c,
|
||||
None => continue,
|
||||
@@ -2441,14 +2560,7 @@ impl TimelinePane {
|
||||
};
|
||||
|
||||
// Draw clip instances for this layer
|
||||
let clip_instances: &[ClipInstance] = match layer {
|
||||
lightningbeam_core::layer::AnyLayer::Vector(vl) => &vl.clip_instances,
|
||||
lightningbeam_core::layer::AnyLayer::Audio(al) => &al.clip_instances,
|
||||
lightningbeam_core::layer::AnyLayer::Video(vl) => &vl.clip_instances,
|
||||
lightningbeam_core::layer::AnyLayer::Effect(el) => &el.clip_instances,
|
||||
lightningbeam_core::layer::AnyLayer::Group(_) => &[],
|
||||
lightningbeam_core::layer::AnyLayer::Raster(_) => &[],
|
||||
};
|
||||
let clip_instances = layer_clips(layer, &audio_cache);
|
||||
|
||||
// For moves, precompute the clamped offset so all selected clips move uniformly
|
||||
let group_move_offset = if self.clip_drag_state == Some(ClipDragType::Move) {
|
||||
@@ -3248,6 +3360,7 @@ impl TimelinePane {
|
||||
audio_controller: Option<&std::sync::Arc<std::sync::Mutex<daw_backend::EngineController>>>,
|
||||
context_layers: &[&lightningbeam_core::layer::AnyLayer],
|
||||
editing_clip_id: Option<&uuid::Uuid>,
|
||||
audio_cache: &HashMap<uuid::Uuid, Vec<ClipInstance>>,
|
||||
) {
|
||||
// Only allocate content area (ruler + layers) with click and drag
|
||||
let content_response = ui.allocate_rect(
|
||||
@@ -3317,14 +3430,7 @@ impl TimelinePane {
|
||||
let _layer_data = layer.layer();
|
||||
|
||||
// Get clip instances for this layer
|
||||
let clip_instances: &[ClipInstance] = match layer {
|
||||
lightningbeam_core::layer::AnyLayer::Vector(vl) => &vl.clip_instances,
|
||||
lightningbeam_core::layer::AnyLayer::Audio(al) => &al.clip_instances,
|
||||
lightningbeam_core::layer::AnyLayer::Video(vl) => &vl.clip_instances,
|
||||
lightningbeam_core::layer::AnyLayer::Effect(el) => &el.clip_instances,
|
||||
lightningbeam_core::layer::AnyLayer::Group(_) => &[],
|
||||
lightningbeam_core::layer::AnyLayer::Raster(_) => &[],
|
||||
};
|
||||
let clip_instances = layer_clips(layer, &audio_cache);
|
||||
|
||||
// Check if click is within any clip instance
|
||||
let click_stacking = compute_clip_stacking(document, layer, clip_instances);
|
||||
@@ -3610,6 +3716,7 @@ impl TimelinePane {
|
||||
content_rect,
|
||||
header_rect,
|
||||
editing_clip_id,
|
||||
&audio_cache,
|
||||
) {
|
||||
// If this clip is not selected, select it (respecting shift key)
|
||||
if !selection.contains_clip_instance(&clip_id) {
|
||||
@@ -3663,7 +3770,7 @@ impl TimelinePane {
|
||||
HashMap::new();
|
||||
|
||||
// Iterate through all layers (including group children) to find selected clip instances
|
||||
for (layer, clip_instances) in all_layer_clip_instances(context_layers) {
|
||||
for (layer, clip_instances) in all_layer_clip_instances(context_layers, &audio_cache) {
|
||||
let layer_id = layer.id();
|
||||
// Find selected clip instances in this layer
|
||||
for clip_instance in clip_instances {
|
||||
@@ -3705,7 +3812,7 @@ impl TimelinePane {
|
||||
> = HashMap::new();
|
||||
|
||||
// Iterate through all layers (including group children) to find selected clip instances
|
||||
for (layer, clip_instances) in all_layer_clip_instances(context_layers) {
|
||||
for (layer, clip_instances) in all_layer_clip_instances(context_layers, &audio_cache) {
|
||||
let layer_id = layer.id();
|
||||
|
||||
// Find selected clip instances in this layer
|
||||
@@ -3830,7 +3937,7 @@ impl TimelinePane {
|
||||
ClipDragType::LoopExtendRight => {
|
||||
let mut layer_loops: HashMap<uuid::Uuid, Vec<lightningbeam_core::actions::loop_clip_instances::LoopEntry>> = HashMap::new();
|
||||
|
||||
for (layer, clip_instances) in all_layer_clip_instances(context_layers) {
|
||||
for (layer, clip_instances) in all_layer_clip_instances(context_layers, &audio_cache) {
|
||||
let layer_id = layer.id();
|
||||
|
||||
for clip_instance in clip_instances {
|
||||
@@ -3896,7 +4003,7 @@ impl TimelinePane {
|
||||
// Extend loop_before (pre-loop region)
|
||||
let mut layer_loops: HashMap<uuid::Uuid, Vec<lightningbeam_core::actions::loop_clip_instances::LoopEntry>> = HashMap::new();
|
||||
|
||||
for (layer, clip_instances) in all_layer_clip_instances(context_layers) {
|
||||
for (layer, clip_instances) in all_layer_clip_instances(context_layers, &audio_cache) {
|
||||
let layer_id = layer.id();
|
||||
|
||||
for clip_instance in clip_instances {
|
||||
@@ -4140,6 +4247,7 @@ impl TimelinePane {
|
||||
content_rect,
|
||||
header_rect,
|
||||
editing_clip_id,
|
||||
&audio_cache,
|
||||
) {
|
||||
match drag_type {
|
||||
ClipDragType::TrimLeft | ClipDragType::TrimRight => {
|
||||
@@ -4420,17 +4528,31 @@ impl PaneRenderer for TimelinePane {
|
||||
// Use virtual row count (includes expanded group children) for height calculations
|
||||
let layer_count = build_timeline_rows(&context_layers).len();
|
||||
|
||||
// Build audio clip cache from backend snapshot (backend-as-source-of-truth for audio).
|
||||
// Uses doc UUIDs via reverse lookup of clip_instance_to_backend_map so that selection
|
||||
// and action dispatch continue to work with doc UUIDs.
|
||||
// Falls back to AudioLayer::clip_instances for layers with no snapshot data yet
|
||||
// (e.g., layers where recording is in progress but not yet finalized).
|
||||
let audio_cache: HashMap<uuid::Uuid, Vec<ClipInstance>> =
|
||||
if let Some(snap_arc) = shared.clip_snapshot.as_ref() {
|
||||
if let Ok(snap) = snap_arc.read() {
|
||||
build_audio_clip_cache(
|
||||
&snap,
|
||||
shared.layer_to_track_map,
|
||||
document,
|
||||
shared.clip_instance_to_backend_map,
|
||||
)
|
||||
} else {
|
||||
HashMap::new()
|
||||
}
|
||||
} else {
|
||||
HashMap::new()
|
||||
};
|
||||
|
||||
// Calculate project duration from last clip endpoint across all layers
|
||||
let mut max_endpoint: f64 = 10.0; // Default minimum duration
|
||||
for &layer in &context_layers {
|
||||
let clip_instances: &[ClipInstance] = match layer {
|
||||
lightningbeam_core::layer::AnyLayer::Vector(vl) => &vl.clip_instances,
|
||||
lightningbeam_core::layer::AnyLayer::Audio(al) => &al.clip_instances,
|
||||
lightningbeam_core::layer::AnyLayer::Video(vl) => &vl.clip_instances,
|
||||
lightningbeam_core::layer::AnyLayer::Effect(el) => &el.clip_instances,
|
||||
lightningbeam_core::layer::AnyLayer::Group(_) => &[],
|
||||
lightningbeam_core::layer::AnyLayer::Raster(_) => &[],
|
||||
};
|
||||
let clip_instances = layer_clips(layer, &audio_cache);
|
||||
|
||||
for clip_instance in clip_instances {
|
||||
let clip_duration = effective_clip_duration(document, layer, clip_instance);
|
||||
@@ -4499,7 +4621,7 @@ impl PaneRenderer for TimelinePane {
|
||||
|
||||
// Render layer rows with clipping
|
||||
ui.set_clip_rect(content_rect.intersect(original_clip_rect));
|
||||
let video_clip_hovers = self.render_layers(ui, content_rect, shared.theme, document, shared.active_layer_id, shared.focus, shared.selection, shared.midi_event_cache, shared.raw_audio_cache, shared.waveform_gpu_dirty, shared.target_format, shared.waveform_stereo, &context_layers, shared.video_manager);
|
||||
let video_clip_hovers = self.render_layers(ui, content_rect, shared.theme, document, shared.active_layer_id, shared.focus, shared.selection, shared.midi_event_cache, shared.raw_audio_cache, shared.waveform_gpu_dirty, shared.target_format, shared.waveform_stereo, &context_layers, shared.video_manager, &audio_cache);
|
||||
|
||||
// Render playhead on top (clip to timeline area)
|
||||
ui.set_clip_rect(timeline_rect.intersect(original_clip_rect));
|
||||
@@ -4526,6 +4648,7 @@ impl PaneRenderer for TimelinePane {
|
||||
shared.audio_controller,
|
||||
&context_layers,
|
||||
editing_clip_id.as_ref(),
|
||||
&audio_cache,
|
||||
);
|
||||
|
||||
// Context menu: detect right-click on clips or empty timeline space
|
||||
@@ -4534,7 +4657,7 @@ impl PaneRenderer for TimelinePane {
|
||||
if secondary_clicked {
|
||||
if let Some(pos) = ui.input(|i| i.pointer.interact_pos()) {
|
||||
if content_rect.contains(pos) {
|
||||
if let Some((_drag_type, clip_id)) = self.detect_clip_at_pointer(pos, document, content_rect, layer_headers_rect, editing_clip_id.as_ref()) {
|
||||
if let Some((_drag_type, clip_id)) = self.detect_clip_at_pointer(pos, document, content_rect, layer_headers_rect, editing_clip_id.as_ref(), &audio_cache) {
|
||||
// Right-clicked on a clip
|
||||
if !shared.selection.contains_clip_instance(&clip_id) {
|
||||
shared.selection.select_only_clip_instance(clip_id);
|
||||
@@ -4562,14 +4685,7 @@ impl PaneRenderer for TimelinePane {
|
||||
let mut enabled = false;
|
||||
if let Some(layer_id) = *shared.active_layer_id {
|
||||
if let Some(layer) = document.get_layer(&layer_id) {
|
||||
let instances: &[ClipInstance] = match layer {
|
||||
AnyLayer::Vector(vl) => &vl.clip_instances,
|
||||
AnyLayer::Audio(al) => &al.clip_instances,
|
||||
AnyLayer::Video(vl) => &vl.clip_instances,
|
||||
AnyLayer::Effect(el) => &el.clip_instances,
|
||||
AnyLayer::Group(_) => &[],
|
||||
AnyLayer::Raster(_) => &[],
|
||||
};
|
||||
let instances = layer_clips(layer, &audio_cache);
|
||||
for inst in instances {
|
||||
if !shared.selection.contains_clip_instance(&inst.id) { continue; }
|
||||
if let Some(dur) = document.get_clip_duration(&inst.clip_id) {
|
||||
@@ -4593,14 +4709,7 @@ impl PaneRenderer for TimelinePane {
|
||||
let mut enabled = false;
|
||||
if let Some(layer_id) = *shared.active_layer_id {
|
||||
if let Some(layer) = document.get_layer(&layer_id) {
|
||||
let instances: &[ClipInstance] = match layer {
|
||||
AnyLayer::Vector(vl) => &vl.clip_instances,
|
||||
AnyLayer::Audio(al) => &al.clip_instances,
|
||||
AnyLayer::Video(vl) => &vl.clip_instances,
|
||||
AnyLayer::Effect(el) => &el.clip_instances,
|
||||
AnyLayer::Group(_) => &[],
|
||||
AnyLayer::Raster(_) => &[],
|
||||
};
|
||||
let instances = layer_clips(layer, &audio_cache);
|
||||
// Check each selected clip
|
||||
enabled = instances.iter()
|
||||
.filter(|ci| shared.selection.contains_clip_instance(&ci.id))
|
||||
|
||||
Reference in New Issue
Block a user