2011-03-12 87 views
0

當我通過核心音頻從麥克風接收數據時,緩衝區有時只有一個樣本,有時它們有20個樣本。有些時候,樣本的值是0.00000,有時他們的值是NaN,有些時候,他們是你所期望的。來自麥克風的iphone核心音頻數據是NaN

什麼問題?

這裏是我的代碼:

-(void)startListeningWithFrequency:(float)frequency; 
{ 
OSStatus status; 
//AudioComponentInstance audioUnit; 
AudioComponentDescription desc; 
desc.componentType = kAudioUnitType_Output; 
desc.componentSubType = kAudioUnitSubType_RemoteIO; 
desc.componentFlags = 0; 
desc.componentFlagsMask = 0; 
desc.componentManufacturer = kAudioUnitManufacturer_Apple; 

AudioComponent inputComponent = AudioComponentFindNext(NULL, &desc); 
status = AudioComponentInstanceNew(inputComponent, &audioUnit); 
checkStatus(status); 

UInt32 flag = 1; 
status = AudioUnitSetProperty(audioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input,kInputBus, &flag, sizeof(flag)); 
checkStatus(status); 

AudioStreamBasicDescription audioFormat; 
audioFormat.mSampleRate   = 44100.00;//44100.00; 
audioFormat.mFormatID   = kAudioFormatLinearPCM; 
audioFormat.mFormatFlags  = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked; 
audioFormat.mFramesPerPacket = 1; 
audioFormat.mChannelsPerFrame = 1; 
audioFormat.mBitsPerChannel  = 16; 
audioFormat.mBytesPerPacket  = 2; 
audioFormat.mBytesPerFrame  = 2; 

status = AudioUnitSetProperty(audioUnit, 
           kAudioUnitProperty_StreamFormat, 
           kAudioUnitScope_Output, 
           kInputBus, 
           &audioFormat, 
           sizeof(audioFormat)); 
checkStatus(status); 
//status = AudioUnitSetProperty(audioUnit, 
//       kAudioUnitProperty_StreamFormat, 
//       kAudioUnitScope_Input, 
//       kOutputBus, 
//       &audioFormat, 
//       sizeof(audioFormat)); 
checkStatus(status); 
AURenderCallbackStruct callbackStruct; 
callbackStruct.inputProc = recordingCallback; 
callbackStruct.inputProcRefCon = self; 
status = AudioUnitSetProperty(audioUnit, 
           kAudioOutputUnitProperty_SetInputCallback, 
           kAudioUnitScope_Global, 
           kInputBus, &callbackStruct, sizeof(callbackStruct)); 
checkStatus(status); 
/* UInt32 shouldAllocateBuffer = 1; 
AudioUnitSetProperty(audioUnit, kAudioUnitProperty_ShouldAllocateBuffer, kAudioUnitScope_Global, 1, &shouldAllocateBuffer, sizeof(shouldAllocateBuffer)); 
*/ 
//float bufferLength = 0.005; 
//AudioSessionSetProperty(kAudioSessionProperty_PreferredHardwareIOBufferDuration, sizeof(bufferLength), &bufferLength); 
status = AudioOutputUnitStart(audioUnit); 

} 

和回調:

static OSStatus recordingCallback(void *inRefCon, 
           AudioUnitRenderActionFlags *ioActionFlags, 
           const AudioTimeStamp *inTimeStamp, 
           UInt32 inBusNumber, 
           UInt32 inNumberFrames, 
           AudioBufferList *ioData) { 
AudioBuffer buffer; 

buffer.mNumberChannels = 1; 
buffer.mDataByteSize = inNumberFrames * 2; 
NSLog(@"%d",inNumberFrames); 
buffer.mData = malloc(inNumberFrames * 2); 

// Put buffer in a AudioBufferList 
AudioBufferList bufferList; 
bufferList.mNumberBuffers = 1; 
bufferList.mBuffers[0] = buffer; 



OSStatus status; 
status = AudioUnitRender(audioUnit, 
         ioActionFlags, 
         inTimeStamp, 
         inBusNumber, 
         inNumberFrames, 
         &bufferList); 
checkStatus(status); 
double *q = (double *)(&bufferList)->mBuffers[0].mData; 
for(int i=0; i < strlen((const char *)(&bufferList)->mBuffers[0].mData); i++) 
{ 
    //i sometimes doesn't get past 0, sometimes goes into 20s 
    NSLog(@"%f",q[i]);//returns NaN, 0.00, or some times actual data 
} 
} 

任何幫助,將不勝感激, 謝謝 NONONO

回答

3

因爲您傳遞kAudioFormatFlagIsSignedInteger標誌對於流格式,您的示例僅僅是:16位有符號整數(int16_t)而不是fl燕麥。您需要以這種方式處理樣本,或使用kAudioFormatFlagIsFloat標誌代替(並且您需要使用float而不是double作爲數據類型AFAIK)。