I just don't get AudioFileReadPackets

I tried to write the smallest piece of code to narrow down the problem. Now, this is just a few lines and it won't work, which makes it pretty clear that I have a fundamental misunderstanding of how to use AudioFileReadPackets. I've read docs and other examples on the internet and apparently I just don't get it. Could you explain this to me?

This is what this block should do: I opened a file earlier. I only want to read one packet - the first file - and then print it. But it falls on the AudioFileReadPackets line:

    AudioFileID mAudioFile2; 
    AudioFileOpenURL (audioFileURL, 0x01, 0, &mAudioFile2);
    UInt32 *audioData2 = (UInt32 *)malloc(sizeof(UInt32) * 1);
    AudioFileReadPackets(mAudioFile2, false, NULL, NULL, 0, (UInt32*)1, audioData2);
    NSLog(@"first packet:%i",audioData2[0]);

      

(For clarity, I've removed all error handling.)

This is the AFRP line that is failing.

(I understand that the third and fourth arguments are useful, and in my "real" code I use them, but they are not required, right? So NULL should work in this case, right?) So what happens then?

Any guidance would be much appreciated.

Thanks.

+2


source to share


1 answer


I think your problem is related to the number of package arguments. You are just creating a pointer to 0x00000001. The problem is that it tries to exploit this - the result of how many packets were actually read. Perhaps your sound buffer is too small. According to the documentation, it is assumed that the length of the packets exceeds the upper limit of the packet length.

Try the following:



AudioFileID mAudioFile2; 
AudioFileOpenURL (audioFileURL, 0x01, 0, &mAudioFile2);
UInt32 packetCount = 1;
void *audioData2 = (void *)malloc(packetCount * maxPacketSize);
AudioFileReadPackets(mAudioFile2, false, NULL, NULL, 0, &packetCount, audioData2);
NSLog(@"first packet:%i",audioData2[0]);

      

Not sure how to get maxPacketSize (never worked with that).

+4


source







All Articles