coder
coder

Reputation: 10520

Convert NSData to double array in Objective C

I have an audio file in my iPhone app that I convert to an NSData object. Ideally, I would like to get an array of doubles from the audio file. Is there a way to convert NSData to an array of doubles?

Here is the current output from the line NSLog(@"%@\n", data) where data is an NSData object of the audio file:

<0000001c 66747970 6d703432 00000001 6d703431 6d703432 69736f6d 00000008 77696465 004d956e 6d646174 21000340 681c210c 53ed990c 1f33e94d ab588b95 55a61078 08799c67 f1f706cc 595b4eb6 08cfb807 ea0e3c40 03c13303 e674e05a...

Upvotes: 0

Views: 1438

Answers (1)

kennytm
kennytm

Reputation: 523344

The -bytes method return the raw bytes of the NSData. You can then cast it to a pointer to double (assuming correct endianness).

const double* array_of_doubles = (const double*)[data bytes];
NSUInteger size_of_array = [data length] / sizeof(double);

Edit: The data is an MP4 file. You cannot convert MP4 to a meaningful double array directly. I don't know what you want to do, but maybe AVAssetReader would help.

Upvotes: 2

Related Questions