gpt4 book ai didi

ios - 麦克风播放的音频断断续续,听起来像是空气吹入麦克风

转载 作者:行者123 更新时间:2023-11-30 12:37:24 26 4
gpt4 key购买 nike

我正在使用以下内容录制音频:

    localInput?.installTap(onBus: 0, bufferSize: 4096, format: localInputFormat) {
(buffer, time) -> Void in

let audioBuffer = self.audioBufferToBytes(audioBuffer: buffer)
let output = self.outputStream!.write(audioBuffer, maxLength: Int(buffer.frameLength))

if output > 0 {
print("\(#file) > \(#function) > \(output) bytes written from queue \(self.currentQueueName())")
}
else if output == -1 {
let error = self.outputStream!.streamError
print("\(#file) > \(#function) > Error writing to stream: \(error?.localizedDescription)")
}
}

我的 localInputFormat 如下:

self.localInput = self.localAudioEngine.inputNode
self.localAudioEngine.attach(self.localAudioPlayer)
self.localInputFormat = self.localInput?.inputFormat(forBus: 0)
self.localAudioEngine.connect(self.localAudioPlayer, to: self.localAudioEngine.mainMixerNode, format: self.localInputFormat)

audioBufferToBytes函数如下:

func audioBufferToBytes(audioBuffer: AVAudioPCMBuffer) -> [UInt8] {
let srcLeft = audioBuffer.floatChannelData![0]
let bytesPerFrame = audioBuffer.format.streamDescription.pointee.mBytesPerFrame
let numBytes = Int(bytesPerFrame * audioBuffer.frameLength)

// initialize bytes by 0
var audioByteArray = [UInt8](repeating: 0, count: numBytes)

srcLeft.withMemoryRebound(to: UInt8.self, capacity: numBytes) { srcByteData in
audioByteArray.withUnsafeMutableBufferPointer {
$0.baseAddress!.initialize(from: srcByteData, count: numBytes)
}
}

return audioByteArray
}

在其他设备上,当我收到数据时,我必须将其转换回来。因此,当它收到时,它会执行以下操作:

func bytesToAudioBuffer(_ buf: [UInt8]) -> AVAudioPCMBuffer {

let fmt = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 44100, channels: 1, interleaved: true)
let frameLength = UInt32(buf.count) / fmt.streamDescription.pointee.mBytesPerFrame

let audioBuffer = AVAudioPCMBuffer(pcmFormat: fmt, frameCapacity: frameLength)
audioBuffer.frameLength = frameLength

let dstLeft = audioBuffer.floatChannelData![0]

buf.withUnsafeBufferPointer {
let src = UnsafeRawPointer($0.baseAddress!).bindMemory(to: Float.self, capacity: Int(frameLength))
dstLeft.initialize(from: src, count: Int(frameLength))
}

return audioBuffer
}

最后,我们播放音频数据:

                self.audioPlayerQueue.async {
self.peerAudioPlayer.scheduleBuffer(audioBuffer)

if (!self.peerAudioPlayer.isPlaying && self.localAudioEngine.isRunning) {
self.peerAudioPlayer.play()
}
}

但是,在任一扬声器上,我都只能听到有人每半秒敲击一次麦克风的声音。不是他们实际上在说话或什么。我想这是由于我从音频缓冲区到字节的转换造成的,但我不确定。有人发现上述内容有任何问题吗?

谢谢。

最佳答案

如果有人对解决方案感兴趣,基本上问题是录音设备上的音频是 17640 字节,但为了流式传输它,它会将其分解成更小的片段,而在接收设备上我必须读取第一个 17640 字节字节然后播放音频。不播放收到的每一小块数据。

关于ios - 麦克风播放的音频断断续续,听起来像是空气吹入麦克风,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42701280/

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