gpt4 book ai didi

objective-c - AVCaptureDevice,以特定采样频率捕获WAV音频

转载 作者:行者123 更新时间:2023-12-03 16:30:43 24 4
gpt4 key购买 nike

所以问题很简单:如何使用 AVCaptureDevice、AVCaptureSession、AVCaptureDeviceInput 和 AVCaptureAudioFileOutput 以特定的采样频率、比特率等捕获 WAV 文件?我有代码:

captureSession = [[AVCaptureSession alloc] init];
AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
NSError *error = nil;
AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&error];
AVCaptureAudioFileOutput *audioOutput = [[AVCaptureAudioFileOutput alloc] init];
if (audioInput && [captureSession canAddInput:audioInput] && [captureSession canAddOutput:audioOutput]) {
[captureSession addInput:audioInput];
[captureSession addOutput:audioOutput];
[captureSession startRunning];
}

之后,我处理其他函数的启动:

- (void)captureSessionStartedNotification:(NSNotification *)notification
{
AVCaptureSession *session = notification.object;
id audioOutput = session.outputs[0];
.......
[audioOutput startRecordingToOutputFileURL:[NSURL fileURLWithPath:filePath] outputFileType:AVFileTypeWAVE recordingDelegate:self];
}

我在哪里可以设置捕获属性或类似的东西?附: setSessionPreset 根本不起作用。

最佳答案

好吧,这并不完全符合我的预期,但无论如何我已经想出了解决方案。就我而言,我需要 AVSampleRateKey eq 为 16000,AVLinearPCMBitDepthKey eq 为 16 和 1 个 channel 。但不幸的是,设置这些属性并尝试录制 wav 格式有点复杂。要设置我使用的属性:

NSDictionary *recordSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:kAudioFormatLinearPCM],
AVFormatIDKey,
[NSNumber numberWithInt:16],
AVLinearPCMBitDepthKey,
[NSNumber numberWithBool:NO],
AVLinearPCMIsFloatKey,
[NSNumber numberWithInt: 1],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:16000.0],
AVSampleRateKey,
nil];

[audioOutput setAudioSettings:recordSettings];

有两个选项:

  1. [audioOutput startRecordingToOutputFileURL:[NSURL fileURLWithPath:filePath] outputFileType:AVFileTypeCoreAudioFormat reportingDelegate:self];
  2. [audioOutput startRecordingToOutputFileURL:[NSURL fileURLWithPath:filePath] outputFileType:AVFileTypeWAVE reportingDelegate:self];

在第一种情况下,我什至可以编写 .wav 文件,但由于某些原因,我在上面设置的某些属性根本不起作用(例如,频率)。在第二种情况下,我什至无法写任何东西。所有行都没有错误,但在某些时候,文件没有被创建。因此做出了决定:用我的参数编写 .caf,然后将其转换为所需的 .wav 文件。所以上面的设置+第一种情况可以编写简单的.caf文件。之后我使用了转换函数:

-(BOOL)convertFromCafToWav:(NSString*)filePath
{
NSError *error = nil ;

NSDictionary *audioSetting = [NSDictionary dictionaryWithObjectsAndKeys:
[ NSNumber numberWithFloat:16000.0], AVSampleRateKey,
[ NSNumber numberWithInt:1], AVNumberOfChannelsKey,
[ NSNumber numberWithInt:16], AVLinearPCMBitDepthKey,
[ NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,
[ NSNumber numberWithBool:NO], AVLinearPCMIsFloatKey,
[ NSNumber numberWithBool:0], AVLinearPCMIsBigEndianKey,
[ NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved,
[ NSData data], AVChannelLayoutKey, nil ];

NSString *audioFilePath = filePath;
AVURLAsset * URLAsset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:audioFilePath] options:nil];

if (!URLAsset) return NO ;

AVAssetReader *assetReader = [AVAssetReader assetReaderWithAsset:URLAsset error:&error];
if (error) return NO;

NSArray *tracks = [URLAsset tracksWithMediaType:AVMediaTypeAudio];
if (![tracks count]) return NO;

AVAssetReaderAudioMixOutput *audioMixOutput = [AVAssetReaderAudioMixOutput
assetReaderAudioMixOutputWithAudioTracks:tracks
audioSettings :audioSetting];

if (![assetReader canAddOutput:audioMixOutput]) return NO ;

[assetReader addOutput :audioMixOutput];

if (![assetReader startReading]) return NO;

NSString *outPath = [NSString stringWithFormat:@"%@/%@.wav", NSHomeDirectory(), @"myWav"]; //for example

NSURL *outURL = [NSURL fileURLWithPath:outPath];
AVAssetWriter *assetWriter = [AVAssetWriter assetWriterWithURL:outURL
fileType:AVFileTypeWAVE
error:&error];
if (error) return NO;
AVAssetWriterInput *assetWriterInput = [ AVAssetWriterInput assetWriterInputWithMediaType :AVMediaTypeAudio
outputSettings:audioSetting];
assetWriterInput. expectsMediaDataInRealTime = NO;
if (![assetWriter canAddInput:assetWriterInput]) return NO ;
[assetWriter addInput :assetWriterInput];
if (![assetWriter startWriting]) return NO;
[assetWriter startSessionAtSourceTime:kCMTimeZero ];
dispatch_queue_t queue = dispatch_queue_create( "assetWriterQueue", NULL );
[assetWriterInput requestMediaDataWhenReadyOnQueue:queue usingBlock:^{
while (TRUE)
{
if ([assetWriterInput isReadyForMoreMediaData] && (assetReader.status == AVAssetReaderStatusReading)) {

CMSampleBufferRef sampleBuffer = [audioMixOutput copyNextSampleBuffer];

if (sampleBuffer) {
[assetWriterInput appendSampleBuffer :sampleBuffer];
CFRelease(sampleBuffer);
} else {
[assetWriterInput markAsFinished];
break;
}
}
}
[assetWriter finishWritingWithCompletionHandler:^{}];

NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir;
NSString* resultDir = NSHomeDirectory(); //for example
if([fileManager fileExistsAtPath:resultDir isDirectory:&isDir]){
if ([fileManager fileExistsAtPath:cafPath]){
[fileManager removeItemAtPath:cafPath error:nil];
}
}

}];
return YES;
}

谢谢你的回答:Passing Sound (wav) file to javascript from objective c

关于objective-c - AVCaptureDevice,以特定采样频率捕获WAV音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37287481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com