Popping noise between AudioQueueBuffers

99封情书 提交于 2021-02-10 05:26:11

问题


I'm trying to play a pure sine wave tone using Core Audio AudioQueue's (Swift 3).

It plays nicely, but I'm getting popping noises every time my AudioQueueOutputCallback is invoked to fill a new buffer with audio data.

My AudioStreamer class looks like:

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

Audio data generator method of a ViewController class with just a "play" button (theta is a stored property on that 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
        }
    }
}

The problem looks like something similar to that on this question, but nobody answered.

Any help will be greatly appreciated.


回答1:


In your output callback, you did not generate a new set of audio data, where the phase of the new buffer's first sample is an increment of the last sample of from previous buffer.



来源:https://stackoverflow.com/questions/38509920/popping-noise-between-audioqueuebuffers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!