2

I'm newbbie in Objective-C and iOS and I need your help. I would like to take my microphone audio input and redirect it directly inside the speaker output (without passing through a file).

I'm here : using AVCaptureSession to get my microphone input and AudioQueue to try to play the output buffer. But I cannot find anything on the internet, it seems that nobody needs to do that. Am I misunderstanding something ? I just want to know how to convert my CMsampleBufferRef into a good pcmbuffer that my AudioQueue can use to play (and so, what to modify in my AudioQueue to link it with my AVCaptureSession). Is it the good way to do ? I found many things to write the outputbuffer into a file, but for doing something on-the-fly, nothing, or only short explanations that I'm not able to use yet.

Please could you give me very detailed explanations with sample code, because I'm a newbbie. For now I can make working my AVCaptureSession (there is something in the CMSampleBufferRef), I can play a file with my AudioQueue, but when I want to link these two ones, I cannot figure out how to do.

Thanks in advance

Cyril
  • 31
  • 1
  • 6

1 Answers1

0

See the code below I wrote (using references I found online). I haven't thoroughly tested it.

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
    AudioBufferList bufList;
    CMBlockBufferRef blockBuffer;
    CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer, NULL, &bufList, sizeof(bufList), NULL, NULL, kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment, &blockBuffer);


    NSMutableData *currentSample = nil;
    for(int i = 0; i < bufList.mNumberBuffers; i++)
    {
        if(i == 0)
        {
            currentSample = [NSMutableData dataWithBytes:bufList.mBuffers[i].mData length:bufList.mBuffers[i].mDataByteSize];
        }else
        {
            [currentSample appendBytes:bufList.mBuffers[i].mData length:bufList.mBuffers[i].mDataByteSize];
        }
    }
}
bsirang
  • 473
  • 4
  • 14