如何从iOS中的AudioQueueRef获取浮点音频数据数组? [英] How to get array of float audio data from AudioQueueRef in iOS?

查看:792
本文介绍了如何从iOS中的AudioQueueRef获取浮点音频数据数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力将音频传输到iPhone中,我可以将其传递给(C ++)分析算法。当然,有很多选择:AudioQueue教程在trailsinthes和开始。

I'm working on getting audio into the iPhone in a form where I can pass it to a (C++) analysis algorithm. There are, of course, many options: the AudioQueue tutorial at trailsinthesand gets things started.

音频回调虽然提供了 AudioQueueRef ,但我发现Apple的文档在这方面很薄。内置方法写入文件,但没有任何地方你实际上在数据包内查看数据。

The audio callback, though, gives an AudioQueueRef, and I'm finding Apple's documentation thin on this side of things. Built-in methods to write to a file, but nothing where you actually peer inside the packets to see the data.

我需要数据。我不想在文件中写任何内容,这就是所有教程 - 甚至Apple的便利I / O对象 - 似乎都是针对的。 Apple的 AVAudioRecorder (令人愤怒)将为您提供级别并写入数据,但实际上并不允许您访问它。除非我遗漏了什么...

I need data. I don't want to write anything to a file, which is what all the tutorials — and even Apple's convenience I/O objects — seem to be aiming at. Apple's AVAudioRecorder (infuriatingly) will give you levels and write the data, but not actually give you access to it. Unless I'm missing something...

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

How to do this? In the code below there is inBuffer->mAudioData which is tantalizingly close but I can find no information about what format this 'data' is in or how to access it.

AudioQueue回调:

AudioQueue Callback:

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

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

And the code to write the audio to a file:

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 )array samples 您可以从 samples [0] 访问 samples [sampleCount- 1]

Then you have your (in this case SInt16) array samples which you can access from samples[0] to samples[sampleCount-1].

这篇关于如何从iOS中的AudioQueueRef获取浮点音频数据数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