iosaudioportaudiolow-latency

How to patch portaudio for low-latency iOS usage?


I am currently working on a patch that should make portaudio work for iOS. For now I successfully applied portaudio member Hans Petter's patch for iOS usage

https://www.dropbox.com/s/6hf9bjqpa6b6uv3/0001-Add-basic-support-for-iOS-to-portaudio.patch?dl=0

and at least I call tell that the audio process does work, however, it is currently stuck at 1024 samples. When I try decreasing it to lower values the callback function terminates immediately. When using 48 kHz it terminates without any warning or error message but when using 44.1 kHz it crashes with t the following error:

Assertion failed: (*streamCallbackResult == paContinue || *streamCallbackResult == paComplete || *streamCallbackResult == paAbort), function PaUtil_EndBufferProcessing, file pa_process.c, line 1499. dyld4 config: DYLD_LIBRARY_PATH=/usr/lib/system/introspection DYLD_IMAGE_SUFFIX=_debug DYLD_INSERT_LIBRARIES=/Developer/usr/lib/libBacktraceRecording.dylib:/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib

referring to line 1497 in pa_process.c -->

assert( *streamCallbackResult == paContinue || *streamCallbackResult == paComplete || streamCallbackResult == paAbort ); / don't forget to pass in a valid callback result value */

I'd like to get as low as possible in terms of buffer size (ideally 128 or even 64), however, since I am not familiar with audio on iOS am now seeking for first inspirational comments. On OSX, Win or Linux this problem does not exist. Maybe iOS expects special low-latency flags ?


Solution

  • Integrating Objective C code is the secret and changing the file's suffix to mm:

    auto session = [AVAudioSession sharedInstance];
    [session setActive:TRUE  error: nil];
    [session setPreferredSampleRate: sampleRate error: nil];
    [session setPreferredIOBufferDuration: bufferDuration  error:nil];