Tanshik
Tanshik

Reputation: 17

UnsafeMutableBufferPointer<AudioBuffer> Speech To Text. Dangling Buffer Pointer

I have been trying to implement Google's Speech to Text API in iOS and I cam across this piece of code in their AudioController file. This piece of code

    let buffers = UnsafeMutableBufferPointer<AudioBuffer>(start:
                                                            &bufferList.mBuffers, count: Int(bufferList.mNumberBuffers))
    
    buffers[0].mNumberChannels = 1
    buffers[0].mDataByteSize = inNumberFrames * 2
    buffers[0].mData = nil

gives me this error

Initialization of 'UnsafeMutableBufferPointer<AudioBuffer>' results in a dangling buffer pointer

I've tried the solutions over on this question Warning: Initialization of 'UnsafeBufferPointer<T>' results in a dangling buffer pointer, but the solutions don't work as intended.

Any help?

Upvotes: 1

Views: 124

Answers (1)

Henadzi Rabkin
Henadzi Rabkin

Reputation: 7033

I am using the same AudioController. I have found the solution here

Code can be updated like this:

let buffers = withUnsafeMutablePointer(to: &bufferList.mBuffers) {
    UnsafeMutableBufferPointer(start: $0, count: Int(bufferList.mNumberBuffers))
  }
  ...
  ...    
  // get the recorded samples
  status = withUnsafeMutablePointer(to: &bufferList) {
      AudioUnitRender(AudioController.sharedInstance.remoteIOUnit!,
                               ioActionFlags,
                               inTimeStamp,
                               inBusNumber,
                               inNumberFrames,
                               $0)
  }

Works for me fine

Upvotes: 1

Related Questions