如何从iPhone上录制的音频中获取PCM数据?

时间:2012-06-27 12:23:11

标签: objective-c ios audio-recording avaudiorecorder endianness

我正在使用AVAudioRecorderAVAudioPlayer框架在iphone上录制和播放录制的音频。它的工作。我可以使用麦克风录制,然后我可以播放它。

但我需要从录制的音频中获取pcm数据。因为我使用LE_SpectrumWorkx来修改声音,它需要" .pcm"输入数据。我必须获取pcm数据并将其发送到LE_SpectrumWorkx(有关此内容的更多信息:http://www.littleendian.com)。

如何从录制的音频中获取pcm数据?我应该转换" sound.caf"任何类型?

这是当前的代码:

@synthesize btnRecord, btnPlay, btnStop, txtResult;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    btnPlay.enabled = NO;
    btnStop.enabled = NO;

    NSArray *dirPaths;
    NSString *docsDir;

    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    docsDir = [dirPaths objectAtIndex:0];

    NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.caf"];
    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

    NSDictionary *recordSettings =
    [
        NSDictionary dictionaryWithObjectsAndKeys:
        [NSNumber numberWithInt:kAudioFormatLinearPCM],AVFormatIDKey,
        [NSNumber numberWithInt:AVAudioQualityMin],    AVEncoderAudioQualityKey,
        [NSNumber numberWithInt:16],                   AVEncoderBitRateKey, 
        [NSNumber numberWithInt:2],                    AVNumberOfChannelsKey,
        [NSNumber numberWithFloat:44100.0],            AVSampleRateKey,
        nil
     ];

    NSError *error = nil;

    audioRecorder = 
    [
     [AVAudioRecorder alloc] 
     initWithURL:soundFileURL 
     settings:recordSettings 
     error:&error
    ];

    if(error)
    {
        NSLog(@"error: %@", [error localizedDescription]);
    }
    else 
    {
        [audioRecorder prepareToRecord];
    }     
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    audioPlayer = nil;
    audioRecorder = nil;
    btnRecord = nil;
    btnPlay = nil;
    btnStop = nil;
}

- (void)dealloc 
{

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

-(IBAction)onRecord:(id)sender
{
    NSLog(@" //-- record start button clicked");
    if(!audioRecorder.recording)
    {
        btnPlay.enabled = NO;
        btnStop.enabled = YES;
        [audioRecorder record];
    }
}

-(IBAction)onStop:(id)sender
{
    NSLog(@" //-- record stop button clicked");
    btnStop.enabled = NO;
    btnPlay.enabled = YES;
    btnRecord.enabled = YES;

    if(audioRecorder.recording)
    {
        [audioRecorder stop];
    }
    else 
    {
        [audioPlayer stop];
    }
}

-(IBAction)onPlayAudio:(id)sender
{
    NSLog(@" //-- play");

    if(!audioRecorder.recording)
    {
        btnStop.enabled = YES;
        btnRecord.enabled = NO;

       // if(audioPlayer) [audioPlayer release];

        NSError *error;

        audioPlayer = [
                        [AVAudioPlayer alloc]
                        initWithContentsOfURL:audioRecorder.url 
                        error:&error
                       ];
        audioPlayer.delegate = self;
        if(error)
        {
            NSLog(@"Error: %@", [error localizedDescription]);
        }
        else 
        {
            [audioPlayer play];
        }
    }
}

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    NSLog(@"Playback Finished");

    btnRecord.enabled = YES;
    btnStop.enabled = NO;

    [self onStop:nil];
}

-(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
    NSLog(@"Decode Error occured");
}

-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag
{
    NSLog(@"Record Finished");
}

-(void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error
{
    NSLog(@"Encode Error occured");
}

0 个答案:

没有答案