Objective c 核心音频文件读取数据包。。。寻找原始音频

Objective c 核心音频文件读取数据包。。。寻找原始音频,objective-c,core-audio,pcm,sample-data,audiounit,Objective C,Core Audio,Pcm,Sample Data,Audiounit,我试图从文件中获取原始音频数据(我习惯于看到介于-1和1之间的浮点值) 我正试图实时从缓冲区中提取这些数据,以便为应用程序提供某种类型的计量 我基本上是使用AudioFileReadPackets将整个文件读入内存。我已经创建了一个RemoteIO音频单元来进行播放,在playbackCallback内部,我将mData提供给AudioBuffer,以便将其发送到硬件 我遇到的最大问题是,从我的数据数组(来自AudioFileReadPackets)发送到缓冲区的数据是UInt32。。。我真的很

我试图从文件中获取原始音频数据(我习惯于看到介于-1和1之间的浮点值)

我正试图实时从缓冲区中提取这些数据,以便为应用程序提供某种类型的计量

我基本上是使用AudioFileReadPackets将整个文件读入内存。我已经创建了一个RemoteIO音频单元来进行播放,在playbackCallback内部,我将mData提供给AudioBuffer,以便将其发送到硬件

我遇到的最大问题是,从我的数据数组(来自AudioFileReadPackets)发送到缓冲区的数据是UInt32。。。我真的很困惑。看起来是32位,我已经将每个包/帧设置为4字节。我该如何从中获取原始音频数据(从-1到1)

这是我的格式说明

// Describe format
audioFormat.mSampleRate         = 44100.00;
audioFormat.mFormatID           = kAudioFormatLinearPCM;
audioFormat.mFormatFlags        = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
audioFormat.mFramesPerPacket    = 1;
audioFormat.mChannelsPerFrame   = 2;
audioFormat.mBitsPerChannel     = 16;
audioFormat.mBytesPerPacket     = 4;
audioFormat.mBytesPerFrame      = 4;
我正在读一个wave文件


谢谢

看看这个函数。。。 数据是16

