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

Core Audio Swift Equalizer adjusts all bands at once?

我在 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),但这些似乎根本不起作用。感谢您的帮助!

由于评论不足,评论添加为答案。

以下代码在 Objective-c 中,但它应该有助于确定您的问题。

有很多地方这可能会失败。首先,您应该检查 AudioUnitSetParameter 的状态,实际上是所有 AudioUnit 调用的状态,因为这会让您更清楚地知道您的代码在哪里失败。

我已经在 Objective-C 中成功完成了这项工作,并且如果您需要,我可以提供一个测试应用程序,它显示了完整的图形设置以及通过移动滑块设置波段和增益.. .回到你的具体问题。以下内容对我来说很好,这可能会帮助您排除特定部分。

您可以尝试获取当前"gain",这将表明您的手环设置是否正确。

  - (AudioUnitParameterValue)gainForBandAtPosition:(uint)bandPosition
  {
      AudioUnitParameterValue gain;
      AudioUnitParameterID parameterID = kAUNBandEQParam_Gain + bandPosition;
      OSStatus status = AudioUnitGetParameter(equalizerUnit,
                                             parameterID,
                                  kAudioUnitScope_Global,
                                                       0,
                                                 &gain);

    if (status != noErr) {
    @throw [NSException exceptionWithName:@"gettingParamGainErrorException"
                                   reason:[NSString stringWithFormat:@"OSStatus Error on getting EQ Gain! Status returned %d).", (int)status]
                                 userInfo:nil];
    }

    return gain;
  }

然后可以通过以下方式设置增益;

  - (void)setGain:(AudioUnitParameterValue)gain forBandAtPosition:(uint)bandPosition
  {
      AudioUnitParameterID parameterID = kAUNBandEQParam_Gain + bandPosition;
      OSStatus status = AudioUnitSetParameter(equalizerUnit,
                                                parameterID,
                                     kAudioUnitScope_Global,
                                                          0,
                                                       gain,
                                                        0);

    if (status != noErr) {
    @throw [NSException exceptionWithName:@"settingParamGainAudioErrorException"
                                   reason:[NSString stringWithFormat:@"OSStatus Error on setting EQ gain! Status returned %d).", (int)status]
                                 userInfo:nil];
    }

  }

最后,你想设置什么值,有效范围(如果我没记错的话)是-125.0到25.0