2017-06-21 133 views
2

我有一個帶有自定義AURenderCallback的AVAudioEngine。當我運行的引擎,CPU峯值起來,它似乎已經與UnsafeMutableBufferPointer.getter和.setter行動做:使用getter時UnsafeMutableBufferPointer速度較慢

func performRender(ioActionFlags: UnsafeMutablePointer<AudioUnitRenderActionFlags>, inTimeStamp: UnsafePointer<AudioTimeStamp>, inBusNumber: UInt32, inNumberFrames: UInt32, ioData: UnsafeMutablePointer<AudioBufferList>?) -> OSStatus { 

    let blI = UnsafeMutableAudioBufferListPointer(ioData) 

    let numSamples = Int((blI?[0].mDataByteSize)!/UInt32(MemoryLayout<Float32>.stride)) 

    for input in 0 ..< blI!.count{ 

     let bI = blI?[input] 

     guard let inputData = bI?.mData else { 

      //assert(false) 

      return kAudioUnitErr_Uninitialized 
     } 

     let samplesBI = UnsafeMutablePointer<Float32>(inputData.assumingMemoryBound(to: Float32.self)) 

     let samplesI = UnsafeMutableBufferPointer<Float32>(start: samplesBI, count: numSamples) 

     for sampleIndex in 0 ..< samplesI.count { 

      samplesI[sampleIndex] *= 0.5 
     } 

    } 

    return noErr 
} 

Instruments Debug Session

什麼會導致獲取和設置的這個低效行爲指針數據?由於它正在處理音頻樣本,因此效率低下會導致音頻信號中出現斷斷續續的情況。

回答

2

可以跳過UnsafeMutableBufferPointer()調用,和索引直接樣品指針:

let bufferListPtr = UnsafeMutableAudioBufferListPointer(ioData) 
... 
let mBuffer : AudioBuffer = bufferListPtr[0] 
let count = Int(mBuffer.mDataByteSize)/yourSampleSizeInBytes 
let dataPointer = UnsafeMutableRawPointer(mBuffer.mData) 
if let dptr = dataPointer { 
    let sampleArray = dptr.assumingMemoryBound(to: yourSampleType.self) 
    for i in 0..<(count) { 
     let x = sampleArray[i] 
     let y = myModifySample(x) 
     sampleArray[i] = y 
    } 
} 

我使用的Int16爲mySampleType(大小= 2個字節),但它應該爲4個字節的類型浮動工作,以及。

直接使用原始內存指針可能會避免一些緩衝區getter/setter效率低下(也需要驗證,所以要小心!)。