I am trying to get the raw audio data from the system microphone using AudioToolbox and CoreFoundation frameworks. So far the writing packets to file logic works but when I try to capture the raw data into a file I am getting white noise.
Callback function looks like this:
static void MyAQInputCallback(void *inUserData, AudioQueueRef inQueue,
AudioQueueBufferRef inBuffer,
const AudioTimeStamp *inStartTime,
UInt32 inNumPackets,
const AudioStreamPacketDescription *inPacketDesc)
{
MyRecorder *recorder = (MyRecorder *)inUserData;
if (inNumPackets > 0)
{
CheckError(AudioFileWritePackets(recorder->recordFile, FALSE, inBuffer->mAudioDataByteSize,
inPacketDesc, recorder->recordPacket, &inNumPackets,
inBuffer->mAudioData), "AudioFileWritePackets failed");
recorder->recordPacket += inNumPackets;
int sampleCount = inBuffer->mAudioDataByteSize / sizeof(AUDIO_DATA_TYPE_FORMAT);
AUDIO_DATA_TYPE_FORMAT* samples = (AUDIO_DATA_TYPE_FORMAT*)inBuffer->mAudioData;
FILE *fp = fopen(filename, "a");
for (int i = 0; i < sampleCount; i++){
fprintf(fp, "%i;\n",samples[i]);
}
fclose(fp);
}
if (recorder->running)
CheckError(AudioQueueEnqueueBuffer(inQueue, inBuffer, 0, NULL), "AudioQueueEnqueueBuffer failed");
}
Some parameters:
NumberRecordBuffers = 3
buffer duration = 0.1
format->mFramesPerPacket = 4096
samplerate = 44100
inNumPackets = 1
recordFormat.mFormatID = kAudioFormatAppleLossless;
recordFormat.mChannelsPerFrame = 1;
recordFormat.mBitsPerChannel = 16;
Is this the correct way to do this? I could not find much information in the documentation. Any help is appreciated. Thank you in advance.