1

我已经基于苹果的aurio Touch 示例应用程序编写了呼吸检测器。但我不知道如何设置音频单元或音频会话不播放来自音频输入的声音。现在,当对着麦克风吹气时,我可以听到来自 iPhone 扬声器的呼吸声。如何防止这种情况?

这是苹果的音频会话初始化代码:

            XThrowIfError(AudioSessionInitialize(NULL, NULL, rioInterruptionListener, self), "couldn't initialize audio session");
        XThrowIfError(AudioSessionSetActive(true), "couldn't set audio session active\n");


        UInt32 audioCategory = kAudioSessionCategory_RecordAudio;
        XThrowIfError(AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(audioCategory), &audioCategory), "couldn't set audio category");
        XThrowIfError(AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, propListener, self), "couldn't set property listener");

        Float32 preferredBufferSize = .005;
        XThrowIfError(AudioSessionSetProperty(kAudioSessionProperty_PreferredHardwareIOBufferDuration, sizeof(preferredBufferSize), &preferredBufferSize), "couldn't set i/o buffer duration");

        UInt32 size = sizeof(hwSampleRate);
        XThrowIfError(AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareSampleRate, &size, &hwSampleRate), "couldn't get hw sample rate");

        XThrowIfError(SetupRemoteIO(rioUnit, inputProc, thruFormat), "couldn't setup remote i/o unit");

        dcFilter = new DCRejectionFilter[thruFormat.NumberChannels()];

        UInt32 maxFPS;
        size = sizeof(maxFPS);
        XThrowIfError(AudioUnitGetProperty(rioUnit, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, &maxFPS, &size), "couldn't get the remote I/O unit's max frames per slice");

        fftBufferManager = new FFTBufferManager(maxFPS);
        l_fftData = new int32_t[maxFPS/2];



        XThrowIfError(AudioOutputUnitStart(rioUnit), "couldn't start remote i/o unit");

        size = sizeof(thruFormat);
        XThrowIfError(AudioUnitGetProperty(rioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &thruFormat, &size), "couldn't get the remote I/O unit's output client format");
4

1 回答 1

0

非常感谢Tim Bolstad 他的回答:AURIOTouch 使用单个回调将音频从输入传递到输出。因此,最简单的方法是在完成呼吸检测后将 ioData 结构清零。

就像是:

for (UInt32 i=0; i < inData->mNumberBuffers; i++)
memset(inData->mBuffers[i].mData, 0, inData->mBuffers[i].mDataByteSize);

或者您可以查看 CAPlayThrough 示例,该示例具有用于输入和输出的单独回调。

于 2010-11-15T04:46:37.553 回答