如何从iOS中的AudioQueueRef浮点数据获取数组?

我正在将audio传入iPhone,并将其传递给(C ++)分析algorithm。 当然,还有很多选项:path中的AudioQueue教程以及启动的东西。

audiocallback,但是,给了一个AudioQueueRef ,我发现苹果的文档在这方面的东西很薄。 内置的方法来写入文件,但没有什么地方你实际上在数据包内部查看数据。

我需要数据。 我不想写任何文件,这是所有的教程 – 甚至苹果的便利I / O对象 – 似乎都瞄准了。 苹果的AVAudioRecorder (令人生气的)会给你关卡和写入数据,但实际上并没有给你访问它。 除非我错过了一些东西…

这个怎么做? 在下面的代码中有inBuffer->mAudioData这是非常接近,但我不能find什么格式这个'数据'是在或如何访问它的信息。

AudioQueuecallback:

 void AudioInputCallback(void *inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer, const AudioTimeStamp *inStartTime, UInt32 inNumberPacketDescriptions, const AudioStreamPacketDescription *inPacketDescs) { static int count = 0; RecordState* recordState = (RecordState*)inUserData; AudioQueueEnqueueBuffer(recordState->queue, inBuffer, 0, NULL); ++count; printf("Got buffer %d\n", count); } 

以及将audio写入文件的代码:

 OSStatus status = AudioFileWritePackets(recordState->audioFile, false, inBuffer->mAudioDataByteSize, inPacketDescs, recordState->currentPacket, &inNumberPacketDescriptions, inBuffer->mAudioData); // THIS! This is what I want to look inside of. if(status == 0) { recordState->currentPacket += inNumberPacketDescriptions; } 

  // so you don't have to hunt them all down when you decide to switch to float: #define AUDIO_DATA_TYPE_FORMAT SInt16 // the actual sample-grabbing code: int sampleCount = inBuffer->mAudioDataBytesCapacity / sizeof(AUDIO_DATA_TYPE_FORMAT); AUDIO_DATA_TYPE_FORMAT *samples = (AUDIO_DATA_TYPE_FORMAT*)inBuffer->mAudioData; 

然后你有你的(在这种情况下是SInt16 )arrayssamples ,你可以从samples[0]samples[sampleCount-1]

上面的解决scheme对我不起作用,我自己弄错了样本数据。(endian issue)如果以后有人得到错误的样本数据,我希望这可以帮助你:

– (void)feedSamplesToEngine:(UInt32)audioDataBytesCapacity audioData:(void *)audioData {int sampleCount = audioDataBytesCapacity / sizeof(SAMPLE_TYPE);

 SAMPLE_TYPE *samples = (SAMPLE_TYPE*)audioData; //SAMPLE_TYPE *sample_le = (SAMPLE_TYPE *)malloc(sizeof(SAMPLE_TYPE)*sampleCount );//for swapping endians std::string shorts; double power = pow(2,10); for(int i = 0; i < sampleCount; i++) { SAMPLE_TYPE sample_le = (0xff00 & (samples[i] << 8)) | (0x00ff & (samples[i] >> 8)) ; //Endianess issue char dataInterim[30]; sprintf(dataInterim,"%f ", sample_le/power); // normalize it. shorts.append(dataInterim); }