2014-07-22 3 views
0

iOS7에서 실시간 오디오 분석기를 만들려고합니다. 내가 얻고 자하는 것은 iPod Touch Gen 5의 기본 마이크에서 볼륨 및 피치를 얻고 타임 스탬프와 함께 CSV에 기록하는 것입니다. 나는 그것을 7 채널로 나누고, 8Hz에서 샘플을 만들고 싶다. 나는 많은 문서와 코드 샘플을 살펴 보았지만 아무 것도 할 수 없었다.iOS의 오디오 처리로 음량 및 음높이를 얻으십시오.

나는 처음부터 단순한 것을 시작하려고 노력하고 있지만 위에서 언급 한 것을 달성 할 수있는 방법을 간략히 설명하지는 않습니다.

최근에는 신호 처리를 위해 AVAudioSessionCategoryAudioProcessing을 사용할 수 있기를 기대했지만 오디오 세션 문서에서는 자동 신호 처리 만 수행 할 수 있다고 제안했습니다. 음성 또는 영상 채팅 모드에서만 가능합니다.

- (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]; 
} 

내가 원하는 것을 얻으려면 간단한 오디오 세션이 있습니까?

답변

1

일부 간격으로 peakPowerForChannel : 값을 얻기 위해 타이머에 AVAudioRecorder 메서드 updateMeters를 사용할 수 있음을 알게되었습니다.

- (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]); 
} 
+0

좋은 답변이지만 어떻게하면 오디오 피치를 얻을 수 있습니까? –

+0

나는 그 부분에 결코 도착할 수 없었다. 저기 밖으로 작동하는 라이브러리가 몇 가지 있지만, 그들은 내 머리를 통해 목표 -C++에서 코딩해야합니다. – RoboArch