如何从AudioQueueRef在iOS中获得浮动音频数据数组



我正在努力让音频进入iPhone的形式,我可以把它传递给(c++)分析算法。当然,有很多选择:trailsinsand的AudioQueue教程可以让你开始学习。

音频回调,虽然,给出了一个AudioQueueRef,我发现苹果的文档薄在这方面的事情。写入文件的内置方法,但没有实际在数据包中查看数据的方法。

我需要数据。我不想写任何东西到文件中,这是所有教程——甚至苹果的便利I/O对象——似乎都在瞄准的目标。苹果的AVAudioRecorder(令人恼火的是)会给你关卡和写入数据,但实际上不给你访问权限。除非我错过了什么…

如何做到这一点?在下面的代码中,有inBuffer->mAudioData,这是诱人的接近,但我找不到关于这个"数据"是什么格式或如何访问它的信息。

AudioQueue回调:

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 %dn", count);
}

以及将音频写入文件的代码:

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)数组samples,你可以从samples[0]samples[sampleCount-1]访问。

上面的解决方案不适合我,我得到了错误的样本数据本身。如果有人在将来得到错误的样本数据,我希望这对你有帮助:

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

相关内容

  • 没有找到相关文章

最新更新