2012-05-16 174 views
-1

我正在嘗試創建一個應用程序,它可以監聽並計算iPhone周圍環境中的平均音頻電平。一旦這個「平均」水平被超越,說有人喊,那麼應用程序會通知用戶平均聲級已被超過。你能否給我提供一個示例代碼或易於遵循的教程,因爲我很新,而且我遇到的所有教程都太難理解,或者不適用於我的XCODE版本(4.3.2 ) 謝謝。音頻電平檢測

+1

這是一個令人難以置信廣泛的問題.. – Lloyd

+0

只需計算RMS功率在合理的時間間隔。如果您不確定如何執行此操作,請搜索「RMS」。 –

回答

3

赫雷什計算輸出並打印一個例子:

-(void)loadView{ 
    /*****ALLOC VIEW*****/ 
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame]; 

    UIView *contentView = [[UIView alloc] initWithFrame:applicationFrame]; 

    contentView.backgroundColor = [UIColor whiteColor]; 

    self.view = contentView; 
    [contentView release]; 

} 
- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UIImageView *bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 90, 320, 247)]; 
    bg.image = [UIImage imageNamed:@"BF_pre.png"]; 
    [self.view addSubview: bg];//IGNORE 
    NSURL *url = [NSURL fileURLWithPath:@"/dev/null"]; 

    NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys: 
          [NSNumber numberWithFloat: 44100.0], AVSampleRateKey, 
          [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey, 
          [NSNumber numberWithInt: 1], AVNumberOfChannelsKey, 
          [NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey, 
          nil]; 

    NSError *error; 

    recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error]; 

    if (recorder) { 
     [recorder prepareToRecord]; 
     recorder.meteringEnabled = YES; 
     [recorder record]; 
     levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES]; 
    } 
} 


- (void)levelTimerCallback:(NSTimer *)timer { 
    [recorder updateMeters]; 

    const double ALPHA = 0.05; 
    double peakPowerForChannel = pow(10, (0.05 * [recorder peakPowerForChannel:0])); 
    lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults; 
    NSLog(@"%f",(lowPassResults*100.0f)); 
} 


- (void)dealloc { 
    [levelTimer release]; 
    [recorder release]; 
    [super dealloc]; 
} 
+0

這對我工作,謝謝 – channi