fix(recording): prevent double mic offset in studio editor after AV start gate trim#1972
Merged
richiemcilroy merged 1 commit intoJul 2, 2026
Conversation
…prevent double-count in editor mic offset
Comment on lines
+204
to
+206
| let trim_duration = Duration::from_nanos( | ||
| trim_samples as u64 * 1_000_000_000 / sample_rate as u64, | ||
| ); |
There was a problem hiding this comment.
The trim_samples * 1_000_000_000 / sample_rate math is safe here given current buffer sizes, but it’s a bit brittle if this ever changes. Using u128 for the intermediate avoids accidental overflow and makes intent clearer.
Suggested change
| let trim_duration = Duration::from_nanos( | |
| trim_samples as u64 * 1_000_000_000 / sample_rate as u64, | |
| ); | |
| let trim_duration_ns = | |
| (trim_samples as u128) * 1_000_000_000u128 / sample_rate as u128; | |
| let trim_duration = Duration::from_nanos(trim_duration_ns as u64); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When mic audio arrives before the first video frame, apply_video_start_gate trims the leading samples to align audio with video start.
The trimmed AudioFrame kept the original pre-trim timestamp, so mic_start_time in metadata reflected the buffer start rather than the first committed sample.
The editor computes mic_offset = latest_start - mic_start_time and seeks that many seconds into the audio file. With the wrong timestamp, mic_offset was inflated by the trim duration, causing the editor to skip that same gap a second time.
Result: mic audio starts late by the trim amount (typically 20–150ms) in studio recording exports.
Fix: advance frame.timestamp by trim_duration in the UseFrame path so first_timestamp (and therefore mic_start_time) reflects the actual first committed sample. mic_offset then computes to ~0 and the editor plays from the start of the already-trimmed file.
Only affects studio mode MultipleSegments recordings opened in the editor. Instant recordings are unaffected (audio/video are muxed at capture time, editor offset calculation is not involved).
Greptile Summary
This PR fixes a double-offset bug in studio mode recordings where mic audio was delayed in the editor by the same duration that
apply_video_start_gatehad already trimmed. The root cause was that the trimmedAudioFramekept the original pre-trim timestamp, somic_start_timein the recording metadata reflected the buffer start rather than the first committed sample.frame.timestamp + trim_durationis now passed toAudioFrame::newin theUseFramepath, sofirst_timestamp(and thereforemic_start_time) correctly reflects the first kept sample.apply_gate_audio_leads_trims_fronttest verifies that the returned frame's timestamp equalsstart + trim_duration, directly exercising the corrected behaviour.Confidence Score: 5/5
Safe to merge — the change is isolated to the UseFrame branch of apply_video_start_gate and has no impact on Passthrough or DropFrame paths or on instant recordings.
The arithmetic is correct: trim_samples is already guarded against zero (sample_rate == 0 returns Passthrough before this code runs), the multiplication trim_samples * 1_000_000_000 fits comfortably in u64 given the 500 ms alignment limit, and the small rounding residual (~1 sample period at most) is negligible. The fix correctly maps back to wall-clock time after the integer sample-count round-trip. The new test assertion directly validates the changed behaviour.
No files require special attention.
Important Files Changed
Reviews (1): Last reviewed commit: "fix(recording): advance gate frame times..." | Re-trigger Greptile