Skip to content

Stop microphone crackle by counting samples during a take - #44

Open
michaldojtrowski wants to merge 3 commits into
omacom:masterfrom
michaldojtrowski:fix/microphone-crackle
Open

michaldojtrowski wants to merge 3 commits into
omacom:masterfrom
michaldojtrowski:fix/microphone-crackle

Conversation

@michaldojtrowski

@michaldojtrowski michaldojtrowski commented Nov 13, 2026 •

Copy link
Copy Markdown

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 fragsize that AudioCapture requests. The clicks sit on capture
buffer 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's capturedAt into a frame position and
compares it with m_audioFramesWritten. Any difference is trimmed from the
buffer or filled with zeros. capturedAt is clock - 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:

  • Every buffer held 960 frames and arrived on schedule. The sample count
    tracked the monotonic clock with no measurable drift.
  • The reported latency moved on its own. It shifted by about 24 ms during the
    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, in audiocapture.h) places capture buffers on one
    continuous timeline.
    • Between takes it follows the smoothed measurement (5 s, starting as a
      plain mean so one early reading does not set the origin).
    • Backend::startTake() locks it. From then on buffer times are contiguous
      by sample count, so the writer has nothing to trim or pad.
    • While locked, an average offset above 20 ms, from clock drift or a few
      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.
    • A reading more than 50 ms off is ignored on its own. Three such readings
      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 whole
    microseconds, 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 52a9c76 rejected a plain sample counter, because
anchoring 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/test passes, including the new
    microphoneTimelineCountsSamplesDuringATake (jitter, wander, gaps, lost
    samples, and 30 minutes of 100 ppm clock drift).

  • Takes started right after launching the app:

    Microphone Without the change With the change
    USB 21.2 clicks/s 1.0/s, not on buffer boundaries
    analog 18.0 clicks/s 0.1/s, not on buffer boundaries

    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

  • The 20 ms tolerance, 5 s smoothing, and 50 ms restart confirmed over three
    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.
  • I have only one machine, and I do not know why this has not been reported.
    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.
  • After a real gap, the first two buffers are still placed by count while the
    restart is confirmed, so the silence lands about 40 ms after the gap.
  • A take started in the first seconds after the microphone opens can carry an
    audio offset of up to about 15 ms against video, because the reported
    latency has not settled yet. I have not measured lip sync.
  • Walking back drift and lost samples by single frames is covered only by the
    unit test. It did not occur on my hardware, so I have not heard it.

Environment

  • Omarchy (Arch Linux). Takes recorded with 0.4.0 built from v0.4.0;
    bin/test also passes on master (add2122), which has the same audio
    code.
  • Qt 6.12.0 with the FFmpeg backend, FFmpeg 9.0.2
  • PipeWire 1.6.9 with pipewire-pulse, WirePlumber 0.5.18, 48 kHz, quantum 1024
    configured and 512 while capturing, with the microphone driving the graph
  • A USB microphone (48 kHz mono) and a headset on the built-in analog input

🤖 Generated with Claude Code

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>
@greptile-apps

greptile-apps Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

The 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.

  • Microphone recordings keep a steadier timeline during each take.

Reviews (3) · Last reviewed commit: "Count the first reading in the timeline'..." · Reviewed by Greptile

Comment thread tests/backend_tests.cpp Resolved
michaldojtrowski and others added 2 commits November 13, 2026 20:56
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>
@RemyDuijkeren

Copy link
Copy Markdown

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:

Build Clicks per second Click spacing on a multiple of 960 samples
0.4.0 + #43, 10.3 s take 10.5 47%
0.4.0 + #43 + this PR, 5.7 s take 1.4 0%
0.4.0 + #43 + this PR, 8.9 s take 2.5 0%
Same mic recorded without Monologue, 28 s 1.7 0%

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.

  • fifine USB microphone through PipeWire's PulseAudio server, with EasyEffects processing the input
  • AMD Ryzen 7 8845HS, Omarchy 4.5.0, qt6-multimedia 6.12.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants