我一直在研究 Apple 的 Aurio Touch 示例代码。我想知道如何像风速计一样使用它来测量风速。对此有何建议?麦克风会对声音做出反应,将空气吹入麦克风确实会单独测量声音。如何实现这个来测量风速?任何人都可以帮助我吗?
问问题
691 次
1 回答
2
您可以使用 AVAudioRecorder 来执行此操作。
// 1 .Init 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
于 2013-01-16T03:52:35.077 回答