2012-05-16 127 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