7

我正在尝试使用它编译和运行的 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)
    }
4

4 回答 4

7

使用AVCaptureAudioDataOutputSampleBufferDelegate的方法

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

AVCaptureConnection从最后一个参数获取。

然后AVCaptureAudioChannelconnection.audioChannels

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

audioChannel.averagePowerLevel
audioChannel.peakHoldLevel
于 2019-03-11T10:52:07.023 回答
6

嘿,我不太了解,但这是一个有效的 Swift 5 版本:

func captureOutput(_            output      : AVCaptureOutput,
                   didOutput    sampleBuffer: CMSampleBuffer,
                   from         connection  : AVCaptureConnection) {

    var buffer: CMBlockBuffer? = nil

    // Needs to be initialized somehow, even if we take only the address
    let convenianceBuffer = AudioBuffer(mNumberChannels: 1, mDataByteSize: 0, mData: nil)
    var audioBufferList = AudioBufferList(mNumberBuffers: 1,
                                          mBuffers: convenianceBuffer)

    CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(
        sampleBuffer,
        bufferListSizeNeededOut: nil,
        bufferListOut: &audioBufferList,
        bufferListSize: MemoryLayout<AudioBufferList>.size(ofValue: audioBufferList),
        blockBufferAllocator: nil,
        blockBufferMemoryAllocator: nil,
        flags: UInt32(kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment),
        blockBufferOut: &buffer
    )

    let abl = UnsafeMutableAudioBufferListPointer(&audioBufferList)

    for buffer in abl {
        let originRawPtr = buffer.mData
        let ptrDataSize = Int(buffer.mDataByteSize)

        // From raw pointer to typed Int16 pointer
        let buffPtrInt16 = originRawPtr?.bindMemory(to: Int16.self, capacity: ptrDataSize)

        // From pointer typed Int16 to pointer of [Int16]
        // So we can iterate on it simply
        let unsafePtrByteSize = ptrDataSize/Int16.bitWidth
        let samples = UnsafeMutableBufferPointer<Int16>(start: buffPtrInt16,
                                                        count: unsafePtrByteSize)

        // Average of each sample squared, then root squared
        let sumOfSquaredSamples = samples.map(Float.init).reduce(0) { $0 + $1*$1 }
        let averageOfSomething = sqrt(sumOfSquaredSamples / Float(samples.count))

        DispatchQueue.main.async {
            print("Calulcus of something: \(String(averageOfSomething))" )
        }
    }
}
于 2020-03-13T17:47:28.403 回答
2

看来我有它的工作。在进行任何操作之前,我sample都选择了一个。Int64

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

        var sum:Int64 = 0

        for sample in samples {
         let s = Int64(sample)
         sum +=s*s
        }


        dispatch_async(dispatch_get_main_queue()) {

            self.volLevel.text = String(sqrt(Float(sum/Int64(samples.count))))
        }
于 2015-10-09T08:24:15.783 回答
1

我玩过你的例子。这是一个完整的 swift 2 代码片段:

// also define a variable in class scope, otherwise captureOutput will not be called
    var session : AVCaptureSession!



    func startCapture() {
    if let device : AVCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio){

        do {
            self.session = AVCaptureSession()
            try device.lockForConfiguration()

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

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


            let audioOutput = AVCaptureAudioDataOutput()

            audioOutput.setSampleBufferDelegate(self, queue: dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0))

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


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

        } catch {

        }
    }
}

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

    var buffer: CMBlockBuffer? = nil

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

    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:Int64 = 0

        for sample in samples {
            let s = Int64(sample)
            sum = (sum + s*s)
        }

        dispatch_async(dispatch_get_main_queue()) {

            print( String(sqrt(Float(sum/Int64(samples.count)))))
        }
    }
}
于 2016-06-09T15:19:24.050 回答