iphone – 我做正确的事情将分贝从-120 – 0转换为0 – 120

iphone – 我做正确的事情将分贝从-120 – 0转换为0 – 120,第1张

概述我想测量周围的音量,不太确定我做的是否正确. 我想创建一个范围为0(安静)到120(非常嘈杂)的VU表. 我获得了峰值和平均功率,但在正常安静的环境中非常高. 请给我一些指针. - (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view, typically 我想测量周围的音量,不太确定我做的是否正确.

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

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

- (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,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)时,将线性幅度转换为分贝的公式是

20 * log10(amp);

因此,我不确定查看代码的意图,但您可能想要

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

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

所以,经过以上这一行:

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

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

编辑:我重读了,似乎你可能已经有了分贝值.

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

所以改变

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

double peakPowerForChannel = [recorder averagePowerForChannel:0];

你应该没事的去吧.

总结

以上是内存溢出为你收集整理的iphone – 我做正确的事情将分贝从-120 – 0转换为0 – 120全部内容,希望文章能够帮你解决iphone – 我做正确的事情将分贝从-120 – 0转换为0 – 120所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1059756.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存