麦克风分析频率时,AudioKit可以播放声音吗?

时间:2019-06-19 16:43:37

标签: audiokit xcode10.2

我想制作一个耳部训练应用程序,所以我想在麦克风分析频率时发出声音。我刚开始证明这个概念,所以现在,我只是使用AudioKit的示例应用程序MicrophoneAnalisys并添加了一些代码来发出声音。

    import AudioKit
    import AudioKitUI
    import UIKit

    class ViewController: UIViewController {

        var oscillator1 = AKOscillator()
        var oscillator2 = AKOscillator()
        var mixer = AKMixer()

        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            mixer = AKMixer(oscillator1, oscillator2)

            mixer.volume = 0.5
            AudioKit.output = mixer
            do {
                try AudioKit.start()
            } catch {
                AKLog("AudioKit did not start!")
            }
        }

        var mic: AKMicrophone!
        var tracker: AKFrequencyTracker!
        var silence: AKBooster!

    override func viewDidLoad() {
        super.viewDidLoad()

        AKSettings.audioInputEnabled = true
        mic = AKMicrophone()
        tracker = AKFrequencyTracker(mic)
        silence = AKBooster(tracker, gain: 0)
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        AudioKit.output = silence
        do {
            try AudioKit.start()
        } catch {
            AKLog("AudioKit did not start!")
        }
        setupPlot()
        Timer.scheduledTimer(timeInterval: 0.1,
                             target: self,
                             selector: #selector(ViewController.updateUI),
                             userInfo: nil,
                             repeats: true)
    }

    @objc func updateUI() {
        if tracker.amplitude > 0.1 {
            frequencyLabel.text = String(format: "%0.1f", tracker.frequency)

            var frequency = Float(tracker.frequency)
            while frequency > Float(noteFrequencies[noteFrequencies.count - 1]) {
                frequency /= 2.0
            }
            while frequency < Float(noteFrequencies[0]) {
                frequency *= 2.0
            }

            var minDistance: Float = 10_000.0
            var index = 0

            for i in 0..<noteFrequencies.count {
                let distance = fabsf(Float(noteFrequencies[i]) - frequency)
                if distance < minDistance {
                    index = i
                    minDistance = distance
                }
            }
    let octave = Int(log2f(Float(tracker.frequency) / frequency))
    noteNameWithSharpsLabel.text = "\(noteNamesWithSharps[index])\

(octave)"
            noteNameWithFlatsLabel.text = "\(noteNamesWithFlats[index])\(octave)"
        }
        amplitudeLabel.text = String(format: "%0.2f", tracker.amplitude)
    }


    @IBAction func didTapASound(_ sender: Any) {
        print("didTapASound")

        mixer = AKMixer(oscillator1, oscillator2)

        // Cut the volume in half since we have two oscillators
        mixer.volume = 0.5
        AudioKit.output = mixer
        do {
            try AudioKit.start()
        } catch {
            AKLog("AudioKit did not start!")
        }

        if oscillator1.isPlaying {
            oscillator1.stop()
            oscillator2.stop()
        } else {
            oscillator1.frequency = random(in: 220 ... 880)
            oscillator1.start()
            oscillator2.frequency = random(in: 220 ... 880)
            //            oscillator2.start()
        }

    }

我知道尝试运行

肯定是错的
AudioKit.output = mixer
do {
  try AudioKit.start()
} catch {
  AKLog("AudioKit did not start!")
}

AudioKit.output = mixer
do {
    try AudioKit.start()
} catch {
    AKLog("AudioKit did not start!")
}

同时

我遇到以下错误。

[avae] AVAEInternal.h:70:_AVAE_Check: required condition is false: [AVAudioIONodeImpl.mm:910:SetOutputFormat: (IsFormatSampleRateAndChannelCountValid(hwFormat))]
*** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: IsFormatSampleRateAndChannelCountValid(hwFormat)'

我读了this article,但不明白询问问题的人是否能够解决问题。有人可以告诉我在用AudioKit接通麦克风的同时能否发出声音,并指向正确的方向,使我学习如何做到这一点?谢谢!

1 个答案:

答案 0 :(得分:0)

我在回答自己的问题。当然,它可以发出声音并同时收听麦克风。我要做的就是将它们与AKMixer整合在一起。

var oscillator = AKOscillator()

var mic: AKMicrophone!
    var tracker: AKFrequencyTracker!
    var silence: AKBooster!

override func viewDidLoad() {
        super.viewDidLoad()

        AKSettings.audioInputEnabled = true
        mic = AKMicrophone()
        tracker = AKFrequencyTracker(mic)
        silence = AKBooster(tracker, gain: 0)
    }

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    AudioKit.output = AKMixer(silence,oscillator)
    //AudioKit.output = silence
    do {
        try AudioKit.start()
    } catch {
        AKLog("AudioKit did not start!")
    }
    setupPlot()
    Timer.scheduledTimer(timeInterval: 0.1,
                         target: self,
                         selector: #selector(ViewController.updateUI),
                         userInfo: nil,
                         repeats: true)
}

@IBAction func makeASound(_ sender: Any) {

    if oscillator.isPlaying {
        print("Stop A Sound")
        oscillator.stop()
    } else {
        print("Make A Sound")
        oscillator.amplitude = 1.0
        oscillator.frequency = 440.0
        oscillator.start()
    }

}

以防万一有人有相同的问题。