1use crate::{ActiveIndex, EvalError, IndexedClip, collect_overlapping, count_overlapping};
2use alpha2_media::SourceId;
3use alpha2_model::{AudioProperties, ClipId, Project, TimeMap, TrackId};
4use alpha2_time::{MediaTime, TimeRange};
5
6#[derive(Clone, Copy, Debug, PartialEq)]
10pub struct ActiveAudioClip<'a> {
11 pub clip_id: &'a ClipId,
13 pub source_id: &'a SourceId,
15 pub track_id: &'a TrackId,
17 pub track_order: usize,
19 pub clip_order: usize,
21 pub visual_track: bool,
23 pub timeline_range: TimeRange,
25 pub source_range: TimeRange,
27 pub time_map: &'a TimeMap,
29 pub active_range: TimeRange,
31 pub audio: &'a AudioProperties,
33}
34
35impl ActiveAudioClip<'_> {
36 #[must_use]
43 pub fn speed(&self) -> f64 {
44 match self.time_map {
45 TimeMap::Loop { .. } | TimeMap::Curve { .. } => return 1.0,
46 TimeMap::Freeze { .. } => return 0.0,
47 TimeMap::Forward | TimeMap::Reverse => {}
48 }
49 let timeline = self.timeline_range.duration().map_or(0, MediaTime::ticks);
50 let source = self.source_range.duration().map_or(0, MediaTime::ticks);
51 if timeline <= 0 || source <= 0 {
52 return 1.0;
53 }
54 #[allow(clippy::cast_precision_loss)]
55 {
56 source as f64 / timeline as f64
57 }
58 }
59
60 #[must_use]
62 pub fn source_time_at(&self, time: MediaTime) -> Option<MediaTime> {
63 crate::map_source_time(
64 self.timeline_range,
65 self.source_range,
66 self.time_map,
67 crate::Mirror::TICK,
68 time,
69 )
70 }
71}
72
73#[derive(Clone, Debug, PartialEq)]
75pub struct AudioSpanDesc<'a> {
76 pub range: TimeRange,
78 pub clips: Vec<ActiveAudioClip<'a>>,
80}
81
82pub fn evaluate_audio<'a>(
96 project: &Project,
97 index: &'a ActiveIndex,
98 range: TimeRange,
99) -> Result<AudioSpanDesc<'a>, EvalError> {
100 crate::ensure_index_matches(project, index)?;
101 let capacity = index.root.map_or(0, |root| {
102 count_overlapping(&index.nodes, root, range, &|entry| entry.source_is_audible)
103 });
104 let mut scratch = AudioSpanScratch {
105 nodes: Vec::with_capacity(capacity),
106 };
107 let span = fill_span(index, range, &mut scratch)?;
108 Ok(AudioSpanDesc {
109 range,
110 clips: span.iter().collect(),
111 })
112}
113
114#[derive(Clone, Debug, Default)]
118pub struct AudioSpanScratch {
119 nodes: Vec<usize>,
120}
121
122#[derive(Clone, Copy, Debug)]
124pub struct AudioSpan<'a, 's> {
125 index: &'a ActiveIndex,
126 range: TimeRange,
127 nodes: &'s [usize],
128}
129
130impl<'a> AudioSpan<'a, '_> {
131 #[must_use]
133 pub const fn range(&self) -> TimeRange {
134 self.range
135 }
136
137 #[must_use]
139 pub const fn len(&self) -> usize {
140 self.nodes.len()
141 }
142
143 #[must_use]
145 pub const fn is_empty(&self) -> bool {
146 self.nodes.is_empty()
147 }
148
149 #[must_use = "iterators are lazy and do nothing unless consumed"]
151 pub fn iter(&self) -> impl ExactSizeIterator<Item = ActiveAudioClip<'a>> {
152 let (index, range) = (self.index, self.range);
153 self.nodes
154 .iter()
155 .map(move |&node| active_clip(index.nodes[node].entry(), range))
156 }
157}
158
159pub fn evaluate_audio_in<'a, 's>(
164 project: &Project,
165 index: &'a ActiveIndex,
166 range: TimeRange,
167 scratch: &'s mut AudioSpanScratch,
168) -> Result<AudioSpan<'a, 's>, EvalError> {
169 crate::ensure_index_matches(project, index)?;
170 fill_span(index, range, scratch)
171}
172
173fn fill_span<'a, 's>(
174 index: &'a ActiveIndex,
175 range: TimeRange,
176 scratch: &'s mut AudioSpanScratch,
177) -> Result<AudioSpan<'a, 's>, EvalError> {
178 let nodes = &mut scratch.nodes;
179 nodes.clear();
180 let mix_order = |entry: &IndexedClip| {
181 (
182 entry.track_order,
183 entry.timeline_range.start(),
184 entry.clip_order,
185 )
186 };
187 let mut unmappable: Option<&IndexedClip> = None;
188 if let Some(root) = index.root {
189 collect_overlapping(&index.nodes, root, range, &mut |node, entry| {
190 if !entry.source_is_audible {
191 return;
192 }
193 let Some(active_range) = entry.timeline_range.intersection(range) else {
194 return;
195 };
196 if active_range.is_empty() {
197 return;
198 }
199 if crate::map_source_time(
201 entry.timeline_range,
202 entry.source_range,
203 &entry.time_map,
204 crate::Mirror::TICK,
205 active_range.start(),
206 )
207 .is_none()
208 {
209 if unmappable.is_none_or(|first| mix_order(entry) < mix_order(first)) {
210 unmappable = Some(entry);
211 }
212 return;
213 }
214 nodes.push(node);
215 });
216 }
217 if let Some(entry) = unmappable {
218 return Err(EvalError::UnmappableClip {
219 clip: entry.clip_id.clone(),
220 });
221 }
222 nodes.sort_unstable_by_key(|&node| mix_order(index.nodes[node].entry()));
223 Ok(AudioSpan {
224 index,
225 range,
226 nodes,
227 })
228}
229
230fn active_clip(entry: &IndexedClip, range: TimeRange) -> ActiveAudioClip<'_> {
231 ActiveAudioClip {
232 clip_id: &entry.clip_id,
233 source_id: &entry.source_id,
234 track_id: &entry.track_id,
235 track_order: entry.track_order,
236 clip_order: entry.clip_order,
237 visual_track: entry.track_kind == alpha2_model::TrackKind::Video,
238 timeline_range: entry.timeline_range,
239 source_range: entry.source_range,
240 time_map: &entry.time_map,
241 active_range: entry.timeline_range.intersection(range).unwrap_or(range),
242 audio: &entry.audio,
243 }
244}