0

I have been attempting to pass and array to a method within DiracLE audio library.

The array looks like this in the debugger

enter image description here

- (OSStatus) readFloatsConsecutive:(SInt64)numFrames intoArray:(float**)audio withOffset:(long)offset

That fills the array up like so

if (audio) {
    for (long c = 0; c < mExtAFNumChannels; c++) {
        if (!audio[c]) continue;  // this executes for both channels 
                                  // but doesnt proceed into next for loop
        for (long v = 0; v < numFrames; v++) {
            if (v < loadedPackets) audio[c][v+offset] = (float)data[v*mExtAFNumChannels+c] / 32768.f;
            else audio[c][v+offset] = 0.f;
        }
    }
}

I call it like this

[reader readFloatsConsecutive:frameCount intoArray:arrayToFill];

arrayToFill being an argument to the current function scope

[self readAudioDataForFile:temp withArray:tempArray];

The array was initially passed into the function like this

 // this array was passed into the function as tempArray  which is float **tempArray = NULL;
 arrayToFill  = (float **) malloc ( (frameCount * channelCount) * sizeof( float ));

As I needed to extract audio data from the file in my method I have to malloc the array there and pass it into the dirac function for filling. I malloc like so arrayToFill = (float **) malloc ( (frameCount * channelCount) * sizeof( float )); and then pass it to the dirac function as mentioned before.

This array could be a 2 dimensional or 1 dimensional array depending on channel count

user773578
  • 1,161
  • 2
  • 13
  • 24
  • how are you trying to log them? with `%@`? Because you should use normal C `%f`as format specifier. – Jack Jul 22 '12 at 03:51
  • I did use that, it looks like the array is empty when it gets to the for loop to fill the passed in array. if (audio) fails so I have nothing in the array? Do I malloc the array correctly because it seems like after passing it through two function calls it doesnt exist. – user773578 Jul 22 '12 at 03:55
  • @Jack - Nice game on your site link. :) – user773578 Jul 22 '12 at 03:56
  • You're not using names consistently so just to check...the array is allocated with `frameCount` but the loop iterates up to `numFrames`? Is that a typo? And if not, could `numFrames` simply be zero? – Kevin Grant Jul 22 '12 at 04:17
  • That isnt a typo, numFrames is called in the Dirac funtion I pass my array to. When I breakpoint on the if(!audio[c]) continue statement it executes 4 times. Something is definitely wrong with the array I think. The channelCount is 2 – user773578 Jul 22 '12 at 04:27

1 Answers1

1

The problem relies on allocation in my opinion.

Allocating a 1 dimensional array would look like:

arrayToFill = (float *) malloc ( (frameCount * channelCount) * sizeof( float ));

and it would be enought.

Allocating a 2 dimensional array would be different though, because you have to allocate even inner arrays. If I understood correctly if you have two channels then the array is bidimensional, you should do something like:

arrayToFill = (float **)calloc(channelCount, sizeof(float*));
for (int i = 0; i < channelCount; ++i)
  arrayToFill[i] = (float*)calloc(frameCount, sizeof(float));

This because you need to allocate a pointer to pointer to float. So in the first step you allocate a 2 dimensional array of pointers to float, these pointers are initialized to NULL so you have to loop through it and allocate them separately.

If the channel is inner inside the array (eg. the first index chooses the frame and not the channel) then you should swap dimensions.

Jack
  • 131,802
  • 30
  • 241
  • 343