문제

I'm working through some of the code examples in Learning Core Audio: A Hands-on Guide to Audio Programming for Mac and 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.

There are a few C functions and idioms that I'm not sure about though. Can anybody tell me IF and HOW I can re-write the following in RM?

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

I've got as far as identifying that asbd is a Pointer. So:

asbd = Pointer.new(AudioStreamBasicDescription.type)

But both the sizeof() and memset() functions cough up NoMethodErrors

도움이 되었습니까?

해결책

asbd is not a pointer, but a structure. In RubyMotion that is represented by a class. The memset is just initializing the memory of the structure to all zeros. 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):

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_ptr = Pointer.new(AudioStreamBasicDescription.type)
asbd_ptr[0] = asbd

So if wanted to pass &asbd, you would pass asbd_ptr instead.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top