gpt4 book ai didi

ios - 快速使用 AVCaptureAudioDataOutputSampleBufferDelegate 捕获音量级别

转载 作者:可可西里 更新时间:2023-10-31 23:44:30 26 4
gpt4 key购买 nike

我正在尝试使用它编译和运行的 AVCaptureDevice 等来调整音量级别,但这些值似乎是随机的,而且我也不断收到溢出错误。

编辑:

另外,RMS 范围在 0 到 20000 左右是否正常?

        if let audioCaptureDevice : AVCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio){



try audioCaptureDevice.lockForConfiguration()

let audioInput = try AVCaptureDeviceInput(device: audioCaptureDevice)
audioCaptureDevice.unlockForConfiguration()

if(captureSession.canAddInput(audioInput)){
captureSession.addInput(audioInput)
print("added input")
}


let audioOutput = AVCaptureAudioDataOutput()

audioOutput.setSampleBufferDelegate(self, queue: GlobalUserInitiatedQueue)

if(captureSession.canAddOutput(audioOutput)){
captureSession.addOutput(audioOutput)
print("added output")
}


//supposed to start session not on UI queue coz it takes a while
dispatch_async(GlobalUserInitiatedQueue) {
print("starting captureSession")
self.captureSession.startRunning()
}
}

...

func captureOutput(captureOutput: AVCaptureOutput!, let didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {

// Needs to be initialized somehow, even if we take only the address
var audioBufferList = AudioBufferList(mNumberBuffers: 1,
mBuffers: AudioBuffer(mNumberChannels: 1, mDataByteSize: 0, mData: nil))
//this needs to be in method otherwise only runs 125 times?
var blockBuffer: CMBlockBuffer?

CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(
sampleBuffer,
nil,
&audioBufferList,
sizeof(audioBufferList.dynamicType),
nil,
nil,
UInt32(kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment),
&buffer
)



let abl = UnsafeMutableAudioBufferListPointer(&audioBufferList)

for buffer in abl{
let samples = UnsafeMutableBufferPointer<Int16>(start: UnsafeMutablePointer(buffer.mData),
count: Int(buffer.mDataByteSize)/sizeof(Int16))


var sum:Int = 0
for sample in samples {
sum = sum + Int(sample*sample)

}

let rms = sqrt(Double(sum)/count)
}

最佳答案

使用AVCaptureAudioDataOutputSampleBufferDelegate的方法

captureOutput(captureOutput: AVCaptureOutput!, let didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!)

从最后一个参数获取 AVCaptureConnection

然后从connection.audioChannels获取AVCaptureAudioChannel

然后您可以从中获取音量级别:

audioChannel.averagePowerLevel
audioChannel.peakHoldLevel

关于ios - 快速使用 AVCaptureAudioDataOutputSampleBufferDelegate 捕获音量级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33030425/

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