Me gustaría medir el volumen del sonido del entorno, no estoy seguro de si estoy haciendo lo correcto.Estoy haciendo lo correcto para convertir decibeles de -120 - 0 a 0 - 120
Me gustaría crear un medidor VU de un rango de 0 (silencioso) a 120 (muy ruidoso).
Obtuve la potencia máxima y media pero soy muy alto en un entorno silencioso normal. Dame un puntero.
- (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);
}
Hola Michael, gracias por la respuesta. Creo que el averagePowerForChannel son el valor de decibelios en -x le gustaría convertirlo en 0 - 120 Valor – Desmond
@Desmond: Ok, así que hacer el último paso le sugiero cambiar peakPowerForChannel utilizar directamente el valor de decibelios. Usted está agregando 120 más tarde. También necesitarás asegurarte de que no sea menor a cero usando un máximo (0, db) como lo hice con 'db = db <0? 0: db; ' –
gracias Michael, sin embargo, los decibelios son muy altos en una habitación silenciosa ... descargo una aplicación de decibel10 para comprobar que los diferentes decibelios son enormes. La aplicación muestra aproximadamente 40db, la mía muestra 70db. mi objetivo principal aquí es comprobar si el usuario está haciendo ruido. si excede el umbral se disparará algo. – Desmond