Nektarios
Nektarios

Reputation: 10371

How to read a sound file in to floating point array on iOS?

I want to read a sound file, I generate this file myself so I can pick anything that audacity can output, in to a float array so that I can have float mySoundOneSecond[44100]; and have it full of the data so that I can manipulate that 'raw' float data as I please on iOS devices.

Upvotes: 1

Views: 467

Answers (2)

rage
rage

Reputation: 1447

You will have to use ExtFileReader C API in CoreAudio. That will read your file into lPCM. Beware though, that technically the buffers that you get aren't going to hold float values, but 8.24bit fixed point integers of type AudioUnitSampleType. Do not be fooled by the typedef of this type, it is 8.24bit.

Upvotes: 1

Rob Napier
Rob Napier

Reputation: 299265

NSMutableData is probably your easiest way to read this file. Something like this::

NSMutableData *data = [NSMutableData dataWithContentsOfFile:path options:0 error:&error];
if (!data) { ... do something with the error ... }

float *mySoundOnSecond = [data mutableBytes];

You may want to pass NSDataReadingMapped in your options for this kind of application. That will save you some time by mapping the file to memory rather than reading it all at once.

Note that this approach will block the thread it's on. If the file is large, you can either do it on a background thread or use the asynchronous reading approaches. See the File System Programming Guide for the various options.

Upvotes: 0

Related Questions