The 1-LSB Bug: How a Tiny Rounding Mistake Broke Bit-Perfect Audio (and How I Fixed It)
Imagine a function that takes an audio sample, sends it across the network, and gives it back changed by one part in 32,768. You could never hear it. Every test you wrote would pass.
And yet it can silently destroy a Dolby E stream, fail every null test, and make "bit-perfect" a lie.
That was issue #136 in an open-source macOS AES67 audio driver. It was labelled good first issue, I asked to be assigned, and the maintainer said yes. Here is what the bug was, why nobody caught it, and how I fixed it and proved the fix.
First, 60 seconds of AES67
AES67 is an interoperability standard for sending high-quality audio over ordinary IP networks. At a system level:
- Audio is cut into small packets, commonly 1 ms each, which is 48 samples per channel at 48 kHz.
- Packets travel as RTP over UDP, with sequence numbers and timestamps so the receiver can detect loss and put audio back in the right place.
- Devices share a clock through PTP, and SDP describes the stream's format.
- On the wire, samples are plain big-endian signed integers: 16-bit (
L16) or 24-bit (L24).
Inside macOS, CoreAudio works in floating-point samples between -1.0 and +1.0. So a driver like this converts at the boundary: integer to float on receive, float to integer on transmit.
The issue was that those two conversions were not inverses of each other.
The bug in one table
On receive, the driver did this:
samples[i] = pcm / 32768.0f; // L16
samples[i] = pcm / 8388608.0f; // L24 (2^23)
On transmit, it did this:
int16_t pcm = static_cast<int16_t>(sample * 32767.0f); // L16
int32_t pcm = static_cast<int32_t>(sample * 8388607.0f); // L24
Divide by 32768 going in, multiply by 32767 going out. Look at what happens to an audio stream that is received and sent straight back out:
| Original sample | After decode (float) | Old encode | New encode |
|---|---|---|---|
| -32768 | -1.0 | -32767 | -32768 |
| -1 | -0.0000305 | 0 | -1 |
| 0 | 0.0 | 0 | 0 |
| 16384 | 0.5 | 16383 | 16384 |
| 32767 | 0.99997 | 32766 | 32767 |
A brute-force check over every possible value made it undeniable. With the old encoder, 65,535 of the 65,536 L16 values and 16,777,215 of the 16,777,216 L24 values came back different. Only zero survived.
Why the negative side is bigger: two's complement
To see the fix, you need one fact about signed integers.
In two's complement, the top bit carries a negative weight. Take a tiny 3-bit version:
| Bits | 000 | 001 | 010 | 011 | 100 | 101 | 110 | 111 |
|---|---|---|---|---|---|---|---|---|
| Value | 0 | 1 | 2 | 3 | -4 | -3 | -2 | -1 |
Zero uses up one of the "sign bit clear" patterns, so there are 3 positive values but 4 negative ones. Scale that to 16 bits and the range is -32768 to +32767, which is 32,768 negative values and 32,767 positive ones. For 24 bits it is -8,388,608 to +8,388,607. The negative end, 0x8000, has no positive twin.
So when a float of exactly -1.0 arrives, it has a home (-32768). When +1.0 arrives, it does not, because +32768 does not exist. Keep that in mind, because it comes back later.
Why dividing by 32768 and multiplying by 32767 breaks things
Compose the two steps and you get:
x / 32768 * 32767 = x - x/32768
Every sample is shrunk by a hair, less than one LSB. That alone would be a small error.
But then C++ does what the language says. Converting a float to an integer truncates toward zero. For a positive sample that is slightly under its true value, truncation drops it a whole step: x becomes x - 1. For a negative one, the shrink pulls it toward zero and truncation finishes the job: x becomes x + 1.
So the bug is not a tiny error spread around. It is an exact, systematic one-LSB step toward zero for every nonzero sample.
Truncation also leaves a second flaw. With the old multiplier, any float inside roughly ±1/32767 becomes 0, so the "zero bucket" is two LSBs wide, and everything is biased inward.
Why every test passed
I went looking for the round-trip tests and found their tolerances:
- L16 round trip: error must be below 0.01, which is about 328 LSBs.
- L24 round trip: error must be below 0.001, which is about 8,389 LSBs.
The bug is one LSB. The test would have stayed green for a bug 300 times larger. The tests were checking that the audio roughly survived. Nobody had checked that it survived exactly.
Why one LSB matters
For music, it does not matter. But AES67 is also used as a transparent pipe:
- Non-audio data. Formats such as Dolby E travel inside AES3 as words that merely look like PCM. They carry sync words and framing, and one flipped bit corrupts the stream.
- Null tests. Subtract the output from the input and you should get silence. With an off-by-one on every sample, you get a constant error signal instead.
- Trust. "Bit-transparent" is a promise. Either every bit is identical, or the promise is false.
The fix: three decisions
I changed the encoder to make three decisions explicitly.
// Decode divides by 2^(N-1) (full scale); encode must multiply by the same
// power of two or decode -> encode is not the identity.
constexpr float kL16Scale = 32768.0f; // 2^15
constexpr float kL24Scale = 8388608.0f; // 2^23
int32_t quantize(float sample, float scale, int32_t lo, int32_t hi) {
const long scaled = std::lrintf(clipSample(sample) * scale);
return static_cast<int32_t>(std::min<long>(hi, std::max<long>(lo, scaled)));
}
1. Same scale in both directions. Multiply by 2^(N-1), the exact inverse of the decode. This is the cleanest part of the fix. A float is a 24-bit mantissa times a power of two, so multiplying or dividing by a power of two only changes the exponent and loses nothing. A 16-bit integer fits the mantissa easily. A 24-bit integer needs exactly 24 bits, and it fits. So decode is exact, scaling back is exact, and the original integer comes back.
2. Round to nearest, don't truncate. lrintf rounds to the nearest integer (ties to even, in the default mode). The error is at most half an LSB and is unbiased. Truncation always errs toward zero. Rounding also shrinks the zero bucket from two LSBs to one.
3. Clamp the one value that cannot fit. +1.0 times 32768 is +32768, which two's complement cannot represent. So it saturates to +32767. That single-code asymmetry is not a bug. It is the number system. The existing behaviour that NaN becomes silence and out-of-range input is clipped stays unchanged.
Proving it, not hoping
"It works on my examples" is the sentence that let the original bug live. The input space is small enough to test all of it.
// every 16-bit two's complement value
for (int v = -32768; v <= 32767; ++v) putL16(v, &wire[(v + 32768) * 2]);
L16Codec::decode(wire.data(), wire.size(), decoded.data());
L16Codec::encode(decoded.data(), decoded.size(), back.data());
// every byte must match the original
The same pattern runs over all 16,777,216 L24 values, in chunks. On top of that come the edge cases: 0.0, ±1 LSB, ±full scale, out-of-range values, ±infinity, NaN, and the rounding and tie behaviour.
The scoreboard:
| Encoder | L16 values changed | L24 values changed |
|---|---|---|
| Old | 65,535 of 65,536 | 16,777,215 of 16,777,216 |
| New | 0 | 0 |
The plot twist: fixing it broke two tests
Once the encoder was right, two other tests started to look wrong, and they were wrong in an interesting way. They had enshrined the bug.
- One test asserted that full-scale negative input encodes to
-32767and-8388607. The correct answers are-32768and-8388608. The test had been written to match what the code did. - A timing test generated samples whose 24-bit value equals the media position, using
(position + 0.5) / 8388607. The+0.5only worked because the encoder truncated. With the new encoder that trick would be wrong for nearly every position, so I replaced it with the exact value,position / 8388608.
I also tightened the old round-trip tolerances from "hundreds of LSBs" to "at most 1 LSB". The only sample that actually reaches 1 LSB of error is the sine's +1.0 peak, which is exactly the saturation case from the clamp.
The lesson is that a test that mirrors the implementation does not test anything. It just takes a photograph of the bug.
The part I did not expect: I couldn't build it
The driver is macOS-only. It uses CoreAudio and macOS socket options, and I was developing on Windows. I could not compile the project, let alone run its test suite.
So I split the problem. I wrote a small standalone program with the new encode and the existing decode, and ran it on all 16.7 million values, on my own machine and in a second environment. I built the actual new test file against the actual patched codec lines in a stub harness. Everything that depends on macOS I left to the project's CI, and I said so in the pull request instead of pretending otherwise.
If you can't run something, find the smallest part you can run, and be honest about the rest.
What I took away
- Encode and decode are a pair. Write them together, and test them as a pair, with an identity check.
- Tolerances hide bugs. "Close enough" tests answer a different question than "exactly right". For anything meant to be bit-transparent, test for equality.
- Exhaustive beats clever. When the input space is 16 million values, just check all of them.
- Know your number system. Two's complement's one extra negative value, truncation toward zero, and floating-point exactness for powers of two were the whole story.
At the time of writing, the fix is open as pull request #222, waiting on CI and review. The only thing I left out on purpose is sharing one decode between the receiver and the codecs, which is tracked separately as #133.
If you are looking for a first open-source contribution, find a good first issue, read the surrounding code before you touch it, ask to be assigned, and write the test first. The bug is usually smaller than you fear, and proving the fix is the most satisfying part.
I'm Nartan, a computer science student at NIE Mysuru who learns by building things. You can find me on GitHub.
About the author
- Hi, I'm Nartan, a Software Engineer focused on full-stack and AI applications, in my final year of B.E. Computer Science at NIE Mysuru. I came to CSE from a diploma in Automobile Engineering, so I like knowing how systems really work. What I've built: • CreatorLens AI: curates Instagram creator profiles; a staging pipeline gets past Vercel's 10-second limit • Unpack: AI codebase documentation; a custom scanner cuts LLM input by 98% • CyberCube: AI threat detection and SOC platform with explainable alerts Stack: React, Node.js, FastAPI, Flutter, Python, JavaScript, C++, SQL, Firebase, MongoDB, Supabase, PostgreSQL, Docker, GitHub Actions. I write about real project problems: serverless limits, LLM context and cost, API security, and shipping ML into products. Open to Software Engineering internships in India. GitHub: github.com/NartanNaik LinkedIn: linkedin.com/in/nartan-naik-90499a359
Comments
Join the discussionNo comments yet. Be the first to comment.