Tôi muốn đo khối lượng âm thanh xung quanh, không quá chắc chắn nếu tôi đang làm đúng.Tôi đang làm điều đúng để chuyển đổi decibel từ -120 - 0 thành 0 - 120
Tôi muốn tạo đồng hồ VU có dải từ 0 (yên tĩnh) đến 120 (rất ồn).
Tôi nhận được Đỉnh và Công suất trung bình nhưng rất cao trong môi trường yên tĩnh bình thường. Hãy cho tôi một số con trỏ.
- (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);
}
Xin chào Michael, cảm ơn bạn đã trả lời. tôi tin rằng giá trị trung bìnhPowerForChannel là giá trị decibel trong -x sẽ chuyển đổi thành 0 - 120 giá trị – Desmond
@Desmond: Ok, do đó, bước cuối cùng tôi đề nghị thay đổi peakPowerForChannel để sử dụng giá trị decibel trực tiếp. Bạn đang thêm 120 sau. Bạn cũng sẽ cần phải chắc chắn rằng nó không phải là ít hơn số không bằng cách sử dụng một tối đa (0, db) như thế nào tôi đã làm với 'db = db <0? 0: db; ' –
cảm ơn Michael, tuy nhiên decibel là rất rất cao trong một căn phòng yên tĩnh .... tôi tải về một ứng dụng decibel10 để kiểm tra chống lại nó decibel khác nhau là rất lớn. Các ứng dụng hiển thị khoảng 40db, mỏ cho thấy 70db. Mục tiêu chính của tôi ở đây là để kiểm tra xem người dùng có đang tạo ra tiếng ồn hay không. nếu nó vượt quá ngưỡng sẽ kích hoạt một cái gì đó. – Desmond