本文介绍了我做正确的事情将分贝从-120 - 0转换为0 - 120的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测量周围的音量,不太确定我做的是否合适。

I will like to measure the sound volume of the surrounding, not too sure if I am doing the right thing.

我想创建一个VU表范围为0(安静)到120(非常嘈杂)。

I will like to create a VU meter of a range of 0(quiet) to 120(very noisy).

我获得了峰值和平均功率,但在正常安静的环境中非常高。
请给我一些指针。

I gotten the Peak and Avg power but are very high in normal quiet environment.Do give me some pointer.

- (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.0作为参考(对于0db)时幅度为分贝,是

The formula for converting a linear amplitude to decibels when you want to use 1.0 as your reference (for 0db), is

20 * log10(amp);

所以我不确定查看代码的意图,但你可能想要

So I'm not sure about the intent from looking at your code, but you probably want

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

这将从幅度为零的-infinity到幅度为1的0db。 b $ b如果你真的需要它上升到0到120之间,你可以添加120并使用最大函数为零。

This will go from -infinity at an amplitude of zero, to 0db at an amplitude of 1.If you really need it to go up to between 0 and 120 you can add 120 and use a max function at zero.

所以,在上面一行之后:

So, after the above line:

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

您使用的公式似乎是将DB转换为放大器的公式,我认为这是与你想要的相反。

The formula you are using appears to be the formula for converting DB to amp, which I think is the opposite of what you want.

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

I reread and it seems you may already have the decibel value.

如果是这种情况,只需不要转换为幅度并添加120.

If this is the case, just don't convert to amplitude and add 120.

所以改变

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

double peakPowerForChannel = [recorder averagePowerForChannel:0];

你应该可以去。

这篇关于我做正确的事情将分贝从-120 - 0转换为0 - 120的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 22:56