2016-07-07 23 views
0

我試圖在iOS設備上檢測dB,但是,我是AV音頻基礎新手,無法真正弄清楚。我碰到過這個帖子:iOS - Detect Blow into Mic and convert the results! (swift),但它不適合我。如何檢測最大dB Swift

我當前的代碼是這樣的:

import Foundation 
import UIKit 
import AVFoundation 
import CoreAudio 

class ViewController: UIViewController { 

var recorder: AVAudioRecorder! 
var levelTimer = NSTimer() 
var lowPassResults: Double = 0.0 

override func viewDidLoad() { 
    super.viewDidLoad() 

    //make an AudioSession, set it to PlayAndRecord and make it active 
    var audioSession:AVAudioSession = AVAudioSession.sharedInstance() 
    audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: nil) 
    audioSession.setActive(true, error: nil) 

    //set up the URL for the audio file 
    var documents: AnyObject = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] 
    var str = documents.stringByAppendingPathComponent("recordTest.caf") 
    var url = NSURL.fileURLWithPath(str as String) 

    // make a dictionary to hold the recording settings so we can instantiate our AVAudioRecorder 
    var recordSettings: [NSObject : AnyObject] = [AVFormatIDKey:kAudioFormatAppleIMA4, 
                AVSampleRateKey:44100.0, 
                AVNumberOfChannelsKey:2,AVEncoderBitRateKey:12800, 
                AVLinearPCMBitDepthKey:16, 
                AVEncoderAudioQualityKey:AVAudioQuality.Max.rawValue 

    ] 

    //declare a variable to store the returned error if we have a problem instantiating our AVAudioRecorder 
    var error: NSError? 

    //Instantiate an AVAudioRecorder 
    recorder = AVAudioRecorder(URL:url, settings: recordSettings, error: &error) 
    //If there's an error, print otherwise, run prepareToRecord and meteringEnabled to turn on metering (must be run in that order) 
    if let e = error { 
     print(e.localizedDescription) 
    } else { 
     recorder.prepareToRecord() 
     recorder.meteringEnabled = true 

     //start recording 
     recorder.record() 

     //instantiate a timer to be called with whatever frequency we want to grab metering values 
     self.levelTimer = NSTimer.scheduledTimerWithTimeInterval(0.02, target: self, selector: #selector(ViewController.levelTimerCallback), userInfo: nil, repeats: true) 

    } 

} 

//This selector/function is called every time our timer (levelTime) fires 
func levelTimerCallback() { 
    //we have to update meters before we can get the metering values 
    recorder.updateMeters() 

    //print to the console if we are beyond a threshold value. Here I've used -7 
    if recorder.averagePowerForChannel(0) > -7 { 
     print("Dis be da level I'm hearin' you in dat mic ") 
     print(recorder.averagePowerForChannel(0)) 
     print("Do the thing I want, mofo") 
    } 
} 



override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


} 

回答

3

我目前正在建設我關於電影製作的應用程序,並學到了如何以dB爲單位計量的音量。

源數據recorder.averagePowerForChannel不是聲音的分貝真正水平,它的提供是-160的水平範圍 - 0,所以我們需要做一些修改,使這個數據更加合理

所以我找到了一些讓這個數據(值)轉換成dB級別數據的東西。

(!對不起忘了我在那裏發現了它)

這裏是代碼

/** 
Format dBFS to dB 

- author: RÅGE_Devil_Jåmeson 
- date: (2016-07-13) 20:07:03 

- parameter dBFSValue: raw value of averagePowerOfChannel 

- returns: formatted value 
*/ 
func dBFS_convertTo_dB (dBFSValue: Float) -> Float 
{ 
var level:Float = 0.0 
let peak_bottom:Float = -60.0 // dBFS -> -160..0 so it can be -80 or -60 

if dBFSValue < peak_bottom 
{ 
    level = 0.0 
} 
else if dBFSValue >= 0.0 
{ 
    level = 1.0 
} 
else 
{ 
    let root:Float    = 2.0 
    let minAmp:Float   = powf(10.0, 0.05 * peak_bottom) 
    let inverseAmpRange:Float = 1.0/(1.0 - minAmp) 
    let amp:Float    = powf(10.0, 0.05 * dBFSValue) 
    let adjAmp:Float   = (amp - minAmp) * inverseAmpRange 

    level = powf(adjAmp, 1.0/root) 
} 
return level 
} 

我是注意到你正在錄製白衣2個通道,因此這將是我的代碼有點不同;

希望能幫助你或給你一些想法:d

最後更新

更改編碼語言迅速

+0

哦,順便說一句,'averagePowerForChannel' *不*回報分貝。他們從-160變爲0,因爲它們是[dbFS](https://en.wikipedia.org/wiki/DBFS)。 -160表示靜音,並且它一路*上*到0,這是滿級。 – Moritz

+1

哦,thx關於語言提及(和wiki當然:))。我會盡快重寫我的答案代碼,並儘快修改我的答案。 –

+0

感謝您的幫助:) –