如何读/分配指向结构C中的数组的指针中的元素++ [英] how to read/assign the elements of a pointer that points to an array of structures in C++

查看:303
本文介绍了如何读/分配指向结构C中的数组的指针中的元素++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS的核心音频还有就是API <一个href=\"https://developer.apple.com/library/mac/#documentation/MusicAudio/Reference/AudioFileConvertRef/Reference/reference.html#//apple_ref/c/func/AudioFileWritePackets\"相对=nofollow> 的AudioFileWritePackets有一个 inPacketDescriptions 参数定义为
'的指针分组描述用于音频数据的数组。

In iOS core audio there is the API AudioFileWritePackets that has a inPacketDescriptions parameter defined as 'A pointer to an array of packet descriptions for the audio data.'

和它看起来像这样的方法签名:结果
常量AudioStreamPacketDescription * inPacketDescriptions,

and it looks like this in the method signature:
const AudioStreamPacketDescription *inPacketDescriptions,

现在的结构AudioStreamPacketDescription定义如下:

now the struct AudioStreamPacketDescription is defined as follows:

struct  AudioStreamPacketDescription
{
    SInt64  mStartOffset;
    UInt32  mVariableFramesInPacket;
    UInt32  mDataByteSize;
};
typedef struct AudioStreamPacketDescription AudioStreamPacketDescription;

我想知道如何创建和填充这样的指针结构数组,甚至一度给出的变量,如何读它。使用从苹果,我把我在哪里接受变量一个断点,并企图把其所有内容到日志speakHere 例子..这是一个尝试的例子:

I would like to know how to create and populate such a 'pointer to an array of structs', or even once given the variable, how to read it. Using the speakHere example from apple I put a breakpoint where I receive the variable and tried to dump all its contents to the log.. this is an example of an attempt:

void AQRecorder::printPacketDescriptionContents(const AudioStreamPacketDescription * inPacketDescriptions, UInt32 inNumberPackets)
{
    for (int i = 0; i < inNumberPackets; ++i)
    {
        NSLog(@"\n----------------\n");
        NSLog(@"this is packetDescriptionArray[%d].mStartOffset: %lld", i, (*inPacketDescriptions).mStartOffset);
        NSLog(@"this is packetDescriptionArray[%d].mVariableFramesInPacket: %lu", i, (*inPacketDescriptions).mVariableFramesInPacket);
        NSLog(@"this is packetDescriptionArray[%d].mDataByteSize: %lu", i, (*inPacketDescriptions).mDataByteSize);
        NSLog(@"\n----------------\n");   

    }        
}

什么想法?

更新:这里是我与它周围试图乱..也许它可以在它不断出现空底部的答案帮助(注意示例日志..它不有道理,整个事情仅仅是一个零的包,因为它是由回调返回的变量应的正确填充,也注意到它告诉我,我回来了数据包的数量。) ..另外,如果我跑与的code((常量AudioStreamPacketDescription *)(inPacketDescriptions + I)) - &GT; mDataByteSize)我得到一个EXC_BAD_ACCESS错误

Update: here is a sample log of me trying to mess around with it.. maybe it can help in the answer (notice at the bottom it keeps on appearing null.. it doesn't make sense that the whole thing is just a pack of zeroes since it's a variable returned by a callback that should be properly populated, also notice that it informs me about the number of packets I got back..).. also if i run the code with ((const AudioStreamPacketDescription *)(inPacketDescriptions +i))->mDataByteSize) i get a EXC_BAD_ACCESS error

(lldb) po **(inPacketDescriptions)
error: indirection requires pointer operand ('const AudioStreamPacketDescription' invalid)
error: 1 errors parsing expression
(lldb) po *(inPacketDescriptions)
(AudioStreamPacketDescription) $1 = [no Objective-C description available]
(lldb) po *(inPacketDescriptions).mStartOffset
error: member reference type 'const AudioStreamPacketDescription *' is a pointer; maybe you meant to use '->'?
error: indirection requires pointer operand ('SInt64' (aka 'long long') invalid)
error: 2 errors parsing expression
(lldb) po (*inPacketDescriptions).mStartOffset
(SInt64) $2 = 0 <nil>

