Closed Bug 1633747 Opened 6 months ago Closed 6 months ago

Handle NaN better in Web Audio

Categories

(Core :: Web Audio, defect, P1)

defect

Tracking

()

RESOLVED FIXED
mozilla78
Tracking Status
firefox78 --- fixed

People

(Reporter: padenot, Assigned: padenot)

Details

Attachments

(3 files)

It's problematic to pass buffers containing NaN to the OS. Sheppy reports that it kills his system audio and he needed to reboot, and in my case, it kills the audio of the machine until the page that were outputing the audio is closed. We are both on OSX.

Since it's fairly easy to make a mistake in an AudioWorkletProcessor and output NaN, we need to do something about it.

I have some vector code that change NaN to 0.0f running in about 30ns for a block of 128 frames (compared to 250+ns with naive scalar code) on x86 (I haven't tested ARM yet), so the performance impact is too small to matter.

I don't know if we want to do this per AudioWorkletProcessor instance, and potentially log a message to the developer console. For now the patch is going to be in GraphDriver.cpp.

Assignee: nobody → padenot
Priority: -- → P1
Attachment #9144099 - Attachment description: Bug 1633747 - Convert NaN to zero before handing back the buffers to the OS. r?karlt → Bug 1633747 - Flush NaNs to zeros before handing back the audio output buffer to the OS. r?karlt
Attached file microbenchmark.cpp

A micro-benchmark for this vector code, between scalar, normal SSE, unrolled SSE.

Typical output on my machine (Macbook 2018 core i9, unclear which exact CPU model):

prep:	       1808298ns
scalar code:	145492ns
single sse:	 55848ns
vector sse:	 40060ns

_mm_cmpord_ps is the only new instruction we use here (compared to the other functions), and is in SSE, so it's fine.

I'll note that even with this, Sheppy's page doesn't stop affecting the rest of the audio of the computer, because apparently, computing with NaN makes SpiderMonkey deoptimizes heavily, and the page is around 1.5x to 2x the real-time budget, and the thread is shared: I'm on a mac, that doesn't have audioipc preffed on, so it's using a thread from CoreAudio, macOS audio API. So there is audio, but it's under-runing constantly.

But at least NaN aren't fed into the OS's mixer, so some audio comes out, and it won't confuse audio hardware.

Pushed by padenot@mozilla.com:
https://hg.mozilla.org/integration/autoland/rev/4c436ccdc5a2
Flush NaNs to zeros before handing back the audio output buffer to the OS.  r=karlt
Pushed by padenot@mozilla.com:
https://hg.mozilla.org/integration/autoland/rev/0449241c55ca
Don't call NaN fixing code on platforms where samples are in i16.
Status: NEW → RESOLVED
Closed: 6 months ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla78
You need to log in before you can comment on or make changes to this bug.