0

I've been working on a project to create an application that displays the power level from the microphone. I'm aware of the AVAudioRecorder class what provides the average/peak power, but I want to be able only to record the power level for certain bands. I used the AudioUnitEQ class to set up the bands, and have attached it to an AVAudioEngine and started recording. Currently, I've been using the installTap method to get the AVAudioPCMBuffer, which is where I'm stuck. Is there a way to convert this to a power level? Also, could my approach to this be entirely wrong?

NinjaDeveloper
  • 1,620
  • 3
  • 19
  • 51

1 Answers1

0

You can get avg/peek like this code.

engine.installTap(......) { buffer in 
    guard let data = buffer.floatChannelData?[0] else {
        return
    }
    var dbData = [Float](repeating: 0.0, count: data.count)
    var one: Float = 0.0
    vDSP_vdbcon(&data, 1, &one, &dbData, 1, data.count, 1)

    var avgLevel: Float = 0.0
    var peakLevel: Float = 0.0
    vDSP_rmsqv(dbData, 1, &avgLevel, vDSP_Length(buffer.frameLength))
    vDSP_maxmgv(dbData, 1, &peakLevel, vDSP_Length(buffer.frameLength))
}
woosiki
  • 91
  • 6
  • I currently have something similar setup, my problem is that the floatChannelData that returns is a pointer to an UnsafeMutablePointer of Float. My familiarity with the C APIs is very low, and I'm getting an error on the guard let statement because floatChannelData has no member first. Could you elaborate a little more please? – Chris Davis Nov 06 '17 at 23:29
  • It's my mistake. Sample code needs change "first" to [0]. I will change sample code. Thank you for find mistake. :) – woosiki Nov 07 '17 at 01:22