Change audio volume of some channels using AVAudioEngine
Asked Answered
N

1

1

I'm working on a mac application that mix audio from a VideoCard and an external audio card.

How can I change volume on each channel (not each source) independently using AVAudioEngine.

Let's say I have an AVAudioPCMBuffer with 16 channels interleaved and I want channel 1 and 2 volume to 50% and other 100%

Should I convert from 1 AVAudioPCMBuffer with 16 channels to 16 mono AVAudioPCMBuffer and have one AVPlayerNode for each of them ? Then change the volume on each AVPlayerNode ?

Or can I keep o is there a way to change the underlying Audio Unit from the AVAudioMixerNode ?

Or should I use CoreAudio AudioUnits directly?

Naughton answered 8/11, 2018 at 12:44 Comment(0)
N
4

Changing audio volume by channel instead of by input, requires MatrixMixer. AVAudioEngine MainMixer is not a matrix mixer (mxmx) but a multi channel mixer (mcmx).

In order to use a matrix mixer, use this code:

AudioComponentDescription   mixerUnitDescription;

mixerUnitDescription.componentType          = kAudioUnitType_Mixer;
mixerUnitDescription.componentSubType       = kAudioUnitSubType_MatrixMixer;
mixerUnitDescription.componentManufacturer  = kAudioUnitManufacturer_Apple;
mixerUnitDescription.componentFlags         = 0;
mixerUnitDescription.componentFlagsMask     = 0;

[AVAudioUnit instantiateWithComponentDescription:mixerUnitDescription options:0 completionHandler:^(__kindof AVAudioUnit * _Nullable mixer, NSError * _Nullable error) {

}];

And change audio levels using

AudioUnitSetParameter([_mixer audioUnit], kMatrixMixerParam_Volume, kAudioUnitScope_Input, i, volume, 0);
Naughton answered 20/11, 2018 at 14:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.