我被困了好几天,试图从一个简单的 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
尝试替换:
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