Chciałbym zmierzyć głośność dźwięku otoczenia, nie jestem pewien, czy robię to, co trzeba.Czy robię dobrze, aby przekonwertować decybel z -120 - 0 na 0 - 120
Chciałbym stworzyć miernik VU z zakresu od 0 (cichy) do 120 (bardzo głośny).
Zdobyłam moc szczytową i średnią, ale w normalnych, cichych warunkach jest bardzo wysoka. Daj mi wskazówkę.
- (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);
}
Cześć Michael, dzięki za odpowiedź. Wierzę, że averagePowerForChannel jest wartością decybelową w -x będzie chciał przekonwertować ją na wartość 0 - 120 – Desmond
@Desmond: Ok, więc wykonaj ostatni krok Sugeruję zmianę peakPowerForChannel, aby bezpośrednio użyć wartości decybeli. Dodajesz 120 później. Musisz również upewnić się, że jest nie mniej niż zero, używając max (0, db), tak jak to zrobiłem z "db = db <0? 0: db; ' –
dziękuje Michaelowi, jednak decybel jest bardzo bardzo wysoki w cichym pokoju .... ja ściągam aplikację decibel10, żeby sprawdzić, czy decybel jest inny. Aplikacja pokazuje około 40db, moje pokazuje 70db. moim głównym celem tutaj jest sprawdzenie, czy użytkownik robi hałas. jeśli przekroczy próg, coś wyzwoli. – Desmond