【发布时间】:2020-07-02 15:39:02
【问题描述】:
我一直在尝试从 swift 使用 Apple 的 CoreAudio。
我发现了许多关于如何枚举设备上的流和通道的示例。
但是,在调用UnsafeMutablePointer<AudioBufferList>.allocate() 时,它们似乎都使用了不正确的大小。
他们首先请求属性数据大小,它返回字节数。
然后他们使用这个字节数来分配那个大小的(不安全的)AudioBufferList(使用字节数作为列表的大小!)。
请在下面查看我的 cmets:
var address = AudioObjectPropertyAddress(
mSelector:AudioObjectPropertySelector(kAudioDevicePropertyStreamConfiguration),
mScope:AudioObjectPropertyScope(kAudioDevicePropertyScopeInput),
mElement:0)
var propsize = UInt32(0);
var result:OSStatus = AudioObjectGetPropertyDataSize(self.id, &address, 0, nil, &propsize);
if (result != 0) {
return false;
}
// ABOVE: propsize is set to number of bytes that property data contains, typical number are 8 (no streams), 24 (1 stream, 2 interleaved channels)
// BELOW: propsize is used for AudioBufferList capacity (in number of buffers!)
let bufferList = UnsafeMutablePointer<AudioBufferList>.allocate(capacity:Int(propsize))
result = AudioObjectGetPropertyData(self.id, &address, 0, nil, &propsize, bufferList);
if (result != 0) {
return false
}
let buffers = UnsafeMutableAudioBufferListPointer(bufferList)
for bufferNum in 0..<buffers.count {
if buffers[bufferNum].mNumberChannels > 0 {
return true
}
}
这一直有效,因为它分配的内存比UnsafeMutablePointer<AudioBufferList> 需要的多得多,但这显然是错误的。
我一直在寻找一种从AudioObjectGetPropertyDataSize() 返回的字节数中正确分配UnsafeMutablePointer<AudioBufferList> 的方法,但我一整天都找不到任何东西。请帮忙;)
【问题讨论】:
-
这个答案有帮助吗? stackoverflow.com/a/38982906/3141234 您想分配一个足够大的
AudioBufferList以适合您尝试访问的属性,然后通过引用传递它 -
@Alexander-ReinstateMonica 回答是错误地使用了道具尺寸,下面 OOPer 的回答是正确的
标签: swift core-audio