Repository navigation
Stop microphone crackle by counting samples during a take - #44
Open
michaldojtrowski wants to merge 3 commits into
Open
michaldojtrowski wants to merge 3 commits into
michaldojtrowski wants to merge 3 commits into
Conversation
The writer trimmed or zero-padded each capture buffer to follow its measured time, clock minus the reported latency. That measurement moves by 10 to 24 ms in the seconds after the stream opens and keeps wandering by a few milliseconds, so the writer edited a few samples at buffer boundaries many times a second, which is heard as crackle. AudioTimeline places buffers on one continuous timeline. Between takes it follows the smoothed measurement; once a take starts it counts samples. An offset above 20 ms is walked back one frame at a time, and three readings more than 50 ms off restart the timeline. The writer ignores a one-frame rounding difference. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
[Medium impact] The PR appears safe to merge. SummaryThe PR keeps microphone buffers continuous by sample count during a take, with small frame corrections for drift and a restart after repeated large timing changes.
Reviews (3) · Last reviewed commit: "Count the first reading in the timeline'..." · Reviewed by Greptile |
The drift test only covered a microphone running slow, where frames are repeated. Run it in both directions, so the path that drops frames for a fast microphone is checked the same way. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The buffer count started at zero after the first reading, so the second reading replaced the origin outright instead of being averaged with the first. Start it at one, so the origin is the plain mean the comment describes. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
Confirming this fixes it here. I heard the crackle as a stutter in the audio, and it's gone with this PR. I ran a click detector on the decoded audio, taking the second difference against the local level:
With the PR the rate drops to what the mic gives on its own, which is mostly plosives, and the spacing no longer lines up with the 20 ms buffers.
|
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.
Problem
Every recording has a light crackle in the audio track, with every microphone
I tried. The same microphones are clean in other recorders on the same machine.
A click detector (second difference of the decoded audio compared with the
local signal level) finds clicks spaced at multiples of 960 samples, which is
the 20 ms
fragsizethatAudioCapturerequests. The clicks sit on capturebuffer boundaries. They are worst in a take started right after the app opens,
about 20 per second, with a USB microphone and with a headset on the built-in
analog input alike.
Cause
Writer::audio()turns each buffer'scapturedAtinto a frame position andcompares it with
m_audioFramesWritten. Any difference is trimmed from thebuffer or filled with zeros.
capturedAtisclock - pa_stream_get_latency(),and that measurement is not steady enough to be compared frame by frame.
I logged every buffer for 199 s (9967 buffers) on PipeWire's PulseAudio server:
tracked the monotonic clock with no measurable drift.
first 5 s after the stream opened, came back to about 10 ms over the next
30 s, and then wandered by a few milliseconds for the rest of the run
(standard deviation of 1 to 2 ms within 10 s windows).
So the writer was chasing measurement noise, cutting or zero-filling a few
samples at buffer boundaries many times a second. In the crackling takes
the click phase moves by 2 to 5 samples per buffer, which is that trimming and
padding.
Fix
AudioTimeline(new, inaudiocapture.h) places capture buffers on onecontinuous timeline.
plain mean so one early reading does not set the origin).
Backend::startTake()locks it. From then on buffer times are contiguousby sample count, so the writer has nothing to trim or pad.
lost samples, is walked back by repeating or dropping one frame per
buffer. It is walked all the way back to zero, so the offset does not
rest at the tolerance.
in a row are a real discontinuity, such as dropped samples or a stalled
device: the timeline restarts at the measurement, and the writer pads or
trims it as before.
Writer::audio()ignores a one-frame difference. Capture times are wholemicroseconds, so a contiguous buffer can round one frame to either side of
the running count.
The writer's gap and overlap handling, pause and resume, and Finish are
otherwise unchanged.
The notes added with
52a9c76rejected a plain sample counter, becauseanchoring it to the first callback can shift audio permanently by the initial
buffering delay. This change keeps what that decision protects: until a take
starts, the timeline follows the measured capture time, so the take begins at
the measured position. Only from there does it count samples.
Verification
bin/testpasses, including the newmicrophoneTimelineCountsSamplesDuringATake(jitter, wander, gaps, lostsamples, and 30 minutes of 100 ppm clock drift).
Takes started right after launching the app:
The clicks are no longer audible.
A 52 s take with six pauses has no click at any join and nothing padded or
trimmed.
Limits
buffers come from one machine, a USB microphone and the built-in analog
input. Other servers or devices may settle differently. The largest
smoothed offset I saw was about 15 ms, so the tolerance leaves about 5 ms of
margin. A latency swing that briefly goes
past it is walked back to zero and can leave the timeline a few milliseconds
off once the measurement comes back.
My guess, which I have not verified, is that it depends on how much the
reported latency moves after the stream opens. Here it moves by 10 to 24 ms
in the first 5 to 10 s, depending on the device and on the run, and the
writer edits 40 to 50 buffers a second during that time. Later it still
edits 5 to 18 buffers a second by 3 to 11 samples, so the crackle gets
rarer but not quieter. A machine where the latency moves less would get
fewer edits.
restart is confirmed, so the silence lands about 40 ms after the gap.
audio offset of up to about 15 ms against video, because the reported
latency has not settled yet. I have not measured lip sync.
unit test. It did not occur on my hardware, so I have not heard it.
Environment
v0.4.0;bin/testalso passes onmaster(add2122), which has the same audiocode.
configured and 512 while capturing, with the microphone driving the graph
🤖 Generated with Claude Code