3

So, I have an audio recording iOS app I am working on. In my app I have the need to write audio at any point in the file (in the documents directory) the user chooses, which will overwrite whatever audio is there.

Example: I record 3 min of audio, scroll back to minute two, start recording again for 10 seconds. Now, 2:00 - 2:10 contains the new audio, and the old audio is gone (the audio file does not get longer in this example).

I am using EZAudio and I can record and pause as many times as I want and it will keep appending to the end. I was going to use ExtAudioFileSeek to seek backward in the file and record from there, but you can only use that when the file is open as read-only.

What I don't want: I don't want to record to a different file, and append the two files together using this method: https://stackoverflow.com/a/10562809/1391672 There is significant loading time associated with this. The iOS app Voice Memos appends files together instantaneously.

Can ExtAudioFileWriteAsync be used to append audio at the beginning? Or at a specified sample location? Is there another method I should look at?

Community
  • 1
  • 1
mnearents
  • 646
  • 1
  • 6
  • 31

1 Answers1

1

I think ExtAudioFileSeek is undefined when recording. I use the AudioFile API which ExtAudioFile is built upon. The only catch i that you have to do your own format conversions (I use an AudioConverterRef if necessary). But once you've got all of that squared away you can call AudioFileWritePackets which takes an inStartingPacket argument so you can write anywhere in the file. You can also wrap an AudioFileRef in an ExtAudioFileRef for playback while the file is still open for recording using ExtAudioFileWrapAudioFileID. Headers are in AudioFile.h and AudioConverter.h.

dave234
  • 4,793
  • 1
  • 14
  • 29
  • If I specify an inStartingPacket and start writing audio, does it overwrite what was already there, or does it shift everything over (like an array when you insert at a specified index)? – mnearents May 12 '15 at 17:04
  • Yes, it will overwrite what is there. – dave234 May 12 '15 at 19:01
  • Thanks for the info. If you have any links to example code or resources for someone who doesn't want to learn core audio, let me know. The reason I don't want to learn it is because my app is 99% done using the EZAudio library. I just need to make this one modification and I'm good to go. So I don't want to spend the next year learning core audio just to record at a specified location in a file. So any resources would be helpful. I'm looking around too. – mnearents May 12 '15 at 19:16
  • Do you need to convert the audio format before saving it to file? If so what are your input/output formats? – dave234 May 12 '15 at 20:12
  • I don't think so, maybe EZAudio does a conversion but I assumed it was recording and saving in .m4a format. – mnearents May 12 '15 at 20:37
  • There's most likely a conversion happening from your input audio to m4a. Writing directly to PCM is trivial. I haven't tried to get an AudioFileRef to write to m4a, but it looks a little complicated. If it's speed you're after, you might consider writing your audio to PCM (wave,aiff) while the file is being recorded and edited, then doing the conversion to m4a when you're done with it. Everything is faster and you can write starting at a specific sample rather than having to split packets. The only downside is file size. – dave234 May 12 '15 at 22:03