Xamarin Monotouch Audio Units Callbacks

2019-06-08 18:19发布

问题:

I have a problem with Audio Units in MonoTouch/Xamarin.

It seems like I can't get a callback on recording, just playback. I used this example: https://github.com/xamarin/monotouch-samples/blob/master/AUSoundTriggeredPlayingSoundMemoryBased/ExtAudioBufferPlayer.cs

and looked for Obj C examples. The Obj C examples are pretty much the same like my code so Im a little bit confused about this thing.

The output if running my example is:

INPUT0

Which is the bus number for output.

So the expected output should be:

INPUT1

So my question is: How do I get a recording callback and a playback callback running the same time, or just how do I get a recording callback.

My Code:

void prepareAudioUnit()
{
    // AudioSession
    AudioSession.Initialize();
    AudioSession.Category = AudioSessionCategory.PlayAndRecord;
    AudioSession.PreferredHardwareIOBufferDuration = Config.packetLength;
    AudioSession.PreferredHardwareSampleRate = Format.samplingRate;
    //AudioSession.SetActive (false);
    AudioSession.SetActive(true);
    Logger.log("HWSR:" + AudioSession.CurrentHardwareSampleRate);

    // Getting AudioComponent Remote output 
    _audioComponent = AudioComponent.FindComponent(AudioTypeOutput.VoiceProcessingIO);

    // creating an audio unit instanc
    _audioUnit = new AudioUnit(_audioComponent);

    // turning on microphone
    _audioUnit.SetEnableIO(true,
                            AudioUnitScopeType.Input,
                            1 // Remote Input
                            );
    _audioUnit.SetEnableIO(true,
                            AudioUnitScopeType.Output,
                            0 // Remote output
                            );

    // setting audio format
    _audioUnit.SetAudioFormat(Format.AudioStreamBasicDescription,
                               AudioUnitScopeType.Output,
                               1
                               );
    _audioUnit.SetAudioFormat(Format.AudioStreamBasicDescription,
                               AudioUnitScopeType.Input,
                               0
                               );

    // setting callback method
    _audioUnit.SetRenderCallback(_audioUnit_OutputCallback, AudioUnitScopeType.Global, 0);
    _audioUnit.SetRenderCallback(_audioUnit_InputCallback, AudioUnitScopeType.Global, 1);
}

AudioUnitStatus _audioUnit_OutputCallback(AudioUnitRenderActionFlags actionFlags, AudioTimeStamp timeStamp, uint busNumber, uint numberFrames, AudioBuffers data)
{
    Logger.log("OUTPUT" + busNumber);
    return AudioUnitStatus.NoError;
}

AudioUnitStatus _audioUnit_InputCallback(AudioUnitRenderActionFlags actionFlags, AudioTimeStamp timeStamp, uint busNumber, uint numberFrames, AudioBuffers data)
{
    Logger.log("INPUT" + busNumber);
    return AudioUnitStatus.NoError;
}

回答1:

This problem is a bug in Xamarin, they forgot to add a method for InputCallbacks.

I reported the bug but for the people needing the same:

http://nopaste.info/8d0aca98d9.html

Its not good, but it shows how to solve the problem to write a fix yourself till Xamarin updates this.