Audio Input Samples

Thus, I have some problems with my AudioUnit taking mic / I / O data in iOS. I can configure everything for what, in my opinion, is good, and it calls my recordCallback, but the data that I get from the buffer is incorrect. It always returns the exact same thing, which basically has zeros and random large numbers. Does anyone know what could be causing this. My code is as follows.

Audio device setup

OSStatus status; // Describe audio component AudioComponentDescription desc; desc.componentType = kAudioUnitType_Output; desc.componentSubType = kAudioUnitSubType_RemoteIO; desc.componentFlags = 0; desc.componentFlagsMask = 0; desc.componentManufacturer = kAudioUnitManufacturer_Apple; // Get component AudioComponent inputComponent = AudioComponentFindNext(NULL, &desc); status = AudioComponentInstanceNew(inputComponent, &audioUnit); // Enable IO for recording UInt32 flag = 1; status = AudioUnitSetProperty(audioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, kInputBusNumber, &flag, sizeof(flag)); // Disable playback IO flag = 0; status = AudioUnitSetProperty(audioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, kOutputBusNumber, &flag, sizeof(flag)); // Describe format AudioStreamBasicDescription audioFormat; audioFormat.mSampleRate = 44100.00; audioFormat.mFormatID = kAudioFormatLinearPCM; audioFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked |kAudioFormatFlagIsNonInterleaved; audioFormat.mFramesPerPacket = 1; audioFormat.mChannelsPerFrame = 1; audioFormat.mBitsPerChannel = 32; audioFormat.mBytesPerPacket = 4; audioFormat.mBytesPerFrame = 4; // Apply format status = AudioUnitSetProperty(audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, kInputBusNumber, &audioFormat, sizeof(audioFormat)); // Set input callback AURenderCallbackStruct callbackStruct; callbackStruct.inputProc = recordingCallback; callbackStruct.inputProcRefCon = (__bridge void*)self; status = AudioUnitSetProperty(audioUnit, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, kInputBusNumber, &callbackStruct, sizeof(callbackStruct)); status = AudioUnitInitialize(audioUnit); 

Input callback

 static OSStatus recordingCallback(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData) { AudioBufferList bufferList; bufferList.mNumberBuffers = 1; bufferList.mBuffers[0].mDataByteSize = 4; bufferList.mBuffers[0].mNumberChannels = 1; bufferList.mBuffers[0].mData = malloc(sizeof(float)*inNumberFrames); // InputAudio *input = (__bridge InputAudio*)inRefCon; OSStatus status; status = AudioUnitRender([input audioUnit], ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, &bufferList); float* result = (float*)&bufferList.mBuffers[0].mData; if (input->counter == 5) { for (int i = 0;i<inNumberFrames;i++) { printf("%f ",result[i]); } } input->counter++; return noErr; } 

Does anyone encounter a similar problem or see something clearly wrong in my code. Thanks in advance for your help!

I base it all on Michael Tyson's Core Audio RemoteIO code

+4
source share
2 answers

If I remember correctly, the samples that you get from the sound buffer in the callback are not floating, it's SInt16. Try sampling like this:

 SInt16 *sn16AudioData= (SInt16 *)(bufferList.mBuffers[0].mData); 

And these should be the values โ€‹โ€‹of max and min:

 #define sn16_MAX_SAMPLE_VALUE 32767 #define sn16_MIN_SAMPLE_VALUE -32768 
+2
source

I basically tried to do the same with very similar code, but using AudioGraph() . I had the same problem, zeros in my mic output and failed to get it working until I added the line

 Status = AUGraphConnectNodeInput(graph, ioNode, 1, ioNode, 0); 

Since you are not using a graph, you need to call AudioUnitSetProperty() with kAudioUnitProperty_MakeConnectio n and pass and pass the full structure of AudioUnitConnection

0
source

All Articles