如何验证音频数据有效?

时间:2019-03-14 08:00:20

标签: macos audio avfoundation core-audio avcapturesession

我正在尝试使用AVCaptureSession通过以下设置捕获麦克风的音频,

AVCaptureSession* captureSession = [AVCaptureSession new];

AVCaptureDevice* audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];

NSError* error;
AVCaptureDeviceInput* audioInput = [[AVCaptureDeviceInput alloc] initWithDevice:audioCaptureDevice error:&error];
if (error) {
    NSLog(@"AVCaptureDeviceInput Error: %@", error.localizedDescription);
}

if ([captureSession canAddInput:audioInput]) {
    [captureSession addInput:audioInput];
} else {
    NSLog(@"Unable to add Audio Input to Capture Session");
}

AVCaptureAudioDataOutput* audioOutput = [AVCaptureAudioDataOutput new];

NSDictionary* settings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt: kAudioFormatLinearPCM], AVFormatIDKey, [NSNumber numberWithFloat: 44100.0], AVSampleRateKey, nil];
audioOutput.audioSettings = settings;

dispatch_queue_t audioQueue = dispatch_queue_create("AudioSessionQueue", nil);
[audioOutput setSampleBufferDelegate:self queue:audioQueue];

if ([captureSession canAddOutput:audioOutput]) {
    [captureSession addOutput:audioOutput];
} else {
    NSLog(@"Unable to add Audio Output to Capture Session");
}

[captureSession commitConfiguration];
[captureSession startRunning];

currentSession = captureSession;

我在-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection委托方法中获得了一些数据,并按如下所示将其转换为NSData,

AudioBufferList audioBufferList;
CMBlockBufferRef blockBuffer;
CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer, NULL, &audioBufferList, sizeof(audioBufferList), NULL, NULL, 0, &blockBuffer);
CMItemCount numSamples = CMSampleBufferGetNumSamples(sampleBuffer);
NSUInteger size = sizeof(audioBufferList);
char buffer[size];
((int*)buffer)[0] = (int)numSamples;
memcpy(buffer, &audioBufferList, size);
//This is the Audio data.
NSData *bufferData = [NSData dataWithBytes:buffer length:size];

现在我的问题是如何验证数据是否为有效音频?

0 个答案:

没有答案