2013-08-06 53 views
1

我設法從麥克風獲取樣本that tutorial。我最小化metod processBuffer:將樣本複製到audioBuffer。 我的問題在哪裏,我如何將AudioBuffer轉換爲NSData?AudioBuffer到NSData

+0

結帳這裏http://stackoverflow.com/questions/8553987/how-to-convert-cmsamplebufferref-to-nsdata –

回答

0

以下代碼應該會產生轉換。

-(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); 
} 
+2

哪裏'inAudioBuffer'參考來自哪裏? – inorganik