KAudioUnitProperty_ShouldAllocateBuffer not affected

I'm just working on getting samples for processing from a microphone. I have an audio device configured for input and output, and both have visualization callbacks. My question is about callback for mic callback.

I want core-audio to allocate a microphone callback buffer for me.

UInt32 shouldAllocateBuffer = 1; AudioUnitSetProperty(audioUnit, kAudioUnitProperty_ShouldAllocateBuffer, kAudioUnitScope_Global, 1, &shouldAllocateBuffer, sizeof(shouldAllocateBuffer)); 

Doing this, however, always results in a NULL ioData pointer in the callback. Am I delaying the allocation of my own buffer?

Enter

 static OSStatus recordingCallback(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData) { OSStatus status; status = AudioUnitRender(audioUnit, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, ioData); // ioData is null here } 

Play

 static OSStatus playbackCallback(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData) { // ioData is not NULL here but I get silence in the headphones. return noErr; } 
+4
source share
1 answer

AudioUnitRender still expects the AudioBufferList (ioData) parameter to be non-NULL.

If ioData is a pointer to an AudioBufferList, then the buffer that should be is ioData-> mBuffers [0] .mData for most cases (see docs ). If you do not allocate buffers, the list of buffers should probably be a local variable, since it is valid only for callbacks.

So basically use a local variable, not ioData.

Please note that you still need to configure your AudioBufferList according to the topology you are describing.

Here is an example:

 AudioBufferList bufferList; bufferList.mNumberBuffers = 1; bufferList.mBuffers[0].mData = NULL; OSStatus status; status = AudioUnitRender(audioUnit, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, &bufferList); // bufferList.mBuffers[0].mData is null // Use the input data in now valid bufferList.mBuffers[0].mData // note the buffer is only valid for the callback. 

Note that if you need multiple buffers (you probably don't, even for stereo), you can use malloc to expand the size of the mBuffers array.

+4
source

Source: https://habr.com/ru/post/1397495/


All Articles