Using a slider to continuously set input gain level
Using a slider to continuously set input gain level
- Subject: Using a slider to continuously set input gain level
- From: Lorenzo Thurman <email@hidden>
- Date: Sat, 04 Apr 2015 17:27:39 -0500
I need to create a slider that will adjust the gain of the current input device. I’ve been studying Core Audio for clues or sample code on how to do this, but haven’t found anything. I suspect I may need to create a mixer, but am not sure (I’ve gotten about halfway through "Learning Core Audio” and that reading so far, leads me to that conclusion). Here is some code that I’ve been using to adjust the gain to a given level. If I want to set the gain to say, 75, the code works just fine to do that. I then wired it up to a slider to see if it would work to continuously update the gain level as the slider knob is moved, but what actually happens is that the gain is adjusted to 100 on the first movement of the slider, and remains there until the slider is moved to zero, at which point, the sound is muted. So here is the code, any help is most appreciated.
AudioObjectPropertyAddress address;
AudioDeviceID deviceID;
OSStatus err;
UInt32 size;
UInt32 channels[ 2 ];
Float32 level;
SSAudioDevice * targetDevice = [self getTargetInputDevice];
address.mSelector = kAudioHardwarePropertyDefaultInputDevice;
address.mScope = kAudioObjectPropertyScopeGlobal;
address.mElement = kAudioObjectPropertyElementMaster;
deviceID = targetDevice.deviceID;
if(!targetDevice.ioLevel){
level = 0.5;
}
else{
level = targetDevice.ioLevel.floatValue / 100.0;
}
size = sizeof(deviceID);
err = AudioObjectGetPropertyData( kAudioObjectSystemObject, &address, 0, nil, &size, &deviceID );
if ( ! err ) {
address.mSelector = kAudioDevicePropertyPreferredChannelsForStereo;
address.mScope = kAudioDevicePropertyScopeInput;
address.mElement = kAudioObjectPropertyElementWildcard;
size = sizeof(channels);
err = AudioObjectGetPropertyData( deviceID, &address, 0, nil, &size, &channels );
}
if ( ! err ) {
Boolean hasProperty;
address.mSelector = kAudioDevicePropertyVolumeScalar;
address.mScope = kAudioDevicePropertyScopeInput;
address.mElement = kAudioObjectPropertyElementMaster;
hasProperty = AudioObjectHasProperty( deviceID, &address );
if(!hasProperty){
}
address.mElement = channels[ 0 ];
hasProperty = AudioObjectHasProperty( deviceID, &address );
if(!hasProperty){
}
address.mElement = channels[ 1 ];
hasProperty = AudioObjectHasProperty( deviceID, &address );
if(!hasProperty){
}
}
if ( ! err ) {
address.mSelector = kAudioDevicePropertyVolumeScalar;
address.mScope = kAudioDevicePropertyScopeInput;
size = sizeof(level);
address.mElement = kAudioObjectPropertyElementMaster;
err = AudioObjectSetPropertyData( deviceID, &address, 0, nil, size, &level );
if(err){
address.mElement = channels[ 0 ];
err = AudioObjectSetPropertyData( deviceID, &address, 0, nil, size, &level );
address.mElement = channels[ 1 ];
err = AudioObjectSetPropertyData( deviceID, &address, 0, nil, size, &level );
}
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden