简体   繁体   English

如何在Rubymotion中编写此C代码?

[英]How can I write this C code in Rubymotion?

I'm working through some of the code examples in Learning Core Audio: A Hands-on Guide to Audio Programming for Mac and iOS 我正在研究“ 学习核心音频:Mac和iOS音频编程动手指南”中的一些代码示例。

Although I've worked previously with Objective-C, I'm working on a Rubymotion app at the moment and I'd like to be able to transpose the code in the book to Rubymotion for my app. 尽管我以前使用过Objective-C,但目前我正在开发Rubymotion应用程序,我希望能够将书中的代码转换为我应用程序的Rubymotion。

There are a few C functions and idioms that I'm not sure about though. 虽然我不确定一些C函数和习惯用法。 Can anybody tell me IF and HOW I can re-write the following in RM? 有人可以告诉我IF和我如何在RM中重写以下内容吗?

AudioStreamBasicDescription asbd;
memset(&asbd, 0, sizeof(asbd));

I've got as far as identifying that asbd is a Pointer. 我已经知道asbd是一个指针。 So: 所以:

asbd = Pointer.new(AudioStreamBasicDescription.type)

But both the sizeof() and memset() functions cough up NoMethodErrors 但是sizeof()memset()函数都咳嗽了NoMethodErrors

asbd is not a pointer, but a structure. asbd不是指针,而是结构。 In RubyMotion that is represented by a class. 在RubyMotion中,由类表示。 The memset is just initializing the memory of the structure to all zeros. memset只是将结构的内存初始化为全零。 I'm not sure if RubyMotion sets the fields to zero by default, so you could initialize each field manually like so (which is certainly more clear if a bit wordy): 我不确定RubyMotion是否默认将字段设置为零,因此您可以像这样手动初始化每个字段(如果有点罗certainly,当然会更清楚):

asbd = AudioStreamBasicDescription.new

asbd.mSampleRate = 0.0
asbd.mFormatID = 0
asbd.mFormatFlags = 0
asbd.mBytesPerPacket = 0
asbd.mFramesPerPacket = 0
asbd.mBytesPerFrame = 0
asbd.mChannelsPerFrame = 0
asbd.mBitsPerChannel = 0
asbd.mReserved = 0

If you need a pointer to asbd , I believe you do this: 如果您需要一个指向asbd的指针,我相信您可以这样做:

asbd_ptr = Pointer.new(AudioStreamBasicDescription.type)
asbd_ptr[0] = asbd

So if wanted to pass &asbd , you would pass asbd_ptr instead. 因此,如果要传递&asbd ,则应传递asbd_ptr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM