你有几个选择,对于音乐录音,我推荐 AKBooster,因为它纯粹提升音频,你必须小心提升多少,否则可能会导致削波。
对于口语音频,我推荐 AKPeakLimiter。它会给你最大的音量而不削波。将 attackTime 和 decayTime 设置为较低的值以听到更明显的效果。
在您移动滑块之前,滑块的值不会代表参数的值。
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
}
}
}