本文介绍了使用AudioBufferList与Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Swift中有一个桥接函数,其中一个参数在C中是的C和Swift定义: code>和 AudioBuffer 以方便...

Here are the C and Swift definitions for AudioBufferList and AudioBuffer for convenience...

// C
struct AudioBufferList
{
    UInt32      mNumberBuffers;
    AudioBuffer mBuffers[1]; // this is a variable length array of mNumberBuffers elements
    // ...and a bit more for c++
}


struct AudioBuffer
{
    UInt32  mNumberChannels;
    UInt32  mDataByteSize;
    void*   mData;
};

...

// SWIFT

struct AudioBufferList {
    var mNumberBuffers: UInt32
    var mBuffers: (AudioBuffer)
}

struct AudioBuffer {
    var mNumberChannels: UInt32
    var mDataByteSize: UInt32
    var mData: UnsafePointer<()>
}


推荐答案

奇怪的是,前面的类型实际上与Swift工作时,它建议UnsafeMutableAudioBufferListPointer。其中可以使用UnsafeMutablePointer参数初始化。这种类型是一个MutableCollectionType,并为包含的音频缓冲区提供了下标和生成器的访问。

I found this by accident. Oddly the type ahead was actually working with Swift when it suggested UnsafeMutableAudioBufferListPointer. Which you can initialize with an UnsafeMutablePointer argument. This type is a MutableCollectionType and provides subscript and generator access to the contained Audio Buffers.

例如,您可以使用以下代码设置ABL来静默:

For example you can set an ABL to silence with the following code

func renderCallback(ioData: UnsafeMutablePointer<AudioBufferList>) -> OSStatus {

    let abl = UnsafeMutableAudioBufferListPointer(ioData)

    for buffer in abl {

        memset(buffer.mData, 0, Int(buffer.mDataByteSize))
    }

    return noErr
}

这篇关于使用AudioBufferList与Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 21:19