Tôi đang cố gắng tạo một ứng dụng chạy FFT trên dữ liệu micrô, vì vậy tôi có thể kiểm tra ví dụ: tần số to nhất trong đầu vào.Truyền dữ liệu AVCaptureAudioDataOutput vào vDSP/Accelerate.framework
Tôi thấy rằng có rất nhiều phương pháp nhận đầu vào âm thanh (Remote AudioUnit, AudioQueue dịch vụ và AVFoundation) nhưng có vẻ như AVFoundation là đơn giản nhất. Tôi có thiết lập này:
// Configure the audio session
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryRecord error:NULL];
[session setMode:AVAudioSessionModeMeasurement error:NULL];
[session setActive:YES error:NULL];
// Optional - default gives 1024 samples at 44.1kHz
//[session setPreferredIOBufferDuration:samplesPerSlice/session.sampleRate error:NULL];
// Configure the capture session (strongly-referenced instance variable, otherwise the capture stops after one slice)
_captureSession = [[AVCaptureSession alloc] init];
// Configure audio device input
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:NULL];
[_captureSession addInput:input];
// Configure audio data output
AVCaptureAudioDataOutput *output = [[AVCaptureAudioDataOutput alloc] init];
dispatch_queue_t queue = dispatch_queue_create("My callback", DISPATCH_QUEUE_SERIAL);
[output setSampleBufferDelegate:self queue:queue];
[_captureSession addOutput:output];
// Start the capture session.
[_captureSession startRunning];
(cộng với kiểm tra lỗi, bỏ qua ở đây để dễ đọc).
Sau đó, tôi thực hiện AVCaptureAudioDataOutputSampleBufferDelegate phương pháp sau đây:
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
NSLog(@"Num samples: %ld", CMSampleBufferGetNumSamples(sampleBuffer));
// Usually gives 1024 (except the first slice)
}
tôi không chắc chắn những gì bước tiếp theo nên được. Định dạng CMSampleBuffer
mô tả chính xác (và những giả định nào có thể được thực hiện về nó, nếu có)? Làm cách nào để nhận dữ liệu âm thanh thô thành vDSP_fft_zrip
với số tiền xử lý bổ sung ít nhất có thể? (Ngoài ra, bạn sẽ khuyên bạn nên làm gì để xác minh rằng dữ liệu thô tôi thấy là chính xác?)
"Định dạng âm thanh mặc định cho micrô iPhone là một kênh gồm 16 bit nguyên" - thông tin này đến từ đâu? Tôi lo ngại rằng việc đưa ra các giả định như thế này sẽ không an toàn nói chung trên phần cứng thiết bị khác nhau. – jtbandes
Bạn nói đúng, và giả định là trên thực tế sai, tôi đã cập nhật để kiểm tra định dạng âm thanh. Một số nhận xét ở đây về các mặc định AVCapture: http://developer.apple.com/library/ios/#samplecode/AVCaptureToAudioUnit/Listings/CaptureSessionController_mm.html – Tark