从AudioQueueBufferRef输出队列回调中获取音频浮点数

时间:2018-04-10 19:50:48

标签: ios swift avfoundation core-audio

我试图通过简单的 mp3 音频队列输出 AudioQueueBufferRef 使用.mAudioData

来获取正确的音频浮动数天
let callback : @convention(c) (_ userData : UnsafeMutableRawPointer?,
    _ queue : AudioQueueRef,
    _ bufferToFill : AudioQueueBufferRef) -> Void =
    { (userData, queue, bufferToFill) in
        guard let myInfo = userData?.assumingMemoryBound(to: myInfo.self) else {return}

        Utility.check(AudioFileReadPacketData(myInfo.pointee.playbackFile! ,              // AudioFileID
            false,                                     // use cache?
            &numBytes,                                 // initially - buffer capacity, after - bytes actually read
            myInfo.pointee.packetDescs,                // pointer to an array of PacketDescriptors
            myInfo.pointee.packetPosition,             // index of first packet to be read
            &nPackets,                                 // number of packets
            bufferToFill.pointee.mAudioData),          // output buffer
            operation: "AudioFileReadPacketData failed")

      let size = Int((bufferToFill.pointee.mAudioDataBytesCapacity) / 2 )
      let array = Array(UnsafeMutableBufferPointer(start: bufferToFill.pointee.mAudioData.assumingMemoryBound(to: Int16.self), count: size))
      print(array[0...5],"int16")

      //to floats
      let numSamples = array.count / MemoryLayout<Int16>.size //2
      var factor = Float(Int16.max)

      var floatsArr: [Float] = Array(repeating: 0.0, count: numSamples)
      var scaledFloats: [Float] = Array(repeating: 0.0, count: numSamples)
      print(array[0...20],scaledFloats.count,"int16")

      vDSP_vflt16(array, 1, &floatsArr, 1, vDSP_Length(numSamples))
      print(array[0...20],scaledFloats.count,"floats")

      vDSP_vsdiv(&floatsArr, 1, &factor, &scaledFloats, 1, vDSP_Length(numSamples))//scaling
      print(scaledFloats[0...20],scaledFloats.count, "scaledfloats \n")

}

我得到错误的花车,但我能够成功播放。我想从 AudioQueueBufferRef

中绘制一个波形

谢谢! 如果你想尝试一下 https://github.com/masaldana2/getmAudioData

1 个答案:

答案 0 :(得分:1)

尝试更换:

let numSamples = array.count / MemoryLayout<Int16>.size //2

人:

let numSamples = array.count

我相信你的浮动缓冲区是预期大小的一半,否则。

以下是我用于测试的代码子集的经过修改的独立版本:

import Foundation
import Accelerate

let size = 7
var data : [Int16] = [ 0, 1, 2, 3, 4, -Int16.max, Int16.max ]

let array = Array(UnsafeMutableBufferPointer(start: &data, count: size))

var floatsArr:    [Float] = Array(repeating: 0.0, count: array.count)
var scaledFloats: [Float] = Array(repeating: 0.0, count: array.count)

vDSP_vflt16(array, 1, &floatsArr, 1, vDSP_Length(array.count))

var factor = Float(Int16.max)
vDSP_vsdiv(floatsArr, 1, &factor, &scaledFloats, 1, vDSP_Length(array.count))

print(       array[0 ...        array.count - 1],        array.count, "int16")
print(   floatsArr[0 ...    floatsArr.count - 1],    floatsArr.count, "float")
print(scaledFloats[0 ... scaledFloats.count - 1], scaledFloats.count, "scaled")

及其输出:

[0, 1, 2, 3, 4, -32767, 32767] 7 int16
[0.0, 1.0, 2.0, 3.0, 4.0, -32767.0, 32767.0] 7 float
[0.0, 3.05185094e-05, 6.10370189e-05, 9.15555283e-05, 0.000122074038, -1.0, 1.0] 7 scaled