0
votes

In iOS system settings, To adjust the volume balance, head to Settings > General > Accessibility. Here, you can adjust the left/right volume balance or set the audio to mono.

But can we do it in our app with our code ? objective-c or swift.

My app need play audio,video and virtual midi audio by 'Audio Unit', And I have try follow things:

  1. When I play audio,I can use pan property with AVAudioPlayer. It's work.
  2. When I play video with AVPlayer, Sorry. Here haven't pan property with AVPlayer. And I'm refused how to implement this.
  3. When I play midi virtual audio by Audio Unit. I found related question in How to set pan in IOS Audio Unit Framework

    AudioUnitParameterValue panValue = -1; // panned almost dead-right. possible values are between -1 and 1 int result = AudioUnitSetParameter(mixerUnit, kMultiChannelMixerParam_Pan, kAudioUnitScope_Input, 0, panValue, 0); if (result == 0) { NSLog("success"); }

I get success result,but right channel steal have audio output.

So I'm stuck in video and midi virtual audio part. How to set pan with the audio output? Any advice to me I'm so appreciate.

2

2 Answers

0
votes

I have the same problem when using the AudioToolbox library. However, I found the following code "AudioBalanceFade", but I don't know what "UnsafePointer " parameter is. This code works, but I can't find an example. Maybe you can solve this part, and I'm glad you told me how you did it. here Apple developer code Structure

Sorry for my English!

0
votes

Finally, I solved this out. But it's only my fault.

First of all, this answer is correct. It's not work for me, because I missed the mixer audio unit. I just set the kMultiChannelMixerParam_Pan parameter to a kAudioUnitSubType_MIDISynth Music Device.

So I need create a mixer unit, and connect to my midi synth unit. something like this:

    // create output unit
    AudioComponentDescription cd;
    cd.componentType = kAudioUnitType_Output;
    cd.componentSubType =  kAudioUnitSubType_RemoteIO;
    cd.componentManufacturer = kAudioUnitManufacturer_Apple;
    cd.componentFlags = cd.componentFlagsMask = 0;
    
    err = AUGraphAddNode(_AUGraph, &cd, &_remoteIONode);
    [self checkError:err];
    
    // create midi synth music device unit
    cd.componentType = kAudioUnitType_MusicDevice;
    cd.componentSubType = kAudioUnitSubType_MIDISynth;//kAudioUnitSubType_Sampler;
    err = AUGraphAddNode(_AUGraph, &cd, &_midisynthNode);
    [self checkError:err];
    
    // create multi channel mixer unit
    cd.componentType = kAudioUnitType_Mixer;
    cd.componentSubType = kAudioUnitSubType_MultiChannelMixer;
    err = AUGraphAddNode(_AUGraph, &cd, &_stereoMixerNode);
    [self checkError:err];
    
    err = AUGraphOpen(_AUGraph);
    [self checkError:err];
    
    err = AUGraphNodeInfo(_AUGraph,
                          _midisynthNode,
                          NULL,
                          &_midiSynthUnit);
    [self checkError:err];
    
    err = AUGraphNodeInfo(_AUGraph, _stereoMixerNode, NULL, &_stereoMixerUnit);
    [self checkError:err];
    
    err = AUGraphNodeInfo(_AUGraph,
                          _remoteIONode,
                          NULL,
                          &_remoteIOUnit);
    [self checkError:err];
    
    // connect midi systh node to mixer node
    err = AUGraphConnectNodeInput(_AUGraph, _midisynthNode, 0, _stereoMixerNode, 0);
    [self checkError:err];
    
    // connect mixer node to output node
    err = AUGraphConnectNodeInput(_AUGraph, _stereoMixerNode, 0, _remoteIONode, 0);
    [self checkError:err];

Finally, We can set pan like this:

- (void)setPanvalue:(float)value {
    AudioUnitParameterValue panValue = value;
    OSStatus result = AudioUnitSetParameter(self.stereoMixerUnit, kMultiChannelMixerParam_Pan, kAudioUnitScope_Output, 0, panValue, 0);
    [self checkError:result]; }