在Spotify iOS SDK中混合两个AUNode

时间:2017-07-05 02:05:34

标签: ios audio core-audio spotify

我尝试使用动态处理器和一堆过滤器来压缩spotify方法connectOutputBus中的特定频段但是当我混合kAudioUnitSubType_MultiChannelMixer中的节点时只有声音第一个添加的节点出来了。

obs:我实际上使用sourceNodeCopy上的过滤器来删除将在souceNode上压缩的频率,但为了保持简短,我省略了它们。

以下是代码:

override func connectOutputBus(_ sourceOutputBusNumber: UInt32, ofNode  sourceNode: AUNode, toInputBus destinationInputBusNumber: UInt32, ofNode destinationNode: AUNode, in graph: AUGraph!) throws {

  let sourceNodeCopy = sourceNode //original node without the harsh freq

  //create a filter for the harsh frequencies
  var filterDescription = AudioComponentDescription()
  filterDescription.componentType = kAudioUnitType_Effect
  filterDescription.componentSubType = kAudioUnitSubType_BandPassFilter
  filterDescription.componentManufacturer = kAudioUnitManufacturer_Apple
  filterDescription.componentFlags = 0
  filterDescription.componentFlagsMask = 0

  AUGraphAddNode(graph, &filterDescription, &filterNode!) // Add the filter node
  AUGraphNodeInfo(graph, filterNode!, nil, &filterUnit!) // Get the Audio Unit from the node
  AudioUnitInitialize(filterUnit!) // Initialize the audio unit
  // Set filter params
  AudioUnitSetParameter(filterUnit!, kBandpassParam_CenterFrequency, kAudioUnitScope_Global, 0, 10038, 0)

  //create a processor to compress the frequency
  var dynamicProcessorDescription = AudioComponentDescription()
  dynamicProcessorDescription.componentType = kAudioUnitType_Effect
  dynamicProcessorDescription.componentSubType = kAudioUnitSubType_DynamicsProcessor
  dynamicProcessorDescription.componentManufacturer = kAudioUnitManufacturer_Apple
  dynamicProcessorDescription.componentFlags = 0
  dynamicProcessorDescription.componentFlagsMask = 0

  // Add the dynamic processor node
  AUGraphAddNode(graph, &dynamicProcessorDescription, &dynamicProcessorNode)
  AUGraphNodeInfo(graph, dynamicProcessorNode, nil, &dynamicProcessorUnit)
  AudioUnitInitialize(dynamicProcessorUnit!)

  // Set compressor params
  AudioUnitSetParameter(dynamicProcessorUnit!, kDynamicsProcessorParam_Threshold, kAudioUnitScope_Global, 0, -35, 0)
  AudioUnitSetParameter(dynamicProcessorUnit!, kDynamicsProcessorParam_AttackTime, kAudioUnitScope_Global, 0, 0.02, 0)
  AudioUnitSetParameter(dynamicProcessorUnit!, kDynamicsProcessorParam_ReleaseTime, kAudioUnitScope_Global, 0, 0.04, 0)
  AudioUnitSetParameter(dynamicProcessorUnit!, kDynamicsProcessorParam_HeadRoom, kAudioUnitScope_Global, 0, 0, 0)

  //mixer
  var mixerDescription = AudioComponentDescription()
  mixerDescription.componentType = kAudioUnitType_Mixer
  mixerDescription.componentSubType = kAudioUnitSubType_MultiChannelMixer
  mixerDescription.componentManufacturer = kAudioUnitManufacturer_Apple
  mixerDescription.componentFlags = 0
  mixerDescription.componentFlagsMask = 0

  AUGraphAddNode(graph, &mixerDescription, &audioEffectsController.mixerNode)
  AUGraphNodeInfo(graph, audioEffectsController.mixerNode, nil,
                    &audioEffectsController.mixerUnit)
  AudioUnitInitialize(audioEffectsController.mixerUnit!)

  AudioUnitSetParameter(mixerUnit!, kMultiChannelMixerParam_Volume, kAudioUnitScope_Input, 0, 1.0, 0);
    AudioUnitSetParameter(mixerUnit!, kMultiChannelMixerParam_Volume, kAudioUnitScope_Output, 0, 1.0, 0);


  //connect the nodes
  AUGraphConnectNodeInput(graph, sourceNode, sourceOutputBusNumber, filterNode, 0)
  AUGraphConnectNodeInput(graph, filterNode, sourceOutputBusNumber, dynamicProcessorNode, 0)

  AUGraphConnectNodeInput(graph, sourceNodeCopy, sourceOutputBusNumber, mixerNode, 0)
  AUGraphConnectNodeInput(graph, fiterNode, sourceOutputBusNumber, mixerNode, 0)

  //connect the mixer to the output
  AUGraphConnectNodeInput(graph, audioEffectsController.mixerNode, 0, destinationNode, destinationInputBusNumber)

1 个答案:

答案 0 :(得分:0)

在您的代码中,您可以像这样连接节点

AUGraphConnectNodeInput(graph, sourceNodeCopy, sourceOutputBusNumber, mixerNode, 0)
AUGraphConnectNodeInput(graph, fiterNode, sourceOutputBusNumber, mixerNode, 0)

如果您这样做,则会将sourceNodeCopyfilterNode连接到MixerNode的同一输入(0)。但只有一个可以连接......

你应该试试这个

AUGraphConnectNodeInput(graph, sourceNodeCopy, sourceOutputBusNumber, mixerNode, 0)
AUGraphConnectNodeInput(graph, fiterNode, sourceOutputBusNumber, mixerNode, 1)