简体   繁体   English

如何使用AudioUnits静音麦克风输入音量?

[英]How can I mute microphone input volume using AudioUnits?

I'm using AudioUnits to record and play sound . 我正在使用AudioUnits录制和播放声音。 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. 不知道,也许我需要对audioUnit进行某种刷新。 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 在我的情况下,我使用的是ULAW压缩,因此只需在数组中填充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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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