我尝试使用Core Audio AudioQueue(Swift 3)播放纯正弦波音。
播放效果很好,但每次调用AudioQueueOutputCallback来填充带有音频数据的新缓冲区时,我都会发出砰砰声。
我的AudioStreamer类看起来像:
let kNumberBuffers = 3
protocol AudioStreamerDelegate {
func requestAudioData() -> [Float]
}
let sampleRate = 48000.0
let bufferSize = Int(sampleRate) / 50
let bufferByteSize = UInt32(bufferSize * sizeof(Float)) // 20 mili sec of audio
class AudioStreamer {
var delegate: AudioStreamerDelegate
var outputQueue: AudioQueueRef?
var buffers = [AudioQueueBufferRef?](repeatElement(nil, count: kNumberBuffers))
var streamBasicDescription = AudioStreamBasicDescription(
mSampleRate: sampleRate,
mFormatID: kAudioFormatLinearPCM,
mFormatFlags: kAudioFormatFlagsNativeFloatPacked | kAudioFormatFlagIsNonInterleaved,
mBytesPerPacket: UInt32(sizeof(Float)),
mFramesPerPacket: 1,
mBytesPerFrame: UInt32(sizeof(Float)),
mChannelsPerFrame: 1,
mBitsPerChannel: UInt32(8 * sizeof(Float)),
mReserved: 0
)
init(delegate: AudioStreamerDelegate) {
// create new output audio queue
self.delegate = delegate
}
func play() {
let queue = DispatchQueue.main
queue.async(execute: {
let selfPointer = unsafeBitCast(self, to: UnsafeMutablePointer<Void>.self)
AudioQueueNewOutput(
&self.streamBasicDescription,
AudioStreamerOuputCallback,
selfPointer,
nil,
nil,
0,
&self.outputQueue
)
// allocate buffers
for i in 0 ..< kNumberBuffers {
AudioQueueAllocateBuffer(
self.outputQueue!,
bufferByteSize,
&self.buffers[i]
)
if let bufferRef = self.buffers[i] {
// configure audio buffer
let selfPointer = unsafeBitCast(self, to: UnsafeMutablePointer<Void>.self)
bufferRef.pointee.mUserData = selfPointer
bufferRef.pointee.mAudioDataByteSize = bufferByteSize
}
}
AudioQueuePrime(self.outputQueue!, 0, nil)
for bufferRef in self.buffers {
AudioStreamerOuputCallback(userData: unsafeBitCast(self, to: UnsafeMutablePointer<Void>.self), queueRef: self.outputQueue!, buffer: bufferRef!)
}
AudioQueueStart(self.outputQueue!, nil)
})
}
}
func AudioStreamerOuputCallback(userData: Optional<UnsafeMutablePointer<Void>>, queueRef: AudioQueueRef, buffer: AudioQueueBufferRef) {
let this = Unmanaged<AudioStreamer>.fromOpaque(OpaquePointer(userData!)).takeUnretainedValue()
let audioData = this.delegate.requestAudioData()
memcpy(buffer.pointee.mAudioData, unsafeBitCast(audioData, to: UnsafeMutablePointer<Void>.self), Int(bufferByteSize))
AudioQueueEnqueueBuffer(queueRef, buffer, 0, nil)
}
ViewController类的音频数据生成器方法,只有&#34; play&#34;按钮(theta是该ViewController上的存储属性):
func generateAudioData(frequency: Double) {
semaphore.wait()
let amplitude: Double = 0.25
let theta_increment: Double = 2.0 * M_PI * frequency / sampleRate
for j in 0 ..< Int(bufferSize) {
audioData[j] = Float(sin(theta) * amplitude)
theta += theta_increment
if theta > 2.0 * M_PI {
theta -= 2.0 * M_PI
}
}
}
问题看起来与this question上的问题类似,但没有人回答。
非常感谢任何帮助。
答案 0 :(得分:0)
在输出回调中,您没有生成一组新的音频数据,其中新缓冲区的第一个样本的相位是前一个缓冲区的最后一个样本的增量。