static void recordingCallback (
    void                                *inUserData,
    AudioQueueRef                       inAudioQueue,
    AudioQueueBufferRef                 inBuffer,
    const AudioTimeStamp                *inStartTime,
    UInt32                              inNumPackets,
    const AudioStreamPacketDescription  *inPacketDesc
) {


    // This callback, being outside the implementation block, needs a reference to the AudioRecorder object
    AudioRecorder *recorder = (AudioRecorder *) inUserData;

    // if there is audio data, write it to the file
    if (inNumPackets > 0) {

        SInt16 *frameBuffer = inBuffer->mAudioData;
        //NSLog(@"byte size %i, number of packets %i, starging packetnumber %i", inBuffer->mAudioDataByteSize, inNumPackets,recorder.startingPacketNumber);

        //int totalSlices = 1;
        //int framesPerSlice = inNumPackets/totalSlices;
        float total = 0;
        for (UInt32 frame=0; frame<inNumPackets; frame+=20) {
            total += (float)abs((SInt16)frameBuffer[frame]) ; 
        }
静态无效记录回调(
void*inUserData,
AudioQueueRef inAudioQueue,
AudioQueueBufferRef inBuffer,
const AudioTimeStamp*inStartTime,
UInt32 inNumPackets,
const AudioStreamPacketDescription*inPacketDesc
) {
//此回调位于实现块之外,需要对AudioRecorder对象的引用
录音机*录音机=(录音机*)用户数据;
//如果有音频数据,请将其写入文件
如果(inNumPackets>0){
SInt16*frameBuffer=inBuffer->mAudioData;
//NSLog(@“字节大小%i,数据包数%i,起始数据包数%i”,在缓冲->mAudioDataByteSize,在多个数据包,记录器。起始数据包数);
//int-totalSlices=1;
//int framesPerSlice=inNumPackets/totalSlices;
浮动总数=0;

对于(UInt32 frame=0;frame请查看此函数。。。 数据是16

static void recordingCallback (
    void                                *inUserData,
    AudioQueueRef                       inAudioQueue,
    AudioQueueBufferRef                 inBuffer,
    const AudioTimeStamp                *inStartTime,
    UInt32                              inNumPackets,
    const AudioStreamPacketDescription  *inPacketDesc
) {


    // This callback, being outside the implementation block, needs a reference to the AudioRecorder object
    AudioRecorder *recorder = (AudioRecorder *) inUserData;

    // if there is audio data, write it to the file
    if (inNumPackets > 0) {

        SInt16 *frameBuffer = inBuffer->mAudioData;
        //NSLog(@"byte size %i, number of packets %i, starging packetnumber %i", inBuffer->mAudioDataByteSize, inNumPackets,recorder.startingPacketNumber);

        //int totalSlices = 1;
        //int framesPerSlice = inNumPackets/totalSlices;
        float total = 0;
        for (UInt32 frame=0; frame<inNumPackets; frame+=20) {
            total += (float)abs((SInt16)frameBuffer[frame]) ; 
        }
静态无效记录回调(
void*inUserData,
AudioQueueRef inAudioQueue,
AudioQueueBufferRef inBuffer,
const AudioTimeStamp*inStartTime,
UInt32 inNumPackets,
const AudioStreamPacketDescription*inPacketDesc
) {
//此回调位于实现块之外,需要对AudioRecorder对象的引用
录音机*录音机=(录音机*)用户数据;
//如果有音频数据,请将其写入文件
如果(inNumPackets>0){
SInt16*frameBuffer=inBuffer->mAudioData;
//NSLog(@“字节大小%i,数据包数%i,起始数据包数%i”,在缓冲->mAudioDataByteSize,在多个数据包,记录器。起始数据包数);
//int-totalSlices=1;
//int framesPerSlice=inNumPackets/totalSlices;
浮动总数=0;

对于(UInt32 frame=0;frame我不确定您从这个回调中获取UInt32数据的确切原因,尽管我怀疑它实际上是两个交错的UInt16数据包,每个通道一个。无论如何,如果您想要从文件中获取浮点数据,它需要转换,我不相信@John Ballinger建议的方法是corr我的建议是:

// Get buffer in render/read callback
SInt16 *frames = inBuffer->mAudioData;
for(int i = 0; i < inNumPackets; i++) {
  Float32 currentFrame = frames[i] / 32768.0f;
  // Do stuff with currentFrame (add it to your buffer, etc.)
}
//在呈现/读取回调中获取缓冲区
SInt16*frames=inBuffer->mAudioData;
for(int i=0;i

不能简单地将帧强制转换为所需的格式。如果需要浮点数据,则需要除以32768,这是16位采样的最大可能值。这将在{-1.0..1.0}中生成正确的浮点数据范围。

我不确定您从这个回调中获取UInt32数据的确切原因,尽管我怀疑它实际上是两个交错的UInt16数据包,每个通道一个。无论如何,如果您想要从文件中获取浮点数据,它需要被转换,我不相信@John Ballinger建议的方法是正确的。m我的建议是:

// Get buffer in render/read callback
SInt16 *frames = inBuffer->mAudioData;
for(int i = 0; i < inNumPackets; i++) {
  Float32 currentFrame = frames[i] / 32768.0f;
  // Do stuff with currentFrame (add it to your buffer, etc.)
}
//在呈现/读取回调中获取缓冲区
SInt16*frames=inBuffer->mAudioData;
for(int i=0;i

不能简单地将帧强制转换为所需的格式。如果需要浮点数据,则需要除以32768,这是16位采样的最大可能值。这将在{-1.0..1.0}中生成正确的浮点数据范围。

因此,我将UInt32的低阶位放入1个SInt16,另一个放入另一个SInt16,似乎每一个都很好地代表了两个独立的频道。我使用的是Aran的示例代码,它是从Michael Bolton关于如何在iphone上使用RemoteIO音频单元的示例代码中编辑而来的,看起来Aran刚刚通过将两个交叉通道作为UInt32连接到remoteIO单元,因为他没有进行任何类型的测量/波形绘制。在将UInt32分解为双SInt16之后,看起来一切都应该如此。此外,数据不一定是浮点型的。我创建了一个波形绘制类,该类希望数据不是浮点型的o处于浮点状态,它实际上是单独打开音频文件,并使用AudioFileRead()函数拉出数据(以浮点形式)。因此,我的应用程序正在执行以下操作:录制完成后,打开录制的文件绘制波形,读取每个第X个样本并将其添加到数组中,循环通过生成的数组并使用核心图形绘制波形。然后,当用户按play时,它初始化AU。因此,我使用UInt32并拉低o顺序将一个比特分为一个SInt16,另一个比特分为另一个SInt16,这两个看起来都一样