ios - iOS中的音频处理以获得音量和音调

标签 ios ios7 avaudiosession audio-analysis

我正在尝试在 iOS7 上创建一个实时音频分析器。我想要获得的是 iPod Touch Gen 5 上 native 麦克风的音量和音调,并连同时间戳一起写入 CSV。我想将其分成 7 个 channel ,并以 8Hz 采样。我查看了一堆文档和代码示例,但没有任何效果。

我现在正尝试从头开始做一些简单的事情,但在我看来,没有任何内容概述我如何实现上面提到的目标。

最近我尝试了 AVAudioSessionCategoryAudioProcessing 希望能够将其用于信号处理,但 Audio Session 文档表明只能完成自动信号处理......并且只能以语音或视频聊天模式。

- (void)analyzeAudio
{
AVAudioSession *audioSession = [AVAudioSession sharedInstance];

audioUnit = (AudioUnit*)malloc(sizeof(AudioUnit));

NSError *activationError = nil;

BOOL success = [[AVAudioSession sharedInstance] setActive: YES error: &activationError];

if (!success)
{
    NSLog(@"AudioSession could not init");
}

[audioSession setCategory:AVAudioSessionCategoryAudioProcessing error:nil];

[audioSession setActive:YES error:nil];
}

有没有一种简单的方法可以通过 Audio Session 来获取我正在寻找的内容?

最佳答案

发现我可以在计时器上使用 AVAudioRecorder 方法 updateMeters 来获取 peakPowerForChannel: 某个时间间隔的值。

- (void)recordAudio
{
_audioSession = [AVAudioSession sharedInstance];

NSError *error;
[_audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
[_audioSession setActive:YES error:&error];

NSMutableDictionary *settings = [NSMutableDictionary dictionary];
[settings setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[settings setValue:[NSNumber numberWithFloat:8000.0] forKey:AVSampleRateKey];
[settings setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];
[settings setValue:[NSNumber numberWithFloat:16000] forKey:AVEncoderBitRateKey];
[settings setValue:[NSNumber numberWithInt:AVAudioQualityMin] forKey:AVEncoderAudioQualityForVBRKey];

NSArray *dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [dirPath objectAtIndex:0];

long currentTime = [[NSDate date] timeIntervalSince1970];
NSString *filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"audio_%ld.aac", currentTime]];
NSURL *audioFileURL = [NSURL fileURLWithPath:filePath];

_audioRecorder = [[AVAudioRecorder alloc] initWithURL:audioFileURL settings:settings error:&error];

if (error)
{
    NSLog(@"audio record error: %@", [error localizedDescription]);

} else {
    [_audioRecorder prepareToRecord];
    _audioRecorder.meteringEnabled = YES;
    [_audioRecorder record];
    [self addTextToLog:@"Recording Audio"];
    self.audioTimer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(updateAudioMeters) userInfo:nil repeats:YES];
}
}
- (void)updateAudioMeters
{
[_audioRecorder updateMeters];

NSLog(@"pkPwr: %f", [_audioRecorder peakPowerForChannel:0]);
}

关于ios - iOS中的音频处理以获得音量和音调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24894931/

相关文章:

ios - 在 dynamicLinks.handleUniversalLink(url) 的回调中接收 nil

objective-c - 创建具有向前和向后导航的模拟 3D "space"

objective-c - UIActivityViewController 调用 ViewWillAppear 但不调用 ViewWillDisappear

ios - 从 NSData 为 localNotification 设置自定义声音

css - 如何使用 css 模糊滤镜?

ios - 在 iOS UITextField 上显示验证错误类似于 Android 的 TextView.setError()

objective-c - 试图使 UITableView 的 tableHeaderView 超出表格边界

ios - 来电后无法录音

ios - 如何获取/设置iOS流音量?

swift - AVAudioSession setCategory 在 Swift 4.2 中的可用性