2

I manage to get samples from microphone with that tutorial. I minimalize metod processBuffer: to copy samples to audioBuffer. My question in, how can I convert AudioBuffer to NSData?

Błażej
  • 3,617
  • 7
  • 35
  • 62

1 Answers1

0

Following code should produce the conversion.

-(NSData *)processBuffer: (AudioBufferList*) audioBufferList

{

    AudioBuffer sourceBuffer = audioBufferList->mBuffers[0];

    // we check here if the input data byte size has changed
    if (inAudioBuffer.mDataByteSize != sourceBuffer.mDataByteSize) {
        // clear old buffer
        free(inAudioBuffer.mData);
        // assing new byte size and allocate them on mData
        inAudioBuffer.mDataByteSize = sourceBuffer.mDataByteSize;
        inAudioBuffer.mData = malloc(sourceBuffer.mDataByteSize);
    }
    int currentBuffer =0;
    int maxBuf = 800;

    NSMutableData *data=[[NSMutableData alloc] init];
    // CMBlockBufferRef blockBuffer;
    // CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(ref, NULL, &audioBufferList, sizeof(audioBufferList), NULL, NULL, 0, &blockBuffer);
    // NSLog(@"%@",blockBuffer);


    // audioBufferList->mBuffers[0].mData, audioBufferList->mBuffers[0].mDataByteSize
    for( int y=0; y<audioBufferList->mNumberBuffers; y++ )
    {
        if (currentBuffer < maxBuf){
            AudioBuffer audioBuff = audioBufferList->mBuffers[y];
            Float32 *frame = (Float32*)audioBuff.mData;


            [data appendBytes:frame length:inAudioBuffer.mDataByteSize];
            currentBuffer += audioBuff.mDataByteSize;
        }
        else{
            break;
        }

    }

  return data;

    // copy incoming audio data to the audio buffer (no need since we are not using playback)
    //memcpy(inAudioBuffer.mData, audioBufferList->mBuffers[0].mData, audioBufferList->mBuffers[0].mDataByteSize);
}
Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265
din Ba
  • 1