Last active
November 9, 2022 13:55
-
-
Save SteveTrewick/2889beb15e65d5e89971a1a82054498b to your computer and use it in GitHub Desktop.
Set the buffer size for AVFoundation captures including, e.g, AVCaptureSession via an AVCaptureAudioDataOutput
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// couple of useful functions for dealing with an AVCaptureSession | |
// chucked out there for posterity and search | |
// What led to me to do this : Set size of AVCaptureAudioDataOutput buffer | |
// What might also lead you here : Swift AudioValueTranslation | |
// PLEASE NOTE : There are no error checks in this code, demo purposes only. | |
import AVFoundation | |
struct Device { // I am great at names! | |
// original code from a gist : https://gist.github.com/pdesantis/b1899706ea14045eb345de5584d2a3bf | |
// updated to remove the warnings and make the behaviour defined. | |
// get the AudioDeviceID from te uniqueID string | |
static func id(of uniqueID: String) -> AudioDeviceID { | |
// simples ... | |
var uuid = uniqueID as CFString | |
var deviceID = kAudioDeviceUnknown | |
// still so so .. | |
var address = AudioObjectPropertyAddress ( | |
mSelector: kAudioHardwarePropertyDeviceForUID, | |
mScope : kAudioObjectPropertyScopeGlobal, | |
mElement : kAudioObjectPropertyElementMaster | |
) | |
let insize = UInt32(MemoryLayout<CFString>.stride) | |
let outsize = UInt32(MemoryLayout<AudioObjectID>.stride) | |
var transize = UInt32(MemoryLayout<AudioValueTranslation>.stride) | |
// real shit ... | |
withUnsafeMutablePointer(to: &uuid) { uid in | |
withUnsafeMutablePointer(to: &deviceID) { dev in | |
var translation = AudioValueTranslation ( | |
mInputData : uid, | |
mInputDataSize : insize, | |
mOutputData : dev, | |
mOutputDataSize: outsize | |
) | |
AudioObjectGetPropertyData (UInt32(kAudioObjectSystemObject), &address, 0, nil, &transize, &translation) | |
} | |
} | |
// wait, but why? | |
// well, long story : https://developer.apple.com/forums/thread/674633 | |
// short : swift does not guarantee the life cycle of the pointers you create with & | |
// this is fine for a scalar but pointers to pointers in a struct? Undefined. | |
return deviceID // dont forget to check that. | |
} | |
// set the capture sample buffer size for AVCaptureAudioDataOutput in an AVCaptureSession | |
// or just generally for any core audio device you have the ID of | |
static func setBufferSize(to size: UInt32, for device: UInt32) { | |
var size = size | |
var address = AudioObjectPropertyAddress ( | |
mSelector: kAudioDevicePropertyBufferFrameSize, | |
mScope : kAudioDevicePropertyScopeInput, | |
mElement : kAudioObjectPropertyElementMaster | |
) | |
AudioObjectSetPropertyData(device, &address, 0, nil, UInt32(MemoryLayout<UInt32>.stride), &size) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment