如何将该 UnsafeMutablePointer<UnsafeMutablePointer<Float>> 变量转换为 AudioBufferList?
Posted
技术标签:
【中文标题】如何将该 UnsafeMutablePointer<UnsafeMutablePointer<Float>> 变量转换为 AudioBufferList?【英文标题】:How to convert that UnsafeMutablePointer<UnsafeMutablePointer<Float>> variable into AudioBufferList? 【发布时间】:2016-06-16 15:12:23 【问题描述】:我的 Swift 项目中有这个 EZAudio 方法,用于从麦克风捕获音频:
func microphone(microphone: EZMicrophone!, hasAudioReceived bufferList: UnsafeMutablePointer<UnsafeMutablePointer<Float>>, withBufferSize bufferSize: UInt32, withNumberOfChannels numberOfChannels: UInt32)
但我真正需要的是让“bufferList”参数作为 AudioBufferList 类型传入,以便通过套接字发送这些音频数据包,就像我在 Objective C 中所做的那样:
//Objective C pseudocode:
for(int i = 0; i < bufferList.mNumberBuffers; ++i)
AudioBuffer buffer = bufferList.mBuffers[i];
audio = ["audio": NSData(bytes: buffer.mData, length: Int(buffer.mDataByteSize))];
socket.emit("message", audio);
如何将 UnsafeMutablePointer> 变量转换为 AudioBufferList?
【问题讨论】:
你有什么新的更新吗,我正在尝试实现相同的目标,我需要将 nsdata 发送到我正在使用 cocoasyncsocket 和 ezaudio 的套接字 嗨@MuhammadFaizanKhatri,我前段时间搞定了,代码粘贴在下面作为答案。如果可行,请点赞! 【参考方案1】:我能够将麦克风中的音频流式传输到套接字中,如下所示:
func microphone(microphone: EZMicrophone!, hasBufferList bufferList: UnsafeMutablePointer<AudioBufferList>, withBufferSize bufferSize: UInt32, withNumberOfChannels numberOfChannels: UInt32)
let blist:AudioBufferList=bufferList[0]
let buffer:AudioBuffer = blist.mBuffers
let audio = ["audio": NSData(bytes: buffer.mData, length: Int(buffer.mDataByteSize))];
socket.emit("message", audio);//this socket comes from Foundation framework
这个通用的 AudiostreamDescriptor 设置对我有用,您可能需要根据自己的需要对其进行调整或省略某些部分,例如波形动画:
func initializeStreaming()
var streamDescription:AudioStreamBasicDescription=AudioStreamBasicDescription()
streamDescription.mSampleRate = 16000.0
streamDescription.mFormatID = kAudioFormatLinearPCM
streamDescription.mFramesPerPacket = 1
streamDescription.mChannelsPerFrame = 1
streamDescription.mBytesPerFrame = streamDescription.mChannelsPerFrame * 2
streamDescription.mBytesPerPacket = streamDescription.mFramesPerPacket * streamDescription.mBytesPerFram
streamDescription.mBitsPerChannel = 16
streamDescription.mFormatFlags = kAudioFormatFlagIsSignedInteger
microphone = EZMicrophone(microphoneDelegate: self, withAudioStreamBasicDescription: sstreamDescription, startsImmediately: false)
waveview?.plotType=EZPlotType.Buffer
waveview?.shouldFill = false
waveview?.shouldMirror = false
让这个东西运行起来很复杂,祝你好运!
【讨论】:
我尝试设置为 kAudioFormatLinearPCM 格式,它工作正常,但我需要将格式转换为 ULaw ,该配置给我带来了困难,你有任何关于它的初始化配置吗?跨度> @MuhammadFaizanKhatri 您是否尝试过使用 kAudioFormatULaw 代替?理论上,您可以在那里使用许多音频格式,但实际上,我发现大多数格式组合在 iOS 中崩溃或无法正常工作。我使用此配置是因为我尝试过的所有其他配置都崩溃了。 @MuhammadFaizanKhatri 有些人在这里尝试使用 ULaw:***.com/questions/8426967/…【参考方案2】:我相信你会创建一个AudioBufferList
指针并使用memory
函数的结果。
let audioBufferList = UnsafePointer<AudioBufferList>(bufferList).memory
【讨论】:
以上是关于如何将该 UnsafeMutablePointer<UnsafeMutablePointer<Float>> 变量转换为 AudioBufferList?的主要内容,如果未能解决你的问题,请参考以下文章
如何转换 UnsafePointer<UnsafeMutablePointer<Float>>?到 UnsafeMutablePointer<UnsafeMutableP
如何在 Swift 3 中使用 UnsafeMutablePointer?
如何通过 `UnsafeMutablePointer<CFTypeRef?>?` 参数返回字典?