Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering audio in AudioKit

What i need to do:

  • record audio file;
  • as it's record from iPhone/iPad microphone it can be quiet, so i need to filter it to make it louder;
  • save filtered record;

I'm new in audio programming, but as I understand so far I need "All Pass" filter (if not please correct me).

For this task I've found two libs: Novocaine and AudioKit, but Novocaine written in C, so it's harder to implement it in swift, and I decided to use AudioKit, but I didn't found "All Pass" filter there.

Does anybody know how to implement it in AudioKit and save filtered file? Thank you!

like image 738
Roman Simenok Avatar asked Mar 07 '23 14:03

Roman Simenok


1 Answers

You have a few choices, for musical recordings I recommend AKBooster as it purely boosts the audio, you have to be careful how much you boost, otherwise you might cause clipping.

For spoken word audio I recommend AKPeakLimiter. It will give you the maximum volume without clipping. Set the attackTime and decayTime to lower values to hear a more pronounced effect.

The values of the sliders won't represent the values of the parameters until you move them.

import UIKit
import AudioKit

class ViewController: UIViewController {

    let mic = AKMicrophone()
    let boost = AKBooster()
    let limiter = AKPeakLimiter()

    override func viewDidLoad() {
        super.viewDidLoad()

        mic >>> boost >>> limiter
        AudioKit.output = limiter
        AudioKit.start()

        let inset: CGFloat = 10.0
        let width = view.bounds.width - inset * 2


        for i in 0..<4 {
            let y = CGFloat(100 + i * 50)
            let slider = UISlider(frame: CGRect(x: inset, y: y, width: width, height: 30))
            slider.tag = i
            slider.addTarget(self, action: #selector(sliderAction(slider:)), for: .valueChanged)
            view.addSubview(slider)
        }

        boost.gain = 1

    }

    @objc func sliderAction(slider: UISlider) {
        switch slider.tag {
        case 0:
            boost.gain = slider.value * 40
        case 1:
            limiter.preGain = slider.value * 40
        case 2:
            limiter.attackTime = max(0.001, slider.value * 0.03)
        case 4:
            limiter.decayTime = max(0.001, slider.value * 0.06)
        default: break

        }
    }

}
like image 142
dave234 Avatar answered Mar 19 '23 14:03

dave234