gpt4 book ai didi

ios - 从 AudioQueueBufferRef 输出队列回调中获取音频 float

转载 作者:行者123 更新时间:2023-11-28 14:52:05 24 4
gpt4 key购买 nike

几天来,我一直在尝试使用 .mAudioData 从简单的 mp3 音频队列输出 AudioQueueBufferRef 中获取正确的音频 float

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")

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

中绘制波形

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

最佳答案

尝试替换:

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

通过:

let numSamples = array.count

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

这是我用于测试的代码子集的修改后的独立版本:

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

关于ios - 从 AudioQueueBufferRef 输出队列回调中获取音频 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49761845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com