在Swift中使用AudioBufferList

在Swift中使用AudioBufferList,第1张

在Swift中使用AudioBufferList

编辑: 亚当·里特瑙尔的答案可能是现在最好的答案。要对此进行扩展,您可以查看iOS
8.3核心音频更改
中的新实用程序功能/类型。

UnsafeMutableAudioBufferListPointer
可用于读取/访问某些给定数据

struct UnsafeMutableAudioBufferListPointer {    init(_ p: UnsafeMutablePointer<AudioBufferList>)    var count: Int    subscript (index: Int) -> AudioBuffer { get nonmutating set }}

您可以使用AudioBuffer和AudioBufferList上的扩展来分配自己的扩展名:

extension AudioBufferList {    static func sizeInBytes(maximumBuffers maximumBuffers: Int) -> Int    static func allocate(maximumBuffers maximumBuffers: Int) -> UnsafeMutableAudioBufferListPointer}extension AudioBuffer {    init<Element>(_ typedBuffer: UnsafeMutableBufferPointer<Element>, numberOfChannels: Int)}

旧答案:

这有点棘手,因为

AudioBufferList
它实际上是一个可变大小的结构。这意味着它被声明为具有single
AudioBuffer
,但实际上它具有
mNumberBuffers
成员指定的数量。这个概念不能很好地转化为Swift,这就是为什么您看到了
varmBuffers: (AudioBuffer)

因此,访问这些缓冲区及其数据的规范方法是使用

UnsafeArray
。下面的代码提供了一些想法,但是
UnsafePointer
UnsafeArray
没有得到很好的记录,因此这可能是错误的。

// ***WARNING: UNTESTED CODE AHEAD***let foo: UnsafePointer<AudioBufferList> // from elsewhere...// This looks intuitive, but accessing `foo.memory` may be doing a copy.let bufs = UnsafeArray<AudioBuffer>(start: &foo.memory.mBuffers, length: Int(foo.memory.mNumberBuffers))// This is another alternative that should work...let bufsStart = UnsafePointer<AudioBuffer>(UnsafePointer<UInt32>(foo) + 1) // Offset to mBuffers memberlet bufs = UnsafeArray<AudioBuffer>(start: bufsStart, length: Int(foo.memory.mNumberBuffers))// Hopefully this isn't doing a copy, but it shouldn't be too much of a problem anyway.let buf: AudioBuffer = bufs[0] // or you could use a for loop over bufs, etc.typealias MySample = Float32let numSamples = Int(buf.mDataByteSize / UInt32(sizeof(MySample)))let samples = UnsafeArray<MySample>(start: UnsafePointer<MySample>(buf.mData), length: numSamples)// Now use the samples array...

这似乎在 *** 场上可行,但对我来说很难测试真实的音频数据。特别是,我不确定100%

start:&foo.memory.mBuffers
会按预期使用。(尽管数据似乎存在,但它返回的指针与原始指针不同。)试一下并报告!

编辑:顺便调试一下,您可以例如:

(lldb) p foo(UnsafePointer<AudioBufferList>) $R1 = (value = Builtin.RawPointer = 0x0000000100700740)(lldb) expr -lc -- ((int*)0x0000000100700740)[0](int)  = 42(lldb) expr -lc -- ((int*)0x0000000100700740)[1](int)  = 43...


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5642216.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存