我正在尝试使用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类的音频数据生成器方法(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
}
}
}
这个问题看起来和这个问题类似,但没有人回答。
任何帮助都将不胜感激。
在输出回调中,您没有生成一组新的音频数据,其中新缓冲区的第一个样本的相位是上一个缓冲区的最后一个样本的增量。