Core Audio Swift均衡器一次调整所有波段?

时间:2015-04-30 14:18:04

标签: ios swift core-audio audiounit equalizer

我在Swift中设置kAudioUnitSubType_NBandEQ时遇到问题。这是我的初始化EQ的代码:

  var cd:AudioComponentDescription = AudioComponentDescription(componentType: OSType(kAudioUnitType_Effect),componentSubType: OSType(kAudioUnitSubType_NBandEQ),componentManufacturer: OSType(kAudioUnitManufacturer_Apple),componentFlags: 0,componentFlagsMask: 0)

    // Add the node to the graph
    status = AUGraphAddNode(graph, &cd, &MyAppNode)
    println(status)

     // Once the graph has been opened get an instance of the equalizer
    status = AUGraphNodeInfo(graph, self.MyAppNode, nil, &MyAppUnit)
    println(status)

    var eqFrequencies: [UInt32] = [ 32, 250, 500, 1000, 2000, 16000 ]

    status = AudioUnitSetProperty(
        self.MyAppUnit,
        AudioUnitPropertyID(kAUNBandEQProperty_NumberOfBands),
        AudioUnitScope(kAudioUnitScope_Global),
        0,
        eqFrequencies,
        UInt32(eqFrequencies.count*sizeof(UInt32))
    )
    println(status)

    status = AudioUnitInitialize(self.MyAppUnit)
    println(status)

    var ioUnitOutputElement:AudioUnitElement = 0
    var samplerOutputElement:AudioUnitElement = 0

    AUGraphConnectNodeInput(graph, sourceNode, sourceOutputBusNumber, self.MyAppNode, 0)

    AUGraphConnectNodeInput(graph, self.MyAppNode, 0, destinationNode, destinationInputBusNumber)

然后应用频率增益的变化,我的代码如下:

if (MyAppUnit == nil) {return}
    else{

        var bandValue0 :Float32 = tenBands.objectAtIndex(0) as! Float32
        var bandValue1 :Float32 = tenBands.objectAtIndex(1) as! Float32
        var bandValue2 :Float32 = tenBands.objectAtIndex(2) as! Float32
        var bandValue3 :Float32 = tenBands.objectAtIndex(3) as! Float32
        var bandValue4 :Float32 = tenBands.objectAtIndex(4) as! Float32
        var bandValue5 :Float32 = tenBands.objectAtIndex(5) as! Float32

        AudioUnitSetParameter(self.MyAppUnit, 0, AudioUnitScope(kAudioUnitScope_Global), 0, bandValue0, 0);
        AudioUnitSetParameter(self.MyAppUnit, 1, AudioUnitScope(kAudioUnitScope_Global), 0, bandValue1, 0);
        AudioUnitSetParameter(self.MyAppUnit, 2, AudioUnitScope(kAudioUnitScope_Global), 0, bandValue2, 0);
        AudioUnitSetParameter(self.MyAppUnit, 3, AudioUnitScope(kAudioUnitScope_Global), 0, bandValue3, 0);
        AudioUnitSetParameter(self.MyAppUnit, 4, AudioUnitScope(kAudioUnitScope_Global), 0, bandValue4, 0);
        AudioUnitSetParameter(self.MyAppUnit, 5, AudioUnitScope(kAudioUnitScope_Global), 0, bandValue5, 0);
}

有谁可以指出我在这里做错了什么?我认为它与AudioUnitSetParameter中的第二个变量有关。我已尝试使用AudioUnitParameterID(0)和AudioUnitParameterID(kAUNBandEQParam_Gain + 1)来获取此值,但这些似乎根本不起作用。任何帮助表示赞赏!

0 个答案:

没有答案