Thursday, June 6, 2013

AVAudioSession inputGain example in Objective C (iOS).

AVAudioSession inputGain

The gain applied to inputs associated with the session. (read-only)

@property(readonly) float inputGain

Discussion of [AVAudioSession inputGain]
The value in this property is in the range 0.0 to 1.0, where 0.0 represents no additional gain and 1.0 represents the maximum possible gain.

You can observe changes to the value of this property using key-value observing.

AVAudioSession inputGain example.
On ios6+ you can use AVAudioSession properties

        CGFloat gain = sender.value;
        NSError* error;
        self.audioSession = [AVAudioSession sharedInstance];
        if (self.audioSession.isInputGainSettable) {
            BOOL success = [self.audioSession setInputGain:gain
               if (!success){} //error handling
        } else {
            NSLog(@"ios6 - cannot set input gain");
On ios5 you can get/set audio input gain properties using AudioSession functions

    UInt32 ui32propSize = sizeof(UInt32);
    UInt32 f32propSize = sizeof(Float32);
    UInt32 inputGainAvailable = 0;
    Float32 inputGain = sender.value;

    OSStatus err =
                            , &ui32propSize
                            , &inputGainAvailable);

    if (inputGainAvailable) {
    OSStatus err =
                             , sizeof(inputGain)
                             , &inputGain);
    } else {
        NSLog(@"ios5 - cannot set input gain");
    OSStatus err =
                              , &f32propSize
                              , &inputGain);
    NSLog(@"inputGain: %0.2f",inputGain);

Example of [AVAudioSession inputGain].
self.audioSession = [AVAudioSession sharedInstance];
if (self.audioSession.isInputGainSettable) {
    BOOL success = [self.audioSession setInputGain:sender.value error:&error];
    if (!success) NSLog(@"inputGain error: %@",error);

End of AVAudioSession inputGain example article.