AudioUnitRender error -50 with odd length buffers

ⅰ亾dé卋堺 提交于 2020-01-05 03:29:08

问题


I have a RemoteIO unit configured with AVAudioSessionCategoryPlayAndRecord. I find some strange behavior in it. I open the app, and immediately close it before audioUnit initializes fully (it actually initializes in background as I quit the app too soon). Next I bring the app to foreground and immediately on relaunch, I see AudioUnitRender failing with error -50 continuously. I find inNumberFrames to be 1115 and it fails whenever this number is odd.

  func recordingCallback(inRefCon:UnsafeMutableRawPointer,
                ioActionFlags:UnsafeMutablePointer<AudioUnitRenderActionFlags>,
                inTimeStamp:UnsafePointer<AudioTimeStamp>,
                inBusNumber:UInt32,
                inNumberFrames:UInt32,
                ioData:UnsafeMutablePointer<AudioBufferList>?) -> OSStatus
  {
       let controller = unsafeBitCast(inRefCon, to: MicrophoneOutput.self) 

      let listPtr = controller.audioBufferList.unsafeMutablePointer

     let buffers = UnsafeBufferPointer<AudioBuffer>(start: &listPtr.pointee.mBuffers, count: Int(listPtr.pointee.mNumberBuffers))

   for var buf in buffers {
       buf.mDataByteSize = inNumberFrames * UInt32(sampleWordSize)
    }

   let status = AudioUnitRender(controller.audioUnit!, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, listPtr)

if noErr != status {
   print("Error \(status)");
   NSLog("\(AVAudioSession.sharedInstance().currentRoute.inputs[0])")
   fatalError("Render status \(status)")
  // return status;
}

return noErr

}

Strangely, if I hardcode inNumberFrames to be 1114 in AudioUnitRender call, it succeeds! Does anyone know what is going on? The pattern is as soon as the app resumes from background, there is a route change notification, and then the first sample in callback is of length 1114 frames but the one next to it has 1115 frames which fails.


回答1:


It turned out it was a programming error due to misunderstandings in Swift language. The line that was culprit was this:

for var buf in buffers {
   buf.mDataByteSize = inNumberFrames * UInt32(sampleWordSize)
}

This answer provides more detail - Swift vs Objective C pointer manipulation issue



来源:https://stackoverflow.com/questions/52061575/audiounitrender-error-50-with-odd-length-buffers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!