简体   繁体   中英

How can I mute microphone input volume using AudioUnits?

I'm using AudioUnits to record and play sound . It's part of a soft phone.

This is my initialisation:

AudioStreamBasicDescription audioFormat;
    audioFormat.mSampleRate = 8000;
    audioFormat.mFormatID = kAudioFormatULaw;
    audioFormat.mFormatFlags = kAudioFormatFlagIsPacked | kAudioFormatFlagIsSignedInteger;
    audioFormat.mFramesPerPacket = 1;
    audioFormat.mChannelsPerFrame = 1;
    audioFormat.mBitsPerChannel = 16;
    audioFormat.mBytesPerPacket = 2;
    audioFormat.mBytesPerFrame = 2;

status = AudioUnitSetProperty(audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, kInputBus, &audioFormat, sizeof(audioFormat));

AURenderCallbackStruct callbackStruct;
    callbackStruct.inputProc = recordingCallback;
    callbackStruct.inputProcRefCon = (__bridge void *)(self);
    status = AudioUnitSetProperty(audioUnit,
                                  kAudioOutputUnitProperty_SetInputCallback,
                                  kAudioUnitScope_Global,
                                  kInputBus,
                                  &callbackStruct,
                                  sizeof(callbackStruct));

During the recording process I'm using a callback to process the sound:

static OSStatus recordingCallback(void *inRefCon,
                                  AudioUnitRenderActionFlags *ioActionFlags,
                                  const AudioTimeStamp *inTimeStamp,
                                  UInt32 inBusNumber,
                                  UInt32 inNumberFrames,
                                  AudioBufferList *ioData)

Now at some point I would like to mute the microphone. After googling, I found this as a solution:

-(void) setMuteOn {
    AudioUnitParameterValue volume = 0.0;
    AudioUnitSetProperty(audioUnit, kMultiChannelMixerParam_Volume, kAudioUnitScope_Input, 1, &volume, 0);
}

But it doesn't work. Perhaps I need to do some kind of refresh on my audioUnit, I don't know. Any help would be great.

Actually it was easier than I thought. In the callback method I just overwrote those sound buffers with silence. In my case I was using ULAW compression, so just filled my array with 0xFF

The microphone was still recording, but I stopped using the data.

You could do the following which I think is a little cleaner.

-(BOOL)microphoneInput:(BOOL)enable;
{
    UInt32 enableInput = (enable)? 1 : 0;
    OSStatus status = AudioUnitSetProperty(
                                           ioUnit,//our I/O unit
                                           kAudioOutputUnitProperty_EnableIO, //property we are changing
                                           kAudioUnitScope_Input,
                                           kInputBus, //#define kInputBus 1
                                           &enableInput,
                                           sizeof (enableInput)
                                           );
    CheckStatus(status, @"Unable to enable/disable input");
    return (status == noErr);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM