views:

401

answers:

3

Hi!

Is it possible to cut audio file with iPhone SDK? (file has .caf extension) I just need to cut off the silence at the beginning.

(Also, maybe it's possible to write new file from the existing one with specified start and end time.)

Thanks in advance!

+1  A: 

Its not hard, its just fiddly, you can use Extended Audio File Services. have a look at the reference for ExtAudioFileRead & ExtAudioFileWrite (they have sample code) then you can open one audio file read it, trim it and then write the new one.

Aran Mulholland
Dmitry
yes you have to open the file and trim it in your code.
Aran Mulholland
ok. I understand it. but the questions is - how to trim audio file? I can't just cutoff a peace of bytes, it will corrupt the file.
Dmitry
if it is straight pcm data (not compressed) then you can work out what to cut. on my profile there is some iphone core audio sample projects. one of them (the mixer) has a class called InMemoryAudioFile. it opens an audio file and puts its data in an array. PCM data is simply laid out. if it is a stereo file of 16 bit data, you will have 32 bit packets each representing one stereo sample. once you can get at this data its not much of a stretch to trim it.
Aran Mulholland
A: 

There is no trim function. You have to write some code that opens a file for reading and creates and opens another file for writing. Then write a loop that reads the audio sample or packet data, discards the silence at the beginning, and writes the audio data to the output file.

lucius
A: 

See example 6 here:

http://www.modejong.com/iPhone/

The example shows how to use ExtAudioFile to fade an audio clip out after truncating it, but you could easily adapt the logic to support a starting point as well as an ending point. The main thing is that the example provides a code sample that works on the iPhone and also works as a Mac OS X command line app.

MoDJ
thanks! it's very useful sample
Dmitry