5

I've looked everywhere, and I can't find a way to adjust the input volume of an input device for an AVCaptureSession. The best I can do, which doesn't help me at all, is get the levels of audio from the device by accessing the connections (AVCaptureConnections) from the AVCaptureAudioDataOutput - i.e., by monitoring the levels from a preview output. Is there any way to change the input gain, or even to get the audio input level directly in AVFoundation? I'm still learning a bit, so I apologize if I have missed anything obvious.

Edit: I should note that this is for OSX.

foundry
  • 31,615
  • 9
  • 90
  • 125
ewrobinson
  • 351
  • 4
  • 9

3 Answers3

2

So, I used some info that He Was linked, along with some research into Core Audio, and I turned it into a github project. That way, other people wanting to change the input volume of a device while using AVFoundation don't have to reinvent the wheel. You can find the classes here: https://github.com/ewrobinson/ERVolumeAdjust

ewrobinson
  • 351
  • 4
  • 9
1
self.audioSession = [AVAudioSession sharedInstance];
if (self.audioSession.isInputGainSettable) {
    BOOL success = [self.audioSession setInputGain:sender.value error:&error];
    if (!success) NSLog(@"inputGain error: %@",error);
}

This is for ios6+ ... for ios5, you can use AudioSessionGetProperty / AudioSessionSetProperty functions to achieve the same.

However not all hardware is capable of adjustable input gain. For example, I get gain settable on an iPhone 3GS/ios6 with builtin mic, but gain NOT settable on an iPad mini with builtin mic. On the iPhone 3GS gain is also not settable until recording has actually started. (edit: iPhone 4S/ios5 also has gain settable with builtin mic)

update
As you have pointed out, you are looking for an OSX solution, not iOS. I have moved this answer (with more detail) to a better place.

For OSX, you may find this Q&A useful:

How do you set the input level (gain) on the built-in input (OSX Core Audio / Audio Unit)?

Community
  • 1
  • 1
foundry
  • 31,615
  • 9
  • 90
  • 125
  • I failed to mention that this was for OSX, not ios. Sorry about that. – ewrobinson May 07 '13 at 04:29
  • @ewrobinson - I've added OSX tag to your question. My efforts weren't wasted, I found an [unanswered question](http://stackoverflow.com/questions/10871231/how-to-control-hardware-mic-input-gain-level-on-iphone/16420963#16420963) that made a good fit, so filled this out with more detail and posted it there. – foundry May 07 '13 at 14:15
  • Ah, I'm a bit new to this site, so I forgot to tag it as OSX. Thanks. If only OSX had some of the AVAudioSession capabilities... The link you posted that uses CoreAudio to change the input gain on the default input device appears to have the beginning of what I need. I had (wrongly) assumed that this kind of ability would have been available in OSX's AVFoundaiton (it appears to in ios), but that is not the case. It looks like my next task is to figure out which device the AVCaptureSession is using and how to identify that in CoreAudio. Thanks for the help. – ewrobinson May 07 '13 at 20:12
1

You can adjust the gain by setting the volume property of your session output's audio connection.
Take into account that the connection might have multiple channels.

I am assuming a reference to a fileOutput ivar here, that I stored before adding the output to the session.

AVCaptureConnection* audioConnection = [fileOutput connectionWithMediaType:AVMediaTypeAudio];
if(audioConnection)
{
    for(AVCaptureAudioChannel* audioChannel in [audioConnection audioChannels])
    {
        audioChannel.volume = 0.5;
    }
}
Thomas Zoechling
  • 34,177
  • 3
  • 81
  • 112
  • Unfortunately, I think this only affects the preview output, not the input levels. My goal is to change the input gain directly. – ewrobinson May 07 '13 at 20:17
  • Hmmm. Did you try it? I successfully use it here, with the difference that I use an instance of AVCaptureFileOutput instead of AVCaptureAudioDataOutput. To me it seems to be the only option to influence signal gain on the Mac. OS X lacks the whole AVAudioSession stuff, but compared to iOS it offers the volume property on AVCaptureAudioChannel. – Thomas Zoechling May 07 '13 at 20:41
  • But doesn't that just make the output (to either the preview or the file) louder/softer without respect to the input volume? I was under the impression that it wouldn't help if, say, a portion of the audio was clipped for being too loud - it would lower the volume of the file that is saved, but the clipped audio would be lost (which is what I'm trying to avoid). It's entirely possible that I've misunderstood, of course. – ewrobinson May 07 '13 at 21:53