views:

177

answers:

2

Hi

I am using AVAudioRecorder in my app in order to create a .wav file based on user voice input. I want to be able to stuff" silence in the beginning of an audio file for some time before the actual recording is done.

How can I do this using AVAudioRecorder? Can I mention a time for which I want the "silence" to be recorded?

Thanks.

A: 

I'm sure there's probably a better way to do it, but how about simply pre-recording the silent stream you want and then concatenating that file with a file of the user generated content before saving out the final version?

Mark Hammonds
yeah. i'm sure there must be a better way! The time I want the silence to go on is not fixed. and again, how do I concatenate the two files?
lostInTransit
+1  A: 

I haven't worked with the AVAudioRecorder in particular. But silence in PCM audio is just zeros as sample values. You could set the encoding of the AVAudioRecorder to PCM, store the file and then edit the file by prepending the desired number of zeros in the beginning. E.g. at 44100 Hz, 8-bit encoding, you'd add 44100 zero-bytes to the beginning of the file for each second of silence you would like to have. Hope this helps as an idea.

Note: Keep the file header of the PCM file intact and edit the "data chunk".

Hauke
Hauke, I'm going ahead with this method and using the AudioFileWritePackets method. But since I already have a wav file in place, I want to append data to the beginning of the file. Not really sure if am passing the correct vals. can you please help me out! Thanks.
lostInTransit
I haven't worked with it yet, so unfortunately I can't help you on the details. But the API sounds like you have quite a lot of options. I recommend playing around with all kinds of variants and keep inspecting the resulting audio file in Audacity or a similar audio tool.
Hauke