Distorted Audio When Recording External Mics With AVCaptureSession and AVAssetWriter

I’m working on a macOS app, written in Swift. My goal is to record audio from an external microphone, e.g., one connected via USB.

For this, I’m using an AVCaptureSession and recording its output with an AVAssetWriter. This works perfectly in principle (and reliably with internal microphones, for example).

The problem occurs after my app has successfully completed the first recording and I then want to make additional recordings (which makes me think it might be process-dependent, because it works again after restarting the app).

The problem: Noisy or distorted-sounding audio files. In addition, the following error message appears in the Console from CoreAudio / its AudioConverter:

Input data proc returned inconsistent 512 packets for 2048 bytes; at 3 bytes per packet, that is actually 682 packets

It is easy to reproduce. This problem is reproducible even if I don’t configure the AVAssetWriter manually and instead let it receive its audioSettings using a preset from an AVOutputSettingsAssistant. I’m running on macOS 15.0 (24A335).

I’ve filed a feedback including a demo project → FB15333298 🎟️

I would greatly appreciate any help!

Have a great day, Martin

Hello @mrtn, thank you for submitting a bug report. If possible, please capture a sysdiagnose after reproducing the issue locally and attach it to your bug report, mentioning the exact time you were able to reproduce the issue.

The sysdiagnose file is about 500 MB in size, and I’m not sure how to provide it to you. The issue, as described, is reliably reproducible. I have filed a detailed feedback, created and shared a demo project, and opened this thread upon request after my appeal for code-level support. I am hopeful that you can investigate the root cause of the problem using the information already provided.

Hello @mrtn, thank you again for submitting a bug report with a test project. You mentioned that using an Opal C1 webcam, AVAssetWriter fails. What is the error you get when it fails?

Also, when you get a distorted recording using an Apple Studio Display, AVAssetWriter does not fail, but logs the following error message:

Input data proc returned inconsistent 512 packets for 2048 bytes; at 3 bytes per packet, that is actually 682 packets

This message is indicative of a format mismatch. If you register for the formatDescriptionDidChangeNotification, do you get notified of any format changes before a faulty recording? In addition to that, is the runtimeErrorNotification ever triggered on your capture session?

It’s been a month—do you have any updates on this?

Not a solution—but workaround: Using a different filename for each recording—in this case, the recordings sound clean.

Distorted Audio When Recording External Mics With AVCaptureSession and AVAssetWriter
 
 
Q