Getting Decibels from AVAudioRecorder
<p>I'm trying to get a decibel value from an AVAudio Recorder. This is my code currently. I have a method to start the recorder, then a method to read the decibel values.</p>

<pre><code>    var recorder: AVAudioRecorder!
</code></pre>

<p>Recorder defined globally, then used here:</p>

<pre><code>    func init_recorder() -&gt; Void
{
    let recordersettings = [
        AVFormatIDKey: Int(kAudioFormatAppleIMA4),
        AVSampleRateKey: 44100,
        AVNumberOfChannelsKey: 1 as NSNumber,
        AVLinearPCMBitDepthKey: 16,
        AVLinearPCMIsFloatKey: false,
        AVLinearPCMIsBigEndianKey: false,
        //AVEncoderAudioQualityKey: AVAudioQuality.High.rawValue
    ]
    let filename = NSURL(fileURLWithPath: NSTemporaryDirectory().stringByAppendingString("tmp.caf"))

    do
    {
        recorder = try AVAudioRecorder(URL: filename, settings: recordersettings)
        recorder.meteringEnabled = true
        recorder.prepareToRecord()
        recorder.record()
        recorder.updateMeters()

    }
    catch
    {
        print("error")
    }


}
</code></pre>

<p>Then to get the decibel values I have</p>

<pre><code>    func get_decibels() -&gt; Float
{
    recorder.updateMeters()
    return recorder.averagePowerForChannel(0)
}
</code></pre>

<p>For some reason this is returning a value of -120.0 every time it is called and i'm not too sure why. I have read from <a href="http://b2cloud.com.au/tutorial/obtaining-decibels-from-the-ios-microphone/" rel="nofollow">http://b2cloud.com.au/tutorial/obtaining-decibels-from-the-ios-microphone/</a> and a few other stack overflow threads but most of the examples seem to be quite similar to mine.</p>

<p>Any help appreciated, many thanks!</p>