Hi, I'm Glenn, from Android Audio.
I like putting stuff together, especially music and software.
Today, I want to share some cool tricks
I've been learning about audio formats.
For most of our careers, we've been
told to use integer, or fixed point arithmetic for audio.
There's some good reasons why we all use integers.
They're great.
Fixed point, too.
From digital to analog converters,
to CDs to USB audio, integers and fixed point are everywhere.
As we all know, hackers avoid Floats as much as we do basic.
So let's review the reasons why we don't use floating point.
First of all, it's not there.
No affordable floating point unit.
This was my first FPU.
5,000 flops, and so expensive, we only
used it when we absolutely had two.
Floats are way big.
Four bytes each.
OK, this is just gross.
But you get the idea.
Floats are huge.
This one is my favorite.
The decadence of Rome, I mean Floats.
Even if you have the CPU and memory for floating point,
it's just sinful to waste resources so extravagantly,
on something that's clearly a luxury.
It's kind of like using a 32-bit, um--
I mean 64-bit ant to represent a Boolean.
You do realize this is what actually happens now?
So, Floats are clearly out.
Or so we think.
Now, what do we really want from our numbers in audio?
Wide dynamic range for soft and loud, consistent accuracy
across that range, and lots of headroom.
No clipping.
So let's examine our friends.
Integer and fixed point.
First, integer.
This is simple.
Adjacent values differ by one.
Integers are represented in two's complement binary.
Fixed point is the same, with an implied binary point.
For audio, the implied binary point
is almost always at the left of the most significant bit.
And adjacent values differ by a constant power of two.
It's still two's complement binary.
Now earlier, we thought Floats work out.
But are they really?
Let's look at them anyway-- don't worry,
it's just to make fun of them.
Or maybe not?
Floating point starts with a fixed point mantissa.
The number of bits in the mantissa
determines the overall accuracy.
Then, we add an integer exponent.
This increases the range.
The wider the exponent, the more range.
There's a signed bit too, but we'll ignore that for now.
Now, remember what we all want in audio.
Wide dynamic range for soft and loud, consistent accuracy
across that range, and lots of headroom to prevent clipping.
Let's see how well Float does on all three.
We'll start with dynamic range.
Here's the formula for a Float, combining sine, mantissa
and exponent.
Turns out the exponent increases the dynamic range dramatically,
compared to integer, or fixed point.
How many db or bits you need for content delivery
is controversial.
I'm not going there.
What is certain, is that a wide dynamic range is useful
internally.
And Float provides that.
Now, let's look at accuracy.
Remember, adjacent integer values differ by one,
and adjacent fixed point values by a constant power of two.
Sounds good at first.
Within some fixed point, though the differences are constant,
the ratios between adjacent values
are not constant, for either integer, or fixed point.
Whoah!
What's that going to do to consistent accuracy?
We lose accuracy, where we need it most.
Quiet signals.
In this graph, the ratios approach two
as we get quieter, instead of one.
And here's our ratio chart for Float.
The ratios between adjacent values
are now approximately constant, and approach one.
The distances are logarithmic.
Notice the tighter packing near zero.
This means consistent accuracy across the full dynamic range.
So, soft and loud both sound great.
Finally, let's look at Headroom.
Headroom is a safety zone, above your nominal maximum level,
to help prevent clipping during intermediate calculations,
and transience.
This is where floating point really shines.
It's almost impossible to clip samples
during intermediate calculations,
because the range to afloat is astronomical.
Maximize your headroom.
So, it's time to think differently.
Those were some compelling reasons
to consider floating point, in your next audio app.
You don't need to be an expert in numerical analysis
to use floating point.
But floating point is not a panacea.
You do need to know the gotchas, and there are a few.
Check out the references at the end,
with some tips and tricks that will
help you avoid the most common problems.
So, I ask you to consider floating point
in your next audio app.
And what about all those reasons not to use floating point?
Now, you're probably expecting me
to say those reasons aren't valid anymore.
Uh uh.
Some of them are still true.
Especially the moral depravity one.
But the benefits mean it's time to hold our noses,
and consider floating point anyway.
Be forewarned.
If you use floating point, others
will try to shame you during code reviews.
So, be prepared for glory.
Be proud.
Wear the scarlet F, you'll do fine.
Make sure to check out this link for more information.
I'm Glenn Kasten.
Thank you for watching.