Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS AVAudioRecorder, How to record only when audio input present (non-silence)

I'm using AVAudioRecorder to record audio from the iphone's mic but I want to discard the silence periods: start recording when detecting sound, and stop recording when next silence.

Can't figure out how to do that

Any advice?

Thanx!

like image 438
Martha Avatar asked Oct 04 '10 13:10

Martha


3 Answers

Perhaps you could use the AVAudioRecorder's support for audio level metering to keep track of the audio levels and enable recording when the levels are above a given threshold. You'd need to enable metering with:

[anAVAudioRecorder setMeteringEnabled:YES];

and then you could periodically call:

[anAVAudioRecorder updateMeters];
power = [anAVAudioRecorder averagePowerForChannel:0];
if (power > threshold && anAVAudioRecorder.recording==NO) {
    [anAVAudioRecorder record];
} else if (power < threshold && anAVAudioRecorder.recording==YES) {
    [anAVAudioRecorder stop];
}

Or something like that.

like image 177
Jake Avatar answered Nov 15 '22 18:11

Jake


I've found the way based on Audio Queue Services. It is alot more complicated but alot more fun too as you define your queue buffers for the incoming audio packets.

You need to define the callback when the buffer if full, so you have the buffer full of packets that you can process as you wish, in my case to detect silence and a few more things.

Later having more time ill post the solution. If anyone urged that just cant wait drop me an email and ill be glad to help.

Check speakhere example here: http://developer.apple.com/library/ios/#samplecode/SpeakHere/Introduction/Intro.html

like image 45
Martha Avatar answered Nov 15 '22 18:11

Martha


You can determine the background audio level by recording to a garbage file before you start recording for real:

AVAudioRecorder stops working when writing to /dev/null on iPhone 5s with iOS 7

like image 25
user2683747 Avatar answered Nov 15 '22 18:11

user2683747