[ios]我做正确的事转为分贝-120 0 0-120

发布时间: 2017/3/27 19:01:34
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想要测量的周边,音量不太肯定,如果我要做正确的事。

我想创建一系列的 0(quiet) 到 120(very noisy) VU 米。

我得到的峰值和 Avg 的力量但都非常高在正常安静的环境。 能给我一些指针。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


    //creating an audio CAF file in the temporary directory, this isn’t ideal but it’s the only way to get this class functioning (the temporary directory is erased once the app quits). Here we also specifying a sample rate of 44.1kHz (which is capable of representing 22 kHz of sound frequencies according to the Nyquist theorem), and 1 channel (we do not need stereo to measure noise).

    NSDictionary* recorderSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                      [NSNumber numberWithInt:kAudioFormatLinearPCM],AVFormatIDKey,
                                      [NSNumber numberWithInt:44100],AVSampleRateKey,
                                      [NSNumber numberWithInt:1],AVNumberOfChannelsKey,
                                      [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,
                                      [NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,
                                      [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
                                      nil];
    NSError* error;

    NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
    recorder = [[AVAudioRecorder alloc] initWithURL:url settings:recorderSettings error:&error];

    //enable measuring
    //tell the recorder to start recording:
    [recorder record];

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

    } else
    {
        NSLog(@"%@",[error description]);
    }        
}

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

    const double ALPHA = 0.05;
    double peakPowerForChannel = pow(10, (0.05 * [recorder averagePowerForChannel:0]));
    lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;     

    NSLog(@"Average input: %f Peak input: %f Low pass results: %f", [recorder averagePowerForChannel:0], [recorder peakPowerForChannel:0], lowPassResults);

    float tavgPow =[recorder averagePowerForChannel:0] + 120.0;
    float tpPow = [recorder peakPowerForChannel:0] + 120.0;

    float avgPow = tavgPow;//(float)abs([recorder averagePowerForChannel:0]);
    float pPow = tpPow;//(float)abs([recorder peakPowerForChannel:0]);

    NSString *tempAvg = [NSString stringWithFormat:@"%0.2f",avgPow];
        NSString *temppeak = [NSString stringWithFormat:@"%0.2f",pPow];
    [avg setText:tempAvg];
        [peak setText:temppeak];
    NSLog(@"Average input: %f Peak input: %f Low pass results: %f", avgPow,pPow , lowPassResults);
}   

解决方法 1:

计算的公式为一个线性的振幅转换分贝,当你想要 (为 0db),作为你的参考使用 1.0

20 * log10(amp);

所以我不相信的意图是从看着你的代码,但是您可能想要

float db = 20 * log10([recorder averagePowerForChannel:0]);

这会从无穷远处在零到 0db 振幅 1 幅。 如果你真的需要它去达 0 到 120 之间你可以添加 120 并使用 max 函数在零。

因此后上面的行,︰

db += 120;
db = db < 0 ? 0 : db;

您正在使用的公式似乎是将数据库转换为 amp,我认为恰恰相反,你想要的公式。

编辑︰ 我重读了,看来你可能已经有的分贝值。

如果是这样,只是不要将转换为振幅和添加 120。

因此改变

double peakPowerForChannel = pow(10, (0.05 * [recorder averagePowerForChannel:0]));

double peakPowerForChannel = [recorder averagePowerForChannel:0];

和你应该可以去。

官方微信
官方QQ群
31647020