(lldb) po (const AudioStreamPacketDescription *)(inPacketDescriptions +1)
(const class AudioStreamPacketDescription *) $3 = 0x00000010 [no Objective-C description available]
(lldb) po (const AudioStreamPacketDescription *)(inPacketDescriptions +1)->mStartOffset
error: Execution was interrupted, reason: Attempted to dereference an invalid pointer..
The process has been returned to the state before execution.
(lldb) po ((const AudioStreamPacketDescription *)(inPacketDescriptions +1))->mStartOffset
(SInt64) $5 = 0 <nil>
(lldb) po ((const AudioStreamPacketDescription *)(inPacketDescriptions +1))->mDataByteSize
(UInt32) $6 = 0 <nil>
(lldb) po ((const AudioStreamPacketDescription *)(inPacketDescriptions +100))->mDataByteSize
(UInt32) $7 = 0 <nil>
(lldb) po ((const AudioStreamPacketDescription *)(inPacketDescriptions +500))->mDataByteSize
(UInt32) $8 = 0 <nil>

(lldb) po inPacketDescriptions[0].mStartOffset
error: parent failed to evaluate: parent is NULL
(lldb) 

也在这里是什么样子的X code检查:

also here is what it looks like in the XCode inspector:

推荐答案

我不记得这个特定的结构是有史以来您居住的情况下,客户端。您将需要创建的存储的这些结构,你那么多次调用传递,以成功应对读写音频数据。几个非PCM格式将需要此信息,这取决于如何音频数据已被存储

I can't remember an instance where this specific struct is ever populated by you, the client. You will need to create storage for these structs which you then pass across multiple calls in order to successfully deal with reading and writing audio data. Several non-PCM formats will need this information, depending on how the audio data has been stored.

我想知道如何创建和填充这样的指针结构数组,甚至一度给出的变量,如何读它。

I would like to know how to create and populate such a 'pointer to an array of structs', or even once given the variable, how to read it.

那么,有在使用这种结构的AudioFile I / O和AudioConvertor接口API的屈指可数。基本上,你不要自行填充此类型。基本流程是这样的:

Well, there are a handful of APIs in AudioFile I/O and AudioConvertor interfaces which use this structure. Basically, you don't populate this type yourself. The basic flow goes like this:

// this is not for PCM audio data
//
// we'll read up to 8 packets at a time:
const size_t MaxPacketsToRead(8);

// allocate MaxPacketsToRead ASPDs on the stack:
AudioStreamPacketDescription aspds[MaxPacketsToRead];

// audio file read function:
AudioFileID inAudioFile = ...;
Boolean inUseCache = ...;
UInt32 outNumBytes = ...;
AudioStreamPacketDescription* outPacketDescriptions(aspds);
SInt64 inStartingPacket = ...;
UInt32 ioNumPackets = MaxPacketsToRead; // << you may not get all the packets
                                        // you request, but this sets the
                                        // upper limit.
void* outBuffer = ...;


OSStatus result(AudioFileReadPackets(inAudioFile,
                                     inUseCache,
                                     &outNumBytes,
                                     outPacketDescriptions,
                                     inStartingPacket,
                                     &ioNumPackets,
                                     outBuffer
));

if (noErr != result) {
  ...uh-oh...
}

// *now* we know that we have ioNumPackets worth of valid ASPDs,
// populated by the reader. and we have the associated audio data
// in other parameters.
// we can now safely pass all this information off to a function which 
// reads the ASPDs, such as AudioFileWritePackets.

(知道OP的问题在一些细节)的在很多情况下,你能避免所有这些复杂性,简单地创建一个 ExtAudioFile 重新presentation,并且指定 kExtAudioFileProperty_ClientDataFormat 为您的目的地采样格式 - 然后ExtAudioFile的API会为您创建一个内部转换器将输入转换的音频文件任意类型的一些特定的PCM重新presentation它可用于播放样本数据。如果你想支持多种文件格式实现这一切在这个级别其实是相当复杂的。 ExtAudioFile使得转换样本数据非常容易 - 如果这是一个选项,如果你流的情况很好地发挥

(knowing the OP's problem in some more detail) In many cases, you can avoid all this complexity and simply create an ExtAudioFile representation, and the specify kExtAudioFileProperty_ClientDataFormat for your destination sample format -- then the ExtAudioFile APIs will create an internal convertor on your behalf which will convert the input of an audio file of an arbitrary type to some specified PCM representation which you can use for the playback sample data. Implementing all this at this level is actually quite complex if you want to support many file formats. ExtAudioFile makes converting the sample data very easy -- if that is an option and if it plays nicely with your streaming scenario.

至于测井,你试图打印NULL结构的领域,从它的外观。

As far as the logging, well you're attempting to print the fields of a NULL structure, from the looks of it.

这篇关于如何读/分配指向结构C中的数组的指针中的元素++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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