Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to record at a low sample (around 1000 Hz) on an iPhone

I am writing an app to recording single-channel audio with the built-in microphone on an iPhone 6. The apps works as expected when configured to record at 8000 Hz. Here's the code

    // Set up audio session
    let session = AVAudioSession.sharedInstance()

    // Configure audio session
    do {
        try session.setCategory(AVAudioSessionCategoryPlayAndRecord)

        var recordSettings = [String:AnyObject]()
        recordSettings[AVFormatIDKey] = Int(kAudioFormatLinearPCM) as AnyObject

        // Set the sampling rate
        recordSettings[AVSampleRateKey] = 8000.0 as AnyObject
        recordSettings[AVNumberOfChannelsKey] = 1 as AnyObject

        recorder = try AVAudioRecorder(url: outputFileURL, settings: recordSettings)

        recorder?.delegate = self
        recorder?.isMeteringEnabled = true

        recorder?.prepareToRecord()

        return true

    }
    catch {
        throw Error.AVConfiguration
    }

To reduce the storage requirements, I would like to record at a much lower sample rate (ideally less than 1000 Hz). If I set the sample rate to 1000 Hz, the app records at 8000 Hz.

According to Apple's documentation,

The available range for hardware sample rate is device dependent. It typically ranges from 8000 through 48000 hertz.

Question...is it possible to use AVAudioSession (or other framework) to record audio at a low sample rate?

like image 705
Epsilon Avatar asked Jan 27 '17 20:01

Epsilon


2 Answers

Audio recording on iPhone are made with hardware codecs so available frame rates are hardcoded and can't be changed. But if you need to have 1kHz sample rate you can record at 8kHz and than just resample you record with some resample library. Personally, I prefer to use ffmpeg for such tasks.

like image 93
Alexander Ushakov Avatar answered Oct 13 '22 00:10

Alexander Ushakov


I hope you are aware that by the niquist theorem you cannot expect very useful results of what you try to achieve.

That is, except you are targeting for low frequencies only. For that case you might want to use a low-pass filter first. It's almost impossible to understand voices with olny frequencies below 500 Hz. Speech is usually said to require 3 kHz, that makes for a sample rate of 6000.

For an example of what you'd have to expect try something similar to:

ffmpeg -i tst.mp3 -ar 1000 tst.wav

with e.g. some vocals and listen to the result. You can however possibly achieve some acceptable trade-off using e.g. a sample rate of 3000.

An alternative would be to do some compression on the fly as @manishg suggested. As Smartphones these days can do video compression in real time that should be totally feasible with iPhone's Hard- and Software. But it's a totally different thing than reducing the sample rate.

like image 31
RuDevel Avatar answered Oct 12 '22 23:10

RuDevel