我一直在研究Apple的Aurio Touch示例代码。我想知道如何使用它来测量像风速计那样的风速。有什么建议吗?麦克风对声音做出反应,吹入麦克风的空气确实可以测量声音。如何实现这个来测量风速?有人可以帮我这个吗?
答案 0 :(得分:2)
您可以使用AVAudioRecorder执行此操作。
// 1。像这样启动AVAudioRecorder:
NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
NSError *error;
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
if (recorder) {
[recorder prepareToRecord];
// recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorder record];
}
// 2.初始化计时器以获得平均功率
levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: @selector(levelTimerCallback) userInfo: nil repeats: YES];
-(void)levelTimerCallback
{
[recorder updateMeters];
float peak = [recorder averagePowerForChannel:0];
float value = [meterTable valueAt:peak];
//meterTable = [[MeterTable alloc] initWithMinDecibels:-80. TableSize:50 Root: 1.3];
//Value 0.0~1.0 ,
}
您可以从Apple示例avTouch
获取MeterTable