2016-07-11 22 views
0

要查询你做下面的一个MatrixMixer AudioUnit:kAudioUnitProperty_MatrixLevels斯威夫特

// code from MatrixMixerTest sample project in c++ 

UInt32 dims[2]; 
UInt32 theSize = sizeof(UInt32) * 2; 
Float32 *theVols = NULL; 
OSStatus result; 


ca_require_noerr (result = AudioUnitGetProperty (au, kAudioUnitProperty_MatrixDimensions, 
         kAudioUnitScope_Global, 0, dims, &theSize), home); 

theSize = ((dims[0] + 1) * (dims[1] + 1)) * sizeof(Float32); 

theVols = static_cast<Float32*> (malloc (theSize)); 

ca_require_noerr (result = AudioUnitGetProperty (au, kAudioUnitProperty_MatrixLevels, 
         kAudioUnitScope_Global, 0, theVols, &theSize), home); 

kAudioUnitProperty_MatrixLevelsAudioUnitGetProperty返回值(在文档和示例代码中定义),一个浮点32。

我试图在swift中查找矩阵级别,并且可以在没有问题的情况下获得矩阵维度。但我不知道如何创建一个空的Float32元素数组,它是一个UnsafeMutablePointer<Void>。这是我曾尝试没有成功:

var size = ((dims[0] + 1) * (dims[1] + 1)) * UInt32(sizeof(Float32)) 
var vols = UnsafeMutablePointer<Float32>.alloc(Int(size)) 

在MatrixMixerTest阵列使用,如:theVols[0]

+0

“没有成功”的意思是什么? – Alexander

+0

我无法使用'vols'作为数组,它使用'EXC_BAD_ACCESS'崩溃 – GWRodriguez

+0

您试图在数组边界内访问的索引是什么? – Alexander

回答

2

可能需要根据你如何转化的其他部分, 但你的C的最后部分进行修改++代码可以写在斯威夫特这样的:

theSize = ((dims[0] + 1) * (dims[1] + 1)) * UInt32(sizeof(Float32)) 

    var theVols: [Float32] = Array(count: Int(theSize)/sizeof(Float32), repeatedValue: 0) 

    result = AudioUnitGetProperty(au, kAudioUnitProperty_MatrixLevels, 
      kAudioUnitScope_Global, 0, &theVols, &theSize) 
    guard result == noErr else { 
     //... 
     fatalError() 
    } 

当C函数基于API声称一个UnsafeMutablePointer<Void>,你只需要一个任意类型的Array变量,并通过我t作为inout参数。

+0

这完全工作。非常感谢 – GWRodriguez