0

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...
coder
  • 10,460
  • 17
  • 72
  • 125

1 Answers1

2

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.

Community
  • 1
  • 1
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • I am getting huge numbers doing this which I'm not sure are correct. What do you mean by "assuming correct endianness"? – coder Dec 27 '11 at 16:10
  • @coder: http://en.wikipedia.org/wiki/Endianness#Examples_of_storing_the_value_0A0B0C0Dh_in_memory. And are you sure they are stored as doubles? I think integers are more likely. – kennytm Dec 27 '11 at 16:16
  • They aren't stored as doubles or ints...it looks like hex values when I run this line: NSLog(@"data: %@", data); Where data is an NSData object... – coder Dec 27 '11 at 16:23
  • @coder: NSData's are always printed like that... What are the hex values? – kennytm Dec 27 '11 at 16:24