Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions apps/desktop-gpui/src/transcript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ pub(crate) struct TranscriptState {
pub energy: Option<Arc<EnergyEnvelope>>,
pub energy_loading: bool,
pub energy_offsets: Vec<(f64, f64)>,
pub voiced_for: usize,
}

impl TranscriptState {
Expand Down Expand Up @@ -213,6 +214,7 @@ impl TranscriptState {
energy: None,
energy_loading: false,
energy_offsets: Vec::new(),
voiced_for: 0,
}
}

Expand Down Expand Up @@ -260,6 +262,18 @@ impl TranscriptState {
self.cleanup.ignored.clear();
self.doc = Some(Arc::new(doc));
self.edl_key = 0;
self.voiced_for = 0;
changed = true;
}
let energy_key = self
.energy
.as_ref()
.map_or(1, |energy| Arc::as_ptr(energy) as usize);
if energy_key != self.voiced_for
&& let Some(doc) = self.doc.as_mut()
{
Arc::make_mut(doc).refine_voiced(self.energy.as_deref());
self.voiced_for = energy_key;
changed = true;
}
let key = timeline_key(project.timeline.as_ref(), recording_durations);
Expand Down
13 changes: 13 additions & 0 deletions apps/desktop-gpui/src/transcript/automation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@ impl EditorWindow {
"text": word.text,
"start": word.start,
"end": word.end,
"voiced_start": doc.voiced[index].start,
"voiced_end": doc.voiced[index].end,
"kept": edl.is_kept(index),
"output_start": edl.source_to_output(word.start),
"output_end": edl.source_to_output(word.end),
Expand All @@ -369,6 +371,17 @@ impl EditorWindow {
.collect::<Vec<_>>()
}),
"words": words,
"energy": self.transcript.energy.is_some(),
"silences": self.transcript.energy.as_ref().map(|energy| energy.silences().to_vec()),
"pauses": self.transcript_pause_plan().iter().map(|pause| {
serde_json::json!({
"after_word": pause.after_word,
"next_word": pause.next_word,
"length": pause.length,
"saves": pause.saves,
"cuts": pause.cuts,
})
}).collect::<Vec<_>>(),
});
match std::fs::write(path, serde_json::to_vec_pretty(&body).unwrap_or_default()) {
Ok(()) => format!("wrote {path}"),
Expand Down
132 changes: 102 additions & 30 deletions apps/desktop-gpui/src/transcript/cuts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,18 @@ const TOUCHING_SEARCH: f64 = 0.04;
const EDGE_PENALTY_DB: f32 = 6.0;
const MAX_SEARCH_STEPS: usize = 4_000;
const MIN_PAUSE_CUT: f64 = 0.05;
pub const LONG_GAP: f64 = 0.8;

pub fn quietest_point(energy: Option<&EnergyEnvelope>, lo: f64, hi: f64) -> f64 {
quietest_mapped(energy, lo, hi, Some)
}

fn quietest_mapped(
energy: Option<&EnergyEnvelope>,
lo: f64,
hi: f64,
to_source: impl Fn(f64) -> Option<f64>,
) -> f64 {
let (lo, hi) = if lo <= hi { (lo, hi) } else { (hi, lo) };
let centre = (lo + hi) / 2.0;
let Some(energy) = energy else {
Expand All @@ -36,7 +46,7 @@ pub fn quietest_point(energy: Option<&EnergyEnvelope>, lo: f64, hi: f64) -> f64
let mut best: Option<(f32, f64)> = None;
for index in 0..=steps {
let time = (lo + index as f64 * step).min(hi);
let Some(db) = energy.smoothed_db(time) else {
let Some(db) = to_source(time).and_then(|source| energy.smoothed_db(source)) else {
continue;
};
let score = db + EDGE_PENALTY_DB * ((time - centre).abs() / half) as f32;
Expand All @@ -47,13 +57,26 @@ pub fn quietest_point(energy: Option<&EnergyEnvelope>, lo: f64, hi: f64) -> f64
best.map_or(centre, |(_, time)| time)
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Removing {
Before,
After,
}

/// The cut point between word `index - 1` and word `index` (`index` may be 0
/// or `words.len()` for the edges). Deterministic per gap, so cutting two
/// neighbouring words one at a time lands on exactly the same boundary as
/// cutting them together.
pub fn gap_cut(doc: &TranscriptDoc, energy: Option<&EnergyEnvelope>, index: usize) -> f64 {
let previous = index.checked_sub(1).and_then(|at| doc.words.get(at));
let next = doc.words.get(index);
/// or `words.len()` for the edges), measured between the words' voiced parts.
/// Up to `LONG_GAP` the point is the same whichever side is removed, so
/// cutting two neighbours one at a time lands where cutting them together
/// does. In a longer silence the removed word takes at most `LONG_GAP / 2` of
/// it, and the rest stays for the pause controls to judge.
pub fn gap_cut(
doc: &TranscriptDoc,
energy: Option<&EnergyEnvelope>,
index: usize,
removing: Removing,
) -> f64 {
let previous = index.checked_sub(1).and_then(|at| doc.voiced.get(at));
let next = doc.voiced.get(index);
let (previous_end, next_start) = match (previous, next) {
(Some(previous), Some(next)) => (previous.end, next.start),
(None, Some(next)) if next.start <= EDGE_REACH * 2.0 => return 0.0,
Expand All @@ -62,7 +85,12 @@ pub fn gap_cut(doc: &TranscriptDoc, energy: Option<&EnergyEnvelope>, index: usiz
(None, None) => return 0.0,
};
let gap = next_start - previous_end;
let (lo, hi) = if gap > 0.0 {
let (lo, hi) = if gap > LONG_GAP && previous.is_some() && next.is_some() {
match removing {
Removing::After => (next_start - LONG_GAP / 2.0, next_start - ONSET_GUARD),
Removing::Before => (previous_end + TAIL_GUARD, previous_end + LONG_GAP / 2.0),
}
} else if gap > 0.0 {
(
previous_end + TAIL_GUARD.min(gap * 0.3),
next_start - ONSET_GUARD.min(gap * 0.2),
Expand All @@ -82,22 +110,38 @@ pub fn gap_cut(doc: &TranscriptDoc, energy: Option<&EnergyEnvelope>, index: usiz
pub fn plan_word_cuts(
doc: &TranscriptDoc,
energy: Option<&EnergyEnvelope>,
kept: Option<&[bool]>,
runs: &[Range<usize>],
) -> Vec<Range<f64>> {
let is_kept = |word: usize| kept.is_none_or(|kept| kept.get(word).copied().unwrap_or(true));
merge_ranges(
runs.iter()
.filter(|run| run.start < run.end && run.end <= doc.words.len())
.map(|run| gap_cut(doc, energy, run.start)..gap_cut(doc, energy, run.end))
.map(|run| {
let start = if run.start > 0 && !is_kept(run.start - 1) {
Removing::Before
} else {
Removing::After
};
let end = if run.end < doc.words.len() && !is_kept(run.end) {
Removing::After
} else {
Removing::Before
};
gap_cut(doc, energy, run.start, start)..gap_cut(doc, energy, run.end, end)
})
.collect(),
)
}

#[derive(Clone, Debug, PartialEq)]
pub struct PauseCut {
pub after_word: usize,
pub next_word: usize,
pub length: f64,
pub saves: f64,
pub cut: Range<f64>,
pub output: Range<f64>,
pub cuts: Vec<Range<f64>>,
}

pub fn plan_pause_cuts(
Expand All @@ -114,48 +158,76 @@ pub fn plan_pause_cuts(
.iter()
.map(|mapping| mapping.timescale)
.fold(f64::INFINITY, f64::min);
let to_source = |output: f64| edl.output_to_source(output);
let mut pauses = Vec::new();
for index in 1..doc.words.len() {
if !edl.is_kept(index - 1) || !edl.is_kept(index) {
let mut previous: Option<(usize, Range<f64>)> = None;
for index in 0..doc.voiced.len() {
Comment thread
richiemcilroy marked this conversation as resolved.
Resolved
if doc.voiced[index].is_empty() {
continue;
}
let previous_end = doc.words[index - 1].end;
let next_start = doc.words[index].start;
if slowest > 0.0 && (next_start - previous_end) / slowest <= floor {
let Some(heard) = edl.audible_part(&doc.voiced[index]) else {
continue;
};
let Some((before, before_heard)) = previous.replace((index, heard.clone())) else {
continue;
};
let source = before_heard.end..heard.start;
if source.end <= source.start
|| (slowest > 0.0 && source.end - source.start <= floor * slowest)
{
continue;
}
let Some(timescale) = edl.timescale_over(previous_end..next_start) else {
let (Some(from), Some(until)) = (
edl.output_end_of(&before_heard),
edl.output_start_of(&heard),
) else {
continue;
};
let length = (next_start - previous_end) / timescale;
if length <= longer_than || length <= keep + MIN_PAUSE_CUT {
let length = until - from;
if length <= floor {
continue;
}
let lead = keep * 0.45 * timescale;
let trail = keep * timescale - lead;
let lo_limit = previous_end + TAIL_GUARD.min(lead);
let hi_limit = next_start - ONSET_GUARD.min(trail);
let start_target = previous_end + lead;
let end_target = next_start - trail;
let start = quietest_point(
let Some(pieces) = edl.source_pieces(from..until) else {
continue;
};
if pieces
.iter()
.any(|piece| piece.start < source.start - 1e-6 || piece.end > source.end + 1e-6)
{
continue;
}
let lead = keep * 0.45;
let trail = keep - lead;
let lo_limit = from + TAIL_GUARD.min(lead);
let hi_limit = until - ONSET_GUARD.min(trail);
let start_target = from + lead;
let end_target = until - trail;
let start = quietest_mapped(
energy,
(start_target - 0.06).max(lo_limit),
(start_target + 0.06).min(end_target),
to_source,
);
let end = quietest_point(
let end = quietest_mapped(
energy,
(end_target - 0.06).max(start),
(end_target + 0.06).min(hi_limit),
to_source,
);
let saves = (end - start) / timescale;
let saves = end - start;
if saves < MIN_PAUSE_CUT {
continue;
}
let Some(cuts) = edl.source_pieces(start..end) else {
continue;
};
pauses.push(PauseCut {
after_word: index - 1,
after_word: before,
next_word: index,
length,
saves,
cut: start..end,
output: from..until,
cuts,
});
}
pauses
Expand Down Expand Up @@ -405,7 +477,7 @@ pub fn restore_words(
let Some(index) = words.clone().find(|word| !edl.is_kept(*word)) else {
break;
};
let word = &doc.words[index];
let word = &doc.voiced[index];
let first = edl
.kept_ranges
.partition_point(|range| range.end <= word.start);
Expand Down
Loading
Loading