Re: extaudiofileread stereo float (iOS)
Re: extaudiofileread stereo float (iOS)
- Subject: Re: extaudiofileread stereo float (iOS)
- From: Michael Tyson <email@hidden>
- Date: Sat, 25 Feb 2012 12:01:18 +0100
Whoops - you're totally right, Brian! Thanks for pointing that out - I forgot to add the 'packed' attribute to the anonymous struct in that code, which, unless I'm mistaken, should resolve the problem by ensuring the two structures are packed together - but you're probably right; it's less error-prone to do it on the heap (just a little more work, in certain circumstances where you need to avoid allocating memory, because you then need to allocate it in advance).
Sorry for the misleading code snippet =)
So, unless I'm mistaken about this working (please correct me if I am!), it should've been:
struct { AudioBufferList bufferList; AudioBuffer secondBuffer; } __attribute__((packed)) buffers;
buffers.bufferList.mNumberBuffers = 2;
for ( int i=0; i<buffers.bufferList.mNumberBuffers; i++ ) {
buffers.bufferList.mBuffers[i].mNumberOfChannels = 1;
buffers.bufferList.mBuffers[i].mDataByteSize = kBufferSize * sizeof(float);
buffers.bufferList.mBuffers[i].mData = malloc(kBufferSize * sizeof(float));
}
Or, on the heap:
AudioBufferList *bufferList = (AudioBufferList*)malloc(sizeof(AudioBufferList) + sizeof(AudioBuffer));
bufferList->mNumberBuffers = 2;
for ( int i=0; i<bufferList->mNumberBuffers; i++ ) {
bufferList->mBuffers[i].mNumberOfChannels = 1;
bufferList->mBuffers[i].mDataByteSize = kBufferSize * sizeof(float);
bufferList->mBuffers[i].mData = malloc(kBufferSize * sizeof(float));
}
...
free(bufferList);
Cheers =)
Michael
On 25 Feb 2012, at 02:33, Brian Willoughby wrote:
> Michael,
>
> I hate to sound rude, but that is awful code. Declaring a pair of variables as separate fields in a structure on the stack and then depending upon the compiler to make them concatenated into a single array is very bad code. The behavior is very dependent upon the alignment and packing settings at compile time. It might work for you now, but any number of architecture changes or compiler revisions could cause your fragile construct to fail without warning. Note that if the C Language standard guarantees that separate variables declared in proximity on the stack are guaranteed to work like you're using them, then please point to the specification because it would be news to me.
>
> The proper way to handle variable length arrays is to work with pointers and allocated contiguous memory with a call to malloc() or calloc().
>
>
> Greg,
>
> Don't forget that the Xcode debugger (gdb) has a command line. You can examine variable-length arrays by simply typing in a display command with the appropriate index. I run into so many examples where the Xcode variable browser does not access the information that I need, that I frequently just use the debugger's command line to access the data by basically typing the C code.
>
> Brian Willoughby
> Sound Consulting
> On Thu, Feb 23, 2012 at 12:04 PM, Michael Tyson <email@hidden> wrote:
>> It can be a little confusing, because the AudioBufferList structure defines only a single AudioBuffer within it - you have to either allocate space for the buffer list + the extra buffer, or use a struct or something on the stack.
>>
>> For example, to prepare an AudioBufferList on the stack, to receive kBufferSize floating-point, stereo, non-interleaved samples:
>>
>> struct { AudioBufferList bufferList; AudioBuffer secondBuffer; } buffers;
>> buffers.bufferList.mNumberBuffers = 2;
>> for ( int i=0; i<buffers.bufferList.mNumberBuffers; i++ ) {
>> buffers.bufferList.mBuffers[i].mNumberOfChannels = 1;
>> buffers.bufferList.mBuffers[i].mDataByteSize = kBufferSize * sizeof(float);
>> buffers.bufferList.mBuffers[i].mData = malloc(kBufferSize * sizeof(float));
>> }
>
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden